Skip to content

Commit

Permalink
Remove specs for Migrator methods that should be internal
Browse files Browse the repository at this point in the history
Migrator exposed a lot of public methods that really should be
private.  Remove the specs for them and try to add specs that
use the public methods to check for the previously speced
behavior.
  • Loading branch information
jeremyevans committed May 11, 2010
1 parent b60e01e commit 95b37d0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 96 deletions.
120 changes: 28 additions & 92 deletions spec/extensions/migration_spec.rb
Expand Up @@ -103,117 +103,53 @@ def table_exists?(name)

after do
Object.send(:remove_const, "CreateSessions") if Object.const_defined?("CreateSessions")
Object.send(:remove_const, "CreateNodes") if Object.const_defined?("CreateNodes")
Object.send(:remove_const, "CreateUsers") if Object.const_defined?("CreateUsers")
Object.send(:remove_const, "CreateAltBasic") if Object.const_defined?("CreateAltBasic")
Object.send(:remove_const, "CreateAltAdvanced") if Object.const_defined?("CreateAltAdvanced")
end

specify "#migration_files should return the list of files for a specified version range" do
Sequel::Migrator.migration_files(@dirname, 1..1).map{|f| File.basename(f)}.should == ['001_create_sessions.rb']
Sequel::Migrator.migration_files(@dirname, 1..3).map{|f| File.basename(f)}.should == ['001_create_sessions.rb', '002_create_nodes.rb', '003_create_users.rb']
Sequel::Migrator.migration_files(@dirname, 3..6).map{|f| File.basename(f)}.should == ['003_create_users.rb']
Sequel::Migrator.migration_files(@dirname, 7..8).map{|f| File.basename(f)}.should == []
Sequel::Migrator.migration_files(@alt_dirname, 1..1).map{|f| File.basename(f)}.should == ['001_create_alt_basic.rb']
Sequel::Migrator.migration_files(@alt_dirname, 1..3).map{|f| File.basename(f)}.should == ['001_create_alt_basic.rb','003_3_create_alt_advanced.rb']
end

specify "#latest_migration_version should return the latest version available" do
Sequel::Migrator.latest_migration_version(@dirname).should == 3
Sequel::Migrator.latest_migration_version(@alt_dirname).should == 3
end

specify "#migration_classes should load the migration classes for the specified range for the up direction" do
Sequel::Migrator.migration_classes(@dirname, 3, 0, :up).should == [CreateSessions, CreateNodes, CreateUsers]
Sequel::Migrator.migration_classes(@alt_dirname, 3, 0, :up).should == [CreateAltBasic, CreateAltAdvanced]
end

specify "#migration_classes should load the migration classes for the specified range for the down direction" do
Sequel::Migrator.migration_classes(@dirname, 0, 5, :down).should == [CreateUsers, CreateNodes, CreateSessions]
Sequel::Migrator.migration_classes(@alt_dirname, 0, 3, :down).should == [CreateAltAdvanced, CreateAltBasic]
end

specify "#migration_classes should start from current + 1 for the up direction" do
Sequel::Migrator.migration_classes(@dirname, 3, 1, :up).should == [CreateNodes, CreateUsers]
Sequel::Migrator.migration_classes(@alt_dirname, 3, 2, :up).should == [CreateAltAdvanced]
end

specify "#migration_classes should end on current + 1 for the down direction" do
Sequel::Migrator.migration_classes(@dirname, 2, 5, :down).should == [CreateUsers]
Sequel::Migrator.migration_classes(@alt_dirname, 2, 4, :down).should == [CreateAltAdvanced]
end

specify "#schema_info_dataset should automatically create the schema_info table" do
specify "should automatically create the schema_info table with the version column" do
@db.table_exists?(:schema_info).should be_false
Sequel::Migrator.schema_info_dataset(@db)
Sequel::Migrator.run(@db, @dirname, :target=>0)
@db.table_exists?(:schema_info).should be_true
@db.sqls.should == ["CREATE TABLE schema_info (version integer)"]
@db.dataset.columns.should == [:version]
end

specify "should automatically create new APP_version column in schema_info" do
@db.table_exists?(:schema_info).should be_false
Sequel::Migrator.schema_info_dataset(@db, :column => :alt_version)
@db.table_exists?(:schema_info).should be_true
@db.sqls.should == ["CREATE TABLE schema_info (alt_version integer)"]
specify "should allow specifying the table and columns" do
@db.table_exists?(:si).should be_false
Sequel::Migrator.run(@db, @dirname, :target=>0, :table=>:si, :column=>:sic)
@db.table_exists?(:si).should be_true
@db.dataset.columns.should == [:sic]
end

specify "should automatically create new APP_version column in schema_info" do
@db.table_exists?(:alt_table).should be_false
Sequel::Migrator.schema_info_dataset(@db, :table => :alt_table)
@db.table_exists?(:alt_table).should be_true
Sequel::Migrator.schema_info_dataset(@db, :table => :alt_table, :column=>:alt_version)
@db.sqls.should == ["CREATE TABLE alt_table (version integer)",
"ALTER TABLE alt_table ADD COLUMN alt_version integer"]
end

specify "should return a dataset for the correct table" do
Sequel::Migrator.schema_info_dataset(@db).first_source_alias.should == :schema_info
Sequel::Migrator.schema_info_dataset(@db, :table=>:blah).first_source_alias.should == :blah
end

specify "should use the migration version stored in the database" do
Sequel::Migrator.schema_info_dataset(@db).insert(:version => 4321)
@db.version.should == 4321
end

specify "should set the migration version stored in the database" do
Sequel::Migrator.set_current_migration_version(@db, 6666)
@db.version.should == 6666
specify "should apply migrations correctly in the up direction if no target is given" do
Sequel::Migrator.apply(@db, @dirname)
@db.creates.should == [1111, 2222, 3333]
@db.version.should == 3
end

specify "should apply migrations correctly in the up direction with target" do
Sequel::Migrator.apply(@db, @dirname, 2)
@db.creates.should == [1111, 2222]
@db.version.should == 2
end

specify "should apply migrations correctly in the up direction" do
specify "should apply migrations correctly in the up direction with target and existing" do
Sequel::Migrator.apply(@db, @dirname, 2, 1)
@db.creates.should == [2222]

@db.version.should == 2

Sequel::Migrator.apply(@db, @dirname, 3)
@db.creates.should == [2222, 3333]

@db.version.should == 3
end

specify "should apply migrations correctly in the down direction" do
Sequel::Migrator.apply(@db, @dirname, 1, 3)
@db.drops.should == [3333, 2222]

@db.version.should == 1
end

specify "should apply migrations up to the latest version if no target is given" do
Sequel::Migrator.apply(@db, @dirname)
@db.creates.should == [1111, 2222, 3333]

@db.version.should == 3
end

specify "should apply migrations down to 0 version correctly" do
Sequel::Migrator.apply(@db, @dirname, 0, 3)
specify "should apply migrations correctly in the down direction with target" do
@db[:schema_info].insert(:version=>3)
Sequel::Migrator.apply(@db, @dirname, 0)
@db.drops.should == [3333, 2222, 1111]

@db.version.should == 0
end

specify "should apply migrations correctly in the down direction with target and existing" do
Sequel::Migrator.apply(@db, @dirname, 1, 2)
@db.drops.should == [2222]
@db.version.should == 1
end

specify "should return the target version" do
Sequel::Migrator.apply(@db, @dirname, 3, 2).should == 3
Sequel::Migrator.apply(@db, @dirname, 0).should == 0
Expand Down
2 changes: 1 addition & 1 deletion spec/files/alt_integer_migrations/001_create_alt_basic.rb
@@ -1,4 +1,4 @@
CreateAltBasic = Sequel.migration do
Sequel.migration do
up{create_table(:sm11111){Integer :smc1}}
down{drop_table(:sm11111)}
end
@@ -1,4 +1,4 @@
CreateAltAdvanced = Sequel.migration do
Sequel.migration do
up{create_table(:sm33333){Integer :smc3}}
down{drop_table(:sm33333)}
end
2 changes: 1 addition & 1 deletion spec/files/integer_migrations/002_create_nodes.rb
@@ -1,4 +1,4 @@
CreateNodes = Class.new(Sequel::Migration) do
Class.new(Sequel::Migration) do
def up
create_table(:sm2222){Integer :smc2}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/files/integer_migrations/003_create_users.rb
@@ -1,4 +1,4 @@
CreateUsers = Sequel.migration do
Sequel.migration do
up{create_table(:sm3333){Integer :smc3}}
down{drop_table(:sm3333)}
end

0 comments on commit 95b37d0

Please sign in to comment.