|
| 1 | +import re |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import Response |
| 5 | + |
| 6 | +from mpt_api_client.http.models import GenericResource, Meta |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def meta_data(): |
| 11 | + return {"pagination": {"limit": 10, "offset": 20, "total": 100}, "ignored": ["one"]} # noqa: WPS226 |
| 12 | + |
| 13 | + |
| 14 | +class TestGenericResource: # noqa: WPS214 |
| 15 | + def test_generic_resource_empty(self): |
| 16 | + resource = GenericResource() |
| 17 | + with pytest.raises(AttributeError): |
| 18 | + _ = resource._meta |
| 19 | + |
| 20 | + def test_initialization_with_data(self): |
| 21 | + resource = GenericResource(name="test", value=123) |
| 22 | + |
| 23 | + assert resource.name == "test" |
| 24 | + assert resource.value == 123 |
| 25 | + |
| 26 | + def test_init(self, meta_data): |
| 27 | + resource = {"$meta": meta_data, "key": "value"} # noqa: WPS445 WPS517 |
| 28 | + init_one = GenericResource(resource) |
| 29 | + init_two = GenericResource(**resource) |
| 30 | + assert init_one == init_two |
| 31 | + |
| 32 | + def test_generic_resource_meta_property_with_data(self, meta_data): |
| 33 | + resource = GenericResource({"$meta": meta_data}) |
| 34 | + assert resource._meta == Meta(**meta_data) |
| 35 | + |
| 36 | + def test_generic_resource_box_functionality(self): |
| 37 | + resource = GenericResource(id=1, name="test_resource", nested={"key": "value"}) |
| 38 | + |
| 39 | + assert resource.id == 1 |
| 40 | + assert resource.name == "test_resource" |
| 41 | + assert resource.nested.key == "value" |
| 42 | + |
| 43 | + def test_with_both_meta_and_response(self, meta_data): |
| 44 | + response = Response(200, json={}) |
| 45 | + meta_data["response"] = response |
| 46 | + meta_object = Meta(**meta_data) |
| 47 | + |
| 48 | + resource = GenericResource( |
| 49 | + data="test_data", |
| 50 | + **{"$meta": meta_data}, # noqa: WPS445 WPS517 |
| 51 | + ) |
| 52 | + |
| 53 | + assert resource.data == "test_data" |
| 54 | + assert resource._meta == meta_object |
| 55 | + |
| 56 | + def test_dynamic_attribute_access(self): |
| 57 | + resource = GenericResource() |
| 58 | + |
| 59 | + resource.dynamic_field = "dynamic_value" |
| 60 | + resource.nested_object = {"inner": "data"} |
| 61 | + |
| 62 | + assert resource.dynamic_field == "dynamic_value" |
| 63 | + assert resource.nested_object.inner == "data" |
| 64 | + |
| 65 | + |
| 66 | +class TestGenericResourceFromResponse: |
| 67 | + @pytest.fixture |
| 68 | + def meta_data_single(self): |
| 69 | + return {"ignored": ["one"]} # noqa: WPS226 |
| 70 | + |
| 71 | + @pytest.fixture |
| 72 | + def meta_data_two_resources(self): |
| 73 | + return {"pagination": {"limit": 10, "offset": 0, "total": 2}, "ignored": ["one"]} # noqa: WPS226 |
| 74 | + |
| 75 | + @pytest.fixture |
| 76 | + def meta_data_multiple(self): |
| 77 | + return {"ignored": ["one", "two"]} # noqa: WPS226 |
| 78 | + |
| 79 | + @pytest.fixture |
| 80 | + def single_resource_data(self): |
| 81 | + return {"id": 1, "name": "test"} |
| 82 | + |
| 83 | + @pytest.fixture |
| 84 | + def single_resource_response(self, single_resource_data, meta_data_single): |
| 85 | + return Response(200, json={"data": single_resource_data, "$meta": meta_data_single}) |
| 86 | + |
| 87 | + @pytest.fixture |
| 88 | + def multiple_resource_response(self, single_resource_data, meta_data_two_resources): |
| 89 | + return Response( |
| 90 | + 200, |
| 91 | + json={ |
| 92 | + "data": [single_resource_data, single_resource_data], |
| 93 | + "$meta": meta_data_two_resources, |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + def test_malformed_meta_response(self): |
| 98 | + with pytest.raises(TypeError, match=re.escape("Response $meta must be a dict.")): |
| 99 | + _resource = GenericResource.from_response(Response(200, json={"data": {}, "$meta": 4})) |
| 100 | + |
| 101 | + def test_single_resource(self, single_resource_response): |
| 102 | + resource = GenericResource.from_response(single_resource_response) |
| 103 | + assert resource.id == 1 |
| 104 | + assert resource.name == "test" |
| 105 | + assert isinstance(resource._meta, Meta) |
| 106 | + assert resource._meta.response == single_resource_response |
| 107 | + |
| 108 | + def test_two_resources(self, multiple_resource_response, single_resource_data): |
| 109 | + with pytest.raises(TypeError, match=r"Response data must be a dict."): |
| 110 | + _resource = GenericResource.from_response(multiple_resource_response) |
0 commit comments