Replaced try/finally by mock.patch in geos tests

This commit is contained in:
Claude Paroz 2015-07-18 15:27:59 +02:00
parent a131d9ce55
commit 19fcf083d3
1 changed files with 19 additions and 30 deletions

View File

@ -17,6 +17,7 @@ from django.contrib.gis.geos import (
) )
from django.contrib.gis.geos.base import GEOSBase from django.contrib.gis.geos.base import GEOSBase
from django.contrib.gis.shortcuts import numpy from django.contrib.gis.shortcuts import numpy
from django.test import mock
from django.utils import six from django.utils import six
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.utils.six.moves import range from django.utils.six.moves import range
@ -896,7 +897,19 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
""" Testing `transform` method (SRID match) """ """ Testing `transform` method (SRID match) """
# transform() should no-op if source & dest SRIDs match, # transform() should no-op if source & dest SRIDs match,
# regardless of whether GDAL is available. # regardless of whether GDAL is available.
if gdal.HAS_GDAL: g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
gt = g.tuple
g.transform(4326)
self.assertEqual(g.tuple, gt)
self.assertEqual(g.srid, 4326)
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
g1 = g.transform(4326, clone=True)
self.assertEqual(g1.tuple, g.tuple)
self.assertEqual(g1.srid, 4326)
self.assertIsNot(g1, g, "Clone didn't happen")
with mock.patch('django.contrib.gis.gdal.HAS_GDAL', False):
g = GEOSGeometry('POINT (-104.609 38.255)', 4326) g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
gt = g.tuple gt = g.tuple
g.transform(4326) g.transform(4326)
@ -909,24 +922,6 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(g1.srid, 4326) self.assertEqual(g1.srid, 4326)
self.assertIsNot(g1, g, "Clone didn't happen") self.assertIsNot(g1, g, "Clone didn't happen")
old_has_gdal = gdal.HAS_GDAL
try:
gdal.HAS_GDAL = False
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
gt = g.tuple
g.transform(4326)
self.assertEqual(g.tuple, gt)
self.assertEqual(g.srid, 4326)
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
g1 = g.transform(4326, clone=True)
self.assertEqual(g1.tuple, g.tuple)
self.assertEqual(g1.srid, 4326)
self.assertIsNot(g1, g, "Clone didn't happen")
finally:
gdal.HAS_GDAL = old_has_gdal
def test_transform_nosrid(self): def test_transform_nosrid(self):
""" Testing `transform` method (no SRID or negative SRID) """ """ Testing `transform` method (no SRID or negative SRID) """
@ -942,20 +937,14 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1) g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
self.assertRaises(GEOSException, g.transform, 2774, clone=True) self.assertRaises(GEOSException, g.transform, 2774, clone=True)
@skipUnless(HAS_GDAL, "GDAL is required.") @mock.patch('django.contrib.gis.gdal.HAS_GDAL', False)
def test_transform_nogdal(self): def test_transform_nogdal(self):
""" Testing `transform` method (GDAL not available) """ """ Testing `transform` method (GDAL not available) """
old_has_gdal = gdal.HAS_GDAL g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
try: self.assertRaises(GEOSException, g.transform, 2774)
gdal.HAS_GDAL = False
g = GEOSGeometry('POINT (-104.609 38.255)', 4326) g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
self.assertRaises(GEOSException, g.transform, 2774) self.assertRaises(GEOSException, g.transform, 2774, clone=True)
g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
self.assertRaises(GEOSException, g.transform, 2774, clone=True)
finally:
gdal.HAS_GDAL = old_has_gdal
def test_extent(self): def test_extent(self):
"Testing `extent` method." "Testing `extent` method."