activerecord-postgis-adapter 2.2.2 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,21 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class NestedClassTest < 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
4
  module Foo
10
5
  def self.table_name_prefix
11
6
  'foo_'
12
7
  end
13
- class Bar < ::ActiveRecord::Base
8
+ class Bar < ActiveRecord::Base
9
+ establish_connection YAML.load_file(ActiveSupport::TestCase::DATABASE_CONFIG_PATH)
14
10
  end
15
11
  end
16
12
 
17
- define_test_methods do
18
-
19
- def test_nested_model
20
- Foo::Bar.class_eval do
21
- establish_connection(DATABASE_CONFIG)
22
- end
23
- Foo::Bar.connection.create_table(:foo_bars) do |t|
24
- t.column 'latlon', :point, :srid => 3785
25
- end
26
- Foo::Bar.all
27
- Foo::Bar.connection.drop_table(:foo_bars)
13
+ def test_nested_model
14
+ Foo::Bar.connection.create_table(:foo_bars, force: true) do |t|
15
+ t.column 'latlon', :st_point, srid: 3785
28
16
  end
29
-
17
+ assert_empty Foo::Bar.all
18
+ assert_equal 1, Foo::Bar.connection.drop_table(:foo_bars).result_status
30
19
  end
31
20
 
32
21
  end
@@ -1,97 +1,79 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
4
+ def test_query_point
5
+ create_model
6
+ obj = SpatialModel.new
7
+ obj.latlon = factory.point(1.0, 2.0)
8
+ obj.save!
9
+ id = obj.id
10
+ obj2 = SpatialModel.where(latlon: factory.multi_point([factory.point(1.0, 2.0)])).first
11
+ refute_nil(obj2)
12
+ assert_equal(id, obj2.id)
13
+ obj3 = SpatialModel.where(latlon: factory.point(2.0, 2.0)).first
14
+ assert_nil(obj3)
15
+ end
4
16
 
5
- DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
6
- OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
7
-
8
- include RGeo::ActiveRecord::AdapterTestHelper
9
-
10
- define_test_methods do
11
-
12
- def populate_ar_class(content)
13
- klass = create_ar_class
14
- case content
15
- when :mercator_point
16
- klass.connection.create_table(:spatial_test) do |t|
17
- t.column 'latlon', :point, :srid => 3785
18
- end
19
- when :latlon_point_geographic
20
- klass.connection.create_table(:spatial_test) do |t|
21
- t.column 'latlon', :point, :srid => 4326, :geographic => true
22
- end
23
- when :path_linestring
24
- klass.connection.create_table(:spatial_test) do |t|
25
- t.column 'path', :line_string, :srid => 3785
26
- end
27
- end
28
- klass
29
- end
17
+ def test_query_point_wkt
18
+ create_model
19
+ obj = SpatialModel.new
20
+ obj.latlon = factory.point(1.0, 2.0)
21
+ obj.save!
22
+ id = obj.id
23
+ obj2 = SpatialModel.where(latlon: 'SRID=3785;POINT(1 2)').first
24
+ refute_nil(obj2)
25
+ assert_equal(id, obj2.id)
26
+ obj3 = SpatialModel.where(latlon: 'SRID=3785;POINT(2 2)').first
27
+ assert_nil(obj3)
28
+ end
30
29
 
31
- def test_query_point
32
- klass = populate_ar_class(:mercator_point)
33
- obj = klass.new
34
- obj.latlon = @factory.point(1.0, 2.0)
35
- obj.save!
36
- id = obj.id
37
- obj2 = klass.where(:latlon => @factory.multi_point([@factory.point(1.0, 2.0)])).first
38
- refute_nil(obj2)
39
- assert_equal(id, obj2.id)
40
- obj3 = klass.where(:latlon => @factory.point(2.0, 2.0)).first
41
- assert_nil(obj3)
42
- end
30
+ def test_query_st_distance
31
+ create_model
32
+ obj = SpatialModel.new
33
+ obj.latlon = factory.point(1.0, 2.0)
34
+ obj.save!
35
+ id = obj.id
36
+ obj2 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').lt(2)).first
37
+ refute_nil(obj2)
38
+ assert_equal(id, obj2.id)
39
+ obj3 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').gt(2)).first
40
+ assert_nil(obj3)
41
+ end
43
42
 
44
- def test_query_point_wkt
45
- klass = populate_ar_class(:mercator_point)
46
- obj = klass.new
47
- obj.latlon = @factory.point(1.0, 2.0)
48
- obj.save!
49
- id = obj.id
50
- obj2 = klass.where(:latlon => 'SRID=3785;POINT(1 2)').first
51
- refute_nil(obj2)
52
- assert_equal(id, obj2.id)
53
- obj3 = klass.where(:latlon => 'SRID=3785;POINT(2 2)').first
54
- assert_nil(obj3)
55
- end
43
+ def test_query_st_distance_from_constant
44
+ create_model
45
+ obj = SpatialModel.new
46
+ obj.latlon = factory.point(1.0, 2.0)
47
+ obj.save!
48
+ id = obj.id
49
+ obj2 = SpatialModel.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(SpatialModel.arel_table[:latlon]).lt(2)).first
50
+ refute_nil(obj2)
51
+ assert_equal(id, obj2.id)
52
+ obj3 = SpatialModel.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(SpatialModel.arel_table[:latlon]).gt(2)).first
53
+ assert_nil(obj3)
54
+ end
56
55
 
57
- def test_query_st_distance
58
- klass = populate_ar_class(:mercator_point)
59
- obj = klass.new
60
- obj.latlon = @factory.point(1.0, 2.0)
61
- obj.save!
62
- id = obj.id
63
- obj2 = klass.where(klass.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').lt(2)).first
64
- refute_nil(obj2)
65
- assert_equal(id, obj2.id)
66
- obj3 = klass.where(klass.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').gt(2)).first
67
- assert_nil(obj3)
68
- end
56
+ def test_query_st_length
57
+ create_model
58
+ obj = SpatialModel.new
59
+ obj.path = factory.line(factory.point(1.0, 2.0), factory.point(3.0, 2.0))
60
+ obj.save!
61
+ id = obj.id
62
+ obj2 = SpatialModel.where(SpatialModel.arel_table[:path].st_length.eq(2)).first
63
+ refute_nil(obj2)
64
+ assert_equal(id, obj2.id)
65
+ obj3 = SpatialModel.where(SpatialModel.arel_table[:path].st_length.gt(3)).first
66
+ assert_nil(obj3)
67
+ end
69
68
 
70
- def test_query_st_distance_from_constant
71
- klass = populate_ar_class(:mercator_point)
72
- obj = klass.new
73
- obj.latlon = @factory.point(1.0, 2.0)
74
- obj.save!
75
- id = obj.id
76
- obj2 = klass.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(klass.arel_table[:latlon]).lt(2)).first
77
- refute_nil(obj2)
78
- assert_equal(id, obj2.id)
79
- obj3 = klass.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(klass.arel_table[:latlon]).gt(2)).first
80
- assert_nil(obj3)
81
- end
69
+ private
82
70
 
83
- def test_query_st_length
84
- klass = populate_ar_class(:path_linestring)
85
- obj = klass.new
86
- obj.path = @factory.line(@factory.point(1.0, 2.0), @factory.point(3.0, 2.0))
87
- obj.save!
88
- id = obj.id
89
- obj2 = klass.where(klass.arel_table[:path].st_length.eq(2)).first
90
- refute_nil(obj2)
91
- assert_equal(id, obj2.id)
92
- obj3 = klass.where(klass.arel_table[:path].st_length.gt(3)).first
93
- assert_nil(obj3)
71
+ def create_model
72
+ SpatialModel.connection.create_table(:spatial_models, force: true) do |t|
73
+ t.column 'latlon', :st_point, srid: 3785
74
+ t.column 'path', :line_string, srid: 3785
94
75
  end
95
-
76
+ SpatialModel.reset_column_information
96
77
  end
78
+
97
79
  end
data/test/tasks_test.rb CHANGED
@@ -2,157 +2,156 @@ require 'test_helper'
2
2
  require 'active_record/schema_dumper'
3
3
 
4
4
  class TasksTest < ActiveSupport::TestCase # :nodoc:
5
- DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
6
- OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
7
-
8
- class << self
9
- def before_open_database(args)
10
- @new_database_config = args[:config].merge('database' => 'postgis_adapter_test2')
11
- @new_database_config.stringify_keys!
12
- end
13
- attr_reader :new_database_config
5
+ NEW_CONNECTION = {
6
+ "adapter" => "postgis",
7
+ "host" => "127.0.0.1",
8
+ "database" => "postgis_tasks_test",
9
+ "username" => "postgres",
10
+ "setup" => "default",
11
+ "schema_search_path" => "public",
12
+ }
13
+
14
+ def test_create_database_from_extension_in_public_schema
15
+ drop_db_if_exists
16
+ ActiveRecord::Tasks::DatabaseTasks.create(NEW_CONNECTION)
17
+ refute_empty connection.select_values("SELECT * from public.spatial_ref_sys")
14
18
  end
15
19
 
16
- include RGeo::ActiveRecord::AdapterTestHelper
20
+ def test_create_database_from_extension_in_separate_schema
21
+ drop_db_if_exists
22
+ configuration = NEW_CONNECTION.merge("postgis_schema" => "postgis")
23
+ ActiveRecord::Tasks::DatabaseTasks.create(configuration)
24
+ refute_empty connection.select_values("SELECT * from postgis.spatial_ref_sys")
25
+ end
17
26
 
18
- def cleanup_tables
19
- ::ActiveRecord::Base.remove_connection
20
- ::ActiveRecord::Base.clear_active_connections!
21
- TasksTest::DEFAULT_AR_CLASS.connection.execute("DROP DATABASE IF EXISTS \"postgis_adapter_test2\"")
27
+ def test_empty_sql_dump
28
+ setup_database_tasks
29
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
30
+ sql = File.read(tmp_sql_filename)
31
+ assert(sql !~ /CREATE TABLE/)
22
32
  end
23
33
 
24
- define_test_methods do
25
- def test_create_database_from_extension_in_public_schema
26
- ::ActiveRecord::Tasks::DatabaseTasks.create(TasksTest.new_database_config)
27
- refute_empty connection.select_values("SELECT * from public.spatial_ref_sys")
34
+ def test_basic_geography_sql_dump
35
+ setup_database_tasks
36
+ connection.create_table(:spatial_test, force: true) do |t|
37
+ t.st_point "latlon", geographic: true
28
38
  end
39
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
40
+ data = File.read(tmp_sql_filename)
41
+ assert(data.index('latlon geography(Point,4326)'))
42
+ end
29
43
 
30
- def test_create_database_from_extension_in_separate_schema
31
- configuration = TasksTest.new_database_config.merge('postgis_schema' => 'postgis')
32
- ::ActiveRecord::Tasks::DatabaseTasks.create(configuration)
33
- refute_empty connection.select_values("SELECT * from postgis.spatial_ref_sys")
44
+ def test_index_sql_dump
45
+ setup_database_tasks
46
+ connection.create_table(:spatial_test, force: true) do |t|
47
+ t.st_point "latlon", geographic: true
48
+ t.string "name"
34
49
  end
50
+ connection.add_index :spatial_test, :latlon, using: :gist
51
+ connection.add_index :spatial_test, :name, using: :btree
52
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
53
+ data = File.read(tmp_sql_filename)
54
+ assert(data.index('latlon geography(Point,4326)'))
55
+ assert data.index('CREATE INDEX index_spatial_test_on_latlon ON spatial_test USING gist (latlon);')
56
+ assert data.index('CREATE INDEX index_spatial_test_on_name ON spatial_test USING btree (name);')
57
+ end
35
58
 
36
- def test_empty_sql_dump
37
- setup_database_tasks
38
- ::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
39
- sql = ::File.read(tmp_sql_filename)
40
- assert(sql !~ /CREATE TABLE/)
59
+ def test_empty_schema_dump
60
+ setup_database_tasks
61
+ File.open(tmp_sql_filename, "w:utf-8") do |file|
62
+ ActiveRecord::SchemaDumper.dump(::ActiveRecord::Base.connection, file)
41
63
  end
64
+ data = File.read(tmp_sql_filename)
65
+ assert(data.index('ActiveRecord::Schema'))
66
+ end
42
67
 
43
- def test_basic_geography_sql_dump
44
- setup_database_tasks
45
- connection.create_table(:spatial_test) do |t|
46
- t.point "latlon", geographic: true
47
- end
48
- ::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
49
- data = ::File.read(tmp_sql_filename)
50
- assert(data.index('latlon geography(Point,4326)'))
68
+ def test_basic_geometry_schema_dump
69
+ setup_database_tasks
70
+ connection.create_table(:spatial_test, force: true) do |t|
71
+ t.geometry 'object1'
72
+ t.spatial "object2", srid: connection.default_srid, type: "geometry"
51
73
  end
52
-
53
- def test_index_sql_dump
54
- setup_database_tasks
55
- connection.create_table(:spatial_test) do |t|
56
- t.point "latlon", geographic: true
57
- t.string "name"
58
- end
59
- connection.add_index :spatial_test, :latlon, spatial: true
60
- connection.add_index :spatial_test, :name, using: :btree
61
- ::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
62
- data = ::File.read(tmp_sql_filename)
63
- assert(data.index('latlon geography(Point,4326)'))
64
- assert data.index('CREATE INDEX index_spatial_test_on_latlon ON spatial_test USING gist (latlon);')
65
- assert data.index('CREATE INDEX index_spatial_test_on_name ON spatial_test USING btree (name);')
74
+ File.open(tmp_sql_filename, "w:utf-8") do |file|
75
+ ActiveRecord::SchemaDumper.dump(connection, file)
66
76
  end
77
+ data = File.read(tmp_sql_filename)
78
+ assert data.index("t.geometry \"object1\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"")
79
+ assert data.index("t.geometry \"object2\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"")
80
+ end
67
81
 
68
- def test_empty_schema_dump
69
- setup_database_tasks
70
- ::File.open(tmp_sql_filename, "w:utf-8") do |file|
71
- ::ActiveRecord::SchemaDumper.dump(::ActiveRecord::Base.connection, file)
72
- end
73
- data = ::File.read(tmp_sql_filename)
74
- assert(data.index('ActiveRecord::Schema'))
82
+ def test_basic_geography_schema_dump
83
+ setup_database_tasks
84
+ connection.create_table(:spatial_test, force: true) do |t|
85
+ t.st_point "latlon1", geographic: true
86
+ t.spatial "latlon2", srid: 4326, type: "st_point", geographic: true
75
87
  end
76
-
77
- def test_basic_geometry_schema_dump
78
- setup_database_tasks
79
- connection.create_table(:spatial_test) do |t|
80
- t.geometry 'object1'
81
- t.spatial "object2", limit: { srid: connection.default_srid, type: "geometry" }
82
- end
83
- ::File.open(tmp_sql_filename, "w:utf-8") do |file|
84
- ::ActiveRecord::SchemaDumper.dump(connection, file)
85
- end
86
- data = ::File.read(tmp_sql_filename)
87
- assert(data.index("t.spatial \"object1\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"}"))
88
- assert(data.index("t.spatial \"object2\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"}"))
88
+ File.open(tmp_sql_filename, "w:utf-8") do |file|
89
+ ActiveRecord::SchemaDumper.dump(connection, file)
89
90
  end
91
+ data = File.read(tmp_sql_filename)
92
+ assert data.index(%(t.geography "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
93
+ assert data.index(%(t.geography "latlon2", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
94
+ end
90
95
 
91
- def test_basic_geography_schema_dump
92
- setup_database_tasks
93
- connection.create_table(:spatial_test) do |t|
94
- t.point "latlon1", geographic: true
95
- t.spatial "latlon2", limit: { srid: 4326, type: "point", geographic: true }
96
- end
97
- ::File.open(tmp_sql_filename, "w:utf-8") do |file|
98
- ::ActiveRecord::SchemaDumper.dump(connection, file)
99
- end
100
- data = ::File.read(tmp_sql_filename)
101
- assert(data.index('t.spatial "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
102
- assert(data.index('t.spatial "latlon2", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
96
+ def test_index_schema_dump
97
+ setup_database_tasks
98
+ connection.create_table(:spatial_test, force: true) do |t|
99
+ t.st_point "latlon", geographic: true
103
100
  end
104
-
105
- def test_index_schema_dump
106
- setup_database_tasks
107
- connection.create_table(:spatial_test) do |t|
108
- t.point "latlon", geographic: true
109
- end
110
- connection.add_index :spatial_test, :latlon, spatial: true
111
- ::File.open(tmp_sql_filename, "w:utf-8") do |file|
112
- ::ActiveRecord::SchemaDumper.dump(connection, file)
113
- end
114
- data = ::File.read(tmp_sql_filename)
115
- assert data.index('t.spatial "latlon", limit: {:srid=>4326, :type=>"point", :geographic=>true}')
116
- assert data.index('add_index "spatial_test", ["latlon"], :name => "index_spatial_test_on_latlon", :spatial => true')
101
+ connection.add_index :spatial_test, :latlon, using: :gist
102
+ File.open(tmp_sql_filename, "w:utf-8") do |file|
103
+ ActiveRecord::SchemaDumper.dump(connection, file)
117
104
  end
105
+ data = File.read(tmp_sql_filename)
106
+ assert data.index(%(t.geography "latlon", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
107
+ assert data.index(%(add_index "spatial_test", ["latlon"], name: "index_spatial_test_on_latlon", using: :gist))
108
+ end
118
109
 
119
- def test_add_index_with_nil_options
120
- setup_database_tasks
121
- connection.create_table(:test) do |t|
122
- t.string "name"
123
- end
124
- connection.add_index :test, :name, nil
125
- ::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
126
- data = ::File.read(tmp_sql_filename)
127
- assert data.index('CREATE INDEX index_test_on_name ON test USING btree (name);')
110
+ def test_add_index_with_no_options
111
+ setup_database_tasks
112
+ connection.create_table(:test, force: true) do |t|
113
+ t.string "name"
128
114
  end
115
+ connection.add_index :test, :name
116
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
117
+ data = File.read(tmp_sql_filename)
118
+ assert data.index('CREATE INDEX index_test_on_name ON test USING btree (name);')
119
+ end
129
120
 
130
- def test_add_index_via_references
131
- setup_database_tasks
132
- connection.create_table(:cats)
133
- connection.create_table(:dogs) do |t|
134
- t.references :cats, index: true
135
- end
136
- ::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
137
- data = ::File.read(tmp_sql_filename)
138
- assert data.index('CREATE INDEX index_dogs_on_cats_id ON dogs USING btree (cats_id);')
121
+ def test_add_index_via_references
122
+ setup_database_tasks
123
+ connection.create_table(:cats, force: true)
124
+ connection.create_table(:dogs, force: true) do |t|
125
+ t.references :cats, index: true
139
126
  end
127
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
128
+ data = File.read(tmp_sql_filename)
129
+ assert data.index('CREATE INDEX index_dogs_on_cats_id ON dogs USING btree (cats_id);')
140
130
  end
141
131
 
142
132
  private
143
133
 
144
134
  def connection
145
- ::ActiveRecord::Base.connection
135
+ ActiveRecord::Base.connection
146
136
  end
147
137
 
148
138
  def tmp_sql_filename
149
- ::File.expand_path('../tmp/tmp.sql', ::File.dirname(__FILE__))
139
+ File.expand_path('../tmp/tmp.sql', ::File.dirname(__FILE__))
150
140
  end
151
141
 
152
142
  def setup_database_tasks
153
- ::FileUtils.rm_f(tmp_sql_filename)
154
- ::FileUtils.mkdir_p(::File.dirname(tmp_sql_filename))
155
- ::ActiveRecord::Tasks::DatabaseTasks.create(TasksTest.new_database_config)
143
+ FileUtils.rm_f(tmp_sql_filename)
144
+ FileUtils.mkdir_p(::File.dirname(tmp_sql_filename))
145
+ drop_db_if_exists
146
+ ActiveRecord::ConnectionAdapters::PostGISAdapter::PostGISDatabaseTasks.new(NEW_CONNECTION).create
147
+ rescue ActiveRecord::Tasks::DatabaseAlreadyExists
148
+ # ignore
149
+ end
150
+
151
+ def drop_db_if_exists
152
+ ActiveRecord::ConnectionAdapters::PostGISAdapter::PostGISDatabaseTasks.new(NEW_CONNECTION).drop
153
+ rescue ActiveRecord::Tasks::DatabaseAlreadyExists
154
+ # ignore
156
155
  end
157
156
 
158
157
  end