Esempio n. 1
0
    def test_base_cache(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()
            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True,
                'columns': ['id', 'name']
            })
            from ereports.engine.cache import _cache

            _cache.set('aa', 1)
            assert _cache.get('aa') == 1

            ds1 = TestDatasource.as_datasource()
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(ds1)
                self.assertSequenceEqual(i1, list(ds2))

        with mock.patch('ereports.engine.cache._cache', dummy_cache):
            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True,
                'columns': ['id', 'name']
            })
            ds1 = TestDatasource.as_datasource()
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 2, connections['default']):
                i1 = list(ds1)
                self.assertSequenceEqual(i1, list(ds2))
Esempio n. 2
0
    def test_base_cache(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()
            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                                    'use_cache': True,
                                                                    'columns': ['id', 'name']})
            from ereports.engine.cache import _cache

            _cache.set('aa', 1)
            assert _cache.get('aa') == 1

            ds1 = TestDatasource.as_datasource()
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(ds1)
                self.assertSequenceEqual(i1, list(ds2))

        with mock.patch('ereports.engine.cache._cache', dummy_cache):
            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                                    'use_cache': True,
                                                                    'columns': ['id', 'name']})
            ds1 = TestDatasource.as_datasource()
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 2, connections['default']):
                i1 = list(ds1)
                self.assertSequenceEqual(i1, list(ds2))
Esempio n. 3
0
    def test_queryset_cache(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                                    'use_cache': True,
                                                                    'columns': ['id', 'name']})
            ds = TestDatasource.as_datasource()
            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(ds)
            with _AssertNumQueriesContext(self, 0, connections['default']):
                i2 = list(ds)
            self.assertSequenceEqual(i1, i2)
Esempio n. 4
0
    def test_queryset_cache(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True,
                'columns': ['id', 'name']
            })
            ds = TestDatasource.as_datasource()
            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(ds)
            with _AssertNumQueriesContext(self, 0, connections['default']):
                i2 = list(ds)
            self.assertSequenceEqual(i1, i2)
Esempio n. 5
0
 def test_queryset_no_hit_db(self):
     TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                             'use_cache': False,
                                                             'columns': ['id', 'name']})
     ds = TestDatasource.as_datasource()
     with _AssertNumQueriesContext(self, 0, connections['default']):
         ds._get_queryset()
Esempio n. 6
0
 def test_queryset_no_hit_db(self):
     TestDatasource = type('TestDatasource', (Datasource, ), {
         'model': Permission,
         'use_cache': False,
         'columns': ['id', 'name']
     })
     ds = TestDatasource.as_datasource()
     with _AssertNumQueriesContext(self, 0, connections['default']):
         ds._get_queryset()
Esempio n. 7
0
    def test_cache_different_columns(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission, 'use_cache': True})
            ds1 = TestDatasource.as_datasource(columns=['id', 'name'])
            ds2 = TestDatasource.as_datasource(columns=['id'])

            with _AssertNumQueriesContext(self, 2, connections['default']):
                list(ds1)
                list(ds2)
Esempio n. 8
0
    def test_cache(self):
        locmem_cache = cache.get_cache('django.core.cache.backends.locmem.LocMemCache')
        # dummy_cache = cache.get_cache('django.core.cache.backends.dummy.DummyCache')
        locmem_cache.clear()
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20, boolean=True)
            r1 = BaseReport.as_report(model=SimpleDemoModel, use_cache=True)
            r2 = BaseReport.as_report(model=SimpleDemoModel, use_cache=True)

            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(r1)
                self.assertSequenceEqual(i1, list(r2))
    def assert_queries_count(self, num, func=None, *args, **kwargs):
        from django.test.testcases import _AssertNumQueriesContext
        from django.db import connections, DEFAULT_DB_ALIAS

        using = kwargs.pop("using", DEFAULT_DB_ALIAS)
        conn = connections[using]

        context = _AssertNumQueriesContext(self, num, conn)
        if func is None:
            return None

        with context:
            return func(*args, **kwargs)
Esempio n. 10
0
    def assert_queries_count(self, num, func=None, *args, **kwargs):
        from django.test.testcases import _AssertNumQueriesContext
        from django.db import connections, DEFAULT_DB_ALIAS

        using = kwargs.pop("using", DEFAULT_DB_ALIAS)
        conn = connections[using]

        context = _AssertNumQueriesContext(self, num, conn)
        if func is None:
            return None

        with context:
            return func(*args, **kwargs)
Esempio n. 11
0
    def test_cache_different_columns(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True
            })
            ds1 = TestDatasource.as_datasource(columns=['id', 'name'])
            ds2 = TestDatasource.as_datasource(columns=['id'])

            with _AssertNumQueriesContext(self, 2, connections['default']):
                list(ds1)
                list(ds2)
Esempio n. 12
0
    def test_cache_invalidate_on_delete(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()
            monitor_model(Permission)
            reset(Permission)

            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                                    'use_cache': True,
                                                                    'columns': ['id', 'name']})
            ds1 = TestDatasource.as_datasource()
            list(ds1)
            Permission.objects.filter(pk=1).delete()
            ds2 = TestDatasource.as_datasource()
            with _AssertNumQueriesContext(self, 1, connections['default']):
                list(ds2)
Esempio n. 13
0
    def test_cache_different_filters(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource,), {'model': Permission,
                                                                    'use_cache': True,
                                                                    'columns': ['id', 'name']})
            ds1 = TestDatasource.as_datasource()
            ds1.add_filters(id__gt=0)
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 2, connections['default']):
                i1 = list(ds1)
                i2 = list(ds2)
                self.assertSequenceEqual(i1, i2)
Esempio n. 14
0
    def test_cache_invalidate_on_delete(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()
            monitor_model(Permission)
            reset(Permission)

            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True,
                'columns': ['id', 'name']
            })
            ds1 = TestDatasource.as_datasource()
            list(ds1)
            Permission.objects.filter(pk=1).delete()
            ds2 = TestDatasource.as_datasource()
            with _AssertNumQueriesContext(self, 1, connections['default']):
                list(ds2)
Esempio n. 15
0
    def test_cache_different_filters(self):
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            locmem_cache.clear()

            TestDatasource = type('TestDatasource', (Datasource, ), {
                'model': Permission,
                'use_cache': True,
                'columns': ['id', 'name']
            })
            ds1 = TestDatasource.as_datasource()
            ds1.add_filters(id__gt=0)
            ds2 = TestDatasource.as_datasource()

            with _AssertNumQueriesContext(self, 2, connections['default']):
                i1 = list(ds1)
                i2 = list(ds2)
                self.assertSequenceEqual(i1, i2)
Esempio n. 16
0
    def test_cache(self):
        locmem_cache = cache.get_cache(
            'django.core.cache.backends.locmem.LocMemCache')
        # dummy_cache = cache.get_cache('django.core.cache.backends.dummy.DummyCache')
        locmem_cache.clear()
        with mock.patch('ereports.engine.cache._cache', locmem_cache):
            G(SimpleDemoModel,
              n=2,
              char='abc',
              integer1=10,
              integer2=20,
              boolean=True)
            r1 = BaseReport.as_report(model=SimpleDemoModel, use_cache=True)
            r2 = BaseReport.as_report(model=SimpleDemoModel, use_cache=True)

            with _AssertNumQueriesContext(self, 1, connections['default']):
                i1 = list(r1)
                self.assertSequenceEqual(i1, list(r2))
Esempio n. 17
0
def assert_num_queries(num):
    """Context manager: assert ``num`` queries occur within block."""
    return _AssertNumQueriesContext(FakeTestCase(), num, connection)
Esempio n. 18
0
def assert_num_queries(num):
    """Context manager: assert ``num`` queries occur within block."""
    return _AssertNumQueriesContext(FakeTestCase(), num, connection)