Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit testing Postgis version check

Refs #22334.
  • Loading branch information...
commit 88f1e3d93d8f48a302ff75b1936e471c4a52fff9 1 parent f30e659
Rémy Sanchez Xowap authored claudep committed
Showing with 74 additions and 0 deletions.
  1. +74 −0 django/contrib/gis/tests/tests.py
74 django/contrib/gis/tests/tests.py
View
@@ -0,0 +1,74 @@
+import unittest
+
+from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
+from django.core.exceptions import ImproperlyConfigured
+from django.db import ProgrammingError
+
+
+class FakeConnection(object):
+ def __init__(self):
+ self.settings_dict = {
+ 'NAME': 'test',
+ }
+
+
+class FakePostGISOperations(PostGISOperations):
+ def __init__(self, version=None):
+ self.version = version
+ self.connection = FakeConnection()
+
+ def _get_postgis_func(self, func):
+ if func == 'postgis_lib_version':
+ if self.version is None:
+ raise ProgrammingError
+ else:
+ return self.version
+ else:
+ raise NotImplementedError('This function was not expected to be called')
+
+
+class TestPostgisVersionCheck(unittest.TestCase):
+ """
+ Tests that the postgis version check parses correctly the version numbers
+ """
+
+ def test_get_version(self):
+ expect = '1.0.0'
+ ops = FakePostGISOperations(expect)
+ actual = ops.postgis_lib_version()
+ self.assertEqual(expect, actual)
+
+ def test_version_classic_tuple(self):
+ expect = ('1.2.3', 1, 2, 3)
+ ops = FakePostGISOperations(expect[0])
+ actual = ops.postgis_version_tuple()
+ self.assertEqual(expect, actual)
+
+ def test_version_dev_tuple(self):
+ expect = ('1.2.3dev', 1, 2, 3)
+ ops = FakePostGISOperations(expect[0])
+ actual = ops.postgis_version_tuple()
+ self.assertEqual(expect, actual)
+
+ def test_valid_version_numbers(self):
+ versions = [
+ ('1.3.0', 1, 3, 0),
+ ('2.1.1', 2, 1, 1),
+ ('2.2.0dev', 2, 2, 0),
+ ]
+
+ for version in versions:
+ ops = FakePostGISOperations(version[0])
+ actual = ops.spatial_version
+ self.assertEqual(version[1:], actual)
+
+ def test_invalid_version_numbers(self):
+ versions = ['nope', '123']
+
+ for version in versions:
+ ops = FakePostGISOperations(version)
+ self.assertRaises(Exception, lambda: ops.spatial_version)
+
+ def test_no_version_number(self):
+ ops = FakePostGISOperations()
+ self.assertRaises(ImproperlyConfigured, lambda: ops.spatial_version)

2 comments on commit 88f1e3d

Tim Graham
Owner

This causes a regression when running the tests with SQLite if psycopg2 isn't installed:

ImportError: Failed to import test module: django.contrib.gis.tests.tests
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
    __import__(name)
  File "/var/lib/jenkins/jobs/Django/workspace/database/sqlite3/python/python2.7/django/contrib/gis/tests/tests.py", line 3, in <module>
    from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
  File "/var/lib/jenkins/jobs/Django/workspace/database/sqlite3/python/python2.7/django/contrib/gis/db/backends/postgis/operations.py", line 7, in <module>
    from django.contrib.gis.db.backends.postgis.adapter import PostGISAdapter
  File "/var/lib/jenkins/jobs/Django/workspace/database/sqlite3/python/python2.7/django/contrib/gis/db/backends/postgis/adapter.py", line 6, in <module>
    from psycopg2 import Binary
ImportError: No module named psycopg2
Claude Paroz
Collaborator

Sorry, hopefully fixed in e819a3c

Please sign in to comment.
Something went wrong with that request. Please try again.