@@ -134,3 +134,157 @@ def test_primary_key_update_failure(self):
134134 m0 = TestUpdateModel .create (count = 5 , text = 'monkey' )
135135 with self .assertRaises (ValidationError ):
136136 m0 .update (partition = uuid4 ())
137+
138+
139+ class ModelWithDefault (Model ):
140+ id = columns .Integer (primary_key = True )
141+ mf = columns .Map (columns .Integer , columns .Integer )
142+ dummy = columns .Integer (default = 42 )
143+
144+
145+ class ModelWithDefaultCollection (Model ):
146+ id = columns .Integer (primary_key = True )
147+ mf = columns .Map (columns .Integer , columns .Integer , default = {2 :2 })
148+ dummy = columns .Integer (default = 42 )
149+
150+
151+ class ModelWithDefaultTests (BaseCassEngTestCase ):
152+ def setUp (self ):
153+ sync_table (ModelWithDefault )
154+
155+ def tearDown (self ):
156+ drop_table (ModelWithDefault )
157+
158+ def test_value_override_with_default (self ):
159+ """
160+ Updating a row with a new Model instance shouldn't set columns to defaults
161+
162+ @since 3.9
163+ @jira_ticket PYTHON-657
164+ @expected_result column value should not change
165+
166+ @test_category object_mapper
167+ """
168+ initial = ModelWithDefault (id = 1 , mf = {0 : 0 }, dummy = 0 )
169+ initial .save ()
170+
171+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
172+ {'id' : 1 , 'dummy' : 0 , 'mf' : {0 : 0 }})
173+
174+ second = ModelWithDefault (id = 1 )
175+ second .update (mf = {0 : 1 })
176+
177+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
178+ {'id' : 1 , 'dummy' : 0 , 'mf' : {0 : 1 }})
179+
180+ def test_value_is_written_if_is_default (self ):
181+ """
182+ Check if the we try to update with the default value, the update
183+ happens correctly
184+ @since 3.9
185+ @jira_ticket PYTHON-657
186+ @expected_result column value should be updated
187+
188+ @test_category object_mapper
189+ :return:
190+ """
191+ initial = ModelWithDefault (id = 1 )
192+ initial .mf = {0 : 0 }
193+ initial .dummy = 42
194+ initial .update ()
195+
196+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
197+ {'id' : 1 , 'dummy' : 42 , 'mf' : {0 : 0 }})
198+
199+ def test_null_update_is_respected (self ):
200+ """
201+ Check if the we try to update with None under particular
202+ circumstances, it works correctly
203+ @since 3.9
204+ @jira_ticket PYTHON-657
205+ @expected_result column value should be updated to None
206+
207+ @test_category object_mapper
208+ :return:
209+ """
210+ ModelWithDefault .create (id = 1 , mf = {0 : 0 }).save ()
211+
212+ q = ModelWithDefault .objects .all ().allow_filtering ()
213+ obj = q .filter (id = 1 ).get ()
214+
215+ obj .update (dummy = None )
216+
217+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
218+ {'id' : 1 , 'dummy' : None , 'mf' : {0 : 0 }})
219+
220+ def test_only_set_values_is_updated (self ):
221+ """
222+ Test the updates work as expected when an object is deleted
223+ @since 3.9
224+ @jira_ticket PYTHON-657
225+ @expected_result the non updated column is None and the
226+ updated column has the set value
227+
228+ @test_category object_mapper
229+ """
230+
231+ ModelWithDefault .create (id = 1 , mf = {1 : 1 }, dummy = 1 ).save ()
232+
233+ item = ModelWithDefault .filter (id = 1 ).first ()
234+ ModelWithDefault .objects (id = 1 ).delete ()
235+ item .mf = {1 : 2 }
236+
237+ item .save ()
238+
239+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
240+ {'id' : 1 , 'dummy' : None , 'mf' : {1 : 2 }})
241+
242+ def test_collections (self ):
243+ """
244+ Test the updates work as expected when an object is deleted
245+ @since 3.9
246+ @jira_ticket PYTHON-657
247+ @expected_result the non updated column is None and the
248+ updated column has the set value
249+
250+ @test_category object_mapper
251+ """
252+ ModelWithDefault .create (id = 1 , mf = {1 : 1 , 2 : 1 }, dummy = 1 ).save ()
253+ item = ModelWithDefault .filter (id = 1 ).first ()
254+
255+ item .update (mf = {2 :1 })
256+ self .assertEqual (ModelWithDefault .objects ().all ().get ()._as_dict (),
257+ {'id' : 1 , 'dummy' : 1 , 'mf' : {2 : 1 }})
258+
259+ def test_collection_with_default (self ):
260+ """
261+ Test the updates work as expected when an object is deleted
262+ @since 3.9
263+ @jira_ticket PYTHON-657
264+ @expected_result the non updated column is None and the
265+ updated column has the set value
266+
267+ @test_category object_mapper
268+ """
269+ sync_table (ModelWithDefaultCollection )
270+ item = ModelWithDefaultCollection .create (id = 1 , mf = {1 : 1 }, dummy = 1 ).save ()
271+ self .assertEqual (ModelWithDefaultCollection .objects ().all ().get ()._as_dict (),
272+ {'id' : 1 , 'dummy' : 1 , 'mf' : {1 : 1 }})
273+
274+ item .update (mf = {2 : 2 })
275+ self .assertEqual (ModelWithDefaultCollection .objects ().all ().get ()._as_dict (),
276+ {'id' : 1 , 'dummy' : 1 , 'mf' : {2 : 2 }})
277+
278+ item .update (mf = None )
279+ self .assertEqual (ModelWithDefaultCollection .objects ().all ().get ()._as_dict (),
280+ {'id' : 1 , 'dummy' : 1 , 'mf' : {}})
281+
282+ item = ModelWithDefaultCollection .create (id = 2 , dummy = 2 ).save ()
283+ self .assertEqual (ModelWithDefaultCollection .objects ().all ().get (id = 2 )._as_dict (),
284+ {'id' : 2 , 'dummy' : 2 , 'mf' : {2 : 2 }})
285+
286+ item .update (mf = {1 : 1 , 4 : 4 })
287+ self .assertEqual (ModelWithDefaultCollection .objects ().all ().get (id = 2 )._as_dict (),
288+ {'id' : 2 , 'dummy' : 2 , 'mf' : {1 : 1 , 4 : 4 }})
289+
290+ drop_table (ModelWithDefaultCollection )
0 commit comments