MCPcopy
hub / github.com/django/django / test_proxy

Method test_proxy

tests/gis_tests/geoapp/tests.py:53–121  ·  view source on GitHub ↗

Testing Lazy-Geometry support (using the GeometryProxy).

(self)

Source from the content-addressed store, hash-verified

51 self.assertEqual(2, State.objects.count())
52
53 def test_proxy(self):
54 "Testing Lazy-Geometry support (using the GeometryProxy)."
55 # Testing on a Point
56 pnt = Point(0, 0)
57 nullcity = City(name="NullCity", point=pnt)
58 nullcity.save()
59
60 # Making sure TypeError is thrown when trying to set with an
61 # incompatible type.
62 for bad in [5, 2.0, LineString((0, 0), (1, 1))]:
63 with self.assertRaisesMessage(TypeError, "Cannot set"):
64 nullcity.point = bad
65
66 # Now setting with a compatible GEOS Geometry, saving, and ensuring
67 # the save took, notice no SRID is explicitly set.
68 new = Point(5, 23)
69 nullcity.point = new
70
71 # Ensuring that the SRID is automatically set to that of the
72 # field after assignment, but before saving.
73 self.assertEqual(4326, nullcity.point.srid)
74 nullcity.save()
75
76 # Ensuring the point was saved correctly after saving
77 self.assertEqual(new, City.objects.get(name="NullCity").point)
78
79 # Setting the X and Y of the Point
80 nullcity.point.x = 23
81 nullcity.point.y = 5
82 # Checking assignments pre & post-save.
83 self.assertNotEqual(
84 Point(23, 5, srid=4326), City.objects.get(name="NullCity").point
85 )
86 nullcity.save()
87 self.assertEqual(
88 Point(23, 5, srid=4326), City.objects.get(name="NullCity").point
89 )
90 nullcity.delete()
91
92 # Testing on a Polygon
93 shell = LinearRing((0, 0), (0, 90), (100, 90), (100, 0), (0, 0))
94 inner = LinearRing((40, 40), (40, 60), (60, 60), (60, 40), (40, 40))
95
96 # Creating a State object using a built Polygon
97 ply = Polygon(shell, inner)
98 nullstate = State(name="NullState", poly=ply)
99 self.assertEqual(4326, nullstate.poly.srid) # SRID auto-set from None
100 nullstate.save()
101
102 ns = State.objects.get(name="NullState")
103 self.assertEqual(connection.ops.Adapter._fix_polygon(ply), ns.poly)
104
105 # Testing the `ogr` and `srs` lazy-geometry properties.
106 self.assertIsInstance(ns.poly.ogr, gdal.OGRGeometry)
107 self.assertEqual(ns.poly.wkb, ns.poly.ogr.wkb)
108 self.assertIsInstance(ns.poly.srs, gdal.SpatialReference)
109 self.assertEqual("WGS 84", ns.poly.srs.name)
110

Callers

nothing calls this directly

Calls 11

PointClass · 0.90
LineStringClass · 0.90
LinearRingClass · 0.90
PolygonClass · 0.90
assertRaisesMessageMethod · 0.80
CityClass · 0.70
StateClass · 0.70
saveMethod · 0.45
getMethod · 0.45
deleteMethod · 0.45
_fix_polygonMethod · 0.45

Tested by

no test coverage detected