예제 #1
0
    def test_basic_get_n_set(self):
        todo = FakeTodo(title='My todo')

        with self.assertNumQueries(0):
            todo.creme_entity = self.entity

        with self.assertNumQueries(0):
            ct = todo.entity_content_type
        self.assertEqual(self.get_ct(FakeContact), ct)

        with self.assertNumQueries(0):
            entity = todo.entity
        self.assertEqual(self.entity, entity)

        with self.assertNoException():
            todo.save()

        # ----
        todo = self.refresh(todo)
        self.assertEqual(self.entity.id, todo.entity_id)
        self.assertEqual(ct.id,          todo.entity_content_type_id)

        with self.assertNumQueries(1):
            creme_entity = todo.creme_entity
        self.assertEqual(self.entity, creme_entity)
예제 #2
0
    def test_get_none01(self):
        "Get initial None."
        todo = FakeTodo(
            title='My todo',
            # entity=entity,  # Not set
        )

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertIsNone(creme_entity)

        #  --
        todo.entity = entity = self.entity

        with self.assertRaises(ValueError):
            todo.creme_entity  # NOQA

        # --
        todo.entity_content_type_id = entity.entity_type_id

        with self.assertNumQueries(0):
            creme_entity2 = todo.creme_entity

        self.assertEqual(entity, creme_entity2)
예제 #3
0
    def test_cache_for_set01(self):
        "After a '__set__' with a real entity, '__get__' uses no query."
        entity = self.entity
        todo = FakeTodo(title='My todo')

        with self.assertNumQueries(0):
            todo.creme_entity = entity

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertEqual(entity, creme_entity)
예제 #4
0
    def test_set_none02(self):
        "Set None after not None on virtual field (cache invalidation)."
        entity = self.entity.get_real_entity()
        todo = FakeTodo(title='My todo',
                        creme_entity=entity,
                       )

        todo.creme_entity = None
        self.assertIsNone(todo.entity_id)
        self.assertIsNone(todo.entity_content_type_id)

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertIsNone(creme_entity)
예제 #5
0
    def test_set_none01(self):
        entity = self.entity
        todo = FakeTodo(
            title='My todo',
            entity=entity,
            entity_content_type=entity.entity_type,
        )

        todo.creme_entity = None
        self.assertIsNone(todo.entity_id)
        self.assertIsNone(todo.entity_content_type_id)

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertIsNone(creme_entity)
예제 #6
0
    def test_cache_for_set02(self):
        """After a '__set__' with a real entity, '__get__' uses no query
        (base entity version).
        """
        entity = CremeEntity.objects.get(id=self.entity.id)
        real_entity = entity.get_real_entity()

        todo = FakeTodo(title='My todo')

        with self.assertNumQueries(0):
            todo.creme_entity = entity

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertEqual(real_entity, creme_entity)
예제 #7
0
    def test_get_none02(self):
        "Get initial None (explicitly set)."
        todo = FakeTodo(title='My todo', entity=None)

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertIsNone(creme_entity)
예제 #8
0
    def test_get_real_entity(self):
        """Set a base entity, so '__get__' uses a query to retrieve the real
        entity.
        """
        entity = self.entity
        todo = FakeTodo(title='My todo')

        base_entity = CremeEntity.objects.get(id=entity.id)

        with self.assertNumQueries(0):
            todo.creme_entity = base_entity

        with self.assertNumQueries(1):
            creme_entity = todo.creme_entity
        self.assertEqual(entity, creme_entity)

        with self.assertNumQueries(0):
            creme_entity2 = todo.creme_entity
        self.assertEqual(entity, creme_entity2)
예제 #9
0
    def test_bad_ctype01(self):
        "Bad CT id + base entity id."
        todo = FakeTodo(
            title='My todo',
            entity_id=self.entity.id,
            # Does not correspond to 'self.entity'
            entity_content_type=self.get_ct(FakeOrganisation),
        )

        with self.assertRaises(FakeOrganisation.DoesNotExist):
            todo.creme_entity  # NOQA
예제 #10
0
    def test_missing_ctype02(self):
        "CT not set + entity ID set => error."
        todo = FakeTodo(title='My todo', entity_id=self.entity.id)

        with self.assertRaises(ValueError) as error_context:
            todo.creme_entity  # NOQA

        self.assertEqual(
            'The content type is not set while the entity is. '
            'HINT: set both by hand or just use the RealEntityForeignKey setter.',
            error_context.exception.args[0]
        )
예제 #11
0
    def test_fk_cache(self):
        """Do not retrieve real entity if already stored/retrieved in 'entity'
         attribute.
         """
        entity = self.entity
        todo = FakeTodo(
            title='My todo',
            entity=entity,  # <== real entity
            entity_content_type=entity.entity_type,  # Must be set (consistency protection)
        )

        with self.assertNumQueries(0):
            creme_entity = todo.creme_entity

        self.assertEqual(entity, creme_entity)
예제 #12
0
    def test_bad_ctype02(self):
        "Bad CT + base entity."
        todo = FakeTodo(
            title='My todo',
            # Not real entity...
            entity=CremeEntity.objects.get(id=self.entity.id),
            # Does not correspond to 'self.entity'
            entity_content_type=self.get_ct(FakeOrganisation),
        )

        with self.assertRaises(ValueError) as error_context:
            todo.creme_entity  # NOQA

        self.assertEqual(
            'The content type does not match this entity.',
            error_context.exception.args[0]
        )