Exemple #1
0
    def test_deletion(self):
        """
        When an element is deleted, the cache should be updated automaticly via
        the autoupdate system. So there should be no db queries.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        topic3 = Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')
        list(topic_collection.get_full_data())

        collection.CollectionElement.from_instance(topic3, deleted=True)
        topic3.delete()

        with self.assertNumQueries(0):
            instance_list = list(collection.Collection('topics/topic').get_full_data())
        self.assertEqual(len(instance_list), 2)
Exemple #2
0
    def test_with_cache(self):
        """
        Tests that no db query is used when the list is received twice.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')
        list(topic_collection.get_full_data())

        with self.assertNumQueries(0):
            instance_list = list(topic_collection.get_full_data())
        self.assertEqual(len(instance_list), 3)
    def test_clean_cache(self):
        """
        Tests that the instances are retrieved from the database.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')
        get_redis_connection("default").flushall()

        with self.assertNumQueries(3):
            instance_list = list(topic_collection.get_full_data())
        self.assertEqual(len(instance_list), 3)
Exemple #4
0
    def test_clean_cache(self):
        """
        Tests that the instances are retrieved from the database.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')
        caches['locmem'].clear()

        with self.assertNumQueries(4):
            instance_list = list(
                topic_collection.as_autoupdate_for_projector())
        self.assertEqual(len(instance_list), 3)
Exemple #5
0
    def test_with_some_objects_in_the_cache(self):
        """
        One element (topic3) is in the cache and two are not.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        caches['locmem'].clear()
        Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')

        with self.assertNumQueries(4):
            instance_list = list(
                topic_collection.as_autoupdate_for_projector())
        self.assertEqual(len(instance_list), 3)
Exemple #6
0
    def test_clean_cache(self):
        """
        Tests that the instances are retrieved from the database.

        Currently there are 10 queries needed. This can change in the future,
        but it has to be more then zero.
        """
        Topic.objects.create(title='test topic1')
        Topic.objects.create(title='test topic2')
        Topic.objects.create(title='test topic3')
        topic_collection = collection.Collection('topics/topic')
        caches['locmem'].clear()

        with self.assertNumQueries(4):
            instance_list = list(
                topic_collection.as_autoupdate_for_projector())
        self.assertEqual(len(instance_list), 3)
Exemple #7
0
    def test_element_generator(self, mock_cache, mock_CollectionElement):
        """
        Test with the following scenario: The collection has three elements. Two
        are in the cache and one is not.
        """
        test_collection = collection.Collection('testmodule/model')
        test_collection.get_all_ids = MagicMock(return_value=set([1, 2, 3]))
        test_collection.get_model = MagicMock()
        test_collection.get_model().objects.get_full_queryset(
        ).filter.return_value = ['my_instance']
        mock_cache.get_many.return_value = {
            'testmodule/model:1': 'element1',
            'testmodule/model:2': 'element2'
        }

        list(test_collection.element_generator())

        mock_cache.get_many.assert_called_once_with(
            ['testmodule/model:1', 'testmodule/model:2', 'testmodule/model:3'])
        test_collection.get_model().objects.get_full_queryset(
        ).filter.assert_called_once_with(pk__in={3})
        self.assertEqual(mock_CollectionElement.from_values.call_count, 2)
        self.assertEqual(mock_CollectionElement.from_instance.call_count, 1)
Exemple #8
0
    def test_config_elements_with_cache(self):
        topic_collection = collection.Collection('core/config')
        list(topic_collection.as_autoupdate_for_projector())

        with self.assertNumQueries(0):
            list(topic_collection.as_autoupdate_for_projector())
Exemple #9
0
    def test_config_elements_without_cache(self):
        topic_collection = collection.Collection('core/config')
        caches['locmem'].clear()

        with self.assertNumQueries(1):
            list(topic_collection.as_autoupdate_for_projector())
Exemple #10
0
    def test_raw_cache_key(self):
        test_collection = collection.Collection('testmodule/model')

        self.assertEqual(test_collection.get_cache_key(raw=True),
                         ':1:testmodule/model')