activerecord-postgis-adapter 3.1.0 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/basic_test.rb DELETED
@@ -1,160 +0,0 @@
1
- require 'test_helper'
2
-
3
- class BasicTest < ActiveSupport::TestCase # :nodoc:
4
-
5
- def test_version
6
- refute_nil ActiveRecord::ConnectionAdapters::PostGIS::VERSION
7
- end
8
-
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
13
-
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
21
-
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
30
-
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
39
-
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
51
-
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
63
-
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
74
-
75
- def test_set_point_bad_wkt
76
- create_model
77
- obj = SpatialModel.create(latlon: 'POINT (x)')
78
- assert_nil obj.latlon
79
- end
80
-
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)')
85
- end
86
- end
87
-
88
- def test_custom_factory
89
- klass = SpatialModel
90
- klass.connection.create_table(:spatial_models, force: true) do |t|
91
- t.st_polygon(:area, srid: 4326)
92
- end
93
- klass.reset_column_information
94
- custom_factory = RGeo::Geographic.spherical_factory(buffer_resolution: 8, srid: 4326)
95
- spatial_factory_store.register(custom_factory, geo_type: "polygon", srid: 4326)
96
- object = klass.new
97
- area = custom_factory.point(1, 2).buffer(3)
98
- object.area = area
99
- object.save!
100
- object.reload
101
- assert_equal area.to_s, object.area.to_s
102
- spatial_factory_store.clear
103
- end
104
-
105
- def test_readme_example
106
- spatial_factory_store.register(
107
- RGeo::Geographic.spherical_factory, geo_type: "point", sql_type: "geography")
108
-
109
- klass = SpatialModel
110
- klass.connection.create_table(:spatial_models, force: true) do |t|
111
- t.column(:shape, :geometry)
112
- t.line_string(:path, srid: 3785)
113
- t.st_point(:latlon, geographic: true)
114
- end
115
- klass.reset_column_information
116
- assert_includes klass.columns.map(&:name), "shape"
117
- klass.connection.change_table(:spatial_models) do |t|
118
- t.index(:latlon, using: :gist)
119
- end
120
-
121
- object = klass.new
122
- object.latlon = 'POINT(-122 47)'
123
- point = object.latlon
124
- assert_equal 47, point.latitude
125
- object.shape = point
126
- # assert_equal true, RGeo::Geos.is_geos?(object.shape)
127
-
128
- spatial_factory_store.clear
129
- end
130
-
131
- def test_point_to_json
132
- create_model
133
- obj = SpatialModel.new
134
- assert_match(/"latlon":null/, obj.to_json)
135
- obj.latlon = factory.point(1.0, 2.0)
136
- assert_match(/"latlon":"POINT\s\(1\.0\s2\.0\)"/, obj.to_json)
137
- end
138
-
139
- def test_custom_column
140
- create_model
141
- rec = SpatialModel.new
142
- rec.latlon = 'POINT(0 0)'
143
- rec.save
144
- refute_nil SpatialModel.select("CURRENT_TIMESTAMP as ts").first.ts
145
- end
146
-
147
- private
148
-
149
- def create_model
150
- SpatialModel.connection.create_table(:spatial_models, force: true) do |t|
151
- t.column 'latlon', :st_point, srid: 3785
152
- t.column 'latlon_geo', :st_point, srid: 4326, geographic: true
153
- end
154
- SpatialModel.reset_column_information
155
- end
156
-
157
- def spatial_factory_store
158
- RGeo::ActiveRecord::SpatialFactoryStore.instance
159
- end
160
- end
data/test/database.yml DELETED
@@ -1,6 +0,0 @@
1
- adapter: postgis
2
- host: 127.0.0.1
3
- database: postgis_adapter_test
4
- username: postgres
5
- setup: default
6
- schema_search_path: public
data/test/ddl_test.rb DELETED
@@ -1,323 +0,0 @@
1
- require 'test_helper'
2
-
3
- class DDLTest < ActiveSupport::TestCase # :nodoc:
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.spatial_column_options(type), type
17
- end
18
- end
19
-
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
24
-
25
- def test_create_simple_geometry
26
- klass.connection.create_table(:spatial_models, force: true) do |t|
27
- t.column 'latlon', :geometry
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.spatial?
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
39
-
40
- def test_create_simple_geography
41
- klass.connection.create_table(:spatial_models, force: true) do |t|
42
- t.column 'latlon', :geometry, geographic: true
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.spatial?
48
- assert_equal true, col.geographic?
49
- assert_equal 4326, col.srid
50
- assert_equal 0, count_geometry_columns
51
- end
52
-
53
- def test_create_point_geometry
54
- klass.connection.create_table(:spatial_models, force: true) do |t|
55
- t.column 'latlon', :st_point
56
- end
57
- klass.reset_column_information
58
- assert_equal RGeo::Feature::Point, klass.columns.last.geometric_type
59
- end
60
-
61
- def test_create_geometry_with_index
62
- klass.connection.create_table(:spatial_models, force: true) do |t|
63
- t.column 'latlon', :geometry
64
- end
65
- klass.connection.change_table(:spatial_models) do |t|
66
- t.index([:latlon], using: :gist)
67
- end
68
- klass.reset_column_information
69
- assert_equal :gist, klass.connection.indexes(:spatial_models).last.using
70
- end
71
-
72
- def test_add_geometry_column
73
- klass.connection.create_table(:spatial_models, force: true) do |t|
74
- t.column('latlon', :geometry)
75
- end
76
- klass.connection.change_table(:spatial_models) do |t|
77
- t.column('geom2', :st_point, srid: 4326)
78
- t.column('name', :string)
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].spatial?
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].spatial?
90
- assert_nil columns[-1].geometric_type
91
- assert_equal false, columns[-1].spatial?
92
- end
93
-
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)
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
106
-
107
- def test_add_geography_column
108
- klass.connection.create_table(:spatial_models, force: true) do |t|
109
- t.column('latlon', :geometry)
110
- end
111
- klass.connection.change_table(:spatial_models) do |t|
112
- t.st_point('geom3', srid: 4326, geographic: true)
113
- t.column('geom2', :st_point, srid: 4326, geographic: true)
114
- t.column('name', :string)
115
- end
116
- klass.reset_column_information
117
- assert_equal 1, count_geometry_columns
118
- cols = klass.columns
119
- # latlon
120
- assert_equal RGeo::Feature::Geometry, cols[-4].geometric_type
121
- assert_equal 0, cols[-4].srid
122
- assert_equal true, cols[-4].spatial?
123
- # geom3
124
- assert_equal RGeo::Feature::Point, cols[-3].geometric_type
125
- assert_equal 4326, cols[-3].srid
126
- assert_equal true, cols[-3].geographic?
127
- assert_equal true, cols[-3].spatial?
128
- # geom2
129
- assert_equal RGeo::Feature::Point, cols[-2].geometric_type
130
- assert_equal 4326, cols[-2].srid
131
- assert_equal true, cols[-2].geographic?
132
- assert_equal true, cols[-2].spatial?
133
- # name
134
- assert_nil cols[-1].geometric_type
135
- assert_equal false, cols[-1].spatial?
136
- end
137
-
138
- def test_drop_geometry_column
139
- klass.connection.create_table(:spatial_models, force: true) do |t|
140
- t.column('latlon', :geometry)
141
- t.column('geom2', :st_point, srid: 4326)
142
- end
143
- klass.connection.change_table(:spatial_models) do |t|
144
- t.remove('geom2')
145
- end
146
- klass.reset_column_information
147
- assert_equal 1, count_geometry_columns
148
- cols = klass.columns
149
- assert_equal RGeo::Feature::Geometry, cols[-1].geometric_type
150
- assert_equal 'latlon', cols[-1].name
151
- assert_equal 0, cols[-1].srid
152
- assert_equal false, cols[-1].geographic?
153
- end
154
-
155
- def test_drop_geography_column
156
- klass.connection.create_table(:spatial_models, force: true) do |t|
157
- t.column('latlon', :geometry)
158
- t.column('geom2', :st_point, srid: 4326, geographic: true)
159
- t.column('geom3', :st_point, srid: 4326)
160
- end
161
- klass.connection.change_table(:spatial_models) do |t|
162
- t.remove('geom2')
163
- end
164
- klass.reset_column_information
165
- assert_equal 2, count_geometry_columns
166
- columns = klass.columns
167
- assert_equal RGeo::Feature::Point, columns[-1].geometric_type
168
- assert_equal 'geom3', columns[-1].name
169
- assert_equal false, columns[-1].geographic?
170
- assert_equal RGeo::Feature::Geometry, columns[-2].geometric_type
171
- assert_equal 'latlon', columns[-2].name
172
- assert_equal false, columns[-2].geographic?
173
- end
174
-
175
- def test_create_simple_geometry_using_shortcut
176
- klass.connection.create_table(:spatial_models, force: true) do |t|
177
- t.geometry 'latlon'
178
- end
179
- klass.reset_column_information
180
- assert_equal 1, count_geometry_columns
181
- col = klass.columns.last
182
- assert_equal RGeo::Feature::Geometry, col.geometric_type
183
- assert_equal false, col.geographic?
184
- assert_equal 0, col.srid
185
- klass.connection.drop_table(:spatial_models)
186
- assert_equal 0, count_geometry_columns
187
- end
188
-
189
- def test_create_simple_geography_using_shortcut
190
- klass.connection.create_table(:spatial_models, force: true) do |t|
191
- t.geometry 'latlon', geographic: true
192
- end
193
- klass.reset_column_information
194
- col = klass.columns.last
195
- assert_equal RGeo::Feature::Geometry, col.geometric_type
196
- assert_equal true, col.geographic?
197
- assert_equal 4326, col.srid
198
- assert_equal 0, count_geometry_columns
199
- end
200
-
201
- def test_create_point_geometry_using_shortcut
202
- klass.connection.create_table(:spatial_models, force: true) do |t|
203
- t.st_point 'latlon'
204
- end
205
- klass.reset_column_information
206
- assert_equal RGeo::Feature::Point, klass.columns.last.geometric_type
207
- end
208
-
209
- def test_create_geometry_using_shortcut_with_srid
210
- klass.connection.create_table(:spatial_models, force: true) do |t|
211
- t.geometry 'latlon', srid: 4326
212
- end
213
- klass.reset_column_information
214
- col = klass.columns.last
215
- assert_equal RGeo::Feature::Geometry, col.geometric_type
216
- assert_equal({ srid: 4326, type: 'geometry' }, col.limit)
217
- end
218
-
219
- def test_create_polygon_with_options
220
- klass.connection.create_table(:spatial_models, force: true) do |t|
221
- t.column 'region', :st_polygon, has_m: true, srid: 3785
222
- end
223
- klass.reset_column_information
224
- assert_equal 1, count_geometry_columns
225
- col = klass.columns.last
226
- assert_equal RGeo::Feature::Polygon, col.geometric_type
227
- assert_equal false, col.geographic?
228
- assert_equal false, col.has_z?
229
- assert_equal true, col.has_m?
230
- assert_equal 3785, col.srid
231
- assert_equal({ has_m: true, type: 'polygon', srid: 3785 }, col.limit)
232
- klass.connection.drop_table(:spatial_models)
233
- assert_equal 0, count_geometry_columns
234
- end
235
-
236
- def test_no_query_spatial_column_info
237
- klass.connection.create_table(:spatial_models, force: true) do |t|
238
- t.string 'name'
239
- end
240
- klass.reset_column_information
241
- # `all` queries column info from the database - it should not be called when klass.columns is called
242
- ActiveRecord::ConnectionAdapters::PostGIS::SpatialColumnInfo.any_instance.expects(:all).never
243
- # first column is id, second is name
244
- refute klass.columns[1].spatial?
245
- assert_nil klass.columns[1].has_z
246
- end
247
-
248
- # Ensure that null contraints info is getting captured like the
249
- # normal adapter.
250
- def test_null_constraints
251
- klass.connection.create_table(:spatial_models, force: true) do |t|
252
- t.column 'nulls_allowed', :string, null: true
253
- t.column 'nulls_disallowed', :string, null: false
254
- end
255
- klass.reset_column_information
256
- assert_equal true, klass.columns[-2].null
257
- assert_equal false, klass.columns[-1].null
258
- end
259
-
260
- # Ensure column default value works like the Postgres adapter.
261
- def test_column_defaults
262
- klass.connection.create_table(:spatial_models, force: true) do |t|
263
- t.column 'sample_integer', :integer, default: -1
264
- end
265
- klass.reset_column_information
266
- assert_equal -1, klass.new.sample_integer
267
- end
268
-
269
- def test_column_types
270
- klass.connection.create_table(:spatial_models, force: true) do |t|
271
- t.column 'sample_integer', :integer
272
- t.column 'sample_string', :string
273
- t.column 'latlon', :st_point
274
- end
275
- klass.reset_column_information
276
- assert_equal :integer, klass.columns[-3].type
277
- assert_equal :string, klass.columns[-2].type
278
- assert_equal :geometry, klass.columns[-1].type
279
- end
280
-
281
- def test_array_columns
282
- klass.connection.create_table(:spatial_models, force: true) do |t|
283
- t.column 'sample_array', :string, array: true
284
- t.column 'sample_non_array', :string
285
- end
286
- klass.reset_column_information
287
- assert_equal true, klass.columns[-2].array
288
- assert_equal false, klass.columns[-1].array
289
- end
290
-
291
- def test_reload_dumped_schema
292
- klass.connection.create_table(:spatial_models, force: true) do |t|
293
- t.geography "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}
294
- end
295
- klass.reset_column_information
296
- col = klass.columns.last
297
- assert_equal 4326, col.srid
298
- end
299
-
300
- def test_non_spatial_column_limits
301
- klass.connection.create_table(:spatial_models, force: true) do |t|
302
- t.string :foo, limit: 123
303
- end
304
- klass.reset_column_information
305
- col = klass.columns.last
306
- assert_equal 123, col.limit
307
- end
308
-
309
- private
310
-
311
- def klass
312
- SpatialModel
313
- end
314
-
315
- def count_geometry_columns
316
- klass.connection.select_value(geo_column_sql("geometry_columns", klass.table_name)).to_i
317
- end
318
-
319
- def geo_column_sql(postgis_view, table_name)
320
- "SELECT COUNT(*) FROM #{ postgis_view } WHERE f_table_name='#{ table_name }'"
321
- end
322
-
323
- end