activerecord-postgis-adapter 2.0.2 → 2.1.0

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