test_int_sync_api_gateway.py 1.9 KB

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