2017-09-11 15:56:39 +00:00
|
|
|
import copy
|
2015-10-22 12:42:18 +00:00
|
|
|
import unittest
|
|
|
|
from functools import wraps
|
2017-09-11 15:56:39 +00:00
|
|
|
from unittest import mock
|
2013-09-24 10:22:50 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
from django.conf import settings
|
2015-10-22 12:42:18 +00:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connection
|
2017-09-11 15:56:39 +00:00
|
|
|
from django.db.models.expressions import Func
|
2015-10-22 12:42:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def skipUnlessGISLookup(*gis_lookups):
|
|
|
|
"""
|
|
|
|
Skip a test unless a database supports all of gis_lookups.
|
|
|
|
"""
|
|
|
|
def decorator(test_func):
|
|
|
|
@wraps(test_func)
|
|
|
|
def skip_wrapper(*args, **kwargs):
|
|
|
|
if any(key not in connection.ops.gis_operators for key in gis_lookups):
|
|
|
|
raise unittest.SkipTest(
|
|
|
|
"Database doesn't support all the lookups: %s" % ", ".join(gis_lookups)
|
|
|
|
)
|
|
|
|
return test_func(*args, **kwargs)
|
|
|
|
return skip_wrapper
|
|
|
|
return decorator
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def no_backend(test_func, backend):
|
|
|
|
"Use this decorator to disable test on specified backend."
|
2009-12-22 15:18:51 +00:00
|
|
|
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'].rsplit('.')[-1] == backend:
|
2015-10-22 12:42:18 +00:00
|
|
|
@unittest.skip("This test is skipped on '%s' backend" % backend)
|
2013-09-24 10:22:50 +00:00
|
|
|
def inner():
|
|
|
|
pass
|
|
|
|
return inner
|
2008-08-05 18:13:06 +00:00
|
|
|
else:
|
|
|
|
return test_func
|
|
|
|
|
2013-11-02 17:18:46 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Decorators to disable entire test functions for specific
|
|
|
|
# spatial backends.
|
2013-10-17 08:17:41 +00:00
|
|
|
def no_oracle(func):
|
|
|
|
return no_backend(func, 'oracle')
|
|
|
|
|
2013-11-02 17:18:46 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Shortcut booleans to omit only portions of tests.
|
2009-12-22 15:18:51 +00:00
|
|
|
_default_db = settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'].rsplit('.')[-1]
|
2013-10-22 13:31:43 +00:00
|
|
|
oracle = _default_db == 'oracle'
|
2009-12-22 15:18:51 +00:00
|
|
|
postgis = _default_db == 'postgis'
|
2013-10-22 13:31:43 +00:00
|
|
|
mysql = _default_db == 'mysql'
|
2009-12-22 15:18:51 +00:00
|
|
|
spatialite = _default_db == 'spatialite'
|
2012-06-17 09:39:02 +00:00
|
|
|
|
2014-08-21 16:47:57 +00:00
|
|
|
# MySQL spatial indices can't handle NULL geometries.
|
|
|
|
gisfield_may_be_null = not mysql
|
|
|
|
|
2012-10-27 15:10:41 +00:00
|
|
|
if oracle and 'gis' in settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE']:
|
2014-06-08 07:54:35 +00:00
|
|
|
from django.contrib.gis.db.backends.oracle.models import OracleSpatialRefSys as SpatialRefSys
|
2012-06-17 09:39:02 +00:00
|
|
|
elif postgis:
|
2014-06-08 07:54:35 +00:00
|
|
|
from django.contrib.gis.db.backends.postgis.models import PostGISSpatialRefSys as SpatialRefSys
|
2012-06-17 09:39:02 +00:00
|
|
|
elif spatialite:
|
2014-06-08 07:54:35 +00:00
|
|
|
from django.contrib.gis.db.backends.spatialite.models import SpatialiteSpatialRefSys as SpatialRefSys
|
2012-06-17 09:39:02 +00:00
|
|
|
else:
|
|
|
|
SpatialRefSys = None
|
2017-09-11 15:56:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FuncTestMixin:
|
|
|
|
"""Assert that Func expressions aren't mutated during their as_sql()."""
|
|
|
|
def setUp(self):
|
|
|
|
def as_sql_wrapper(original_as_sql):
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
func = original_as_sql.__self__
|
|
|
|
# Resolve output_field before as_sql() so touching it in
|
|
|
|
# as_sql() won't change __dict__.
|
|
|
|
func.output_field
|
|
|
|
__dict__original = copy.deepcopy(func.__dict__)
|
|
|
|
result = original_as_sql(*args, **kwargs)
|
|
|
|
msg = '%s Func was mutated during compilation.' % func.__class__.__name__
|
|
|
|
self.assertEqual(func.__dict__, __dict__original, msg)
|
|
|
|
return result
|
|
|
|
return inner
|
|
|
|
|
|
|
|
def __getattribute__(self, name):
|
|
|
|
if name != vendor_impl:
|
|
|
|
return __getattribute__original(self, name)
|
|
|
|
try:
|
|
|
|
as_sql = __getattribute__original(self, vendor_impl)
|
|
|
|
except AttributeError:
|
|
|
|
as_sql = __getattribute__original(self, 'as_sql')
|
|
|
|
return as_sql_wrapper(as_sql)
|
|
|
|
|
|
|
|
vendor_impl = 'as_' + connection.vendor
|
|
|
|
__getattribute__original = Func.__getattribute__
|
|
|
|
self.func_patcher = mock.patch.object(Func, '__getattribute__', __getattribute__)
|
|
|
|
self.func_patcher.start()
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
super().tearDown()
|
|
|
|
self.func_patcher.stop()
|