44
55
66class TestRelationships :
7- def _category_subcategory_ids (self , client , category_id ):
7+ @pytest .fixture (autouse = True )
8+ def setup (self , client ):
9+ self .client = client
810 with client .application .app_context ():
11+ assert Category .query .count () == 0
12+ assert Subcategory .query .count () == 0
13+ assert Product .query .count () == 0
14+
15+ def _category_subcategory_ids (self , category_id ):
16+ with self .client .application .app_context ():
917 category = Category .query .get (category_id )
1018 assert category is not None
1119 return sorted ([subcategory .id for subcategory in category .subcategories ])
1220
13- def _subcategory_category_ids (self , client , subcategory_id ):
14- with client .application .app_context ():
21+ def _subcategory_category_ids (self , subcategory_id ):
22+ with self . client .application .app_context ():
1523 subcategory = Subcategory .query .get (subcategory_id )
1624 assert subcategory is not None
1725 return sorted ([category .id for category in subcategory .categories ])
1826
19- def _subcategory_product_ids (self , client , subcategory_id ):
20- with client .application .app_context ():
27+ def _subcategory_product_ids (self , subcategory_id ):
28+ with self . client .application .app_context ():
2129 subcategory = Subcategory .query .get (subcategory_id )
2230 assert subcategory is not None
2331 return sorted ([product .id for product in subcategory .products ])
2432
25- def _product_subcategory_ids (self , client , product_id ):
26- with client .application .app_context ():
33+ def _product_subcategory_ids (self , product_id ):
34+ with self . client .application .app_context ():
2735 product = Product .query .get (product_id )
2836 assert product is not None
2937 return sorted ([subcategory .id for subcategory in product .subcategories ])
3038
31- def _category_product_ids_via_subcategories (self , client , category_id ):
32- with client .application .app_context ():
39+ def _category_product_ids_via_subcategories (self , category_id ):
40+ with self . client .application .app_context ():
3341 category = Category .query .get (category_id )
3442 assert category is not None
3543 return sorted ({product .id for subcategory in category .subcategories for product in subcategory .products .all ()})
3644
37- def test_create_category_with_subcategories (self , client , create_category , create_subcategory ):
45+ def test_create_category_with_subcategories (self , create_category , create_subcategory ):
3846 subcategory1 = create_subcategory ("SC_A" ).get_json ()
3947 subcategory2 = create_subcategory ("SC_B" ).get_json ()
4048 response = create_category ("Cat_AB" , subcategories = [subcategory1 ["id" ], subcategory2 ["id" ]])
4149 assert response .status_code == 201
4250 category = response .get_json ()
4351
44- assert self ._category_subcategory_ids (client , category ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
45- assert category ["id" ] in self ._subcategory_category_ids (client , subcategory1 ["id" ])
46- assert category ["id" ] in self ._subcategory_category_ids (client , subcategory2 ["id" ])
52+ assert self ._category_subcategory_ids (category ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
53+ assert category ["id" ] in self ._subcategory_category_ids (subcategory1 ["id" ])
54+ assert category ["id" ] in self ._subcategory_category_ids (subcategory2 ["id" ])
4755
48- def test_create_subcategory_with_categories_and_products (self , client , create_subcategory , create_category , create_product ):
56+ def test_create_subcategory_with_categories_and_products (self , create_subcategory , create_category , create_product ):
4957 category1 = create_category ("C1" ).get_json ()
5058 category2 = create_category ("C2" ).get_json ()
5159 product1 = create_product ("P1" , "des" ).get_json ()
@@ -55,76 +63,92 @@ def test_create_subcategory_with_categories_and_products(self, client, create_su
5563 assert response .status_code == 201
5664 subcategory = response .get_json ()
5765
58- assert self ._subcategory_category_ids (client , subcategory ["id" ]) == sorted ([category1 ["id" ], category2 ["id" ]])
59- assert self ._subcategory_product_ids (client , subcategory ["id" ]) == sorted ([product1 ["id" ], product2 ["id" ]])
66+ assert self ._subcategory_category_ids (subcategory ["id" ]) == sorted ([category1 ["id" ], category2 ["id" ]])
67+ assert self ._subcategory_product_ids (subcategory ["id" ]) == sorted ([product1 ["id" ], product2 ["id" ]])
6068
61- def test_create_product_with_subcategories_links_to_category_products (self , client , create_category , create_subcategory , create_product ):
69+ def test_create_product_with_subcategories_links_to_category_products (self , create_category , create_subcategory , create_product ):
6270 category = create_category ("C" ).get_json ()
6371 subcategory = create_subcategory ("SC" , categories = [category ["id" ]]).get_json ()
6472 response = create_product ("P" , "desc" , subcategories = [subcategory ["id" ]])
6573 assert response .status_code == 201
6674 product = response .get_json ()
6775
68- assert subcategory ["id" ] in self ._product_subcategory_ids (client , product ["id" ])
69- assert product ["id" ] in self ._category_product_ids_via_subcategories (client , category ["id" ])
70- assert product ["id" ] in self ._subcategory_product_ids (client , subcategory ["id" ])
76+ assert subcategory ["id" ] in self ._product_subcategory_ids (product ["id" ])
77+ assert product ["id" ] in self ._category_product_ids_via_subcategories (category ["id" ])
78+ assert product ["id" ] in self ._subcategory_product_ids (subcategory ["id" ])
7179
72- def test_update_category_adds_subcategories (self , client , create_authenticated_headers , create_category , create_subcategory ):
80+ def test_update_category_adds_subcategories (self , create_authenticated_headers , create_category , create_subcategory ):
7381 subcategory1 = create_subcategory ("U_SC1" ).get_json ()
7482 subcategory2 = create_subcategory ("U_SC2" ).get_json ()
7583 category = create_category ("U_Cat" , subcategories = [subcategory1 ["id" ]]).get_json ()
7684
7785 headers = create_authenticated_headers ()
78- update_response = client .put (f"/category/{ category ['id' ]} /update" , json = {"subcategories" : [subcategory2 ["id" ]]}, headers = headers )
86+ update_response = self . client .put (f"/category/{ category ['id' ]} /update" , json = {"subcategories" : [subcategory2 ["id" ]]}, headers = headers )
7987 assert update_response .status_code == 201
8088
81- assert self ._category_subcategory_ids (client , category ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
89+ assert self ._category_subcategory_ids (category ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
8290
83- def test_update_subcategory_adds_categories_and_products (self , client , create_authenticated_headers , create_category , create_product , create_subcategory ):
91+ def test_update_subcategory_adds_categories_and_products (self , create_authenticated_headers , create_category , create_product , create_subcategory ):
8492 category1 = create_category ("UC1" ).get_json ()
8593 category2 = create_category ("UC2" ).get_json ()
8694 product1 = create_product ("UP1" ).get_json ()
8795 product2 = create_product ("UP2" ).get_json ()
8896 subcategory = create_subcategory ("U_SC" ).get_json ()
8997
9098 headers = create_authenticated_headers ()
91- update_response = client .put (
99+ update_response = self . client .put (
92100 f"/subcategory/{ subcategory ['id' ]} /update" ,
93101 json = {"categories" : [category1 ["id" ], category2 ["id" ]], "products" : [product1 ["id" ], product2 ["id" ]]},
94102 headers = headers ,
95103 )
96104 assert update_response .status_code == 201
97105
98- assert self ._subcategory_category_ids (client , subcategory ["id" ]) == sorted ([category1 ["id" ], category2 ["id" ]])
99- assert self ._subcategory_product_ids (client , subcategory ["id" ]) == sorted ([product1 ["id" ], product2 ["id" ]])
106+ assert self ._subcategory_category_ids (subcategory ["id" ]) == sorted ([category1 ["id" ], category2 ["id" ]])
107+ assert self ._subcategory_product_ids (subcategory ["id" ]) == sorted ([product1 ["id" ], product2 ["id" ]])
100108
101- def test_update_product_adds_subcategories (self , client , create_authenticated_headers , create_product , create_subcategory ):
109+ def test_update_product_adds_subcategories (self , create_authenticated_headers , create_product , create_subcategory ):
102110 subcategory1 = create_subcategory ("UPS1" ).get_json ()
103111 subcategory2 = create_subcategory ("UPS2" ).get_json ()
104112 product = create_product ("UP" , "desc" , subcategories = [subcategory1 ["id" ]]).get_json ()
105113
106114 headers = create_authenticated_headers ()
107- update_response = client .put (f"/product/{ product ['id' ]} /update" , json = {"subcategories" : [subcategory2 ["id" ]]}, headers = headers )
115+ update_response = self . client .put (f"/product/{ product ['id' ]} /update" , json = {"subcategories" : [subcategory2 ["id" ]]}, headers = headers )
108116 assert update_response .status_code == 201
109117
110- assert self ._product_subcategory_ids (client , product ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
118+ assert self ._product_subcategory_ids (product ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
111119
112- def test_get_category_subcategories_empty (self , client , create_category ):
120+ def test_get_category_subcategories_empty (self , create_category ):
113121 category = create_category ("Cat_NoSC" ).get_json ()
114- assert self ._category_subcategory_ids (client , category ["id" ]) == []
122+ resp = self .client .get (f"/category/{ category ['id' ]} /subcategories" )
123+
124+ assert resp .status_code == 200
125+ data = resp .get_json ()
126+ assert "subcategories" in data
127+ assert data ["subcategories" ] == []
115128
116- def test_get_category_subcategories_populated (self , client , create_category , create_subcategory ):
129+ def test_get_category_subcategories_populated (self , create_category , create_subcategory ):
117130 subcategory1 = create_subcategory ("SC1" ).get_json ()
118131 subcategory2 = create_subcategory ("SC2" ).get_json ()
119132 category = create_category ("Cat_WithSC" , subcategories = [subcategory1 ["id" ], subcategory2 ["id" ]]).get_json ()
120133
121- assert self ._category_subcategory_ids ( client , category [ "id" ]) == sorted ([ subcategory1 [ "id" ], subcategory2 [ "id" ]] )
134+ resp = self .client . get ( f"/category/ { category [ 'id' ] } /subcategories" )
122135
123- def test_get_category_products_empty (self , client , create_category ):
136+ assert resp .status_code == 200
137+ data = resp .get_json ()
138+ assert "subcategories" in data
139+ returned_ids = sorted ([sc ["id" ] for sc in data ["subcategories" ]])
140+ assert returned_ids == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
141+
142+ def test_get_category_products_empty (self , create_category ):
124143 category = create_category ("Cat_NoProd" ).get_json ()
125- assert set (self ._category_product_ids_via_subcategories (client , category ["id" ])) == set ()
144+ resp = self .client .get (f"/category/{ category ['id' ]} /products" )
145+
146+ assert resp .status_code == 200
147+ data = resp .get_json ()
148+ assert "products" in data
149+ assert data ["products" ] == []
126150
127- def test_get_category_products_populated_with_pagination (self , client , create_category , create_subcategory , create_product ):
151+ def test_get_category_products_populated_with_pagination (self , create_category , create_subcategory , create_product ):
128152 category = create_category ("Cat_Prod" ).get_json ()
129153 subcategory = create_subcategory ("SC_Prod" , categories = [category ["id" ]]).get_json ()
130154
@@ -133,43 +157,82 @@ def test_get_category_products_populated_with_pagination(self, client, create_ca
133157 product_resp = create_product (f"P{ index } " , "desc" , subcategories = [subcategory ["id" ]])
134158 product_ids .add (product_resp .get_json ().get ("id" ))
135159
136- page1 = client .get (f"/category/{ category ['id' ]} /products?page=1" ).get_json ()
137- page2 = client .get (f"/category/{ category ['id' ]} /products?page=2" ).get_json ()
160+ page1 = self . client .get (f"/category/{ category ['id' ]} /products?page=1" ).get_json ()
161+ page2 = self . client .get (f"/category/{ category ['id' ]} /products?page=2" ).get_json ()
138162 assert len (page1 ["products" ]) == 10
139163 assert len (page2 ["products" ]) == 2
140164
141165 returned_product_ids = set (p ["id" ] for p in page1 ["products" ] + page2 ["products" ])
142166 assert returned_product_ids == product_ids
143167
144- def test_get_subcategory_categories_populated (self , client , create_category , create_subcategory ):
168+ def test_get_subcategory_categories_empty (self , create_subcategory ):
169+ subcategory = create_subcategory ("SC_NoCat" ).get_json ()
170+ resp = self .client .get (f"/subcategory/{ subcategory ['id' ]} /categories" )
171+
172+ assert resp .status_code == 200
173+ data = resp .get_json ()
174+ assert "categories" in data
175+ assert data ["categories" ] == []
176+
177+ def test_get_subcategory_categories_populated (self , create_category , create_subcategory ):
145178 category1 = create_category ("C1" ).get_json ()
146179 category2 = create_category ("C2" ).get_json ()
147180 subcategory = create_subcategory ("SC_Cats" , categories = [category1 ["id" ], category2 ["id" ]]).get_json ()
148181
149- assert self ._subcategory_category_ids (client , subcategory ["id" ]) == sorted ([category1 ["id" ], category2 ["id" ]])
182+ resp = self .client .get (f"/subcategory/{ subcategory ['id' ]} /categories" )
183+
184+ assert resp .status_code == 200
185+ data = resp .get_json ()
186+ assert "categories" in data
187+ returned_ids = sorted ([cat ["id" ] for cat in data ["categories" ]])
188+ assert returned_ids == sorted ([category1 ["id" ], category2 ["id" ]])
189+
190+ def test_get_subcategory_products_empty (self , create_subcategory ):
191+ subcategory = create_subcategory ("SC_NoProd" ).get_json ()
192+ resp = self .client .get (f"/subcategory/{ subcategory ['id' ]} /products" )
150193
151- def test_get_subcategory_products_populated_with_pagination (self , client , create_subcategory , create_product ):
194+ assert resp .status_code == 200
195+ data = resp .get_json ()
196+ assert "products" in data
197+ assert data ["products" ] == []
198+
199+ def test_get_subcategory_products_populated_with_pagination (self , create_subcategory , create_product ):
152200 subcategory = create_subcategory ("SC_Pag" ).get_json ()
153201
154202 product_ids = set ()
155203 for index in range (11 ):
156204 product_resp = create_product (f"SP{ index } " , "desc" , subcategories = [subcategory ["id" ]])
157205 product_ids .add (product_resp .get_json ().get ("id" ))
158206
159- page1 = client .get (f"/subcategory/{ subcategory ['id' ]} /products?page=1" ).get_json ()
160- page2 = client .get (f"/subcategory/{ subcategory ['id' ]} /products?page=2" ).get_json ()
207+ page1 = self . client .get (f"/subcategory/{ subcategory ['id' ]} /products?page=1" ).get_json ()
208+ page2 = self . client .get (f"/subcategory/{ subcategory ['id' ]} /products?page=2" ).get_json ()
161209 assert len (page1 ["products" ]) == 10
162210 assert len (page2 ["products" ]) == 1
163211
164212 returned_product_ids = set (p ["id" ] for p in page1 ["products" ] + page2 ["products" ])
165213 assert returned_product_ids == product_ids
166214
167- def test_get_product_subcategories_populated (self , client , create_product , create_subcategory ):
215+ def test_get_product_subcategories_empty (self , create_product ):
216+ product = create_product ("Prod_NoSC" , "desc" ).get_json ()
217+ resp = self .client .get (f"/product/{ product ['id' ]} /subcategories" )
218+
219+ assert resp .status_code == 200
220+ data = resp .get_json ()
221+ assert "subcategories" in data
222+ assert data ["subcategories" ] == []
223+
224+ def test_get_product_subcategories_populated (self , create_product , create_subcategory ):
168225 subcategory1 = create_subcategory ("S1" ).get_json ()
169226 subcategory2 = create_subcategory ("S2" ).get_json ()
170227 product = create_product ("Prod_SC" , "desc" , subcategories = [subcategory1 ["id" ], subcategory2 ["id" ]]).get_json ()
171228
172- assert self ._product_subcategory_ids (client , product ["id" ]) == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
229+ resp = self .client .get (f"/product/{ product ['id' ]} /subcategories" )
230+
231+ assert resp .status_code == 200
232+ data = resp .get_json ()
233+ assert "subcategories" in data
234+ returned_ids = sorted ([sc ["id" ] for sc in data ["subcategories" ]])
235+ assert returned_ids == sorted ([subcategory1 ["id" ], subcategory2 ["id" ]])
173236
174237 @pytest .mark .parametrize (
175238 "path" ,
@@ -181,6 +244,6 @@ def test_get_product_subcategories_populated(self, client, create_product, creat
181244 "/product/999999/subcategories" ,
182245 ],
183246 )
184- def test_relationship_getters_404 (self , client , path ):
185- resp = client .get (path )
247+ def test_relationship_getters_404 (self , path ):
248+ resp = self . client .get (path )
186249 assert resp .status_code == 404
0 commit comments