1- import re
2-
31import pytest
42from httpx import Response
53
@@ -11,100 +9,43 @@ def meta_data():
119 return {"pagination" : {"limit" : 10 , "offset" : 20 , "total" : 100 }, "ignored" : ["one" ]} # noqa: WPS226
1210
1311
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
12+ def test_generic_resource_empty ():
13+ resource = GenericResource ()
14+ assert resource .meta is None
15+ assert resource .to_dict () == {}
5516
56- def test_dynamic_attribute_access (self ):
57- resource = GenericResource ()
5817
59- resource .dynamic_field = "dynamic_value"
60- resource .nested_object = {"inner" : "data" }
18+ def test_from_response (meta_data ):
19+ record_data = {"id" : 1 , "name" : {"given" : "Albert" , "family" : "Einstein" }}
20+ response = Response (200 , json = {"data" : record_data , "$meta" : meta_data })
21+ expected_meta = Meta .from_response (response )
6122
62- assert resource .dynamic_field == "dynamic_value"
63- assert resource .nested_object .inner == "data"
23+ resource = GenericResource .from_response (response )
6424
25+ assert resource .to_dict () == record_data
26+ assert resource .meta == expected_meta
6527
66- class TestGenericResourceFromResponse :
67- @pytest .fixture
68- def meta_data_single (self ):
69- return {"ignored" : ["one" ]} # noqa: WPS226
7028
71- @pytest .fixture
72- def meta_data_two_resources (self ):
73- return {"pagination" : {"limit" : 10 , "offset" : 0 , "total" : 2 }, "ignored" : ["one" ]} # noqa: WPS226
29+ def test_attribute_getter (mocker , meta_data ):
30+ resource_data = {"id" : 1 , "name" : {"given" : "Albert" , "family" : "Einstein" }}
31+ response = Response (200 , json = {"data" : resource_data , "$meta" : meta_data })
32+ resource = GenericResource .from_response (response )
7433
75- @pytest .fixture
76- def meta_data_multiple (self ):
77- return {"ignored" : ["one" , "two" ]} # noqa: WPS226
34+ assert resource .id == 1
35+ assert resource .name .given == "Albert"
7836
79- @pytest .fixture
80- def single_resource_data (self ):
81- return {"id" : 1 , "name" : "test" }
8237
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 } )
38+ def test_attribute_setter ():
39+ resource_data = { "id" : 1 , "name" : { "given" : "Albert" , "family" : "Einstein" }}
40+ resource = GenericResource ( resource_data )
8641
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- )
42+ resource .id = 2
43+ assert resource .id == 2
9644
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 }))
45+ resource .name .given = "John"
46+ assert resource .name .given == "John"
10047
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
10748
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 )
49+ def test_wrong_data_type ( ):
50+ with pytest .raises (TypeError , match = r"Response data must be a dict." ):
51+ GenericResource .from_response (Response ( 200 , json = { "data" : 1 }) )
0 commit comments