1- import re
2-
31import pytest
42from httpx import Response
53
@@ -11,100 +9,53 @@ 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" })
12+ def test_generic_resource_empty ():
13+ resource = GenericResource ()
14+ assert resource .meta is None
15+ assert resource .to_dict () == {}
3816
39- assert resource .id == 1
40- assert resource .name == "test_resource"
41- assert resource .nested .key == "value"
4217
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 )
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 )
4722
48- resource = GenericResource (
49- data = "test_data" ,
50- ** {"$meta" : meta_data }, # noqa: WPS445 WPS517
51- )
23+ resource = GenericResource .from_response (response )
5224
53- assert resource .data == "test_data"
54- assert resource ._meta == meta_object
25+ assert resource .to_dict () == record_data
26+ assert resource .meta == expected_meta
5527
56- def test_dynamic_attribute_access (self ):
57- resource = GenericResource ()
5828
59- resource .dynamic_field = "dynamic_value"
60- resource .nested_object = {"inner" : "data" }
29+ def test_attribute_access ():
30+ record_data = {"id" : 1 , "name" : {"given" : "Albert" , "family" : "Einstein" }}
31+ meta = Meta .from_response (Response (200 , json = {"$meta" : {}}))
32+ resource = GenericResource (resource = record_data , meta = meta )
6133
62- assert resource .dynamic_field == "dynamic_value"
63- assert resource .nested_object .inner == "data"
34+ assert resource .meta == meta
6435
36+ assert resource .id == 1
6537
66- class TestGenericResourceFromResponse :
67- @pytest .fixture
68- def meta_data_single (self ):
69- return {"ignored" : ["one" ]} # noqa: WPS226
38+ with pytest .raises (AttributeError , match = r"'Box' object has no attribute 'address'" ):
39+ resource .address # noqa: B018
7040
71- @pytest .fixture
72- def meta_data_two_resources (self ):
73- return {"pagination" : {"limit" : 10 , "offset" : 0 , "total" : 2 }, "ignored" : ["one" ]} # noqa: WPS226
41+ with pytest .raises (AttributeError , match = r"'Box' object has no attribute 'surname'" ):
42+ resource .name .surname # noqa: B018
7443
75- @pytest .fixture
76- def meta_data_multiple (self ):
77- return {"ignored" : ["one" , "two" ]} # noqa: WPS226
44+ assert resource .name .given == "Albert"
45+ assert resource .name .to_dict () == record_data ["name" ]
7846
79- @pytest .fixture
80- def single_resource_data (self ):
81- return {"id" : 1 , "name" : "test" }
8247
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 } )
48+ def test_attribute_setter ():
49+ record_data = { "id" : 1 , "name" : { "given" : "Albert" , "family" : "Einstein" }}
50+ resource = GenericResource ( resource = record_data )
8651
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- )
52+ resource .id = 2
53+ assert resource .id == 2
9654
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 }))
55+ resource .name .given = "John"
56+ assert resource .name .given == "John"
10057
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
10758
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 )
59+ def test_wrong_data_type ( ):
60+ with pytest .raises (TypeError , match = r"Response data must be a dict." ):
61+ GenericResource .from_response (Response ( 200 , json = { "data" : 1 }) )
0 commit comments