Exemplo n.º 1
0
    def test_query_is_hashable(self):
        store = IterableStore([])
        manager = store.query(TestModel)
        qs = manager.filter((TestModel.order > 1) & (TestModel.order < 12))
        qs = qs.order_by(TestModel.a, ~TestModel.order)

        query = qs.query
        query.hints["aggregates"] = (aggregates.Sum("value"),)
        a1 = query
        a2 = a1.clone()

        self.assertEqual(hash(a1), hash(a2))
Exemplo n.º 2
0
    def test_query_is_hashable(self):
        store = IterableStore([])
        manager = store.query(TestModel)
        qs = manager.filter((TestModel.order > 1) & (TestModel.order < 12))
        qs = qs.order_by(TestModel.a, ~TestModel.order)

        query = qs.query
        query.hints['aggregates'] = (aggregates.Sum('value'), )
        a1 = query
        a2 = a1.clone()

        self.assertEqual(hash(a1), hash(a2))
Exemplo n.º 3
0
    def test_field_exists(self):

        families = [{
            'name': 'Community',
            'postal_adress': 'Greendale',
        }, {
            'name': 'Misfits',
        }]

        class Family(lifter.models.Model):
            pass

        manager = IterableStore(families).query(Family)

        self.assertEqual(manager.filter(Family.postal_adress.exists()),
                         [families[0]])
        self.assertEqual(manager.filter(~Family.postal_adress.exists()),
                         [families[1]])
    def test_field_exists(self):

        families = [
            {
                'name': 'Community',
                'postal_adress': 'Greendale',
            },
            {
                'name': 'Misfits',
            }
        ]

        class Family(lifter.models.Model):
            pass

        manager = IterableStore(families).query(Family)

        self.assertEqual(manager.filter(Family.postal_adress.exists()), [families[0]])
        self.assertEqual(manager.filter(~Family.postal_adress.exists()), [families[1]])
Exemplo n.º 5
0
    def setUp(self):
        self.cache = caches.DummyCache()

        PARENTS = [
            TestModel(name='parent_1'),
            TestModel(name='parent_2'),
        ]
        OBJECTS = [
            TestModel(name='test_1',
                      order=2,
                      a=1,
                      parent=PARENTS[0],
                      label='alabama',
                      surname='Mister T'),
            TestModel(name='test_2',
                      order=3,
                      a=1,
                      parent=PARENTS[0],
                      label='arkansas',
                      surname='Colonel'),
            TestModel(name='test_3',
                      order=1,
                      a=2,
                      parent=PARENTS[1],
                      label='texas',
                      surname='Lincoln'),
            TestModel(name='test_4',
                      order=4,
                      a=2,
                      parent=PARENTS[1],
                      label='washington',
                      surname='clint'),
        ]
        self.store = IterableStore(OBJECTS,
                                   identifier='test',
                                   cache=self.cache)
        self.manager = self.store.query(TestModel)
Exemplo n.º 6
0
    def setUp(self):
        self.cache = caches.DummyCache()

        PARENTS = [
                TestModel(name='parent_1'),
                TestModel(name='parent_2'),
            ]
        OBJECTS = [
            TestModel(name='test_1', order=2, a=1, parent=PARENTS[0], label='alabama', surname='Mister T'),
            TestModel(name='test_2', order=3, a=1, parent=PARENTS[0], label='arkansas', surname='Colonel'),
            TestModel(name='test_3', order=1, a=2, parent=PARENTS[1], label='texas', surname='Lincoln'),
            TestModel(name='test_4', order=4, a=2, parent=PARENTS[1], label='washington', surname='clint'),
        ]
        self.store = IterableStore(OBJECTS, identifier='test', cache=self.cache)
        self.manager = self.store.query(TestModel)
Exemplo n.º 7
0
class TestCache(unittest.TestCase):
    def setUp(self):
        self.cache = caches.DummyCache()

        PARENTS = [
                TestModel(name='parent_1'),
                TestModel(name='parent_2'),
            ]
        OBJECTS = [
            TestModel(name='test_1', order=2, a=1, parent=PARENTS[0], label='alabama', surname='Mister T'),
            TestModel(name='test_2', order=3, a=1, parent=PARENTS[0], label='arkansas', surname='Colonel'),
            TestModel(name='test_3', order=1, a=2, parent=PARENTS[1], label='texas', surname='Lincoln'),
            TestModel(name='test_4', order=4, a=2, parent=PARENTS[1], label='washington', surname='clint'),
        ]
        self.store = IterableStore(OBJECTS, identifier='test', cache=self.cache)
        self.manager = self.store.query(TestModel)

    def test_store_uses_store_model_app_name_and_hashed_query_for_cache_key(self):
        refined_store = self.manager.store
        query = self.manager.all().query
        cache_parts = [
            self.store.identifier,
            TestModel._meta.app_name,
            TestModel._meta.name,
            refined_store.hash_query(query),
        ]
        expected = ':'.join(cache_parts)
        self.assertEqual(refined_store.get_cache_key(query), expected)

    def test_store_tries_to_return_from_cache_before_executing_query(self):
        with mock.patch('lifter.store.RefinedStore.get_from_cache', side_effect=exceptions.NotInCache()) as m:
            qs = self.manager.all()
            query = qs.query
            list(qs)
            m.assert_called_with(query)

    def test_store_stores_result_in_cache_when_queyr_is_executed(self):
        r = self.manager.count()
        cache_key = list(self.cache._data.keys())[0]
        expires_on, value = self.cache._data[cache_key]
        self.assertEqual(value, r)

    @mock.patch('lifter.caches.Cache._get')
    def test_can_disable_cache(self, mocked):
        with self.cache.disable():
            r = self.manager.count()
            self.manager.count()
            mocked.assert_not_called()
Exemplo n.º 8
0
 def test_model(self):
     manager = IterableStore(self.OBJECTS).query(TestModel)
     self.assertEqual(manager.filter(TestModel.a == 1), self.OBJECTS[:2])
Exemplo n.º 9
0
 def setUp(self):
     self.manager = IterableStore(self.OBJECTS).query(TestModel)
     self.dict_manager = IterableStore(self.DICTS).query(TestModel)
Exemplo n.º 10
0
 def setUp(self):
     class TestModel(lifter.models.Model):
         pass
     self.manager = IterableStore(self.OBJECTS).query(TestModel)
     self.dict_manager = IterableStore(self.DICTS).query(TestModel)
Exemplo n.º 11
0
 def exclude(self, *args, **kwargs):
     all_objects = self.all()
     store = IterableStore(all_objects)
     manager = store.query(self)
     return manager.exclude(*args, **kwargs)
Exemplo n.º 12
0
 def all(self):
     all_instances = [self.klass(eid=i.eid, **i) for i in self._table.all()]
     store = IterableStore(all_instances)
     manager = store.query(self)
     return manager.all()
 def test_model(self):
     manager = IterableStore(self.OBJECTS).query(TestModel)
     self.assertEqual(manager.filter(TestModel.a == 1), self.OBJECTS[:2])
Exemplo n.º 14
0
class TestCache(unittest.TestCase):
    def setUp(self):
        self.cache = caches.DummyCache()

        PARENTS = [
            TestModel(name='parent_1'),
            TestModel(name='parent_2'),
        ]
        OBJECTS = [
            TestModel(name='test_1',
                      order=2,
                      a=1,
                      parent=PARENTS[0],
                      label='alabama',
                      surname='Mister T'),
            TestModel(name='test_2',
                      order=3,
                      a=1,
                      parent=PARENTS[0],
                      label='arkansas',
                      surname='Colonel'),
            TestModel(name='test_3',
                      order=1,
                      a=2,
                      parent=PARENTS[1],
                      label='texas',
                      surname='Lincoln'),
            TestModel(name='test_4',
                      order=4,
                      a=2,
                      parent=PARENTS[1],
                      label='washington',
                      surname='clint'),
        ]
        self.store = IterableStore(OBJECTS,
                                   identifier='test',
                                   cache=self.cache)
        self.manager = self.store.query(TestModel)

    def test_store_uses_store_model_app_name_and_hashed_query_for_cache_key(
            self):
        refined_store = self.manager.store
        query = self.manager.all().query
        cache_parts = [
            self.store.identifier,
            TestModel._meta.app_name,
            TestModel._meta.name,
            refined_store.hash_query(query),
        ]
        expected = ':'.join(cache_parts)
        self.assertEqual(refined_store.get_cache_key(query), expected)

    def test_store_tries_to_return_from_cache_before_executing_query(self):
        with mock.patch('lifter.store.RefinedStore.get_from_cache',
                        side_effect=exceptions.NotInCache()) as m:
            qs = self.manager.all()
            query = qs.query
            list(qs)
            m.assert_called_with(query)

    def test_store_stores_result_in_cache_when_queyr_is_executed(self):
        r = self.manager.count()
        cache_key = list(self.cache._data.keys())[0]
        expires_on, value = self.cache._data[cache_key]
        self.assertEqual(value, r)

    @mock.patch('lifter.caches.Cache._get')
    def test_can_disable_cache(self, mocked):
        with self.cache.disable():
            r = self.manager.count()
            self.manager.count()
            mocked.assert_not_called()