test_api_gateway.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import pytest
  2. from clean_python import ctx
  3. from clean_python import DoesNotExist
  4. from clean_python import Json
  5. from clean_python import Tenant
  6. from clean_python.api_client import SyncApiGateway
  7. from clean_python.api_client import SyncApiProvider
  8. class BooksGateway(SyncApiGateway, path="v1/books/{id}"):
  9. pass
  10. @pytest.fixture
  11. def provider(fastapi_example_app) -> SyncApiProvider:
  12. ctx.tenant = Tenant(id=2, name="")
  13. yield SyncApiProvider(fastapi_example_app + "/", lambda a, b: "token")
  14. ctx.tenant = None
  15. @pytest.fixture
  16. def gateway(provider) -> SyncApiGateway:
  17. return BooksGateway(provider)
  18. @pytest.fixture
  19. def book(gateway: SyncApiGateway):
  20. return gateway.add({"title": "fixture", "author": {"name": "foo"}})
  21. def test_add(gateway: SyncApiGateway):
  22. response = gateway.add({"title": "test_add", "author": {"name": "foo"}})
  23. assert isinstance(response["id"], int)
  24. assert response["title"] == "test_add"
  25. assert response["author"] == {"name": "foo"}
  26. assert response["created_at"] == response["updated_at"]
  27. def test_get(gateway: SyncApiGateway, book: Json):
  28. response = gateway.get(book["id"])
  29. assert response == book
  30. def test_remove_and_404(gateway: SyncApiGateway, book: Json):
  31. assert gateway.remove(book["id"]) is True
  32. assert gateway.get(book["id"]) is None
  33. assert gateway.remove(book["id"]) is False
  34. def test_update(gateway: SyncApiGateway, book: Json):
  35. response = gateway.update({"id": book["id"], "title": "test_update"})
  36. assert response["id"] == book["id"]
  37. assert response["title"] == "test_update"
  38. assert response["author"] == {"name": "foo"}
  39. assert response["created_at"] != response["updated_at"]
  40. def test_update_404(gateway: SyncApiGateway):
  41. with pytest.raises(DoesNotExist):
  42. gateway.update({"id": 123456, "title": "test_update_404"})