mirror of
https://github.com/Hopiu/django-cachalot.git
synced 2026-05-13 07:03:11 +00:00
commit 4a33e7b68196bf6e0ee0b7f942a992532495b616
Author: Andrew-Chen-Wang <acwangpython@gmail.com>
Date: Mon Aug 10 16:04:26 2020 -0400
Replace f-strings with join for Python 3.5
commit dcb33232d605c01025469e776c4ed8eb6ae0a326
Author: Andrew-Chen-Wang <acwangpython@gmail.com>
Date: Mon Aug 10 15:55:09 2020 -0400
Fix sql_flush for dj versions below 3.1
* Removed any other instances of JSONField for Django 3.1 removed many things like JsonAdapter in favor of regular json module
commit 74195e9ff5a52dba2449a55e543a27ebd99e4fc9
Author: Andrew-Chen-Wang <acwangpython@gmail.com>
Date: Mon Aug 10 09:17:03 2020 -0400
Add Django 3.1 to Travis
* Specify Django version when checking if JSONField exists
commit da5c1fa4c8b2f2efba0b12d7b27460c544e2473a
Author: Andrew-Chen-Wang <acwangpython@gmail.com>
Date: Wed Aug 5 17:39:58 2020 -0400
Added support for Django 3.1
* Some fields were removed, others were deprecated. They are now in a list of deprecation to follow. Some new fields were added like PositiveBigIntegerField that won't be tested
* monkey_patch.py has a try/except import for EmptyResultSet that was from archaic Django. Removed apparently due to compatibility issues
88 lines
3.4 KiB
Python
88 lines
3.4 KiB
Python
from django import VERSION as DJANGO_VERSION
|
|
from django.core.management.color import no_style
|
|
from django.db import connection, transaction
|
|
|
|
from .models import PostgresModel
|
|
from ..utils import _get_tables
|
|
|
|
|
|
class TestUtilsMixin:
|
|
def setUp(self):
|
|
self.is_sqlite = connection.vendor == 'sqlite'
|
|
self.is_mysql = connection.vendor == 'mysql'
|
|
self.is_postgresql = connection.vendor == 'postgresql'
|
|
self.force_repoen_connection()
|
|
|
|
# TODO: Remove this workaround when this issue is fixed:
|
|
# https://code.djangoproject.com/ticket/29494
|
|
def tearDown(self):
|
|
if connection.vendor == 'postgresql':
|
|
flush_args = [no_style(), (PostgresModel._meta.db_table,),]
|
|
if float(".".join(map(str, DJANGO_VERSION[:2]))) < 3.1:
|
|
flush_args.append(())
|
|
flush_sql_list = connection.ops.sql_flush(*flush_args)
|
|
with transaction.atomic():
|
|
for sql in flush_sql_list:
|
|
with connection.cursor() as cursor:
|
|
cursor.execute(sql)
|
|
|
|
def force_repoen_connection(self):
|
|
if connection.vendor in ('mysql', 'postgresql'):
|
|
# We need to reopen the connection or Django
|
|
# will execute an extra SQL request below.
|
|
connection.cursor()
|
|
|
|
def assert_tables(self, queryset, *tables):
|
|
tables = {table if isinstance(table, str)
|
|
else table._meta.db_table for table in tables}
|
|
self.assertSetEqual(_get_tables(queryset.db, queryset.query), tables)
|
|
|
|
def assert_query_cached(self, queryset, result=None, result_type=None,
|
|
compare_results=True, before=1, after=0):
|
|
if result_type is None:
|
|
result_type = list if result is None else type(result)
|
|
with self.assertNumQueries(before):
|
|
data1 = queryset.all()
|
|
if result_type is list:
|
|
data1 = list(data1)
|
|
with self.assertNumQueries(after):
|
|
data2 = queryset.all()
|
|
if result_type is list:
|
|
data2 = list(data2)
|
|
if not compare_results:
|
|
return
|
|
assert_functions = {
|
|
list: self.assertListEqual,
|
|
set: self.assertSetEqual,
|
|
dict: self.assertDictEqual,
|
|
}
|
|
assert_function = assert_functions.get(result_type, self.assertEqual)
|
|
assert_function(data2, data1)
|
|
if result is not None:
|
|
assert_function(data2, result)
|
|
|
|
def is_dj_21_below_and_is_sqlite(self):
|
|
"""
|
|
Checks if Django 2.1 or lower and if SQLite is the DB
|
|
Django 2.1 and lower had two queries on SQLite DBs:
|
|
|
|
After an insertion, e.g. Test.objects.create(name="asdf"),
|
|
SQLite returns the queries:
|
|
[{'sql': 'INSERT INTO "cachalot_test" ("name") VALUES (\'asd\')', 'time': '0.001'}, {'sql': 'BEGIN', 'time': '0.000'}]
|
|
|
|
This can be seen with django.db import connection; print(connection.queries)
|
|
In Django 2.2 and above, the latter was removed.
|
|
|
|
:return: bool is Django 2.1 or below and is SQLite the DB
|
|
"""
|
|
if not self.is_sqlite:
|
|
# 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
|