|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, print_function, unicode_literals |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from django.core.exceptions import ImproperlyConfigured |
| 7 | + |
| 8 | +from django_sorcery.db import FromCache |
| 9 | +from django_sorcery.pytest_plugin import sqlalchemy_profiler # noqa |
| 10 | + |
| 11 | +from ..testapp.models import CachedModel, CachedReference, OtherCachedModel, Owner, UnCachedModel, cache, db |
| 12 | + |
| 13 | + |
| 14 | +def test_without_region(): # noqa |
| 15 | + |
| 16 | + with pytest.raises(ImproperlyConfigured): |
| 17 | + Owner.objects.options(FromCache(None)).filter_by(first_name="foo").one_or_none() |
| 18 | + |
| 19 | + |
| 20 | +def test_from_cache_option(sqlalchemy_profiler): # noqa |
| 21 | + db.add(Owner(first_name="foo", last_name="bar")) |
| 22 | + db.flush() |
| 23 | + db.expire_all() |
| 24 | + cache.cache.clear() |
| 25 | + |
| 26 | + with sqlalchemy_profiler: |
| 27 | + Owner.objects.options(FromCache(cache)).filter_by(first_name="foo").one_or_none() |
| 28 | + |
| 29 | + assert sqlalchemy_profiler.stats["select"] == 1 |
| 30 | + assert len(cache.cache) == 1 |
| 31 | + |
| 32 | + with sqlalchemy_profiler: |
| 33 | + Owner.objects.options(FromCache(cache)).filter_by(first_name="foo").one_or_none() |
| 34 | + |
| 35 | + assert sqlalchemy_profiler.stats["select"] == 0 |
| 36 | + assert len(cache.cache) == 1 |
| 37 | + |
| 38 | + FromCache(cache).invalidate(Owner.objects.options(FromCache(cache)).filter_by(first_name="foo")) |
| 39 | + assert len(cache.cache) == 0 |
| 40 | + |
| 41 | + with sqlalchemy_profiler: |
| 42 | + Owner.objects.options(FromCache(cache)).filter_by(first_name="foo").one_or_none() |
| 43 | + assert sqlalchemy_profiler.stats["select"] == 1 |
| 44 | + assert len(cache.cache) == 1 |
| 45 | + |
| 46 | + Owner.objects.options(FromCache(cache)).filter_by(first_name="foo").invalidate() |
| 47 | + assert len(cache.cache) == 0 |
| 48 | + |
| 49 | + |
| 50 | +def test_model_cache_option(sqlalchemy_profiler): # noqa |
| 51 | + instance = UnCachedModel( |
| 52 | + cached=CachedModel(name="cached"), |
| 53 | + other_cached=[OtherCachedModel(name="other cached 1"), OtherCachedModel(name="other cached 2")], |
| 54 | + references=[CachedReference(name="ref1"), CachedReference(name="ref2")], |
| 55 | + ) |
| 56 | + |
| 57 | + db.add(instance) |
| 58 | + db.flush() |
| 59 | + pk = instance.pk |
| 60 | + cached_pk = instance.cached.pk |
| 61 | + db.expire_all() |
| 62 | + cache.cache.clear() |
| 63 | + |
| 64 | + with sqlalchemy_profiler: |
| 65 | + instance = CachedModel.objects.get(cached_pk) |
| 66 | + assert sqlalchemy_profiler.stats["select"] == 1 |
| 67 | + assert len(cache.cache) == 1 |
| 68 | + |
| 69 | + with sqlalchemy_profiler: |
| 70 | + instance = CachedModel.objects.get(cached_pk) |
| 71 | + assert sqlalchemy_profiler.stats["select"] == 0 |
| 72 | + assert len(cache.cache) == 1 |
| 73 | + |
| 74 | + instance = UnCachedModel.objects.filter_by(pk=pk).one() |
| 75 | + db.refresh(instance) |
| 76 | + with sqlalchemy_profiler: |
| 77 | + assert instance.cached.pk == cached_pk |
| 78 | + assert sqlalchemy_profiler.stats["select"] == 0 |
| 79 | + assert len(cache.cache) == 1 # many-to-one cached |
| 80 | + |
| 81 | + instance = UnCachedModel.objects.filter_by(pk=pk).one() |
| 82 | + db.refresh(instance) |
| 83 | + with sqlalchemy_profiler: |
| 84 | + assert len(instance.other_cached) == 2 |
| 85 | + assert sqlalchemy_profiler.stats["select"] == 1 |
| 86 | + assert len(cache.cache) == 1 # one-to-many not cached |
| 87 | + |
| 88 | + instance = UnCachedModel.objects.filter_by(pk=pk).one() |
| 89 | + db.refresh(instance) |
| 90 | + with sqlalchemy_profiler: |
| 91 | + assert len(instance.references) == 2 |
| 92 | + assert sqlalchemy_profiler.stats["select"] == 1 |
| 93 | + assert len(cache.cache) == 1 # many-to-many not cached |
0 commit comments