django-cachalot/cachalot/tests/api.py
Andrew-Chen-Wang 602cdcee1d
Version 2.2.0 (#146)
This PR upgrades cachalot to version 2.2.0 allowing for anyone to install cachalot at any Django package above 1.11. All tests currently function now.

* Added mysql as a service to fix Travis CI

* Removed tox dependency

* Removed upper limit on Django and support 2.2-3.0 CI Test
* Cachalot will only say "officially supports" while removing upper limit to not hinder anyone's progress.
* Added Python 3.8 and Django 2.2 and 3.0 to CI tests

- Max is 200 and we'll get there quickly if we add another Python version. Utilizes #127
- Keep dependencies for six and django.utils.six

* Correctly run Travis test by updating tox.ini
* Originally using tox syntax but changed testenv to travis so that travis env and travis dependencies and stuff are used

* Tox tests should start running command

* testenv without dependencies broke travis. New spot in Tox
* testenv without dependencies broke travis
* I can't figure out where testenv is supposed to be
* Good thing there's squash

* Added missing six in test
* Also added six package to Tox for Django 3.0 coverage.
* The missing six package caused several problems in several tests due to using an incorrect DB

* Resolves #138
* Need to add the panels here in order to satisfy tests

* Adds CachalotPanel to Python 2 backwards compatibility
* Forgot super class took those like save()

* Added databases to certain test cases
* APITestCase, DebugToolbarTestCase, SignalsTestCase required a database set
* Fixes errors like these: https://travis-ci.org/noripyt/django-cachalot/jobs/648691385#L4892

* Dropped OFFICIAL support for Python 3.4 + MySQL
* MySQL and its client on 3.4 is f--king up a lot of the testing and I can't bear with it. We can revisit it later, but, honestly, who uses Python 3.4... and if an organization is, then they will either have no problem with Postgres and SQLite or know that MySQL doesn't work.

* Drop Py3.4 from running. Fixed databases
* Now goes with setting's databases rather than hardcoded since some tests don't have other databases.

* Fix ReadTestCase

Django 2.2+ changed self.queryset at line 1000 with self.query=queryset.query

* Django Dependency Removed from utils.py

* Last commit used Django version. This one used try except

* Fixed assertNumQuery; added allow_failures for 3.4
* Primary: More information in test_utils.py with method is_django_21_below_and_is_sqlite
* The problem with the entire build lie in the self.is_sqlite for the tests. SQLite will make 2 queries when creating or destroying or updating instead of 1.
* In summary, if SQLite is the DB and Django

* Changed build matrix to allow failures for 3.4 instead of taking it out completely so that we can get it to work soon.

* Remove -> From function return
* I love backwards compatibility

* SQLite2 in multi_db.py returned incorrect bool

* Django 2.1 and SQLite checker doesn't work for some tests
* Some tests DO HAVE the BEGIN query that is returned... Not sure why. If the next test show completely different WriteTestCase problems, then we need to find alternative.
* Removed check from atomic

* Adjust Django and is_sqlite errors
* CI testing the Tox errors: https://travis-ci.org/noripyt/django-cachalot?utm_medium=notification&utm_source=github_status

* Adjust Django and is_sqlite errors (590 CI)
* CI testing the Tox errors: https://travis-ci.org/noripyt/django-cachalot?utm_medium=notification&utm_source=github_status
* Adjustment 2

* Make include matrix and Adjust Django and is_sqlite errors (594 CI)
* CI testing the Tox errors: https://travis-ci.org/noripyt/django-cachalot?utm_medium=notification&utm_source=github_status
* Adjustment 3

* Adjust Django and is_sqlite errors (596 CI)
* CI testing the Tox errors: https://travis-ci.org/noripyt/django-cachalot?utm_medium=notification&utm_source=github_status
* Adjustment 4
* Added my intro to ReadTheDocs

* Adjust Django and is_sqlite errors (598 CI)
* CI testing the Tox errors: https://travis-ci.org/noripyt/django-cachalot?utm_medium=notification&utm_source=github_status
* Adjustment 5

* Drop Python 3.4 from tests since PyLibMC not cooperating at that level.

* Bump package version up 1 minor for Django 2.2 and 3.0
2020-02-12 00:52:12 -05:00

350 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding: utf-8
from __future__ import unicode_literals
from time import time, sleep
from unittest import skipIf
from django.conf import settings
from django.contrib.auth.models import User
from django.core.cache import DEFAULT_CACHE_ALIAS, caches
from django.core.management import call_command
from django.db import connection, transaction, DEFAULT_DB_ALIAS
from django.template import engines
from django.test import TransactionTestCase
from jinja2.exceptions import TemplateSyntaxError
from ..api import *
from .models import Test
from .test_utils import TestUtilsMixin
class APITestCase(TestUtilsMixin, TransactionTestCase):
databases = set(settings.DATABASES.keys())
def setUp(self):
super(APITestCase, self).setUp()
self.t1 = Test.objects.create(name='test1')
self.cache_alias2 = next(alias for alias in settings.CACHES
if alias != DEFAULT_CACHE_ALIAS)
def test_invalidate_tables(self):
with self.assertNumQueries(1):
data1 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data1, ['test1'])
with self.settings(CACHALOT_INVALIDATE_RAW=False):
with connection.cursor() as cursor:
cursor.execute(
"INSERT INTO cachalot_test (name, public) "
"VALUES ('test2', %s);", [1 if self.is_sqlite else True])
with self.assertNumQueries(0):
data2 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data2, ['test1'])
invalidate('cachalot_test')
with self.assertNumQueries(1):
data3 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data3, ['test1', 'test2'])
def test_invalidate_models_lookups(self):
with self.assertNumQueries(1):
data1 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data1, ['test1'])
with self.settings(CACHALOT_INVALIDATE_RAW=False):
with connection.cursor() as cursor:
cursor.execute(
"INSERT INTO cachalot_test (name, public) "
"VALUES ('test2', %s);", [1 if self.is_sqlite else True])
with self.assertNumQueries(0):
data2 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data2, ['test1'])
invalidate('cachalot.Test')
with self.assertNumQueries(1):
data3 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data3, ['test1', 'test2'])
def test_invalidate_models(self):
with self.assertNumQueries(1):
data1 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data1, ['test1'])
with self.settings(CACHALOT_INVALIDATE_RAW=False):
with connection.cursor() as cursor:
cursor.execute(
"INSERT INTO cachalot_test (name, public) "
"VALUES ('test2', %s);", [1 if self.is_sqlite else True])
with self.assertNumQueries(0):
data2 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data2, ['test1'])
invalidate(Test)
with self.assertNumQueries(1):
data3 = list(Test.objects.values_list('name', flat=True))
self.assertListEqual(data3, ['test1', 'test2'])
def test_invalidate_all(self):
with self.assertNumQueries(1):
Test.objects.get()
with self.assertNumQueries(0):
Test.objects.get()
invalidate()
with self.assertNumQueries(1):
Test.objects.get()
def test_invalidate_all_in_atomic(self):
with transaction.atomic():
with self.assertNumQueries(1):
Test.objects.get()
with self.assertNumQueries(0):
Test.objects.get()
invalidate()
with self.assertNumQueries(1):
Test.objects.get()
with self.assertNumQueries(1):
Test.objects.get()
def test_get_last_invalidation(self):
invalidate()
timestamp = get_last_invalidation()
self.assertAlmostEqual(timestamp, time(), delta=0.1)
sleep(0.1)
invalidate('cachalot_test')
timestamp = get_last_invalidation('cachalot_test')
self.assertAlmostEqual(timestamp, time(), delta=0.1)
same_timestamp = get_last_invalidation('cachalot.Test')
self.assertEqual(same_timestamp, timestamp)
same_timestamp = get_last_invalidation(Test)
self.assertEqual(same_timestamp, timestamp)
timestamp = get_last_invalidation('cachalot_testparent')
self.assertNotAlmostEqual(timestamp, time(), delta=0.1)
timestamp = get_last_invalidation('cachalot_testparent',
'cachalot_test')
self.assertAlmostEqual(timestamp, time(), delta=0.1)
def test_get_last_invalidation_template_tag(self):
# Without arguments
original_timestamp = engines['django'].from_string(
"{{ timestamp }}"
).render({
'timestamp': get_last_invalidation(),
})
template = engines['django'].from_string("""
{% load cachalot %}
{% get_last_invalidation as timestamp %}
{{ timestamp }}
""")
timestamp = template.render().strip()
self.assertNotEqual(timestamp, '')
self.assertNotEqual(timestamp, '0.0')
self.assertAlmostEqual(float(timestamp), float(original_timestamp),
delta=0.1)
# With arguments
original_timestamp = engines['django'].from_string(
"{{ timestamp }}"
).render({
'timestamp': get_last_invalidation('auth.Group', 'cachalot_test'),
})
template = engines['django'].from_string("""
{% load cachalot %}
{% get_last_invalidation 'auth.Group' 'cachalot_test' as timestamp %}
{{ timestamp }}
""")
timestamp = template.render().strip()
self.assertNotEqual(timestamp, '')
self.assertNotEqual(timestamp, '0.0')
self.assertAlmostEqual(float(timestamp), float(original_timestamp),
delta=0.1)
# While using the `cache` template tag, with invalidation
template = engines['django'].from_string("""
{% load cachalot cache %}
{% get_last_invalidation 'auth.Group' 'cachalot_test' as timestamp %}
{% cache 10 cache_key_name timestamp %}
{{ content }}
{% endcache %}
""")
content = template.render({'content': 'something'}).strip()
self.assertEqual(content, 'something')
content = template.render({'content': 'anything'}).strip()
self.assertEqual(content, 'something')
invalidate('cachalot_test')
content = template.render({'content': 'yet another'}).strip()
self.assertEqual(content, 'yet another')
def test_get_last_invalidation_jinja2(self):
original_timestamp = engines['jinja2'].from_string(
"{{ timestamp }}"
).render({
'timestamp': get_last_invalidation('auth.Group', 'cachalot_test'),
})
template = engines['jinja2'].from_string(
"{{ get_last_invalidation('auth.Group', 'cachalot_test') }}")
timestamp = template.render({})
self.assertNotEqual(timestamp, '')
self.assertNotEqual(timestamp, '0.0')
self.assertAlmostEqual(float(timestamp), float(original_timestamp),
delta=0.1)
def test_cache_jinja2(self):
# Invalid arguments
with self.assertRaises(TemplateSyntaxError,
msg="'invalid' is not a valid keyword argument "
"for {% cache %}"):
engines['jinja2'].from_string("""
{% cache cache_key='anything', invalid='what?' %}{% endcache %}
""")
with self.assertRaises(ValueError, msg='You must set `cache_key` when '
'the template is not a file.'):
engines['jinja2'].from_string(
'{% cache %} broken {% endcache %}').render()
# With the minimum number of arguments
template = engines['jinja2'].from_string("""
{%- cache cache_key='first' -%}
{{ content1 }}
{%- endcache -%}
{%- cache cache_key='second' -%}
{{ content2 }}
{%- endcache -%}
""")
content = template.render({'content1': 'abc', 'content2': 'def'})
self.assertEqual(content, 'abcdef')
invalidate()
content = template.render({'content1': 'ghi', 'content2': 'jkl'})
self.assertEqual(content, 'abcdef')
# With the maximum number of arguments
template = engines['jinja2'].from_string("""
{%- cache get_last_invalidation('auth.Group', 'cachalot_test',
cache_alias=cache),
timeout=10, cache_key='cache_key_name', cache_alias=cache -%}
{{ content }}
{%- endcache -%}
""")
content = template.render({'content': 'something',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
content = template.render({'content': 'anything',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
invalidate('cachalot_test', cache_alias=DEFAULT_CACHE_ALIAS)
content = template.render({'content': 'yet another',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
invalidate('cachalot_test')
content = template.render({'content': 'will you change?',
'cache': self.cache_alias2})
self.assertEqual(content, 'will you change?')
caches[self.cache_alias2].clear()
content = template.render({'content': 'better!',
'cache': self.cache_alias2})
self.assertEqual(content, 'better!')
class CommandTestCase(TransactionTestCase):
multi_db = True
def setUp(self):
self.db_alias2 = next(alias for alias in settings.DATABASES
if alias != DEFAULT_DB_ALIAS)
self.cache_alias2 = next(alias for alias in settings.CACHES
if alias != DEFAULT_CACHE_ALIAS)
self.t1 = Test.objects.create(name='test1')
self.t2 = Test.objects.using(self.db_alias2).create(name='test2')
self.u = User.objects.create_user('test')
def test_invalidate_cachalot(self):
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', verbosity=0)
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', 'auth', verbosity=0)
with self.assertNumQueries(0):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', 'cachalot', verbosity=0)
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', 'cachalot.testchild', verbosity=0)
with self.assertNumQueries(0):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', 'cachalot.test', verbosity=0)
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
with self.assertNumQueries(1):
self.assertListEqual(list(User.objects.all()), [self.u])
call_command('invalidate_cachalot', 'cachalot.test', 'auth.user',
verbosity=0)
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
with self.assertNumQueries(1):
self.assertListEqual(list(User.objects.all()), [self.u])
@skipIf(len(settings.DATABASES) == 1,
'We cant change the DB used since theres only one configured')
def test_invalidate_cachalot_multi_db(self):
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', verbosity=0,
db_alias=self.db_alias2)
with self.assertNumQueries(0):
self.assertListEqual(list(Test.objects.all()), [self.t1])
with self.assertNumQueries(1, using=self.db_alias2):
self.assertListEqual(list(Test.objects.using(self.db_alias2)),
[self.t2])
call_command('invalidate_cachalot', verbosity=0,
db_alias=self.db_alias2)
with self.assertNumQueries(1, using=self.db_alias2):
self.assertListEqual(list(Test.objects.using(self.db_alias2)),
[self.t2])
@skipIf(len(settings.CACHES) == 1,
'We cant change the cache used since theres only one configured')
def test_invalidate_cachalot_multi_cache(self):
with self.assertNumQueries(1):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', verbosity=0,
cache_alias=self.cache_alias2)
with self.assertNumQueries(0):
self.assertListEqual(list(Test.objects.all()), [self.t1])
with self.assertNumQueries(1):
with self.settings(CACHALOT_CACHE=self.cache_alias2):
self.assertListEqual(list(Test.objects.all()), [self.t1])
call_command('invalidate_cachalot', verbosity=0,
cache_alias=self.cache_alias2)
with self.assertNumQueries(1):
with self.settings(CACHALOT_CACHE=self.cache_alias2):
self.assertListEqual(list(Test.objects.all()), [self.t1])