test_sync_api_gateway.py 3.7 KB

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