activerecord-postgis-adapter 2.2.2 → 3.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/active_record/connection_adapters/postgis_adapter.rb +2 -1
- data/lib/active_record/connection_adapters/postgis_adapter/arel_tosql.rb +9 -5
- data/lib/active_record/connection_adapters/postgis_adapter/create_connection.rb +7 -1
- data/lib/active_record/connection_adapters/postgis_adapter/main_adapter.rb +44 -185
- data/lib/active_record/connection_adapters/postgis_adapter/oid/spatial.rb +119 -0
- data/lib/active_record/connection_adapters/postgis_adapter/schema_statements.rb +110 -0
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_column.rb +32 -122
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_column_info.rb +2 -2
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_table_definition.rb +115 -65
- data/lib/active_record/connection_adapters/postgis_adapter/version.rb +1 -1
- data/test/basic_test.rb +129 -159
- data/test/ddl_test.rb +240 -256
- data/test/nested_class_test.rb +7 -18
- data/test/spatial_queries_test.rb +67 -85
- data/test/tasks_test.rb +116 -117
- data/test/test_helper.rb +20 -0
- data/test/type_test.rb +26 -0
- metadata +33 -22
- data/lib/active_record/connection_adapters/postgis_adapter/common_adapter_methods.rb +0 -53
data/test/basic_test.rb
CHANGED
@@ -1,182 +1,152 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class BasicTest < ActiveSupport::TestCase # :nodoc:
|
4
|
-
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__)+'/database.yml'
|
5
|
-
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__)+'/database_local.yml'
|
6
|
-
|
7
|
-
include RGeo::ActiveRecord::AdapterTestHelper
|
8
|
-
|
9
|
-
define_test_methods do
|
10
|
-
|
11
|
-
def populate_ar_class(content)
|
12
|
-
klass = create_ar_class
|
13
|
-
case content
|
14
|
-
when :mercator_point
|
15
|
-
klass.connection.create_table(:spatial_test) do |t_|
|
16
|
-
t_.column 'latlon', :point, :srid => 3785
|
17
|
-
end
|
18
|
-
when :latlon_point_geographic
|
19
|
-
klass.connection.create_table(:spatial_test) do |t_|
|
20
|
-
t_.column 'latlon', :point, :srid => 4326, :geographic => true
|
21
|
-
end
|
22
|
-
when :no_constraints
|
23
|
-
klass.connection.create_table(:spatial_test) do |t_|
|
24
|
-
t_.column 'geo', :geometry, :no_constraints => true
|
25
|
-
end
|
26
|
-
end
|
27
|
-
klass
|
28
|
-
end
|
29
4
|
|
30
|
-
|
31
|
-
|
32
|
-
|
5
|
+
def test_version
|
6
|
+
refute_nil ActiveRecord::ConnectionAdapters::PostGISAdapter::VERSION
|
7
|
+
end
|
33
8
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
9
|
+
def test_postgis_available
|
10
|
+
assert_equal 'PostGIS', SpatialModel.connection.adapter_name
|
11
|
+
assert SpatialModel.connection.postgis_lib_version.start_with? '2.'
|
12
|
+
end
|
39
13
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
14
|
+
def test_arel_visitor
|
15
|
+
visitor = Arel::Visitors::PostGIS.new(SpatialModel.connection)
|
16
|
+
node = RGeo::ActiveRecord::SpatialConstantNode.new('POINT (1.0 2.0)')
|
17
|
+
collector = Arel::Collectors::PlainString.new
|
18
|
+
visitor.accept(node, collector)
|
19
|
+
assert_equal "ST_GeomFromEWKT('POINT (1.0 2.0)')", collector.value
|
20
|
+
end
|
48
21
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
22
|
+
def test_set_and_get_point
|
23
|
+
create_model
|
24
|
+
obj = SpatialModel.new
|
25
|
+
assert_nil obj.latlon
|
26
|
+
obj.latlon = factory.point(1.0, 2.0)
|
27
|
+
assert_equal factory.point(1.0, 2.0), obj.latlon
|
28
|
+
assert_equal 3785, obj.latlon.srid
|
29
|
+
end
|
57
30
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
assert_equal 3785, obj2.latlon.srid
|
67
|
-
assert_equal true, ::RGeo::Geos.is_geos?(obj2.latlon)
|
68
|
-
end
|
31
|
+
def test_set_and_get_point_from_wkt
|
32
|
+
create_model
|
33
|
+
obj = SpatialModel.new
|
34
|
+
assert_nil obj.latlon
|
35
|
+
obj.latlon = 'POINT(1 2)'
|
36
|
+
assert_equal factory.point(1.0, 2.0), obj.latlon
|
37
|
+
assert_equal 3785, obj.latlon.srid
|
38
|
+
end
|
69
39
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
40
|
+
def test_save_and_load_point
|
41
|
+
create_model
|
42
|
+
obj = SpatialModel.new
|
43
|
+
obj.latlon = factory.point(1.0, 2.0)
|
44
|
+
obj.save!
|
45
|
+
id = obj.id
|
46
|
+
obj2 = SpatialModel.find(id)
|
47
|
+
assert_equal factory.point(1.0, 2.0), obj2.latlon
|
48
|
+
assert_equal 3785, obj2.latlon.srid
|
49
|
+
# assert_equal true, RGeo::Geos.is_geos?(obj2.latlon)
|
50
|
+
end
|
81
51
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
52
|
+
def test_save_and_load_geographic_point
|
53
|
+
create_model
|
54
|
+
obj = SpatialModel.new
|
55
|
+
obj.latlon_geo = geographic_factory.point(1.0, 2.0)
|
56
|
+
obj.save!
|
57
|
+
id = obj.id
|
58
|
+
obj2 = SpatialModel.find(id)
|
59
|
+
assert_equal geographic_factory.point(1.0, 2.0), obj2.latlon_geo
|
60
|
+
assert_equal 4326, obj2.latlon_geo.srid
|
61
|
+
# assert_equal false, RGeo::Geos.is_geos?(obj2.latlon_geo)
|
62
|
+
end
|
92
63
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
64
|
+
def test_save_and_load_point_from_wkt
|
65
|
+
create_model
|
66
|
+
obj = SpatialModel.new
|
67
|
+
obj.latlon = 'POINT(1 2)'
|
68
|
+
obj.save!
|
69
|
+
id = obj.id
|
70
|
+
obj2 = SpatialModel.find(id)
|
71
|
+
assert_equal factory.point(1.0, 2.0), obj2.latlon
|
72
|
+
assert_equal 3785, obj2.latlon.srid
|
73
|
+
end
|
98
74
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
end
|
75
|
+
def test_set_point_bad_wkt
|
76
|
+
create_model
|
77
|
+
obj = SpatialModel.create(latlon: 'POINT (x)')
|
78
|
+
assert_nil obj.latlon
|
79
|
+
end
|
105
80
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
end
|
111
|
-
factory = ::RGeo::Geographic.simple_mercator_factory
|
112
|
-
klass.class_eval do
|
113
|
-
set_rgeo_factory_for_column(:latlon, factory)
|
114
|
-
end
|
115
|
-
rec_ = klass.new
|
116
|
-
rec_.latlon = 'POINT(-122 47)'
|
117
|
-
assert_equal factory, rec_.latlon.factory
|
118
|
-
rec_.save!
|
119
|
-
assert_equal factory, rec_.latlon.factory
|
120
|
-
rec2_ = klass.find(rec_.id)
|
121
|
-
assert_equal factory, rec2_.latlon.factory
|
81
|
+
def test_set_point_wkt_wrong_type
|
82
|
+
create_model
|
83
|
+
assert_raises(ActiveRecord::StatementInvalid) do
|
84
|
+
SpatialModel.create(latlon: 'LINESTRING(1 2, 3 4, 5 6)')
|
122
85
|
end
|
86
|
+
end
|
123
87
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
t_.line_string(:path, :srid => 3785)
|
129
|
-
t_.point(:latlon, :geographic => true)
|
130
|
-
end
|
131
|
-
klass.connection.change_table(:spatial_test) do |t_|
|
132
|
-
t_.index(:latlon, :spatial => true)
|
133
|
-
end
|
134
|
-
klass.class_eval do
|
135
|
-
self.rgeo_factory_generator = ::RGeo::Geos.method(:factory)
|
136
|
-
set_rgeo_factory_for_column(:latlon, ::RGeo::Geographic.spherical_factory)
|
137
|
-
end
|
138
|
-
rec_ = klass.new
|
139
|
-
rec_.latlon = 'POINT(-122 47)'
|
140
|
-
loc_ = rec_.latlon
|
141
|
-
assert_equal 47, loc_.latitude
|
142
|
-
rec_.shape = loc_
|
143
|
-
assert_equal true, ::RGeo::Geos.is_geos?(rec_.shape)
|
88
|
+
def test_custom_factory
|
89
|
+
klass = SpatialModel
|
90
|
+
klass.connection.create_table(:spatial_test, force: true) do |t|
|
91
|
+
t.st_point(:latlon, srid: 4326)
|
144
92
|
end
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
klass = populate_ar_class(:no_constraints)
|
149
|
-
factory1_ = ::RGeo::Cartesian.preferred_factory(:srid => 3785)
|
150
|
-
factory2_ = ::RGeo::Cartesian.preferred_factory(:srid => 2000)
|
151
|
-
obj = klass.new
|
152
|
-
obj.geo = factory1_.point(1.0, 2.0)
|
153
|
-
obj.save!
|
154
|
-
id = obj.id
|
155
|
-
obj2 = klass.find(id)
|
156
|
-
assert_equal factory1_.point(1.0, 2.0), obj2.geo
|
157
|
-
assert_equal 3785, obj2.geo.srid
|
158
|
-
obj2.geo = factory2_.point(3.0, 4.0)
|
159
|
-
obj2.save!
|
160
|
-
obj3 = klass.find(id)
|
161
|
-
assert_equal factory2_.point(3.0, 4.0), obj3.geo
|
162
|
-
assert_equal 2000, obj3.geo.srid
|
93
|
+
factory = RGeo::Geographic.simple_mercator_factory
|
94
|
+
klass.class_eval do
|
95
|
+
set_rgeo_factory_for_column(:latlon, factory)
|
163
96
|
end
|
97
|
+
assert_equal factory, klass.rgeo_factory_for_column(:latlon)
|
98
|
+
object = klass.new
|
99
|
+
assert_equal factory, object.class.rgeo_factory_for_column(:latlon)
|
100
|
+
end
|
164
101
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
102
|
+
def test_readme_example
|
103
|
+
klass = SpatialModel
|
104
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
105
|
+
t.column(:shape, :geometry)
|
106
|
+
t.line_string(:path, srid: 3785)
|
107
|
+
t.st_point(:latlon, geographic: true)
|
171
108
|
end
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
109
|
+
klass.reset_column_information
|
110
|
+
assert_includes klass.columns.map(&:name), "shape"
|
111
|
+
klass.connection.change_table(:spatial_models) do |t|
|
112
|
+
t.index(:latlon, using: :gist)
|
113
|
+
end
|
114
|
+
klass.class_eval do
|
115
|
+
self.rgeo_factory_generator = RGeo::Geos.method(:factory)
|
116
|
+
set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory)
|
179
117
|
end
|
118
|
+
object = klass.new
|
119
|
+
object.latlon = 'POINT(-122 47)'
|
120
|
+
point = object.latlon
|
121
|
+
assert_equal 47, point.latitude
|
122
|
+
object.shape = point
|
123
|
+
# assert_equal true, RGeo::Geos.is_geos?(object.shape)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_point_to_json
|
127
|
+
create_model
|
128
|
+
obj = SpatialModel.new
|
129
|
+
assert_match(/"latlon":null/, obj.to_json)
|
130
|
+
obj.latlon = factory.point(1.0, 2.0)
|
131
|
+
assert_match(/"latlon":"POINT\s\(1\.0\s2\.0\)"/, obj.to_json)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_custom_column
|
135
|
+
create_model
|
136
|
+
rec = SpatialModel.new
|
137
|
+
rec.latlon = 'POINT(0 0)'
|
138
|
+
rec.save
|
139
|
+
refute_nil SpatialModel.select("CURRENT_TIMESTAMP as ts").first.ts
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
180
143
|
|
144
|
+
def create_model
|
145
|
+
SpatialModel.connection.create_table(:spatial_models, force: true) do |t|
|
146
|
+
t.column 'latlon', :st_point, srid: 3785
|
147
|
+
t.column 'latlon_geo', :st_point, srid: 4326, geographic: true
|
148
|
+
end
|
149
|
+
SpatialModel.reset_column_information
|
181
150
|
end
|
182
151
|
end
|
152
|
+
|
data/test/ddl_test.rb
CHANGED
@@ -1,303 +1,287 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class DDLTest < ActiveSupport::TestCase # :nodoc:
|
4
|
-
|
5
|
-
|
4
|
+
def test_spatial_column_options
|
5
|
+
[
|
6
|
+
:geography,
|
7
|
+
:geometry,
|
8
|
+
:geometry_collection,
|
9
|
+
:line_string,
|
10
|
+
:multi_line_string,
|
11
|
+
:multi_point,
|
12
|
+
:multi_polygon,
|
13
|
+
:st_point,
|
14
|
+
:st_polygon,
|
15
|
+
].each do |type|
|
16
|
+
assert ActiveRecord::ConnectionAdapters::PostGISAdapter::MainAdapter.spatial_column_options(type), type
|
17
|
+
end
|
18
|
+
end
|
6
19
|
|
7
|
-
|
20
|
+
def test_type_to_sql
|
21
|
+
adapter = SpatialModel.connection
|
22
|
+
assert_equal "geometry(point,4326)", adapter.type_to_sql(:geometry, "point,4326")
|
23
|
+
end
|
8
24
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
klass.connection.create_table(:spatial_test) do |t|
|
13
|
-
t.column 'latlon', :geometry
|
14
|
-
end
|
15
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
16
|
-
col = klass.columns.last
|
17
|
-
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
18
|
-
assert_equal(true, col.has_spatial_constraints?)
|
19
|
-
assert_equal(false, col.geographic?)
|
20
|
-
assert_equal(0, col.srid)
|
21
|
-
assert(klass.cached_attributes.include?('latlon'))
|
22
|
-
klass.connection.drop_table(:spatial_test)
|
23
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
25
|
+
def test_create_simple_geometry
|
26
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
27
|
+
t.column 'latlon', :geometry
|
24
28
|
end
|
29
|
+
klass.reset_column_information
|
30
|
+
assert_equal 1, count_geometry_columns
|
31
|
+
col = klass.columns.last
|
32
|
+
assert_equal RGeo::Feature::Geometry, col.geometric_type
|
33
|
+
assert_equal true, col.has_spatial_constraints?
|
34
|
+
assert_equal false, col.geographic?
|
35
|
+
assert_equal 0, col.srid
|
36
|
+
klass.connection.drop_table(:spatial_models)
|
37
|
+
assert_equal 0, count_geometry_columns
|
38
|
+
end
|
25
39
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
t.column 'latlon', :geometry, :geographic => true
|
30
|
-
end
|
31
|
-
col = klass.columns.last
|
32
|
-
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
33
|
-
assert_equal(true, col.has_spatial_constraints?)
|
34
|
-
assert_equal(true, col.geographic?)
|
35
|
-
assert_equal(4326, col.srid)
|
36
|
-
assert(klass.cached_attributes.include?('latlon'))
|
37
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
40
|
+
def test_create_simple_geography
|
41
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
42
|
+
t.column 'latlon', :geometry, geographic: true
|
38
43
|
end
|
44
|
+
klass.reset_column_information
|
45
|
+
col = klass.columns.last
|
46
|
+
assert_equal RGeo::Feature::Geometry, col.geometric_type
|
47
|
+
assert_equal true, col.has_spatial_constraints?
|
48
|
+
assert_equal true, col.geographic?
|
49
|
+
assert_equal 4326, col.srid
|
50
|
+
assert_equal 0, count_geometry_columns
|
51
|
+
end
|
39
52
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
t.column 'latlon', :point
|
44
|
-
end
|
45
|
-
assert_equal(::RGeo::Feature::Point, klass.columns.last.geometric_type)
|
46
|
-
assert(klass.cached_attributes.include?('latlon'))
|
53
|
+
def test_create_point_geometry
|
54
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
55
|
+
t.column 'latlon', :st_point
|
47
56
|
end
|
57
|
+
klass.reset_column_information
|
58
|
+
assert_equal RGeo::Feature::Point, klass.columns.last.geometric_type
|
59
|
+
end
|
48
60
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
t.column 'latlon', :geometry
|
53
|
-
end
|
54
|
-
klass.connection.change_table(:spatial_test) do |t|
|
55
|
-
t.index([:latlon], :spatial => true)
|
56
|
-
end
|
57
|
-
assert(klass.connection.indexes(:spatial_test).last.spatial)
|
61
|
+
def test_create_geometry_with_index
|
62
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
63
|
+
t.column 'latlon', :geometry
|
58
64
|
end
|
59
|
-
|
60
|
-
|
61
|
-
klass = create_ar_class
|
62
|
-
klass.connection.create_table(:spatial_test) do |t|
|
63
|
-
t.column('latlon', :geometry)
|
64
|
-
end
|
65
|
-
klass.connection.change_table(:spatial_test) do |t|
|
66
|
-
t.column('geom2', :point, :srid => 4326)
|
67
|
-
t.column('name', :string)
|
68
|
-
end
|
69
|
-
assert_equal(2, klass.connection.select_value(geometry_column_count_query).to_i)
|
70
|
-
cols_ = klass.columns
|
71
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
72
|
-
assert_equal(0, cols_[-3].srid)
|
73
|
-
assert_equal(true, cols_[-3].has_spatial_constraints?)
|
74
|
-
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
75
|
-
assert_equal(4326, cols_[-2].srid)
|
76
|
-
assert_equal(false, cols_[-2].geographic?)
|
77
|
-
assert_equal(true, cols_[-2].has_spatial_constraints?)
|
78
|
-
assert_nil(cols_[-1].geometric_type)
|
79
|
-
assert_equal(false, cols_[-1].has_spatial_constraints?)
|
65
|
+
klass.connection.change_table(:spatial_models) do |t|
|
66
|
+
t.index([:latlon], using: :gist)
|
80
67
|
end
|
68
|
+
klass.reset_column_information
|
69
|
+
assert_equal :gist, klass.connection.indexes(:spatial_models).last.using
|
70
|
+
end
|
81
71
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
t.column('latlon_null', :geometry, null: false)
|
86
|
-
t.column('latlon', :geometry)
|
87
|
-
end
|
88
|
-
null_false_column = klass.columns[1]
|
89
|
-
null_true_column = klass.columns[2]
|
90
|
-
|
91
|
-
refute(null_false_column.null, 'Column should be null: false')
|
92
|
-
assert(null_true_column.null, 'Column should be null: true')
|
72
|
+
def test_add_geometry_column
|
73
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
74
|
+
t.column('latlon', :geometry)
|
93
75
|
end
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
klass.connection.create_table(:spatial_test) do |t|
|
98
|
-
t.column('latlon', :geometry)
|
99
|
-
end
|
100
|
-
klass.connection.change_table(:spatial_test) do |t|
|
101
|
-
t.column('geom2', :point, :srid => 4326, :geographic => true)
|
102
|
-
t.column('name', :string)
|
103
|
-
end
|
104
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
105
|
-
cols_ = klass.columns
|
106
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
107
|
-
assert_equal(0, cols_[-3].srid)
|
108
|
-
assert_equal(true, cols_[-3].has_spatial_constraints?)
|
109
|
-
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
110
|
-
assert_equal(4326, cols_[-2].srid)
|
111
|
-
assert_equal(true, cols_[-2].geographic?)
|
112
|
-
assert_equal(true, cols_[-2].has_spatial_constraints?)
|
113
|
-
assert_nil(cols_[-1].geometric_type)
|
114
|
-
assert_equal(false, cols_[-1].has_spatial_constraints?)
|
76
|
+
klass.connection.change_table(:spatial_models) do |t|
|
77
|
+
t.column('geom2', :st_point, srid: 4326)
|
78
|
+
t.column('name', :string)
|
115
79
|
end
|
80
|
+
klass.reset_column_information
|
81
|
+
assert_equal 2, count_geometry_columns
|
82
|
+
columns = klass.columns
|
83
|
+
assert_equal RGeo::Feature::Geometry, columns[-3].geometric_type
|
84
|
+
assert_equal 0, columns[-3].srid
|
85
|
+
assert_equal true, columns[-3].has_spatial_constraints?
|
86
|
+
assert_equal RGeo::Feature::Point, columns[-2].geometric_type
|
87
|
+
assert_equal 4326, columns[-2].srid
|
88
|
+
assert_equal false, columns[-2].geographic?
|
89
|
+
assert_equal true, columns[-2].has_spatial_constraints?
|
90
|
+
assert_nil columns[-1].geometric_type
|
91
|
+
assert_equal false, columns[-1].has_spatial_constraints?
|
92
|
+
end
|
116
93
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
t.column('geom2', :point, :srid => 4326)
|
122
|
-
end
|
123
|
-
klass.connection.change_table(:spatial_test) do |t|
|
124
|
-
t.remove('geom2')
|
125
|
-
end
|
126
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
127
|
-
cols_ = klass.columns
|
128
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-1].geometric_type)
|
129
|
-
assert_equal('latlon', cols_[-1].name)
|
130
|
-
assert_equal(0, cols_[-1].srid)
|
131
|
-
assert_equal(false, cols_[-1].geographic?)
|
94
|
+
def test_add_geometry_column_null_false
|
95
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
96
|
+
t.column('latlon_null', :geometry, null: false)
|
97
|
+
t.column('latlon', :geometry)
|
132
98
|
end
|
99
|
+
klass.reset_column_information
|
100
|
+
null_false_column = klass.columns[1]
|
101
|
+
null_true_column = klass.columns[2]
|
102
|
+
|
103
|
+
refute null_false_column.null, 'Column should be null: false'
|
104
|
+
assert null_true_column.null, 'Column should be null: true'
|
105
|
+
end
|
133
106
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
t.column('latlon', :geometry)
|
138
|
-
t.column('geom2', :point, :srid => 4326, :geographic => true)
|
139
|
-
t.column('geom3', :point, :srid => 4326)
|
140
|
-
end
|
141
|
-
klass.connection.change_table(:spatial_test) do |t|
|
142
|
-
t.remove('geom2')
|
143
|
-
end
|
144
|
-
assert_equal(2, klass.connection.select_value(geometry_column_count_query).to_i)
|
145
|
-
cols_ = klass.columns
|
146
|
-
assert_equal(::RGeo::Feature::Point, cols_[-1].geometric_type)
|
147
|
-
assert_equal('geom3', cols_[-1].name)
|
148
|
-
assert_equal(false, cols_[-1].geographic?)
|
149
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
|
150
|
-
assert_equal('latlon', cols_[-2].name)
|
151
|
-
assert_equal(false, cols_[-2].geographic?)
|
107
|
+
def test_add_geography_column
|
108
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
109
|
+
t.column('latlon', :geometry)
|
152
110
|
end
|
111
|
+
klass.connection.change_table(:spatial_models) do |t|
|
112
|
+
t.column('geom2', :st_point, srid: 4326, geographic: true)
|
113
|
+
t.column('name', :string)
|
114
|
+
end
|
115
|
+
klass.reset_column_information
|
116
|
+
assert_equal 1, count_geometry_columns
|
117
|
+
cols_ = klass.columns
|
118
|
+
assert_equal RGeo::Feature::Geometry, cols_[-3].geometric_type
|
119
|
+
assert_equal 0, cols_[-3].srid
|
120
|
+
assert_equal true, cols_[-3].has_spatial_constraints?
|
121
|
+
assert_equal RGeo::Feature::Point, cols_[-2].geometric_type
|
122
|
+
assert_equal 4326, cols_[-2].srid
|
123
|
+
assert_equal true, cols_[-2].geographic?
|
124
|
+
assert_equal true, cols_[-2].has_spatial_constraints?
|
125
|
+
assert_nil cols_[-1].geometric_type
|
126
|
+
assert_equal false, cols_[-1].has_spatial_constraints?
|
127
|
+
end
|
153
128
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
159
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
160
|
-
col = klass.columns.last
|
161
|
-
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
162
|
-
assert_equal(false, col.geographic?)
|
163
|
-
assert_equal(0, col.srid)
|
164
|
-
assert(klass.cached_attributes.include?('latlon'))
|
165
|
-
klass.connection.drop_table(:spatial_test)
|
166
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
129
|
+
def test_drop_geometry_column
|
130
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
131
|
+
t.column('latlon', :geometry)
|
132
|
+
t.column('geom2', :st_point, srid: 4326)
|
167
133
|
end
|
134
|
+
klass.connection.change_table(:spatial_models) do |t|
|
135
|
+
t.remove('geom2')
|
136
|
+
end
|
137
|
+
klass.reset_column_information
|
138
|
+
assert_equal 1, count_geometry_columns
|
139
|
+
cols_ = klass.columns
|
140
|
+
assert_equal RGeo::Feature::Geometry, cols_[-1].geometric_type
|
141
|
+
assert_equal 'latlon', cols_[-1].name
|
142
|
+
assert_equal 0, cols_[-1].srid
|
143
|
+
assert_equal false, cols_[-1].geographic?
|
144
|
+
end
|
168
145
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
col = klass.columns.last
|
175
|
-
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
176
|
-
assert_equal(true, col.geographic?)
|
177
|
-
assert_equal(4326, col.srid)
|
178
|
-
assert(klass.cached_attributes.include?('latlon'))
|
179
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
146
|
+
def test_drop_geography_column
|
147
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
148
|
+
t.column('latlon', :geometry)
|
149
|
+
t.column('geom2', :st_point, srid: 4326, geographic: true)
|
150
|
+
t.column('geom3', :st_point, srid: 4326)
|
180
151
|
end
|
152
|
+
klass.connection.change_table(:spatial_models) do |t|
|
153
|
+
t.remove('geom2')
|
154
|
+
end
|
155
|
+
klass.reset_column_information
|
156
|
+
assert_equal 2, count_geometry_columns
|
157
|
+
columns = klass.columns
|
158
|
+
assert_equal RGeo::Feature::Point, columns[-1].geometric_type
|
159
|
+
assert_equal 'geom3', columns[-1].name
|
160
|
+
assert_equal false, columns[-1].geographic?
|
161
|
+
assert_equal RGeo::Feature::Geometry, columns[-2].geometric_type
|
162
|
+
assert_equal 'latlon', columns[-2].name
|
163
|
+
assert_equal false, columns[-2].geographic?
|
164
|
+
end
|
181
165
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
t.point 'latlon'
|
186
|
-
end
|
187
|
-
assert_equal(::RGeo::Feature::Point, klass.columns.last.geometric_type)
|
188
|
-
assert(klass.cached_attributes.include?('latlon'))
|
166
|
+
def test_create_simple_geometry_using_shortcut
|
167
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
168
|
+
t.geometry 'latlon'
|
189
169
|
end
|
170
|
+
klass.reset_column_information
|
171
|
+
assert_equal 1, count_geometry_columns
|
172
|
+
col = klass.columns.last
|
173
|
+
assert_equal RGeo::Feature::Geometry, col.geometric_type
|
174
|
+
assert_equal false, col.geographic?
|
175
|
+
assert_equal 0, col.srid
|
176
|
+
klass.connection.drop_table(:spatial_models)
|
177
|
+
assert_equal 0, count_geometry_columns
|
178
|
+
end
|
190
179
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
t.column 'region', :polygon, :has_m => true, :srid => 3785
|
195
|
-
end
|
196
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
197
|
-
col = klass.columns.last
|
198
|
-
assert_equal(::RGeo::Feature::Polygon, col.geometric_type)
|
199
|
-
assert_equal(false, col.geographic?)
|
200
|
-
assert_equal(false, col.has_z?)
|
201
|
-
assert_equal(true, col.has_m?)
|
202
|
-
assert_equal(3785, col.srid)
|
203
|
-
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col.limit)
|
204
|
-
assert(klass.cached_attributes.include?('region'))
|
205
|
-
klass.connection.drop_table(:spatial_test)
|
206
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
180
|
+
def test_create_simple_geography_using_shortcut
|
181
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
182
|
+
t.geometry 'latlon', geographic: true
|
207
183
|
end
|
184
|
+
klass.reset_column_information
|
185
|
+
col = klass.columns.last
|
186
|
+
assert_equal RGeo::Feature::Geometry, col.geometric_type
|
187
|
+
assert_equal true, col.geographic?
|
188
|
+
assert_equal 4326, col.srid
|
189
|
+
assert_equal 0, count_geometry_columns
|
190
|
+
end
|
208
191
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
t.spatial 'region', :limit => {:has_m => true, :srid => 3785, :type => :polygon}
|
213
|
-
end
|
214
|
-
assert_equal(1, klass.connection.select_value(geometry_column_count_query).to_i)
|
215
|
-
col = klass.columns.last
|
216
|
-
assert_equal(::RGeo::Feature::Polygon, col.geometric_type)
|
217
|
-
assert_equal(false, col.geographic?)
|
218
|
-
assert_equal(false, col.has_z)
|
219
|
-
assert_equal(true, col.has_m)
|
220
|
-
assert_equal(3785, col.srid)
|
221
|
-
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col.limit)
|
222
|
-
assert(klass.cached_attributes.include?('region'))
|
223
|
-
klass.connection.drop_table(:spatial_test)
|
224
|
-
assert_equal(0, klass.connection.select_value(geometry_column_count_query).to_i)
|
192
|
+
def test_create_point_geometry_using_shortcut
|
193
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
194
|
+
t.st_point 'latlon'
|
225
195
|
end
|
196
|
+
klass.reset_column_information
|
197
|
+
assert_equal RGeo::Feature::Point, klass.columns.last.geometric_type
|
198
|
+
end
|
226
199
|
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
t.point 'latlon'
|
231
|
-
t.point 'other'
|
232
|
-
end
|
233
|
-
::ActiveRecord::ConnectionAdapters::PostGISAdapter::SpatialColumnInfo.any_instance.expects(:all).once.returns({})
|
234
|
-
klass.columns
|
235
|
-
klass.columns
|
200
|
+
def test_create_geometry_with_options
|
201
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
202
|
+
t.column 'region', :st_polygon, has_m: true, srid: 3785
|
236
203
|
end
|
204
|
+
klass.reset_column_information
|
205
|
+
assert_equal 1, count_geometry_columns
|
206
|
+
col = klass.columns.last
|
207
|
+
assert_equal RGeo::Feature::Polygon, col.geometric_type
|
208
|
+
assert_equal false, col.geographic?
|
209
|
+
assert_equal false, col.has_z?
|
210
|
+
assert_equal true, col.has_m?
|
211
|
+
assert_equal 3785, col.srid
|
212
|
+
assert_equal({ has_m: true, type: 'polygon', srid: 3785 }, col.limit)
|
213
|
+
klass.connection.drop_table(:spatial_models)
|
214
|
+
assert_equal 0, count_geometry_columns
|
215
|
+
end
|
237
216
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
t.string 'name'
|
242
|
-
end
|
243
|
-
# `all` queries column info from the database - it should not be called when klass.columns is called
|
244
|
-
::ActiveRecord::ConnectionAdapters::PostGISAdapter::SpatialColumnInfo.any_instance.expects(:all).never
|
245
|
-
# first column is id, second is name
|
246
|
-
refute klass.columns[1].spatial?
|
247
|
-
assert_nil klass.columns[1].has_z
|
217
|
+
def test_no_query_spatial_column_info
|
218
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
219
|
+
t.string 'name'
|
248
220
|
end
|
221
|
+
klass.reset_column_information
|
222
|
+
# `all` queries column info from the database - it should not be called when klass.columns is called
|
223
|
+
ActiveRecord::ConnectionAdapters::PostGISAdapter::SpatialColumnInfo.any_instance.expects(:all).never
|
224
|
+
# first column is id, second is name
|
225
|
+
refute klass.columns[1].spatial?
|
226
|
+
assert_nil klass.columns[1].has_z
|
227
|
+
end
|
249
228
|
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
t_.column 'nulls_disallowed', :string, :null => false
|
257
|
-
end
|
258
|
-
assert_equal(true, klass_.columns[-2].null)
|
259
|
-
assert_equal(false, klass_.columns[-1].null)
|
229
|
+
# Ensure that null contraints info is getting captured like the
|
230
|
+
# normal adapter.
|
231
|
+
def test_null_constraints
|
232
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
233
|
+
t.column 'nulls_allowed', :string, null: true
|
234
|
+
t.column 'nulls_disallowed', :string, null: false
|
260
235
|
end
|
236
|
+
klass.reset_column_information
|
237
|
+
assert_equal true, klass.columns[-2].null
|
238
|
+
assert_equal false, klass.columns[-1].null
|
239
|
+
end
|
261
240
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
267
|
-
t_.column 'sample_integer_neg_default', :integer, :default => -1
|
268
|
-
end
|
269
|
-
assert_equal(-1, klass_.columns.last.default)
|
241
|
+
# Ensure column default value works like the Postgres adapter.
|
242
|
+
def test_column_defaults
|
243
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
244
|
+
t.column 'sample_integer', :integer, default: -1
|
270
245
|
end
|
246
|
+
klass.reset_column_information
|
247
|
+
assert_equal "-1", klass.columns.last.default
|
248
|
+
assert_equal -1, klass.new.sample_integer
|
249
|
+
end
|
271
250
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
t_.column 'sample_integer', :integer
|
278
|
-
t_.column 'sample_string', :string
|
279
|
-
t_.column 'latlon', :point
|
280
|
-
end
|
281
|
-
assert_equal(:integer, klass_.columns[-3].type)
|
282
|
-
assert_equal(:string, klass_.columns[-2].type)
|
283
|
-
assert_equal(:spatial, klass_.columns[-1].type)
|
251
|
+
def test_column_types
|
252
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
253
|
+
t.column 'sample_integer', :integer
|
254
|
+
t.column 'sample_string', :string
|
255
|
+
t.column 'latlon', :st_point
|
284
256
|
end
|
257
|
+
klass.reset_column_information
|
258
|
+
assert_equal :integer, klass.columns[-3].type
|
259
|
+
assert_equal :string, klass.columns[-2].type
|
260
|
+
assert_equal :geometry, klass.columns[-1].type
|
261
|
+
end
|
285
262
|
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
t_.column 'sample_non_array', :string
|
291
|
-
end
|
292
|
-
assert_equal(true, klass_.columns[-2].array)
|
293
|
-
assert_equal(false, klass_.columns[-1].array)
|
263
|
+
def test_array_columns
|
264
|
+
klass.connection.create_table(:spatial_models, force: true) do |t|
|
265
|
+
t.column 'sample_array', :string, array: true
|
266
|
+
t.column 'sample_non_array', :string
|
294
267
|
end
|
268
|
+
klass.reset_column_information
|
269
|
+
assert_equal true, klass.columns[-2].array
|
270
|
+
assert_equal false, klass.columns[-1].array
|
271
|
+
end
|
295
272
|
|
296
|
-
|
273
|
+
private
|
297
274
|
|
298
|
-
|
299
|
-
|
300
|
-
|
275
|
+
def klass
|
276
|
+
SpatialModel
|
277
|
+
end
|
278
|
+
|
279
|
+
def count_geometry_columns
|
280
|
+
klass.connection.select_value(geo_column_sql("geometry_columns", klass.table_name)).to_i
|
281
|
+
end
|
282
|
+
|
283
|
+
def geo_column_sql(postgis_view, table_name)
|
284
|
+
"SELECT COUNT(*) FROM #{ postgis_view } WHERE f_table_name='#{ table_name }'"
|
301
285
|
end
|
302
286
|
|
303
287
|
end
|