beh_spatial_adapter 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ postgis_connection
2
+
3
+ ActiveRecord::Schema.define() do
4
+ execute <<-SQL
5
+ drop table if exists point_models;
6
+ create table point_models
7
+ (
8
+ id serial primary key,
9
+ extra varchar(255),
10
+ more_extra varchar(255)
11
+ );
12
+ select AddGeometryColumn('point_models', 'geom', 4326, 'POINT', 2);
13
+ create index index_point_models_on_geom on point_models using gist (geom);
14
+ create index index_point_models_on_extra on point_models (extra, more_extra);
15
+
16
+ drop table if exists line_string_models;
17
+ create table line_string_models
18
+ (
19
+ id serial primary key,
20
+ extra varchar(255)
21
+ );
22
+ select AddGeometryColumn('line_string_models', 'geom', 4326, 'LINESTRING', 2);
23
+
24
+ drop table if exists polygon_models;
25
+ create table polygon_models
26
+ (
27
+ id serial primary key,
28
+ extra varchar(255)
29
+ );
30
+ select AddGeometryColumn('polygon_models', 'geom', 4326, 'POLYGON', 2);
31
+
32
+ drop table if exists multi_point_models;
33
+ create table multi_point_models
34
+ (
35
+ id serial primary key,
36
+ extra varchar(255)
37
+ );
38
+ select AddGeometryColumn('multi_point_models', 'geom', 4326, 'MULTIPOINT', 2);
39
+
40
+ drop table if exists multi_line_string_models;
41
+ create table multi_line_string_models
42
+ (
43
+ id serial primary key,
44
+ extra varchar(255)
45
+ );
46
+ select AddGeometryColumn('multi_line_string_models', 'geom', 4326, 'MULTILINESTRING', 2);
47
+
48
+ drop table if exists multi_polygon_models;
49
+ create table multi_polygon_models
50
+ (
51
+ id serial primary key,
52
+ extra varchar(255)
53
+ );
54
+ select AddGeometryColumn('multi_polygon_models', 'geom', 4326, 'MULTIPOLYGON', 2);
55
+
56
+ drop table if exists geometry_collection_models;
57
+ create table geometry_collection_models
58
+ (
59
+ id serial primary key,
60
+ extra varchar(255)
61
+ );
62
+ select AddGeometryColumn('geometry_collection_models', 'geom', 4326, 'GEOMETRYCOLLECTION', 2);
63
+
64
+ drop table if exists geometry_models;
65
+ create table geometry_models
66
+ (
67
+ id serial primary key,
68
+ extra varchar(255)
69
+ );
70
+ select AddGeometryColumn('geometry_models', 'geom', 4326, 'GEOMETRY', 2);
71
+
72
+ drop table if exists pointz_models;
73
+ create table pointz_models
74
+ (
75
+ id serial primary key,
76
+ extra varchar(255)
77
+ );
78
+ select AddGeometryColumn('pointz_models', 'geom', 4326, 'POINT', 3);
79
+
80
+ drop table if exists pointm_models;
81
+ create table pointm_models
82
+ (
83
+ id serial primary key,
84
+ extra varchar(255)
85
+ );
86
+ select AddGeometryColumn('pointm_models', 'geom', 4326, 'POINTM', 3);
87
+
88
+ drop table if exists point4_models;
89
+ create table point4_models
90
+ (
91
+ id serial primary key,
92
+ extra varchar(255)
93
+ );
94
+ select AddGeometryColumn('point4_models', 'geom', 4326, 'POINT', 4);
95
+ SQL
96
+
97
+ if ActiveRecord::Base.connection.supports_geographic?
98
+ execute <<-SQL
99
+ drop table if exists geography_point_models;
100
+ create table geography_point_models
101
+ (
102
+ id serial primary key,
103
+ extra varchar(255),
104
+ geom geography(POINT)
105
+ );
106
+ create index index_geography_point_models_on_geom on geography_point_models using gist (geom);
107
+ create index index_geography_point_models_on_extra on geography_point_models (extra);
108
+
109
+ drop table if exists geography_line_string_models;
110
+ create table geography_line_string_models
111
+ (
112
+ id serial primary key,
113
+ extra varchar(255),
114
+ geom geography(LINESTRING)
115
+ );
116
+
117
+ drop table if exists geography_polygon_models;
118
+ create table geography_polygon_models
119
+ (
120
+ id serial primary key,
121
+ extra varchar(255),
122
+ geom geography(POLYGON)
123
+ );
124
+
125
+ drop table if exists geography_multi_point_models;
126
+ create table geography_multi_point_models
127
+ (
128
+ id serial primary key,
129
+ extra varchar(255),
130
+ geom geography(MULTIPOINT)
131
+ );
132
+
133
+ drop table if exists geography_multi_line_string_models;
134
+ create table geography_multi_line_string_models
135
+ (
136
+ id serial primary key,
137
+ extra varchar(255),
138
+ geom geography(MULTILINESTRING)
139
+ );
140
+
141
+ drop table if exists geography_multi_polygon_models;
142
+ create table geography_multi_polygon_models
143
+ (
144
+ id serial primary key,
145
+ extra varchar(255),
146
+ geom geography(MULTIPOLYGON)
147
+ );
148
+
149
+ drop table if exists geography_geometry_collection_models;
150
+ create table geography_geometry_collection_models
151
+ (
152
+ id serial primary key,
153
+ extra varchar(255),
154
+ geom geography(GEOMETRYCOLLECTION)
155
+ );
156
+
157
+ drop table if exists geography_models;
158
+ create table geography_models
159
+ (
160
+ id serial primary key,
161
+ extra varchar(255),
162
+ geom geography
163
+ );
164
+
165
+ drop table if exists geography_pointz_models;
166
+ create table geography_pointz_models
167
+ (
168
+ id serial primary key,
169
+ extra varchar(255),
170
+ geom geography(POINTZ)
171
+ );
172
+
173
+ drop table if exists geography_pointm_models;
174
+ create table geography_pointm_models
175
+ (
176
+ id serial primary key,
177
+ extra varchar(255),
178
+ geom geography(POINTM)
179
+ );
180
+
181
+ drop table if exists geography_point4_models;
182
+ create table geography_point4_models
183
+ (
184
+ id serial primary key,
185
+ extra varchar(255),
186
+ geom geography(POINTZM)
187
+ );
188
+ SQL
189
+ end
190
+ end
@@ -0,0 +1,65 @@
1
+ class PointModel < ActiveRecord::Base
2
+ end
3
+
4
+ class LineStringModel < ActiveRecord::Base
5
+ end
6
+
7
+ class PolygonModel < ActiveRecord::Base
8
+ end
9
+
10
+ class MultiPointModel < ActiveRecord::Base
11
+ end
12
+
13
+ class MultiLineStringModel < ActiveRecord::Base
14
+ end
15
+
16
+ class MultiPolygonModel < ActiveRecord::Base
17
+ end
18
+
19
+ class GeometryCollectionModel < ActiveRecord::Base
20
+ end
21
+
22
+ class GeometryModel < ActiveRecord::Base
23
+ end
24
+
25
+ class PointzModel < ActiveRecord::Base
26
+ end
27
+
28
+ class PointmModel < ActiveRecord::Base
29
+ end
30
+
31
+ class Point4Model < ActiveRecord::Base
32
+ end
33
+
34
+ class GeographyPointModel < ActiveRecord::Base
35
+ end
36
+
37
+ class GeographyLineStringModel < ActiveRecord::Base
38
+ end
39
+
40
+ class GeographyPolygonModel < ActiveRecord::Base
41
+ end
42
+
43
+ class GeographyMultiPointModel < ActiveRecord::Base
44
+ end
45
+
46
+ class GeographyMultiLineStringModel < ActiveRecord::Base
47
+ end
48
+
49
+ class GeographyMultiPolygonModel < ActiveRecord::Base
50
+ end
51
+
52
+ class GeographyGeometryCollectionModel < ActiveRecord::Base
53
+ end
54
+
55
+ class GeographyModel < ActiveRecord::Base
56
+ end
57
+
58
+ class GeographyPointzModel < ActiveRecord::Base
59
+ end
60
+
61
+ class GeographyPointmModel < ActiveRecord::Base
62
+ end
63
+
64
+ class GeographyPoint4Model < ActiveRecord::Base
65
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+ require 'spatial_adapter/mysql'
3
+ require 'db/mysql_raw'
4
+ require 'models/common'
5
+
6
+ describe "Modified MysqlAdapter" do
7
+ before :each do
8
+ mysql_connection
9
+ @connection = ActiveRecord::Base.connection
10
+ end
11
+
12
+ describe '#supports_geographic?' do
13
+ it "should be false" do
14
+ @connection.supports_geographic?.should == false
15
+ end
16
+ end
17
+
18
+ describe "#columns" do
19
+ describe "type" do
20
+ it "should be SpatialMysqlColumn if column is a spatial data type" do
21
+ PointModel.columns.select{|c| c.name == 'geom'}.first.should be_a(ActiveRecord::ConnectionAdapters::SpatialMysqlColumn)
22
+ end
23
+
24
+ it "should be SpatialMysqlColumn if column is not a spatial data type" do
25
+ PointModel.columns.select{|c| c.name == 'extra'}.first.should be_a(ActiveRecord::ConnectionAdapters::MysqlColumn)
26
+ end
27
+ end
28
+
29
+ describe "@geometry_type" do
30
+ it "should be :point for columns restricted to POINT types" do
31
+ PointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :point
32
+ end
33
+
34
+ it "should be :line_string for columns restricted to LINESTRING types" do
35
+ LineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :line_string
36
+ end
37
+
38
+ it "should be :polygon for columns restricted to POLYGON types" do
39
+ PolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :polygon
40
+ end
41
+
42
+ it "should be :multi_point for columns restricted to MULTIPOINT types" do
43
+ MultiPointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_point
44
+ end
45
+
46
+ it "should be :multi_line_string for columns restricted to MULTILINESTRING types" do
47
+ MultiLineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_line_string
48
+ end
49
+
50
+ it "should be :multi_polygon for columns restricted to MULTIPOLYGON types" do
51
+ MultiPolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_polygon
52
+ end
53
+
54
+ it "should be :geometry_collection for columns restricted to GEOMETRYCOLLECTION types" do
55
+ GeometryCollectionModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry_collection
56
+ end
57
+
58
+ it "should be :geometry for columns not restricted to a type" do
59
+ GeometryModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#indexes" do
65
+ before :each do
66
+ @indexes = @connection.indexes('point_models')
67
+ end
68
+
69
+ it "should return an IndexDefinition for each index on the table" do
70
+ @indexes.should have(2).items
71
+ @indexes.each do |i|
72
+ i.should be_a(ActiveRecord::ConnectionAdapters::IndexDefinition)
73
+ end
74
+ end
75
+
76
+ it "should indicate the correct columns in the index" do
77
+ @indexes.select{|i| i.name == 'index_point_models_on_geom'}.first.columns.should == ['geom']
78
+ @indexes.select{|i| i.name == 'index_point_models_on_extra'}.first.columns.should == ['extra', 'more_extra']
79
+ end
80
+
81
+ it "should be marked as spatial if a spatial index" do
82
+ @indexes.select{|i| i.columns.include?('geom')}.first.spatial.should == true
83
+ end
84
+
85
+ it "should not be marked as spatial if not a spatial index" do
86
+ @indexes.select{|i| i.columns.include?('extra')}.first.spatial.should == false
87
+ end
88
+ end
89
+
90
+ describe "#add_index" do
91
+ after :each do
92
+ @connection.should_receive(:execute).with(any_args())
93
+ @connection.remove_index('geometry_models', 'geom')
94
+ end
95
+
96
+ it "should create a spatial index given :spatial => true" do
97
+ @connection.should_receive(:execute).with(/create spatial index/i)
98
+ @connection.add_index('geometry_models', 'geom', :spatial => true)
99
+ end
100
+
101
+ it "should not create a spatial index unless specified" do
102
+ @connection.should_not_receive(:execute).with(/create spatial index/i)
103
+ @connection.add_index('geometry_models', 'extra')
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'spatial_adapter/mysql'
3
+
4
+ class MigratedGeometryModel < ActiveRecord::Base
5
+ end
6
+
7
+ describe "Spatially-enabled Migrations" do
8
+ before :each do
9
+ mysql_connection
10
+ @connection = ActiveRecord::Base.connection
11
+ end
12
+
13
+ describe "creating tables" do
14
+ after :each do
15
+ @connection.drop_table "migrated_geometry_models"
16
+ end
17
+
18
+ SpatialAdapter.geometry_data_types.keys.each do |type|
19
+ it "should create #{type.to_s} columns" do
20
+ ActiveRecord::Schema.define do
21
+ create_table :migrated_geometry_models, :force => true do |t|
22
+ t.integer :extra
23
+ t.send(type, :geom)
24
+ end
25
+ end
26
+
27
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
28
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
29
+ geom_column.geometry_type.should == type
30
+ geom_column.type.should == :string
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "adding columns" do
36
+ before :each do
37
+ ActiveRecord::Schema.define do
38
+ create_table :migrated_geometry_models, :force => true do |t|
39
+ t.integer :extra
40
+ end
41
+ end
42
+ end
43
+
44
+ after :each do
45
+ @connection.drop_table "migrated_geometry_models"
46
+ end
47
+
48
+ SpatialAdapter.geometry_data_types.keys.each do |type|
49
+ it "should add #{type.to_s} columns" do
50
+ ActiveRecord::Schema.define do
51
+ add_column :migrated_geometry_models, :geom, type
52
+ end
53
+
54
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
55
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
56
+ geom_column.geometry_type.should == type
57
+ geom_column.type.should == :string
58
+ geom_column.with_z.should == false
59
+ geom_column.with_m.should == false
60
+ geom_column.srid.should == -1
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples'
3
+ require 'spatial_adapter/mysql'
4
+ require 'db/mysql_raw'
5
+ require 'models/common'
6
+
7
+ describe "Spatially-enabled Models" do
8
+ before :each do
9
+ mysql_connection
10
+ @connection = ActiveRecord::Base.connection
11
+ GeometryFactory.default_srid = @connection.default_srid
12
+ end
13
+
14
+ describe "inserting records" do
15
+ it 'should save Point objects' do
16
+ model = PointModel.new(:extra => 'test', :geom => GeometryFactory.point)
17
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.point.as_hex_wkb), anything(), anything(), anything(), anything())
18
+ model.save.should == true
19
+ end
20
+
21
+ it 'should save LineString objects' do
22
+ model = LineStringModel.new(:extra => 'test', :geom => GeometryFactory.line_string)
23
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.line_string.as_hex_wkb), anything(), anything(), anything(), anything())
24
+ model.save.should == true
25
+ end
26
+
27
+ it 'should save Polygon objects' do
28
+ model = PolygonModel.new(:extra => 'test', :geom => GeometryFactory.polygon)
29
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.polygon.as_hex_wkb), anything(), anything(), anything(), anything())
30
+ model.save.should == true
31
+ end
32
+
33
+ it 'should save MultiPoint objects' do
34
+ model = MultiPointModel.new(:extra => 'test', :geom => GeometryFactory.multi_point)
35
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_point.as_hex_wkb), anything(), anything(), anything(), anything())
36
+ model.save.should == true
37
+ end
38
+
39
+ it 'should save MultiLineString objects' do
40
+ model = MultiLineStringModel.new(:extra => 'test', :geom => GeometryFactory.multi_line_string)
41
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_line_string.as_hex_wkb), anything(), anything(), anything(), anything())
42
+ model.save.should == true
43
+ end
44
+
45
+ it 'should save MultiPolygon objects' do
46
+ model = MultiPolygonModel.new(:extra => 'test', :geom => GeometryFactory.multi_polygon)
47
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_polygon.as_hex_wkb), anything(), anything(), anything(), anything())
48
+ model.save.should == true
49
+ end
50
+
51
+ it 'should save GeometryCollection objects' do
52
+ model = GeometryCollectionModel.new(:extra => 'test', :geom => GeometryFactory.geometry_collection)
53
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.geometry_collection.as_hex_wkb), anything(), anything(), anything(), anything())
54
+ model.save.should == true
55
+ end
56
+
57
+ it 'should save Geometry objects' do
58
+ model = GeometryModel.new(:extra => 'test', :geom => GeometryFactory.point)
59
+ @connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.point.as_hex_wkb), anything(), anything(), anything(), anything())
60
+ model.save.should == true
61
+ end
62
+ end
63
+
64
+ include CommonModelActions
65
+ end
66
+