@@ -42,6 +42,14 @@ def _category_product_ids_via_subcategories(self, category_id):
4242 assert category is not None
4343 return sorted ({product .id for subcategory in category .subcategories for product in subcategory .products .all ()})
4444
45+ def _assert_related_collection (self , resp , key , expected_ids = [], status_code = 200 ):
46+ assert resp .status_code == status_code
47+ data = resp .get_json ()
48+ assert key in data
49+ returned_ids = sorted ([item ["id" ] for item in data [key ]])
50+ assert returned_ids == sorted (expected_ids )
51+
52+
4553 def test_create_category_with_subcategories (self , create_category , create_subcategory ):
4654 subcategory1 = create_subcategory ("SC_A" ).get_json ()
4755 subcategory2 = create_subcategory ("SC_B" ).get_json ()
@@ -120,33 +128,20 @@ def test_update_product_adds_subcategories(self, create_authenticated_headers, c
120128 def test_get_category_subcategories_empty (self , create_category ):
121129 category = create_category ("Cat_NoSC" ).get_json ()
122130 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" ] == []
131+ self ._assert_related_collection (resp , "subcategories" )
128132
129133 def test_get_category_subcategories_populated (self , create_category , create_subcategory ):
130134 subcategory1 = create_subcategory ("SC1" ).get_json ()
131135 subcategory2 = create_subcategory ("SC2" ).get_json ()
132136 category = create_category ("Cat_WithSC" , subcategories = [subcategory1 ["id" ], subcategory2 ["id" ]]).get_json ()
133137
134138 resp = self .client .get (f"/category/{ category ['id' ]} /subcategories" )
135-
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" ]])
139+ self ._assert_related_collection (resp , "subcategories" , expected_ids = [subcategory1 ["id" ], subcategory2 ["id" ]])
141140
142141 def test_get_category_products_empty (self , create_category ):
143142 category = create_category ("Cat_NoProd" ).get_json ()
144143 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" ] == []
144+ self ._assert_related_collection (resp , "products" )
150145
151146 def test_get_category_products_populated_with_pagination (self , create_category , create_subcategory , create_product ):
152147 category = create_category ("Cat_Prod" ).get_json ()
@@ -168,33 +163,20 @@ def test_get_category_products_populated_with_pagination(self, create_category,
168163 def test_get_subcategory_categories_empty (self , create_subcategory ):
169164 subcategory = create_subcategory ("SC_NoCat" ).get_json ()
170165 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" ] == []
166+ self ._assert_related_collection (resp , "categories" )
176167
177168 def test_get_subcategory_categories_populated (self , create_category , create_subcategory ):
178169 category1 = create_category ("C1" ).get_json ()
179170 category2 = create_category ("C2" ).get_json ()
180171 subcategory = create_subcategory ("SC_Cats" , categories = [category1 ["id" ], category2 ["id" ]]).get_json ()
181172
182173 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" ]])
174+ self ._assert_related_collection (resp , "categories" , expected_ids = [category1 ["id" ], category2 ["id" ]])
189175
190176 def test_get_subcategory_products_empty (self , create_subcategory ):
191177 subcategory = create_subcategory ("SC_NoProd" ).get_json ()
192178 resp = self .client .get (f"/subcategory/{ subcategory ['id' ]} /products" )
193-
194- assert resp .status_code == 200
195- data = resp .get_json ()
196- assert "products" in data
197- assert data ["products" ] == []
179+ self ._assert_related_collection (resp , "products" )
198180
199181 def test_get_subcategory_products_populated_with_pagination (self , create_subcategory , create_product ):
200182 subcategory = create_subcategory ("SC_Pag" ).get_json ()
@@ -215,24 +197,15 @@ def test_get_subcategory_products_populated_with_pagination(self, create_subcate
215197 def test_get_product_subcategories_empty (self , create_product ):
216198 product = create_product ("Prod_NoSC" , "desc" ).get_json ()
217199 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" ] == []
200+ self ._assert_related_collection (resp , "subcategories" )
223201
224202 def test_get_product_subcategories_populated (self , create_product , create_subcategory ):
225203 subcategory1 = create_subcategory ("S1" ).get_json ()
226204 subcategory2 = create_subcategory ("S2" ).get_json ()
227205 product = create_product ("Prod_SC" , "desc" , subcategories = [subcategory1 ["id" ], subcategory2 ["id" ]]).get_json ()
228206
229207 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" ]])
208+ self ._assert_related_collection (resp , "subcategories" , expected_ids = [subcategory1 ["id" ], subcategory2 ["id" ]])
236209
237210 @pytest .mark .parametrize (
238211 "path" ,
0 commit comments