test_int_sync_api_gateway.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(fastapi_example_app + "/", lambda a, b: "token")
  15. ctx.tenant = None
  16. @pytest.fixture
  17. def gateway(provider) -> SyncApiGateway:
  18. return BooksGateway(provider)
  19. @pytest.fixture
  20. def book(gateway: SyncApiGateway):
  21. return gateway.add({"title": "fixture", "author": {"name": "foo"}})
  22. def test_add(gateway: SyncApiGateway):
  23. response = gateway.add({"title": "test_add", "author": {"name": "foo"}})
  24. assert isinstance(response["id"], int)
  25. assert response["title"] == "test_add"
  26. assert response["author"] == {"name": "foo"}
  27. assert response["created_at"] == response["updated_at"]
  28. def test_get(gateway: SyncApiGateway, book: Json):
  29. response = gateway.get(book["id"])
  30. assert response == book
  31. def test_remove_and_404(gateway: SyncApiGateway, book: Json):
  32. assert gateway.remove(book["id"]) is True
  33. assert gateway.get(book["id"]) is None
  34. assert gateway.remove(book["id"]) is False
  35. def test_update(gateway: SyncApiGateway, book: Json):
  36. response = gateway.update({"id": book["id"], "title": "test_update"})
  37. assert response["id"] == book["id"]
  38. assert response["title"] == "test_update"
  39. assert response["author"] == {"name": "foo"}
  40. assert response["created_at"] != response["updated_at"]
  41. def test_update_404(gateway: SyncApiGateway):
  42. with pytest.raises(DoesNotExist):
  43. gateway.update({"id": 123456, "title": "test_update_404"})