test_sync_api_gateway.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from http import HTTPStatus
  2. from unittest import mock
  3. import pytest
  4. from clean_python import DoesNotExist
  5. from clean_python.api_client import ApiException
  6. from clean_python.api_client import SyncApiGateway
  7. from clean_python.api_client import SyncApiProvider
  8. MODULE = "clean_python.api_client.api_provider"
  9. class TstSyncApiGateway(SyncApiGateway, path="foo/{id}"):
  10. pass
  11. @pytest.fixture
  12. def api_provider():
  13. return mock.MagicMock(spec_set=SyncApiProvider)
  14. @pytest.fixture
  15. def api_gateway(api_provider) -> SyncApiGateway:
  16. return TstSyncApiGateway(api_provider)
  17. def test_get(api_gateway: SyncApiGateway):
  18. actual = api_gateway.get(14)
  19. api_gateway.provider.request.assert_called_once_with("GET", "foo/14")
  20. assert actual is api_gateway.provider.request.return_value
  21. def test_add(api_gateway: SyncApiGateway):
  22. actual = api_gateway.add({"foo": 2})
  23. api_gateway.provider.request.assert_called_once_with(
  24. "POST", "foo/", json={"foo": 2}
  25. )
  26. assert actual is api_gateway.provider.request.return_value
  27. def test_remove(api_gateway: SyncApiGateway):
  28. actual = api_gateway.remove(2)
  29. api_gateway.provider.request.assert_called_once_with("DELETE", "foo/2")
  30. assert actual is True
  31. def test_remove_does_not_exist(api_gateway: SyncApiGateway):
  32. api_gateway.provider.request.side_effect = ApiException(
  33. {}, status=HTTPStatus.NOT_FOUND
  34. )
  35. actual = api_gateway.remove(2)
  36. assert actual is False
  37. def test_update(api_gateway: SyncApiGateway):
  38. actual = api_gateway.update({"id": 2, "foo": "bar"})
  39. api_gateway.provider.request.assert_called_once_with(
  40. "PATCH", "foo/2", json={"foo": "bar"}
  41. )
  42. assert actual is api_gateway.provider.request.return_value
  43. def test_update_no_id(api_gateway: SyncApiGateway):
  44. with pytest.raises(DoesNotExist):
  45. api_gateway.update({"foo": "bar"})
  46. assert not api_gateway.provider.request.called
  47. def test_update_does_not_exist(api_gateway: SyncApiGateway):
  48. api_gateway.provider.request.side_effect = ApiException(
  49. {}, status=HTTPStatus.NOT_FOUND
  50. )
  51. with pytest.raises(DoesNotExist):
  52. api_gateway.update({"id": 2, "foo": "bar"})