1. import unittest
    
  2. 
    
  3. from django.contrib.gis.gdal import Envelope, GDALException
    
  4. 
    
  5. 
    
  6. class TestPoint:
    
  7.     def __init__(self, x, y):
    
  8.         self.x = x
    
  9.         self.y = y
    
  10. 
    
  11. 
    
  12. class EnvelopeTest(unittest.TestCase):
    
  13.     def setUp(self):
    
  14.         self.e = Envelope(0, 0, 5, 5)
    
  15. 
    
  16.     def test01_init(self):
    
  17.         "Testing Envelope initialization."
    
  18.         e1 = Envelope((0, 0, 5, 5))
    
  19.         Envelope(0, 0, 5, 5)
    
  20.         Envelope(0, "0", "5", 5)  # Thanks to ww for this
    
  21.         Envelope(e1._envelope)
    
  22.         with self.assertRaises(GDALException):
    
  23.             Envelope((5, 5, 0, 0))
    
  24.         with self.assertRaises(GDALException):
    
  25.             Envelope(5, 5, 0, 0)
    
  26.         with self.assertRaises(GDALException):
    
  27.             Envelope((0, 0, 5, 5, 3))
    
  28.         with self.assertRaises(GDALException):
    
  29.             Envelope(())
    
  30.         with self.assertRaises(ValueError):
    
  31.             Envelope(0, "a", 5, 5)
    
  32.         with self.assertRaises(TypeError):
    
  33.             Envelope("foo")
    
  34.         with self.assertRaises(GDALException):
    
  35.             Envelope((1, 1, 0, 0))
    
  36.         # Shouldn't raise an exception for min_x == max_x or min_y == max_y
    
  37.         Envelope(0, 0, 0, 0)
    
  38. 
    
  39.     def test02_properties(self):
    
  40.         "Testing Envelope properties."
    
  41.         e = Envelope(0, 0, 2, 3)
    
  42.         self.assertEqual(0, e.min_x)
    
  43.         self.assertEqual(0, e.min_y)
    
  44.         self.assertEqual(2, e.max_x)
    
  45.         self.assertEqual(3, e.max_y)
    
  46.         self.assertEqual((0, 0), e.ll)
    
  47.         self.assertEqual((2, 3), e.ur)
    
  48.         self.assertEqual((0, 0, 2, 3), e.tuple)
    
  49.         self.assertEqual("POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))", e.wkt)
    
  50.         self.assertEqual("(0.0, 0.0, 2.0, 3.0)", str(e))
    
  51. 
    
  52.     def test03_equivalence(self):
    
  53.         "Testing Envelope equivalence."
    
  54.         e1 = Envelope(0.523, 0.217, 253.23, 523.69)
    
  55.         e2 = Envelope((0.523, 0.217, 253.23, 523.69))
    
  56.         self.assertEqual(e1, e2)
    
  57.         self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
    
  58. 
    
  59.     def test04_expand_to_include_pt_2_params(self):
    
  60.         "Testing Envelope expand_to_include -- point as two parameters."
    
  61.         self.e.expand_to_include(2, 6)
    
  62.         self.assertEqual((0, 0, 5, 6), self.e)
    
  63.         self.e.expand_to_include(-1, -1)
    
  64.         self.assertEqual((-1, -1, 5, 6), self.e)
    
  65. 
    
  66.     def test05_expand_to_include_pt_2_tuple(self):
    
  67.         "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
    
  68.         self.e.expand_to_include((10, 10))
    
  69.         self.assertEqual((0, 0, 10, 10), self.e)
    
  70.         self.e.expand_to_include((-10, -10))
    
  71.         self.assertEqual((-10, -10, 10, 10), self.e)
    
  72. 
    
  73.     def test06_expand_to_include_extent_4_params(self):
    
  74.         "Testing Envelope expand_to_include -- extent as 4 parameters."
    
  75.         self.e.expand_to_include(-1, 1, 3, 7)
    
  76.         self.assertEqual((-1, 0, 5, 7), self.e)
    
  77. 
    
  78.     def test06_expand_to_include_extent_4_tuple(self):
    
  79.         "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
    
  80.         self.e.expand_to_include((-1, 1, 3, 7))
    
  81.         self.assertEqual((-1, 0, 5, 7), self.e)
    
  82. 
    
  83.     def test07_expand_to_include_envelope(self):
    
  84.         "Testing Envelope expand_to_include with Envelope as parameter."
    
  85.         self.e.expand_to_include(Envelope(-1, 1, 3, 7))
    
  86.         self.assertEqual((-1, 0, 5, 7), self.e)
    
  87. 
    
  88.     def test08_expand_to_include_point(self):
    
  89.         "Testing Envelope expand_to_include with Point as parameter."
    
  90.         self.e.expand_to_include(TestPoint(-1, 1))
    
  91.         self.assertEqual((-1, 0, 5, 5), self.e)
    
  92.         self.e.expand_to_include(TestPoint(10, 10))
    
  93.         self.assertEqual((-1, 0, 10, 10), self.e)