Exemple #1
0
    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())
Exemple #2
0
    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())
Exemple #3
0
    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())
Exemple #4
0
    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())
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    def test_get_or_create_instance_create(self):
        """Builder.get_or_create_instance doesn't create an
        instance if it already exists"""
        ModelForBuilderTests(simple_field=1).save()

        mapper = self.Mapper(None)
        builder = self.Builder(None, None)
        instance = builder.get_or_create_instance(mapper)
        self.assertEqual(1, instance.simple_field)
        # the instance is fetched from db
        self.assertIsNotNone(instance.pk)
        self.assertEqual(1, ModelForBuilderTests.objects.all().count())
        db_instance = ModelForBuilderTests.objects.all()[0]
        self.assertEqual(db_instance, instance)
Exemple #8
0
    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)
Exemple #9
0
    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)
Exemple #10
0
    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)
Exemple #11
0
 def relatedm2m_set(self):
     for i in range(self._mapper.simple_field):
         m = ModelForBuilderTests(simple_field=i)
         self._instance.relatedm2m_set.add(m)