Skip to content

Commit 9ac8602

Browse files
committed
cleanup specs
1 parent 31010b8 commit 9ac8602

4 files changed

Lines changed: 73 additions & 71 deletions

File tree

spec/column_default_spec.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
require 'spec_helper'
22

33
describe "Column definition" do
4-
def stub_model(name, base = ActiveRecord::Base, &block)
5-
klass = Class.new(base)
6-
7-
if block_given?
8-
klass.instance_eval(&block)
9-
end
10-
11-
stub_const(name, klass)
12-
end
13-
144
before(:each) do
15-
define_schema do
5+
apply_migration do
166
create_table :models, :force => true do |t|
177
end
188
end
@@ -125,8 +115,8 @@ def stub_model(name, base = ActiveRecord::Base, &block)
125115
private
126116

127117
def define_test_column(type, **options)
128-
ActiveRecord::Migration.suppress_messages do
129-
ActiveRecord::Migration.create_table Model.table_name, :force => true do |t|
118+
apply_migration do
119+
create_table Model.table_name, :force => true do |t|
130120
t.send type, :test_column, **options
131121
t.integer :dummy
132122
end

spec/migration_spec.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,37 @@
33
describe ActiveRecord::Migration do
44

55
before(:each) do
6-
define_schema do
6+
apply_migration do
77
create_table :posts, :force => true do |t|
88
t.string :content
99
end
1010
end
11-
class Post < ::ActiveRecord::Base ; end
1211
end
1312

1413
context "when table is created" do
15-
16-
before(:each) do
17-
@model = Post
18-
end
14+
let(:model) { stub_model('Post') }
1915

2016
it "should properly handle default values for booleans" do
2117
expect {
22-
recreate_table(@model) do |t|
18+
recreate_table(model) do |t|
2319
t.boolean :bool, :default => true
2420
end
2521
}.to_not raise_error
26-
expect(@model.create.reload.bool).to be true
22+
expect(model.create.reload.bool).to be true
2723
end
2824

2925
it "should properly handle default values for json (#195)", :postgresql => :only do
30-
recreate_table(@model) do |t|
26+
recreate_table(model) do |t|
3127
t.json :json, :default => {}
3228
end
33-
expect(@model.create.reload.json).to eq({})
29+
expect(model.create.reload.json).to eq({})
3430
end
3531

3632
end
3733

3834
def recreate_table(model, opts={}, &block)
39-
ActiveRecord::Migration.suppress_messages do
40-
ActiveRecord::Migration.create_table model.table_name, **opts.merge(:force => true), &block
35+
apply_migration do
36+
create_table model.table_name, **opts.merge(:force => true), &block
4137
end
4238
model.reset_column_information
4339
end

spec/schema_dumper_spec.rb

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,92 @@
22
require 'stringio'
33

44
describe "Schema dump" do
5+
let(:model) { stub_model('Post') }
56

6-
before(:all) do
7-
ActiveRecord::Migration.suppress_messages do
8-
ActiveRecord::Schema.define do
9-
connection.tables.each do |table| drop_table table, force: :cascade end
10-
7+
context "with date default", :postgresql => :only do
8+
before do
9+
apply_migration do
1110
create_table :posts, :force => true do |t|
12-
t.text :body
13-
t.integer :user_id
14-
t.integer :first_comment_id
15-
t.string :string_no_default
16-
t.integer :short_id
17-
t.string :str_short
18-
t.integer :integer_col
19-
t.float :float_col
20-
t.decimal :decimal_col
21-
t.datetime :datetime_col
22-
t.timestamp :timestamp_col
23-
t.time :time_col
24-
t.date :date_col
25-
t.binary :binary_col
26-
t.boolean :boolean_col
2711
end
28-
2912
end
3013
end
31-
class ::Post < ActiveRecord::Base ; end
32-
end
3314

34-
context "with date default", :postgresql => :only do
3515
it "should dump the default hash expr as now()" do
36-
with_additional_column Post, :posted_at, :datetime, :default => :now do
16+
with_additional_column model, :posted_at, :datetime, :default => :now do
3717
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default\s*=>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}\s*$})
3818
end
3919
end
4020

4121
it "should dump the default hash expr as CURRENT_TIMESTAMP" do
42-
with_additional_column Post, :posted_at, :datetime, :default => {:expr => 'date \'2001-09-28\''} do
22+
with_additional_column model, :posted_at, :datetime, :default => { :expr => 'date \'2001-09-28\'' } do
4323
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default\s*=>).*2001-09-28.*})
4424
end
4525
end
26+
end
27+
28+
context 'with a complex expression', postgresql: :only do
29+
before do
30+
apply_migration do
31+
create_table :posts, :force => true do |t|
32+
end
33+
end
34+
end
4635

4736
it "can dump a complex default expression" do
48-
with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
37+
with_additional_column model, :name, :string, :default => { :expr => 'substring(random()::text from 3 for 6)' } do
4938
expect(dump_posts).to match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
5039
end
5140
end
5241
end
5342

5443
context "with date default", :sqlite3 => :only do
44+
before do
45+
apply_migration do
46+
create_table :posts, :force => true do |t|
47+
end
48+
end
49+
end
50+
5551
it "should dump the default hash expr as now" do
56-
with_additional_column Post, :posted_at, :datetime, :default => :now do
52+
with_additional_column model, :posted_at, :datetime, :default => :now do
5753
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(default:|:default\s*=>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"\(DATETIME\('now'\)\)"\s*\}})
5854
end
5955
end
6056

6157
it "should dump the default hash expr string as now" do
62-
with_additional_column Post, :posted_at, :datetime, :default => { :expr => "(DATETIME('now'))" } do
58+
with_additional_column model, :posted_at, :datetime, :default => { :expr => "(DATETIME('now'))" } do
6359
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(default:|:default\s*=>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"\(DATETIME\('now'\)\)"\s*\}})
6460
end
6561
end
6662

6763
it "should dump the default value normally" do
68-
with_additional_column Post, :posted_at, :string, :default => "now" do
64+
with_additional_column model, :posted_at, :string, :default => "now" do
6965
expect(dump_posts).to match(%r{t\.string\s*"posted_at",\s*(?:default:|:default\s*=>)\s*"now"})
7066
end
7167
end
7268
end
7369

7470
it "should leave out :default when default was changed to null" do
75-
ActiveRecord::Migration.suppress_messages do
76-
ActiveRecord::Migration.change_column_default :posts, :string_no_default, nil
71+
apply_migration do
72+
create_table :posts, :force => true do |t|
73+
t.datetime :date_column, default: { expr: :now }
74+
end
75+
76+
change_column_default :posts, :date_column, nil
7777
end
78-
# mysql2 includes 'limit: 255' in the output. that's OK, just want to
79-
# make sure the full line doesn't have 'default' in it.
80-
expect(dump_posts).to match(%r{t\.string\s+"string_no_default"\s*(,\s*limit:\s*\d+)?$})
78+
expect(dump_posts).to match(%r{t\.datetime\s+"date_column"$})
8179
end
8280

8381
protected
82+
8483
def to_regexp(string)
8584
Regexp.new(Regexp.escape(string))
8685
end
8786

8887
def with_additional_column(model, column_name, column_type, options)
89-
table_columns = model.columns.reject{|column| column.name == 'id'}
90-
ActiveRecord::Migration.suppress_messages do
91-
ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
88+
table_columns = model.columns.reject { |column| column.name == 'id' }
89+
apply_migration do
90+
create_table model.table_name, :force => true do |t|
9291
table_columns.each do |column|
9392
t.column column.name, column.type, :default => column.default
9493
end
@@ -98,8 +97,8 @@ def with_additional_column(model, column_name, column_type, options)
9897
yield
9998
end
10099

101-
def dump_schema(opts={})
102-
stream = StringIO.new
100+
def dump_schema(opts = {})
101+
stream = StringIO.new
103102
ActiveRecord::SchemaDumper.ignore_tables = Array.wrap(opts[:ignore]) || []
104103
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
105104
stream.string
@@ -110,4 +109,3 @@ def dump_posts
110109
end
111110

112111
end
113-

spec/spec_helper.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,36 @@
1515

1616
RSpec.configure do |config|
1717
config.warnings = true
18+
19+
config.around do |example|
20+
begin
21+
example.run
22+
ensure
23+
apply_migration do
24+
ActiveRecord::Base.connection.tables.each do |table|
25+
drop_table table, force: :cascade
26+
end
27+
end
28+
end
29+
end
1830
end
1931

20-
def define_schema(&block)
32+
def stub_model(name, base = ActiveRecord::Base, &block)
33+
klass = Class.new(base)
34+
35+
if block_given?
36+
klass.instance_eval(&block)
37+
end
38+
39+
stub_const(name, klass)
40+
end
41+
42+
def apply_migration(&block)
2143
ActiveRecord::Migration.suppress_messages do
2244
ActiveRecord::Schema.define do
23-
connection.tables.each do |table|
24-
drop_table table, force: :cascade
25-
end
2645
instance_eval &block
2746
end
2847
end
2948
end
3049

31-
3250
SimpleCov.command_name "[ruby#{RUBY_VERSION}-activerecord#{::ActiveRecord.version}-#{ActiveRecord::Base.connection.adapter_name}]"

0 commit comments

Comments
 (0)