def test_do_not_populate(self): """If there is no populator method with the proper name, nothing happens""" builder = BaseBuilder(None, None) instance = ModelForBuilderTests(simple_field=1) instance.save() builder.set_m2m_field(None, instance, 'm2m') self.assertEqual(0, instance.m2m.count())
def test_clear_m2m(self): """Tests that set_m2m_field clear the m2m before calling populator method""" builder = BaseBuilder(None, None) instance = ModelForBuilderTests(simple_field=1) instance.save() builder.set_m2m_field(None, instance, 'm2m') self.assertEqual(0, instance.m2m.count())
def test_field_should_not_be_populated(self): class Populator(BasePopulator): _fields_one_to_one = [] builder = BaseBuilder(None, None) instance = ModelForBuilderTests() populator = Populator(None, instance, None, builder) builder.set_field( populator, instance, None, 'simple_field', ) self.assertIsNone(instance.simple_field)
def test_field_is_populated_via_populator_method(self): class Populator(BasePopulator): _fields_one_to_one = [] def simple_field(self): self._instance.simple_field = 123 builder = BaseBuilder(None, None) instance = ModelForBuilderTests() populator = Populator(None, instance, None, builder) builder.set_field( populator, instance, None, 'simple_field', ) self.assertEqual(123, instance.simple_field)
def test_field_is_a_one_to_one(self): class Populator(object): _fields_one_to_one = ('simple_field', ) class Mapper(object): simple_field = 123 builder = BaseBuilder(None, None) populator = Populator() instance = ModelForBuilderTests() mapper = Mapper() builder.set_field( populator, instance, mapper, 'simple_field', ) self.assertEqual(123, instance.simple_field)
def test_populate_through_method(self): """If the populator has a method with the same name as the field is it called""" class Populator(BasePopulator): def m2m(self): related = RelatedM2M() related.save() self.related = related self._instance.m2m.add(related) builder = BaseBuilder(None, None) instance = ModelForBuilderTests(simple_field=1) instance.save() populator = Populator(None, instance, None, builder) builder.set_m2m_field(populator, instance, 'm2m') self.assertEqual(1, instance.m2m.count()) db_related = instance.m2m.all()[0] self.assertEqual(db_related, populator.related)
def test_field_is_a_one_to_one(self): class Populator(object): _fields_one_to_one = ('simple_field',) class Mapper(object): simple_field = 123 builder = BaseBuilder(None, None) populator = Populator() instance = ModelForBuilderTests() mapper = Mapper() builder.set_field( populator, instance, mapper, 'simple_field', ) self.assertEqual(123, instance.simple_field)
def test_skip(self): builder = BaseBuilder(None, None) self.assertRaises(NotImplementedError, builder.skip, None)
def test_instance_is_locally_modified(self): builder = BaseBuilder(None, None) self.assertRaises(NotImplementedError, builder.instance_is_locally_modified, None)
def test_populator(self): builder = BaseBuilder(None, None) self.assertRaises(NotImplementedError, lambda: builder.Populator)
def test_model(self): builder = BaseBuilder(None, None) self.assertRaises(NotImplementedError, lambda: builder.Model)