Exemplo n.º 1
0
    def test_cache_avoid_messing_other_queries(self):
        # This test is to prevent a regression in cached_query._perform_query.
        # In the past it did expunge the just loaded results which would then
        # be merged back by _load_results. The side-effect was that it would
        # also expunge results shared with other queries in the same session
        # making them unusable in the other query.
        mo = SqlaThingWithDate(name=u'something')
        mo.related_thing = SqlaThing(name=u'related')
        SqlaDBSession.add(mo)
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        other_query = SqlaDBSession.query(SqlaThingWithDate).all()

        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).all()

        # Check that after the execution of the cached_query the plain
        # query continues to work
        assert other_query[0].related_thing.name == 'related'

        assert len(q) == 1
        assert q[0].name == mo.name
        cached_result = q[0]

        #run query again to check it is has actually been cached
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).all()

        assert len(q) == 1
        assert q[0] is cached_result
Exemplo n.º 2
0
    def test_cache_one_multiple_results(self):
        SqlaDBSession.add(SqlaThingWithDate(name=u'something'))
        SqlaDBSession.add(SqlaThingWithDate(name=u'something2'))
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).all()
        assert len(q) == 2

        #run query again to check it is has actually been cached and one is used
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).one()
Exemplo n.º 3
0
    def test_cache_one_noresult(self):
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).first()
        assert q is None

        mo = SqlaThingWithDate(name=u'something')
        SqlaDBSession.add(mo)
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        #run query again to check it is has actually been cached
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).one()
Exemplo n.º 4
0
    def test_cache_all(self):
        mo = SqlaThingWithDate(name=u'something')
        SqlaDBSession.add(mo)
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).all()

        assert len(q) == 1
        assert q[0].name == mo.name
        cached_result = q[0]

        #run query again to check it is has actually been cached
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).all()

        assert len(q) == 1
        assert q[0] is cached_result
Exemplo n.º 5
0
    def test_cache_one(self):
        mo = SqlaThingWithDate(name=u'something')
        SqlaDBSession.add(mo)
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).one()

        assert q is not None
        assert q.name == mo.name
        cached_result = q

        #run query again to check it is has actually been cached
        q = cached_query('samplequery',
                         SqlaDBSession.query(SqlaThingWithDate)).one()

        assert q is not None
        assert q is cached_result
Exemplo n.º 6
0
    def test_cache_iter(self):
        mo = SqlaThingWithDate(name=u'something')
        SqlaDBSession.add(mo)
        SqlaDBSession.flush()
        SqlaDBSession.commit()

        q = cached_query('samplequery', SqlaDBSession.query(SqlaThingWithDate))

        for e in q:
            assert e.name == mo.name
            break