def test_error_is_logged_if_field_not_on_object(self): house = TestHouse.objects.create(address='Bottom of the hill') fields = {'address': 'Bottom of the hill', 'buyer=>last_name': 'Jones'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) for msg in self.logger_messages['warning']: self.assertIn('buyer', msg)
def test_update_from_fields_updates_related_fields(self): person = TestPerson.objects.create(first_name="Jill", last_name="Jones") house = TestHouse.objects.create(address='Bottom of the hill') fields = {'address': 'Bottom of the hill', 'owner=>first_name': 'Jill'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) self.assertEqual(person, house.owner)
def test_update_from_uses_none_if_field_is_nullable_and_value_is_empty_string(self): house = TestHouse.objects.create(address='Bottom of the hill') fields = {'address': 'Bottom of the hill', 'built': ''} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) house.save() house.refresh_from_db() self.assertEqual(house.built, None)
def test_it_adds_elements_to_many_to_many_with_plus_referred_to_delimiter(self): bob = TestBuilder.objects.create(first_name="Bob", last_name="The Builder") house = TestHouse.objects.create(address='Bottom of the hill') fields = { 'first_name': 'Bob', 'buildings=>+address': 'Bottom of the hill', } sut = ModelAction(TestBuilder, ['first_name'], fields) sut.update_from_fields(bob, True) self.assertIn(house, bob.buildings.all())
def test_related_fields_not_touched_if_referred_to_object_does_not_exist( self): house = TestHouse.objects.create(address='Bottom of the hill') fields = {'address': 'Bottom of the hill', 'owner=>last_name': 'Jones'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) self.assertEqual(None, house.owner) for msg in self.logger_messages['warning']: self.assertIn('Could not find TestPerson', msg) self.assertIn(str({'last_name': 'Jones'}), msg)
def test_it_logs_an_error_if_action_type_for_many_to_many_referred_fields_is_unknown(self): bob = TestBuilder.objects.create(first_name="Bob", last_name="The Builder") fields = { 'first_name': 'Bob', 'buildings=>*address': 'Bottom of the hill', } sut = ModelAction(TestBuilder, ['first_name'], fields) sut.update_from_fields(bob, True) for msg in self.logger_messages['warning']: self.assertIn('Unknown action type', msg)
def test_related_fields_update_does_update_if_forced(self): jill = TestPerson.objects.create(first_name="Jill", last_name="Jones") jack = TestPerson.objects.create(first_name="Jack", last_name="Jones") house = TestHouse.objects.create(address='Bottom of the hill', owner=jill) fields = { 'address': 'Bottom of the hill', 'owner=>first_name': 'Jack', 'owner=>last_name': 'Jones'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house, True) self.assertEqual(jack, house.owner)
def test_related_fields_are_not_touched_if_referred_to_object_ambiguous( self): TestPerson.objects.create(first_name="Jill", last_name="Jones") TestPerson.objects.create(first_name="Jack", last_name="Jones") house = TestHouse.objects.create(address='Bottom of the hill') fields = {'address': 'Bottom of the hill', 'owner=>last_name': 'Jones'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) self.assertEqual(None, house.owner) for msg in self.logger_messages['warning']: self.assertIn('Found multiple TestPerson objects', msg) self.assertIn(str({'last_name': 'Jones'}), msg)
def test_it_replaces_all_elements_in_many_to_many_with_equals_referred_to_delimiter(self): house1 = TestHouse.objects.create(address='Bottom of the hill') house2 = TestHouse.objects.create(address='Top of the hill') bob = TestBuilder.objects.create(first_name="Bob", last_name="The Builder") bob.buildings.add(house1) bob.save() fields = { 'first_name': 'Bob', 'buildings=>=address': 'Top of the hill', } sut = ModelAction(TestBuilder, ['first_name'], fields) sut.update_from_fields(bob, True) self.assertNotIn(house1, bob.buildings.all()) self.assertIn(house2, bob.buildings.all())
def test_related_fields_update_uses_all_available_filters(self): person = TestPerson.objects.create(first_name="John", last_name="Johnson") TestPerson.objects.create(first_name="Jack", last_name="Johnson") TestPerson.objects.create(first_name="John", last_name="Jackson") TestPerson.objects.create(first_name="Jack", last_name="Jackson") house = TestHouse.objects.create(address='Bottom of the hill') fields = { 'address': 'Bottom of the hill', 'owner=>first_name': 'John', 'owner=>last_name': 'Johnson'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house) self.assertEqual(person, house.owner)
def test_it_uses_all_related_fields_to_find_targets_for_many_to_many_fields(self): house1 = TestHouse.objects.create(address='Bottom of the hill', country='Australia') house2 = TestHouse.objects.create(address='Bottom of the hill', country='Belgium') bob = TestBuilder.objects.create(first_name="Bob", last_name="The Builder") bob.buildings.add(house1) bob.save() fields = { 'first_name': 'Bob', 'buildings=>=address': 'Bottom of the hill', 'buildings=>=country': 'Belgium', } sut = ModelAction(TestBuilder, ['first_name'], fields) sut.update_from_fields(bob, True) self.assertNotIn(house1, bob.buildings.all()) self.assertIn(house2, bob.buildings.all())
def test_related_fields_update_does_not_use_filters_with_values_as_empty_strings(self): """ This effectively prevents over-specification, and allows files to be constructed with "or" style relations """ jill = TestPerson.objects.create(first_name="Jill", last_name="Hill") jack = TestPerson.objects.create(first_name="Jack", last_name="Shack") house = TestHouse.objects.create(address='Bottom of the hill') fields = { 'address': 'Bottom of the hill', 'owner=>first_name': '', 'owner=>last_name': 'Shack'} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house, True) self.assertEqual(jack, house.owner) fields = { 'address': 'Bottom of the hill', 'owner=>first_name': 'Jill', 'owner=>last_name': ''} sut = ModelAction(TestHouse, ['address'], fields) sut.update_from_fields(house, True) self.assertEqual(jill, house.owner)