77from rest_framework .settings import api_settings
88
99from drf_sideloading .serializers import SideLoadableSerializer
10- from tests .models import Category , Supplier , Product , Partner
10+ from tests .models import Category , Supplier , Product , Partner , ProductMetadata , SupplierMetadata
1111from tests .serializers import (
1212 ProductSerializer ,
1313 CategorySerializer ,
1414 SupplierSerializer ,
1515 PartnerSerializer ,
16+ ProductMetadataSerializer ,
1617)
1718from tests .viewsets import ProductViewSet
1819
@@ -32,27 +33,45 @@ def setUpClass(cls):
3233 def setUp (self ):
3334 self .category = Category .objects .create (name = "Category" )
3435 self .supplier1 = Supplier .objects .create (name = "Supplier1" )
36+ self .supplier_metadata_1 = SupplierMetadata .objects .create (
37+ supplier = self .supplier1 , properties = "Supplier1 metadata"
38+ )
3539 self .supplier2 = Supplier .objects .create (name = "Supplier2" )
40+ self .supplier_metadata_2 = SupplierMetadata .objects .create (
41+ supplier = self .supplier2 , properties = "Supplier2 metadata"
42+ )
3643 self .supplier3 = Supplier .objects .create (name = "Supplier3" )
44+ self .supplier_metadata_3 = SupplierMetadata .objects .create (
45+ supplier = self .supplier3 , properties = "Supplier3 metadata"
46+ )
3747 self .supplier4 = Supplier .objects .create (name = "Supplier4" )
48+ self .supplier_metadata_4 = SupplierMetadata .objects .create (
49+ supplier = self .supplier4 , properties = "Supplier4 metadata"
50+ )
3851 self .partner1 = Partner .objects .create (name = "Partner1" )
3952 self .partner2 = Partner .objects .create (name = "Partner2" )
4053 self .partner3 = Partner .objects .create (name = "Partner3" )
4154 self .partner4 = Partner .objects .create (name = "Partner4" )
4255
4356 self .product1 = Product .objects .create (name = "Product1" , category = self .category , supplier = self .supplier1 )
57+ self .product1_metadata = ProductMetadata .objects .create (product = self .product1 , properties = "value 1" )
4458 self .product1 .partners .add (self .partner1 )
4559 self .product1 .partners .add (self .partner2 )
4660 self .product1 .partners .add (self .partner4 )
4761 self .product1 .save ()
4862
4963 self .product2 = Product .objects .create (name = "Product2" , category = self .category , supplier = self .supplier2 )
50- self .product1 .partners .add (self .partner2 )
51- self .product1 .save ()
64+ self .product2_metadata = ProductMetadata .objects .create (product = self .product2 , properties = "value 2" )
65+ self .product2 .partners .add (self .partner2 )
66+ self .product2 .save ()
67+
5268 self .product3 = Product .objects .create (name = "Product3" , category = self .category , supplier = self .supplier3 )
53- self .product1 .partners .add (self .partner3 )
54- self .product1 .save ()
69+ self .product3_metadata = ProductMetadata .objects .create (product = self .product3 , properties = "value 3" )
70+ self .product3 .partners .add (self .partner3 )
71+ self .product3 .save ()
72+
5573 self .product4 = Product .objects .create (name = "Product4" , category = self .category , supplier = self .supplier4 )
74+ self .product4_metadata = ProductMetadata .objects .create (product = self .product4 , properties = "value 4" )
5675
5776
5877###################################
@@ -68,13 +87,16 @@ class TempProductSideloadableSerializer(SideLoadableSerializer):
6887 categories = CategorySerializer (source = "category" , many = True )
6988 suppliers = SupplierSerializer (source = "supplier" , many = True )
7089 partners = PartnerSerializer (many = True )
90+ partners = PartnerSerializer (many = True )
91+ metadata = ProductMetadataSerializer (many = True )
7192
7293 class Meta :
7394 primary = "products"
7495 prefetches = {
7596 "categories" : "category" ,
76- "suppliers" : "supplier" ,
97+ "suppliers" : [ "supplier" , "supplier__metadata" ] ,
7798 "partners" : "partners" ,
99+ "metadata" : "metadata" ,
78100 }
79101
80102 ProductViewSet .sideloading_serializer_class = TempProductSideloadableSerializer
@@ -89,11 +111,39 @@ def test_list(self):
89111 def test_list_sideloading (self ):
90112 """Test sideloading for all defined relations"""
91113 response = self .client .get (
92- path = reverse ("product-list" ), data = {"sideload" : "categories,suppliers,partners" }, ** self .DEFAULT_HEADERS
114+ path = reverse ("product-list" ),
115+ data = {"sideload" : "categories,suppliers,partners,metadata" },
116+ ** self .DEFAULT_HEADERS ,
93117 )
94118 self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
95119 self .assertIsInstance (response .json (), dict )
96- self .assertListEqual (["products" , "categories" , "suppliers" , "partners" ], list (response .json ().keys ()))
120+ self .assertListEqual (
121+ ["products" , "categories" , "suppliers" , "partners" , "metadata" ], list (response .json ().keys ())
122+ )
123+
124+ def test_list_sideloading_with_direct_missing_one_to_one_relation (self ):
125+ """Test sideloading for all defined relations"""
126+ ProductMetadata .objects .all ().delete ()
127+ response = self .client .get (
128+ path = reverse ("product-list" ),
129+ data = {"sideload" : "metadata" },
130+ ** self .DEFAULT_HEADERS ,
131+ )
132+ self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
133+ self .assertIsInstance (response .json (), dict )
134+ self .assertListEqual (["products" , "metadata" ], list (response .json ().keys ()))
135+
136+ def test_list_sideloading_with_indirect_missing_one_to_one_relation (self ):
137+ """Test sideloading for all defined relations"""
138+ SupplierMetadata .objects .all ().delete ()
139+ response = self .client .get (
140+ path = reverse ("product-list" ),
141+ data = {"sideload" : "suppliers" },
142+ ** self .DEFAULT_HEADERS ,
143+ )
144+ self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
145+ self .assertIsInstance (response .json (), dict )
146+ self .assertListEqual (["products" , "suppliers" ], list (response .json ().keys ()))
97147
98148 def test_list_partial_sideloading (self ):
99149 """Test sideloading for selected relations"""
@@ -108,22 +158,48 @@ def test_detail(self):
108158 response = self .client .get (path = reverse ("product-detail" , args = [self .product1 .id ]), ** self .DEFAULT_HEADERS )
109159 self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
110160 self .assertIsInstance (response .json (), dict )
111- self .assertListEqual (["name" , "category" , "supplier" , "partners" ], list (response .json ().keys ()))
161+ self .assertListEqual (["name" , "category" , "supplier" , "partners" , "metadata" ], list (response .json ().keys ()))
112162 # TODO: check details
113163
114164 def test_detail_sideloading (self ):
115165 """Test sideloading for all defined relations in detail view"""
116166 response = self .client .get (
117167 path = reverse ("product-detail" , args = [self .product1 .id ]),
118- data = {"sideload" : "categories,suppliers,partners" },
168+ data = {"sideload" : "categories,suppliers,partners,metadata " },
119169 ** self .DEFAULT_HEADERS ,
120170 )
121171 self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
122172 self .assertIsInstance (response .json (), dict )
123- self .assertListEqual (["products" , "categories" , "suppliers" , "partners" ], list (response .json ().keys ()))
173+ self .assertListEqual (
174+ ["products" , "categories" , "suppliers" , "partners" , "metadata" ], list (response .json ().keys ())
175+ )
124176 self .assertEqual (1 , len (response .json ().get ("products" )))
125177 # TODO: check details
126178
179+ def test_detail_sideloading_with_direct_missing_one_to_one_relation (self ):
180+ """Test sideloading for all defined relations"""
181+ ProductMetadata .objects .all ().delete ()
182+ response = self .client .get (
183+ path = reverse ("product-detail" , args = [self .product1 .id ]),
184+ data = {"sideload" : "metadata" },
185+ ** self .DEFAULT_HEADERS ,
186+ )
187+ self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
188+ self .assertIsInstance (response .json (), dict )
189+ self .assertListEqual (["products" , "metadata" ], list (response .json ().keys ()))
190+
191+ def test_detail_sideloading_with_indirect_missing_one_to_one_relation (self ):
192+ """Test sideloading for all defined relations"""
193+ SupplierMetadata .objects .all ().delete ()
194+ response = self .client .get (
195+ path = reverse ("product-detail" , args = [self .product1 .id ]),
196+ data = {"sideload" : "suppliers" },
197+ ** self .DEFAULT_HEADERS ,
198+ )
199+ self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
200+ self .assertIsInstance (response .json (), dict )
201+ self .assertListEqual (["products" , "suppliers" ], list (response .json ().keys ()))
202+
127203 def test_detail_partial_sideloading (self ):
128204 """Test sideloading for selected relations in detail view"""
129205 response = self .client .get (
@@ -940,7 +1016,7 @@ def test_detail(self):
9401016 )
9411017 self .assertEqual (response .status_code , status .HTTP_200_OK , response .json ())
9421018 self .assertIsInstance (response .json (), dict )
943- self .assertListEqual (["name" , "category" , "supplier" , "partners" ], list (response .json ().keys ()))
1019+ self .assertListEqual (["name" , "category" , "supplier" , "partners" , "metadata" ], list (response .json ().keys ()))
9441020
9451021
9461022class TestDrfSideloadingBrowsableApiPermissions (BaseTestCase ):
@@ -1009,7 +1085,7 @@ def test_sideloading_allow_post_without_sideloading(self):
10091085 )
10101086 self .assertEqual (response .status_code , status .HTTP_201_CREATED )
10111087 self .assertTrue (isinstance (response .json (), dict ))
1012- self .assertListEqual (["name" , "category" , "supplier" , "partners" ], list (response .json ().keys ()))
1088+ self .assertListEqual (["name" , "category" , "supplier" , "partners" , "metadata" ], list (response .json ().keys ()))
10131089
10141090 def test_sideloading_allow_post_with_sideloading (self ):
10151091 # TODO: check response with new detail view sideloading logic!
@@ -1029,7 +1105,7 @@ def test_sideloading_allow_post_with_sideloading(self):
10291105 )
10301106 self .assertEqual (response .status_code , status .HTTP_201_CREATED )
10311107 self .assertTrue (isinstance (response .json (), dict ))
1032- self .assertListEqual (["name" , "category" , "supplier" , "partners" ], list (response .json ().keys ()))
1108+ self .assertListEqual (["name" , "category" , "supplier" , "partners" , "metadata" ], list (response .json ().keys ()))
10331109
10341110
10351111class ProductSideloadSameSourceDuplicationTestCase (BaseTestCase ):
0 commit comments