mirror of
https://github.com/Hopiu/django-cachalot.git
synced 2026-05-26 05:03:43 +00:00
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
142 lines
5.6 KiB
Python
142 lines
5.6 KiB
Python
# coding: utf-8
|
||
|
||
from __future__ import unicode_literals
|
||
from unittest import skipIf
|
||
|
||
from django import VERSION as DJANGO_VERSION
|
||
from django.conf import settings
|
||
from django.db import DEFAULT_DB_ALIAS, connections, transaction
|
||
from django.test import TransactionTestCase
|
||
|
||
from .models import Test
|
||
|
||
|
||
@skipIf(len(settings.DATABASES) == 1,
|
||
'We can’t change the DB used since there’s only one configured')
|
||
class MultiDatabaseTestCase(TransactionTestCase):
|
||
multi_db = True
|
||
|
||
def setUp(self):
|
||
self.t1 = Test.objects.create(name='test1')
|
||
self.t2 = Test.objects.create(name='test2')
|
||
self.db_alias2 = next(alias for alias in settings.DATABASES
|
||
if alias != DEFAULT_DB_ALIAS)
|
||
connection2 = connections[self.db_alias2]
|
||
self.is_sqlite2 = connection2.vendor == 'sqlite'
|
||
self.is_mysql2 = connection2.vendor == 'mysql'
|
||
if connection2.vendor in ('mysql', 'postgresql'):
|
||
# We need to reopen the connection or Django
|
||
# will execute an extra SQL request below.
|
||
connection2.cursor()
|
||
|
||
def is_django_21_below_and_sqlite2(self):
|
||
"""
|
||
Note: See test_utils.py with this function name
|
||
Checks if Django 2.1 or below and SQLite2
|
||
"""
|
||
django_version = DJANGO_VERSION
|
||
if not self.is_sqlite2:
|
||
# Immediately know if SQLite
|
||
return False
|
||
if django_version[0] < 2:
|
||
# Takes Django 0 and 1 out of the picture
|
||
return True
|
||
else:
|
||
if django_version[0] == 2 and django_version[1] < 2:
|
||
# Takes Django 2.0-2.1 out
|
||
return True
|
||
return False
|
||
|
||
def test_read(self):
|
||
with self.assertNumQueries(1):
|
||
data1 = list(Test.objects.all())
|
||
self.assertListEqual(data1, [self.t1, self.t2])
|
||
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data2 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data2, [])
|
||
|
||
with self.assertNumQueries(0, using=self.db_alias2):
|
||
data3 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data3, [])
|
||
|
||
def test_invalidate_other_db(self):
|
||
"""
|
||
Tests if the non-default database is invalidated when modified.
|
||
"""
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data1 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data1, [])
|
||
|
||
with self.assertNumQueries(2 if self.is_django_21_below_and_sqlite2() else 1,
|
||
using=self.db_alias2):
|
||
t3 = Test.objects.using(self.db_alias2).create(name='test3')
|
||
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data2 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data2, [t3])
|
||
|
||
def test_invalidation_independence(self):
|
||
"""
|
||
Tests if invalidation doesn’t affect the unmodified databases.
|
||
"""
|
||
with self.assertNumQueries(1):
|
||
data1 = list(Test.objects.all())
|
||
self.assertListEqual(data1, [self.t1, self.t2])
|
||
|
||
with self.assertNumQueries(2 if self.is_django_21_below_and_sqlite2() else 1,
|
||
using=self.db_alias2):
|
||
Test.objects.using(self.db_alias2).create(name='test3')
|
||
|
||
with self.assertNumQueries(0):
|
||
data2 = list(Test.objects.all())
|
||
self.assertListEqual(data2, [self.t1, self.t2])
|
||
|
||
def test_heterogeneous_atomics(self):
|
||
"""
|
||
Checks that an atomic block for a database nested inside
|
||
another atomic block for another database has no impact on their
|
||
caching.
|
||
"""
|
||
with transaction.atomic():
|
||
with transaction.atomic(self.db_alias2):
|
||
with self.assertNumQueries(1):
|
||
data1 = list(Test.objects.all())
|
||
self.assertListEqual(data1, [self.t1, self.t2])
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data2 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data2, [])
|
||
t3 = Test.objects.using(self.db_alias2).create(name='test3')
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data3 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data3, [t3])
|
||
|
||
with self.assertNumQueries(0):
|
||
data4 = list(Test.objects.all())
|
||
self.assertListEqual(data4, [self.t1, self.t2])
|
||
|
||
with self.assertNumQueries(1):
|
||
data5 = list(Test.objects.filter(name='test3'))
|
||
self.assertListEqual(data5, [])
|
||
|
||
def test_heterogeneous_atomics_independence(self):
|
||
"""
|
||
Checks that interrupting an atomic block after the commit of another
|
||
atomic block for another database nested inside it
|
||
correctly invalidates the cache for the committed transaction.
|
||
"""
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data1 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data1, [])
|
||
|
||
try:
|
||
with transaction.atomic():
|
||
with transaction.atomic(self.db_alias2):
|
||
t3 = Test.objects.using(
|
||
self.db_alias2).create(name='test3')
|
||
raise ZeroDivisionError
|
||
except ZeroDivisionError:
|
||
pass
|
||
with self.assertNumQueries(1, using=self.db_alias2):
|
||
data2 = list(Test.objects.using(self.db_alias2))
|
||
self.assertListEqual(data2, [t3])
|