def test_update_objs_two_fields_to_update(): """ Tests when objects are given to bulk update with two fields to update. """ test_obj_1 = ddf.G(models.TestModel, int_field=1, float_field=1.0) test_obj_2 = ddf.G(models.TestModel, int_field=2, float_field=2.0) # Change the int and float fields on the models test_obj_1.int_field = 3 test_obj_2.int_field = 4 test_obj_1.float_field = 3.0 test_obj_2.float_field = 4.0 # Do a bulk update with the int fields pgbulk.update( models.TestModel, [test_obj_1, test_obj_2], ['int_field', 'float_field'], ) # The test objects int fields should be untouched test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field == 3 assert test_obj_2.int_field == 4 # The float fields should be updated assert test_obj_1.float_field == 3.0 assert test_obj_2.float_field == 4.0
def test_update_char_pk(): """ Tests a bulk update on a model that has a primary key to a char field. """ ddf.G(models.TestPkChar, char_field='hi', my_key='1') pgbulk.update( models.TestPkChar, [models.TestPkChar(my_key='1', char_field='hello')], ['char_field'], ) assert models.TestPkChar.objects.count() == 1 assert models.TestPkChar.objects.filter( char_field='hello', my_key='1' ).exists()
def test_update_foreign_key_pk(): """ Tests a bulk update on a model that has a primary key to a foreign key. It uses the foreign key itself in the update """ t = ddf.G(models.TestPkForeignKey, char_field='hi') pgbulk.update( models.TestPkForeignKey, [models.TestPkForeignKey(my_key=t.my_key, char_field='hello')], ['char_field'], ) assert models.TestPkForeignKey.objects.count() == 1 assert models.TestPkForeignKey.objects.filter( char_field='hello', my_key=t.my_key ).exists()
def test_update_chars_to_null(): """ Tests updating a char field to a null field. """ test_obj_1 = ddf.G(models.TestModel, int_field=1, char_field='2') test_obj_2 = ddf.G(models.TestModel, int_field=2, char_field='3') test_obj_1.char_field = None test_obj_2.char_field = None pgbulk.update(models.TestModel, [test_obj_1, test_obj_2], ['char_field']) test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.char_field is None assert test_obj_2.char_field is None
def test_update_ints_to_null(): """ Tests updating an int field to a null field. """ test_obj_1 = ddf.G(models.TestModel, int_field=1, float_field=2) test_obj_2 = ddf.G(models.TestModel, int_field=2, float_field=3) test_obj_1.int_field = None test_obj_2.int_field = None pgbulk.update(models.TestModel, [test_obj_1, test_obj_2], ['int_field']) test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field is None assert test_obj_2.int_field is None
def test_update_objs_one_field_to_update(): """ Tests when objects are given to bulk update with one field to update. """ test_obj_1 = ddf.G(models.TestModel, int_field=1) test_obj_2 = ddf.G(models.TestModel, int_field=2) # Change the int fields on the models test_obj_1.int_field = 3 test_obj_2.int_field = 4 # Do a bulk update with the int fields pgbulk.update(models.TestModel, [test_obj_1, test_obj_2], ['int_field']) # The test objects int fields should be untouched test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field == 3 assert test_obj_2.int_field == 4
def test_update_objs_no_fields_to_update(): """ Tests when objects are given to bulk update with no fields to update. Nothing should change in the objects. """ test_obj_1 = ddf.G(models.TestModel, int_field=1) test_obj_2 = ddf.G(models.TestModel, int_field=2) # Change the int fields on the models test_obj_1.int_field = 3 test_obj_2.int_field = 4 # Do a bulk update with no update fields pgbulk.update(models.TestModel, [test_obj_1, test_obj_2], []) # The test objects int fields should be untouched test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field == 1 assert test_obj_2.int_field == 2
def test_update_objects_with_custom_db_field_types(): """ Tests when objects are updated that have custom field types """ test_obj_1 = ddf.G( models.TestModel, int_field=1, float_field=1.0, json_field={'test': 'test'}, array_field=['one', 'two'], ) test_obj_2 = ddf.G( models.TestModel, int_field=2, float_field=2.0, json_field={'test2': 'test2'}, array_field=['three', 'four'], ) # Change the fields on the models test_obj_1.json_field = {'test': 'updated'} test_obj_1.array_field = ['one', 'two', 'updated'] test_obj_2.json_field = {'test2': 'updated'} test_obj_2.array_field = ['three', 'four', 'updated'] # Do a bulk update with the int fields pgbulk.update( models.TestModel, [test_obj_1, test_obj_2], ['json_field', 'array_field'], ) # Refetch the objects test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) # Assert that the json field was updated assert test_obj_1.json_field == {'test': 'updated'} assert test_obj_2.json_field == {'test2': 'updated'} # Assert that the array field was updated assert test_obj_1.array_field, ['one', 'two' == 'updated'] assert test_obj_2.array_field, ['three', 'four' == 'updated']
def test_update_no_fields_given(): """ Tests updating when no fields are given. All fields should be updated """ test_obj_1 = ddf.G(models.TestModel, int_field=1, float_field=2) test_obj_2 = ddf.G(models.TestModel, int_field=2, float_field=3) test_obj_1.int_field = 7 test_obj_1.float_field = 8 test_obj_2.int_field = 9 test_obj_2.float_field = 10 pgbulk.update(models.TestModel, [test_obj_1, test_obj_2]) test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field == 7 assert test_obj_1.float_field == 8 assert test_obj_2.int_field == 9 assert test_obj_2.float_field == 10
def test_update_objs_one_field_to_update_ignore_other_field(): """ Tests when objects are given to bulk update with one field to update. This test changes another field not included in the update and verifies it is not updated. """ test_obj_1 = ddf.G(models.TestModel, int_field=1, float_field=1.0) test_obj_2 = ddf.G(models.TestModel, int_field=2, float_field=2.0) # Change the int and float fields on the models test_obj_1.int_field = 3 test_obj_2.int_field = 4 test_obj_1.float_field = 3.0 test_obj_2.float_field = 4.0 # Do a bulk update with the int fields pgbulk.update(models.TestModel, [test_obj_1, test_obj_2], ['int_field']) # The test objects int fields should be untouched test_obj_1 = models.TestModel.objects.get(id=test_obj_1.id) test_obj_2 = models.TestModel.objects.get(id=test_obj_2.id) assert test_obj_1.int_field == 3 assert test_obj_2.int_field == 4 # The float fields should not be updated assert test_obj_1.float_field == 1.0 assert test_obj_2.float_field == 2.0
def test_update_none(): """ Tests when no values are provided to bulk update. """ pgbulk.update(models.TestModel, [], [])
def test_update_foreign_key_by_name(): t_model = ddf.G(models.TestModel) t_fk_model = ddf.G(models.TestForeignKeyModel) t_fk_model.test_model = t_model pgbulk.update(models.TestForeignKeyModel, [t_fk_model], ['test_model']) assert models.TestForeignKeyModel.objects.get().test_model == t_model