Methods
- teardown
- test_add_column_not_null_with_default
- test_add_column_not_null_without_default
- test_add_index
- test_change_column_with_new_default
- test_create_table_with_not_null_column
- test_rename_table
Public Instance methods
[ show source ]
# File test/ar_base_tests_runner.rb, line 136
136: def teardown
137: ActiveRecord::Base.connection.initialize_schema_information
138: ActiveRecord::Base.connection.get_table(:schema_info).update_all {|rec| rec.version = 0}
139:
140: Reminder.connection.drop_table("reminders") rescue nil
141: Reminder.connection.drop_table("people_reminders") rescue nil
142: Reminder.connection.drop_table("prefix_reminders_suffix") rescue nil
143: Reminder.reset_column_information
144:
145: Person.connection.remove_column("people", "last_name") rescue nil
146: Person.connection.remove_column("people", "bio") rescue nil
147: Person.connection.remove_column("people", "age") rescue nil
148: Person.connection.remove_column("people", "height") rescue nil
149: Person.connection.remove_column("people", "birthday") rescue nil
150: Person.connection.remove_column("people", "favorite_day") rescue nil
151: Person.connection.remove_column("people", "male") rescue nil
152: Person.connection.remove_column("people", "administrator") rescue nil
153: Person.reset_column_information
154: end
[ show source ]
# File test/ar_base_tests_runner.rb, line 170
170: def test_add_column_not_null_with_default
171: Person.connection.create_table :testings do |t|
172: t.column :foo, :string
173: end
174: Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default"
175:
176: # changed from ActiveRecord::StatementInvalid as we're operating directly on
177: # the database, and that is what KB spits out. Still, it validates that the
178: # field is now required (not null)
179: assert_raises(ArgumentError) do
180: Person.connection.get_table(:testings).insert :foo => 'hello', :bar => nil
181: end
182: ensure
183: Person.connection.drop_table :testings rescue nil
184: end
[ show source ]
# File test/ar_base_tests_runner.rb, line 186
186: def test_add_column_not_null_without_default
187: Person.connection.create_table :testings do |t|
188: t.column :foo, :string
189: end
190: Person.connection.add_column :testings, :bar, :string, :null => false
191:
192: assert_raises(ArgumentError) do
193: Person.connection.get_table(:testings).insert :foo => 'hello', :bar => nil
194: end
195: ensure
196: Person.connection.drop_table :testings rescue nil
197: end
KirbyBase only supports one index per column, so created new column (middle_name) for those tests. Also, KirbyBase does not support named indexes, so those tests were disabled.
[ show source ]
# File test/ar_base_tests_runner.rb, line 202
202: def test_add_index
203: Person.connection.add_column "people", "last_name", :string
204: Person.connection.add_column "people", "middle_name", :string
205: Person.connection.add_column "people", "administrator", :boolean
206:
207: assert_nothing_raised { Person.connection.add_index("people", "last_name") }
208: assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
209:
210: assert_nothing_raised { Person.connection.add_index("people", ["middle_name", "first_name"]) }
211: assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
212:
213: # assert_nothing_raised { Person.connection.add_index("people", %w(last_name middle_name administrator), :name => "named_admin") }
214: # assert_nothing_raised { Person.connection.remove_index("people", :name => "named_admin") }
215: end
Since this test uses aboolean field with a default, we need to override the change_column statement to use FalseClass rather than 0. In real life migrations, this should be guarded with an if current_adapter …
[ show source ]
# File test/ar_base_tests_runner.rb, line 240
240: def test_change_column_with_new_default
241: Person.connection.add_column "people", "administrator", :boolean, :default => true
242: Person.reset_column_information
243: assert Person.new.administrator?
244:
245: assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
246: Person.reset_column_information
247: assert !Person.new.administrator?
248: end
[ show source ]
# File test/ar_base_tests_runner.rb, line 156
156: def test_create_table_with_not_null_column
157: Person.connection.create_table :testings do |t|
158: t.column :foo, :string, :null => false
159: end
160:
161: # ArgumentError and not ActiveRecord::StatementInvalid because we're inserting directly to the db.
162: # Still showns that this field is required
163: assert_raises(ArgumentError) do
164: Person.connection.get_table(:testings).insert :foo => nil
165: end
166: ensure
167: Person.connection.drop_table :testings rescue nil
168: end
[ show source ]
# File test/ar_base_tests_runner.rb, line 217
217: def test_rename_table
218: begin
219: ActiveRecord::Base.connection.create_table :octopuses do |t|
220: t.column :url, :string
221: end
222: ActiveRecord::Base.connection.rename_table :octopuses, :octopi
223:
224: assert_nothing_raised do
225: ActiveRecord::Base.connection.get_table(:octopi).insert :url => 'http://www.foreverflying.com/octopus-black7.jpg'
226: end
227:
228: assert_equal 'http://www.foreverflying.com/octopus-black7.jpg',
229: ActiveRecord::Base.connection.get_table(:octopi).select{|r|r.recno == 1}.first.url
230:
231: ensure
232: ActiveRecord::Base.connection.drop_table :octopuses rescue nil
233: ActiveRecord::Base.connection.drop_table :octopi rescue nil
234: end
235: end