| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | # This module is a copy paste of test_api_gateway.pyfrom http import HTTPStatusfrom unittest import mockimport pytestfrom clean_python import DoesNotExistfrom clean_python import Jsonfrom clean_python import Mapperfrom clean_python.api_client import ApiExceptionfrom clean_python.api_client import SyncApiGatewayfrom clean_python.api_client import SyncApiProviderclass TstSyncApiGateway(SyncApiGateway, path="foo/{id}"):    pass@pytest.fixturedef api_provider():    return mock.MagicMock(spec_set=SyncApiProvider)@pytest.fixturedef api_gateway(api_provider) -> SyncApiGateway:    return TstSyncApiGateway(api_provider)def test_get(api_gateway: SyncApiGateway):    actual = api_gateway.get(14)    api_gateway.provider.request.assert_called_once_with("GET", "foo/14")    assert actual is api_gateway.provider.request.return_valuedef test_add(api_gateway: SyncApiGateway):    actual = api_gateway.add({"foo": 2})    api_gateway.provider.request.assert_called_once_with(        "POST", "foo/", json={"foo": 2}    )    assert actual is api_gateway.provider.request.return_valuedef test_remove(api_gateway: SyncApiGateway):    actual = api_gateway.remove(2)    api_gateway.provider.request.assert_called_once_with("DELETE", "foo/2")    assert actual is Truedef test_remove_does_not_exist(api_gateway: SyncApiGateway):    api_gateway.provider.request.side_effect = ApiException(        {}, status=HTTPStatus.NOT_FOUND    )    actual = api_gateway.remove(2)    assert actual is Falsedef test_update(api_gateway: SyncApiGateway):    actual = api_gateway.update({"id": 2, "foo": "bar"})    api_gateway.provider.request.assert_called_once_with(        "PATCH", "foo/2", json={"foo": "bar"}    )    assert actual is api_gateway.provider.request.return_valuedef test_update_no_id(api_gateway: SyncApiGateway):    with pytest.raises(DoesNotExist):        api_gateway.update({"foo": "bar"})    assert not api_gateway.provider.request.calleddef test_update_does_not_exist(api_gateway: SyncApiGateway):    api_gateway.provider.request.side_effect = ApiException(        {}, status=HTTPStatus.NOT_FOUND    )    with pytest.raises(DoesNotExist):        api_gateway.update({"id": 2, "foo": "bar"})class TstMapper(Mapper):    def to_external(self, internal: Json) -> Json:        result = {}        if internal.get("id") is not None:            result["id"] = internal["id"]        if internal.get("name") is not None:            result["name"] = internal["name"].upper()        return result    def to_internal(self, external: Json) -> Json:        return {"id": external["id"], "name": external["name"].lower()}class TstMappedSyncApiGateway(SyncApiGateway, path="foo/{id}"):    mapper = TstMapper()@pytest.fixturedef mapped_api_gateway(api_provider) -> SyncApiGateway:    return TstMappedSyncApiGateway(api_provider)def test_get_with_mapper(mapped_api_gateway: SyncApiGateway):    mapped_api_gateway.provider.request.return_value = {"id": 14, "name": "FOO"}    assert mapped_api_gateway.get(14) == {"id": 14, "name": "foo"}def test_add_with_mapper(mapped_api_gateway: SyncApiGateway):    mapped_api_gateway.provider.request.return_value = {"id": 3, "name": "FOO"}    assert mapped_api_gateway.add({"name": "foo"}) == {"id": 3, "name": "foo"}    mapped_api_gateway.provider.request.assert_called_once_with(        "POST", "foo/", json={"name": "FOO"}    )def test_update_with_mapper(mapped_api_gateway: SyncApiGateway):    mapped_api_gateway.provider.request.return_value = {"id": 2, "name": "BAR"}    assert mapped_api_gateway.update({"id": 2, "name": "bar"}) == {        "id": 2,        "name": "bar",    }    mapped_api_gateway.provider.request.assert_called_once_with(        "PATCH", "foo/2", json={"name": "BAR"}    )
 |