Module: ActiveRecord::ConnectionAdapters::PostGIS::TestHelpers
- Included in:
- ActiveRecordPostgis::TestHelper
- Defined in:
- lib/active_record/connection_adapters/postgis/test_helpers.rb
Overview
Test helpers for spatial data assertions
Instance Method Summary collapse
-
#assert_contains(container, contained, msg = nil) ⇒ Object
Assert that a geometry contains another geometry.
-
#assert_disjoint(geom1, geom2, msg = nil) ⇒ Object
Assert that two geometries do not intersect.
-
#assert_geometry_type(geometry, expected_type, msg = nil) ⇒ Object
Assert that a geometry is of the expected type.
- #assert_has_m(geometry, msg = nil) ⇒ Object
-
#assert_has_z(geometry, msg = nil) ⇒ Object
Legacy methods for backward compatibility.
-
#assert_intersects(geom1, geom2, msg = nil) ⇒ Object
Assert that two geometries intersect.
-
#assert_spatial_column(geometry, msg_prefix = nil) ⇒ Object
Chainable spatial column assertion builder.
-
#assert_spatial_equal(expected, actual, msg = nil) ⇒ Object
Assert that two spatial objects are equal.
-
#assert_srid(geometry, expected_srid, msg = nil) ⇒ Object
Assert that a geometry has the expected SRID.
-
#assert_within(inner, outer, msg = nil) ⇒ Object
Assert that a geometry is within another geometry.
-
#assert_within_distance(point1, point2, distance, msg = nil) ⇒ Object
Assert that a point is within a specified distance of another point.
-
#create_point(x, y, srid: 4326, z: nil, m: nil) ⇒ Object
Create a point for testing.
-
#create_test_linestring(srid: 4326) ⇒ Object
Create a test linestring for testing.
-
#create_test_polygon(srid: 4326) ⇒ Object
Create a test polygon for testing.
Instance Method Details
#assert_contains(container, contained, msg = nil) ⇒ Object
Assert that a geometry contains another geometry
27 28 29 30 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 27 def assert_contains(container, contained, msg = nil) msg ||= "Expected #{container.as_text} to contain #{contained.as_text}" assert container.contains?(contained), msg end |
#assert_disjoint(geom1, geom2, msg = nil) ⇒ Object
Assert that two geometries do not intersect
45 46 47 48 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 45 def assert_disjoint(geom1, geom2, msg = nil) msg ||= "Expected #{geom1.as_text} to be disjoint from #{geom2.as_text}" assert geom1.disjoint?(geom2), msg end |
#assert_geometry_type(geometry, expected_type, msg = nil) ⇒ Object
Assert that a geometry is of the expected type
63 64 65 66 67 68 69 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 63 def assert_geometry_type(geometry, expected_type, msg = nil) actual_type = geometry.geometry_type.type_name.downcase expected_type = expected_type.to_s.downcase.gsub("_", "") actual_type = actual_type.gsub("_", "") msg ||= "Expected geometry type #{expected_type} but got #{actual_type}" assert_equal expected_type, actual_type, msg end |
#assert_has_m(geometry, msg = nil) ⇒ Object
76 77 78 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 76 def assert_has_m(geometry, msg = nil) assert_spatial_column(geometry, msg).has_m end |
#assert_has_z(geometry, msg = nil) ⇒ Object
Legacy methods for backward compatibility
72 73 74 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 72 def assert_has_z(geometry, msg = nil) assert_spatial_column(geometry, msg).has_z end |
#assert_intersects(geom1, geom2, msg = nil) ⇒ Object
Assert that two geometries intersect
39 40 41 42 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 39 def assert_intersects(geom1, geom2, msg = nil) msg ||= "Expected #{geom1.as_text} to intersect #{geom2.as_text}" assert geom1.intersects?(geom2), msg end |
#assert_spatial_column(geometry, msg_prefix = nil) ⇒ Object
Chainable spatial column assertion builder
58 59 60 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 58 def assert_spatial_column(geometry, msg_prefix = nil) SpatialColumnAssertion.new(geometry, self, msg_prefix) end |
#assert_spatial_equal(expected, actual, msg = nil) ⇒ Object
Assert that two spatial objects are equal
9 10 11 12 13 14 15 16 17 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 9 def assert_spatial_equal(expected, actual, msg = nil) msg ||= "Expected spatial object #{expected.as_text} but got #{actual.as_text}" if expected.respond_to?(:equals?) && actual.respond_to?(:equals?) assert expected.equals?(actual), msg else assert_equal expected.to_s, actual.to_s, msg end end |
#assert_srid(geometry, expected_srid, msg = nil) ⇒ Object
Assert that a geometry has the expected SRID
51 52 53 54 55 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 51 def assert_srid(geometry, expected_srid, msg = nil) actual_srid = geometry.srid msg ||= "Expected SRID #{expected_srid} but got #{actual_srid}" assert_equal expected_srid, actual_srid, msg end |
#assert_within(inner, outer, msg = nil) ⇒ Object
Assert that a geometry is within another geometry
33 34 35 36 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 33 def assert_within(inner, outer, msg = nil) msg ||= "Expected #{inner.as_text} to be within #{outer.as_text}" assert inner.within?(outer), msg end |
#assert_within_distance(point1, point2, distance, msg = nil) ⇒ Object
Assert that a point is within a specified distance of another point
20 21 22 23 24 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 20 def assert_within_distance(point1, point2, distance, msg = nil) actual_distance = point1.distance(point2) msg ||= "Distance #{actual_distance} exceeds maximum allowed distance of #{distance}" assert actual_distance <= distance, msg end |
#create_point(x, y, srid: 4326, z: nil, m: nil) ⇒ Object
Create a point for testing
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 81 def create_point(x, y, srid: 4326, z: nil, m: nil) if z || m # Use cartesian factory for 3D/4D points factory = RGeo::Cartesian.preferred_factory(srid: srid, has_z_coordinate: !!z, has_m_coordinate: !!m) if z && m factory.point(x, y, z, m) elsif z factory.point(x, y, z) elsif m factory.point(x, y, 0, m) # Default Z to 0 for M-only else factory.point(x, y) end else factory = RGeo::Geographic.simple_mercator_factory(srid: srid) factory.point(x, y) end end |
#create_test_linestring(srid: 4326) ⇒ Object
Create a test linestring for testing
115 116 117 118 119 120 121 122 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 115 def create_test_linestring(srid: 4326) factory = RGeo::Geographic.simple_mercator_factory(srid: srid) factory.line_string([ factory.point(0, 0), factory.point(1, 1), factory.point(2, 0) ]) end |
#create_test_polygon(srid: 4326) ⇒ Object
Create a test polygon for testing
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_record/connection_adapters/postgis/test_helpers.rb', line 101 def create_test_polygon(srid: 4326) factory = RGeo::Geographic.simple_mercator_factory(srid: srid) factory.polygon( factory.linear_ring([ factory.point(0, 0), factory.point(0, 1), factory.point(1, 1), factory.point(1, 0), factory.point(0, 0) ]) ) end |