tests.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import sys
  2. import unittest
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.db import ProgrammingError
  5. from django.utils import six
  6. try:
  7. from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
  8. HAS_POSTGRES = True
  9. except ImportError:
  10. HAS_POSTGRES = False
  11. except ImproperlyConfigured as e:
  12. # If psycopg is installed but not geos, the import path hits
  13. # django.contrib.gis.geometry.backend which will "helpfully" convert
  14. # an ImportError into an ImproperlyConfigured.
  15. # Here, we make sure we're only catching this specific case and not another
  16. # ImproperlyConfigured one.
  17. if e.args and e.args[0].startswith('Could not import user-defined GEOMETRY_BACKEND'):
  18. HAS_POSTGRES = False
  19. else:
  20. six.reraise(*sys.exc_info())
  21. if HAS_POSTGRES:
  22. class FakeConnection(object):
  23. def __init__(self):
  24. self.settings_dict = {
  25. 'NAME': 'test',
  26. }
  27. class FakePostGISOperations(PostGISOperations):
  28. def __init__(self, version=None):
  29. self.version = version
  30. self.connection = FakeConnection()
  31. def _get_postgis_func(self, func):
  32. if func == 'postgis_lib_version':
  33. if self.version is None:
  34. raise ProgrammingError
  35. else:
  36. return self.version
  37. else:
  38. raise NotImplementedError('This function was not expected to be called')
  39. @unittest.skipUnless(HAS_POSTGRES, "The psycopg2 driver is needed for these tests")
  40. class TestPostgisVersionCheck(unittest.TestCase):
  41. """
  42. Tests that the postgis version check parses correctly the version numbers
  43. """
  44. def test_get_version(self):
  45. expect = '1.0.0'
  46. ops = FakePostGISOperations(expect)
  47. actual = ops.postgis_lib_version()
  48. self.assertEqual(expect, actual)
  49. def test_version_classic_tuple(self):
  50. expect = ('1.2.3', 1, 2, 3)
  51. ops = FakePostGISOperations(expect[0])
  52. actual = ops.postgis_version_tuple()
  53. self.assertEqual(expect, actual)
  54. def test_version_dev_tuple(self):
  55. expect = ('1.2.3dev', 1, 2, 3)
  56. ops = FakePostGISOperations(expect[0])
  57. actual = ops.postgis_version_tuple()
  58. self.assertEqual(expect, actual)
  59. def test_valid_version_numbers(self):
  60. versions = [
  61. ('1.3.0', 1, 3, 0),
  62. ('2.1.1', 2, 1, 1),
  63. ('2.2.0dev', 2, 2, 0),
  64. ]
  65. for version in versions:
  66. ops = FakePostGISOperations(version[0])
  67. actual = ops.spatial_version
  68. self.assertEqual(version[1:], actual)
  69. def test_invalid_version_numbers(self):
  70. versions = ['nope', '123']
  71. for version in versions:
  72. ops = FakePostGISOperations(version)
  73. self.assertRaises(Exception, lambda: ops.spatial_version)
  74. def test_no_version_number(self):
  75. ops = FakePostGISOperations()
  76. self.assertRaises(ImproperlyConfigured, lambda: ops.spatial_version)