test_api_gateway.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from http import HTTPStatus
  2. from unittest import mock
  3. import pytest
  4. from clean_python import DoesNotExist
  5. from clean_python import Json
  6. from clean_python import Mapper
  7. from clean_python.api_client import ApiException
  8. from clean_python.api_client import ApiGateway
  9. from clean_python.api_client import ApiProvider
  10. class TstApiGateway(ApiGateway, path="foo/{id}"):
  11. pass
  12. @pytest.fixture
  13. def api_provider():
  14. return mock.MagicMock(spec_set=ApiProvider)
  15. @pytest.fixture
  16. def api_gateway(api_provider) -> ApiGateway:
  17. return TstApiGateway(api_provider)
  18. async def test_get(api_gateway: ApiGateway):
  19. actual = await api_gateway.get(14)
  20. api_gateway.provider.request.assert_called_once_with("GET", "foo/14")
  21. assert actual is api_gateway.provider.request.return_value
  22. async def test_add(api_gateway: ApiGateway):
  23. actual = await api_gateway.add({"foo": 2})
  24. api_gateway.provider.request.assert_called_once_with(
  25. "POST", "foo/", json={"foo": 2}
  26. )
  27. assert actual is api_gateway.provider.request.return_value
  28. async def test_remove(api_gateway: ApiGateway):
  29. actual = await api_gateway.remove(2)
  30. api_gateway.provider.request.assert_called_once_with("DELETE", "foo/2")
  31. assert actual is True
  32. async def test_remove_does_not_exist(api_gateway: ApiGateway):
  33. api_gateway.provider.request.side_effect = ApiException(
  34. {}, status=HTTPStatus.NOT_FOUND
  35. )
  36. actual = await api_gateway.remove(2)
  37. assert actual is False
  38. async def test_update(api_gateway: ApiGateway):
  39. actual = await api_gateway.update({"id": 2, "foo": "bar"})
  40. api_gateway.provider.request.assert_called_once_with(
  41. "PATCH", "foo/2", json={"foo": "bar"}
  42. )
  43. assert actual is api_gateway.provider.request.return_value
  44. async def test_update_no_id(api_gateway: ApiGateway):
  45. with pytest.raises(DoesNotExist):
  46. await api_gateway.update({"foo": "bar"})
  47. assert not api_gateway.provider.request.called
  48. async def test_update_does_not_exist(api_gateway: ApiGateway):
  49. api_gateway.provider.request.side_effect = ApiException(
  50. {}, status=HTTPStatus.NOT_FOUND
  51. )
  52. with pytest.raises(DoesNotExist):
  53. await api_gateway.update({"id": 2, "foo": "bar"})
  54. class TstMapper(Mapper):
  55. def to_external(self, internal: Json) -> Json:
  56. result = {}
  57. if internal.get("id") is not None:
  58. result["id"] = internal["id"]
  59. if internal.get("name") is not None:
  60. result["name"] = internal["name"].upper()
  61. return result
  62. def to_internal(self, external: Json) -> Json:
  63. return {"id": external["id"], "name": external["name"].lower()}
  64. class TstMappedApiGateway(ApiGateway, path="foo/{id}"):
  65. mapper = TstMapper()
  66. @pytest.fixture
  67. def mapped_api_gateway(api_provider) -> ApiGateway:
  68. return TstMappedApiGateway(api_provider)
  69. async def test_get_with_mapper(mapped_api_gateway: ApiGateway):
  70. mapped_api_gateway.provider.request.return_value = {"id": 14, "name": "FOO"}
  71. assert await mapped_api_gateway.get(14) == {"id": 14, "name": "foo"}
  72. async def test_add_with_mapper(mapped_api_gateway: ApiGateway):
  73. mapped_api_gateway.provider.request.return_value = {"id": 3, "name": "FOO"}
  74. assert await mapped_api_gateway.add({"name": "foo"}) == {"id": 3, "name": "foo"}
  75. mapped_api_gateway.provider.request.assert_called_once_with(
  76. "POST", "foo/", json={"name": "FOO"}
  77. )
  78. async def test_update_with_mapper(mapped_api_gateway: ApiGateway):
  79. mapped_api_gateway.provider.request.return_value = {"id": 2, "name": "BAR"}
  80. assert await mapped_api_gateway.update({"id": 2, "name": "bar"}) == {
  81. "id": 2,
  82. "name": "bar",
  83. }
  84. mapped_api_gateway.provider.request.assert_called_once_with(
  85. "PATCH", "foo/2", json={"name": "BAR"}
  86. )