migration_test_helper 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Micah Alles, Patrick Bacon, David Crosby
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,108 @@
1
+ = MigrationTestHelper
2
+
3
+ MigrationTestHelper provides methods which let you assert the current state of the schema and run your migrations against the _test_ database.
4
+
5
+ == Install
6
+
7
+ ./script/plugin install svn://rubyforge.org/var/svn/migrationtest/tags/migration_test_helper
8
+
9
+ OR
10
+
11
+ gem install migration_test_helper
12
+
13
+
14
+ If you're using it outside of a Rails environment (for whatever reason) include the MigrationTestHelper module in your tests:
15
+
16
+ require 'test/unit'
17
+ require 'migration_test_helper'
18
+
19
+ class MyTest < Test::Unit::TestCase
20
+ include MigrationTestHelper
21
+
22
+ def test_something
23
+ ...
24
+ end
25
+ end
26
+
27
+ == Use
28
+
29
+ *assert_schema*: verifies the schema of the database exactly matches the one specified.
30
+
31
+ def test_the_schema
32
+ assert_schema do |s|
33
+ s.table :books do |t|
34
+ t.column :id, :integer
35
+ t.column :title, :string
36
+ t.column :author, :string
37
+ end
38
+
39
+ s.table :reviews do |t|
40
+ t.column :id, :integer
41
+ t.column :book_id, :integer
42
+ t.column :body, :text
43
+ t.column :rating, :integer
44
+ end
45
+ end
46
+ end
47
+
48
+ This would verify there are only two tables defined in the test database: _books_ and _reviews_ (schema_info is ignored). It will also verify that the _book_ table has the three columns, _id_, _title_ and _author_, each with their respective types.
49
+
50
+
51
+ *drop_all_tables*: does just what it says to your _test_ database.
52
+
53
+ *migrate*: executes the migrations against the test database using the same mechanism as rake db:migrate.
54
+
55
+ def test_the_migrations
56
+ migrate
57
+ migrate :version => 0
58
+ migrate :version => 10
59
+ migrate
60
+ end
61
+
62
+
63
+ This would do the same thing as running the following rake commands, but within a test case:
64
+
65
+ rake db:migrate
66
+ rake db:migrate VERSION=0
67
+ rake db:migrate VERSION=10
68
+ rake db:migrate
69
+
70
+
71
+ By combining the two helpers you can write a test that shows you can run all your migrations and get the final schema:
72
+
73
+ def test_should_be_able_to_migrate_from_an_empty_schema
74
+ drop_all_tables
75
+
76
+ # we shouldn't have any tables
77
+ assert_schema do |s|
78
+ end
79
+
80
+ migrate
81
+
82
+ assert_schema do |s|
83
+ s.table :books do |t|
84
+ t.column :id, :integer
85
+ t.column :title, :string
86
+ t.column :author, :string
87
+ end
88
+
89
+ s.table :reviews do |t|
90
+ t.column :id, :integer
91
+ t.column :book_id, :integer
92
+ t.column :body, :text
93
+ t.column :rating, :integer
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+ The *migrate* helper can also be useful for testing data tranformation migrations:
100
+
101
+ def test_should_get_rid_of_bad_data
102
+ drop_all_tables
103
+ migrate :version => 7
104
+ book = Book.create! :title => "bad title\nwith\todd spacing"
105
+ migrate :version => 8 # should cleanse spacing in book titles
106
+ book.reload
107
+ assert_equal "bad title with odd spacing", book.title
108
+ end
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'hoe'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Generate documentation for the migration_test_helper plugin.'
10
+ Rake::RDocTask.new(:rdoc) do |rdoc|
11
+ rdoc.rdoc_dir = 'rdoc'
12
+ rdoc.title = 'MigrationTestHelper'
13
+ rdoc.options << '--line-numbers' << '--inline-source'
14
+ rdoc.rdoc_files.include('README')
15
+ rdoc.rdoc_files.include('LICENSE')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ GEM_VERSION = '0.1.0'
20
+
21
+ Hoe.new('migration_test_helper',GEM_VERSION) do |p|
22
+ p.author = "Micah Alles"
23
+ p.email = "micah@atomicobject.com"
24
+ p.url = "http://migrationtest.rubyforge.org"
25
+ p.summary = "A Rails plugin for testing migrations"
26
+ p.description = <<-EOS
27
+ migration_test_helper makes testing your migrations easier by
28
+ adding helper methods to Test::Unit::TestCase for asserting the
29
+ current state of the schema and executing migrations against the
30
+ test database.
31
+ EOS
32
+
33
+ p.gem_files = FileList["{test,lib,tasks}/**/*"] + %w|LICENSE README Rakefile init.rb install.rb uninstall.rb|
34
+
35
+ p.changes = <<-EOS
36
+ EOS
37
+ p.rubyforge_name = 'migrationtest'
38
+ end
39
+
40
+ desc "Release from current trunk"
41
+ task :plugin_release do
42
+ require 'fileutils'
43
+ include FileUtils::Verbose
44
+ cd File.expand_path(File.dirname(__FILE__)) do
45
+ sh 'svn up'
46
+ status = `svn status`
47
+ raise "Please clean up before releasing.\n#{status}" unless status == ""
48
+
49
+ if `svn ls svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/rel-#{GEM_VERSION} -m` =~ /migration_test_helper/
50
+ sh "svn del svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/rel-#{GEM_VERSION} -m 'Preparing to update stable release tag'"
51
+ end
52
+ sh "svn cp . svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/rel-#{GEM_VERSION} -m 'Releasing version #{GEM_VERSION}'"
53
+ if `svn ls svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/migration_test_helper` =~ /migration_test_helper/
54
+ sh "svn del svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/migration_test_helper -m 'Preparing to update stable release tag'"
55
+ end
56
+ sh "svn cp . svn+ssh://alles@rubyforge.org/var/svn/migrationtest/tags/migration_test_helper -m 'Updating stable tag to version #{GEM_VERSION}'"
57
+ end
58
+ end
data/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ if RAILS_ENV == 'test'
2
+ require 'migration_test_helper'
3
+ require 'test/unit'
4
+ Test::Unit::TestCase.class_eval do
5
+ include MigrationTestHelper
6
+ end
7
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,127 @@
1
+ #--
2
+ # Copyright (c) 2007 Micah Alles, Patrick Bacon, David Crosby
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'test/unit/assertions'
25
+
26
+ module MigrationTestHelper
27
+ def self.migration_dir
28
+ @migration_dir || File.expand_path(RAILS_ROOT + '/db/migrate')
29
+ end
30
+
31
+ #
32
+ # sets the directory in which the migrations reside to be run with migrate
33
+ #
34
+ def self.migration_dir=(new_dir)
35
+ @migration_dir = new_dir
36
+ end
37
+
38
+ #
39
+ # verifies the schema exactly matches the one specified (schema_info does not have to be specified)
40
+ #
41
+ # assert_schema do |s|
42
+ # s.table :dogs do |t|
43
+ # t.column :id, :integer
44
+ # t.column :name, :string
45
+ # end
46
+ # end
47
+ #
48
+ def assert_schema
49
+ schema = Schema.new
50
+ yield schema
51
+ schema.verify
52
+ end
53
+
54
+ #
55
+ # drops all tables in the database
56
+ #
57
+ def drop_all_tables
58
+ ActiveRecord::Base.connection.tables.each do |table|
59
+ ActiveRecord::Base.connection.drop_table(table)
60
+ end
61
+ end
62
+
63
+ #
64
+ # executes your migrations
65
+ #
66
+ # migrate # same as rake db:migrate
67
+ #
68
+ # migrate :version => 10 # same as rake db:migrate VERSION=10
69
+ #
70
+ def migrate(opts={})
71
+ version = opts[:version] ? opts[:version].to_i : nil
72
+ ActiveRecord::Migrator.migrate(MigrationTestHelper.migration_dir, version)
73
+ end
74
+
75
+ module Connection #:nodoc:
76
+ def conn
77
+ ActiveRecord::Base.connection
78
+ end
79
+ end
80
+
81
+ class Schema
82
+ include Connection
83
+ include Test::Unit::Assertions
84
+
85
+ def initialize #:nodoc:
86
+ @tables = []
87
+ end
88
+
89
+ def table(name)
90
+ table = Table.new(name)
91
+ yield table
92
+ table.verify
93
+ @tables << table
94
+ end
95
+
96
+ def verify #:nodoc:
97
+ actual_tables = conn.tables.reject {|t| t == 'schema_info' }
98
+ expected_tables = @tables.map {|t| t.name }
99
+ assert_equal expected_tables.sort, actual_tables.sort, 'wrong tables in schema'
100
+ end
101
+ end
102
+
103
+ class Table
104
+ include Connection
105
+ include Test::Unit::Assertions
106
+ attr_reader :name
107
+
108
+ def initialize(name) #:nodoc:
109
+ @name = name.to_s
110
+ @columns = []
111
+ assert conn.tables.include?(@name), "table <#{@name}> not found in schema"
112
+ end
113
+
114
+ def column(colname,type)
115
+ colname = colname.to_s
116
+ @columns << colname
117
+ col = conn.columns(name).find {|c| c.name == colname }
118
+ assert_not_nil col, "column <#{colname}> not found in table <#{self.name}>"
119
+ assert_equal type, col.type, "wrong type for column <#{colname}> in table <#{name}>"
120
+ end
121
+
122
+ def verify #:nodoc:
123
+ actual_columns = conn.columns(name).map {|c| c.name }
124
+ assert_equal @columns.sort, actual_columns.sort, "wrong columns for table: <#{name}>"
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :migration_test_helper do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: vendor/plugins/migration_test_helper/test/db/test.sqlite3.db
@@ -0,0 +1,11 @@
1
+ class CreateTopDogs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :top_dogs do |t|
4
+ t.column :name, :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :top_dogs
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreateBottomDogs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :bottom_dogs do |t|
4
+ t.column :name, :string
5
+ t.column :sick, :boolean
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :bottom_dogs
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateCats < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :cats do |t|
4
+ t.column :lives, :integer
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :cats
10
+ end
11
+ end
data/test/db/schema.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table "dogs", :force => true do |t|
3
+ t.column "tail", :string
4
+ end
5
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,29 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
3
+ require 'logger'
4
+ require 'test_help'
5
+
6
+ plugin_path = RAILS_ROOT + "/vendor/plugins/migration_test_helper"
7
+
8
+ config_location = File.expand_path(plugin_path + "/test/config/database.yml")
9
+
10
+ config = YAML::load(ERB.new(IO.read(config_location)).result)
11
+ ActiveRecord::Base.logger = Logger.new(plugin_path + "/test/log/test.log")
12
+ ActiveRecord::Base.establish_connection(config['test'])
13
+
14
+ Test::Unit::TestCase.fixture_path = plugin_path + "/test/fixtures/"
15
+
16
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
17
+
18
+ class Test::Unit::TestCase
19
+ def plugin_path(path)
20
+ File.expand_path(File.dirname(__FILE__) + '/../' + path)
21
+ end
22
+
23
+ def load_default_schema
24
+ ActiveRecord::Migration.suppress_messages do
25
+ schema_file = plugin_path("/test/db/schema.rb")
26
+ load(schema_file) if File.exist?(schema_file)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,164 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class MigrationTestHelperTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ load_default_schema
7
+ MigrationTestHelper.migration_dir = plugin_path('test/db/migrate_good')
8
+ end
9
+
10
+ #
11
+ # HELPERS
12
+ #
13
+ def see_failure(pattern='')
14
+ err = assert_raise(Test::Unit::AssertionFailedError) do
15
+ yield
16
+ end
17
+ assert_match(/#{pattern}/mi, err.message)
18
+ end
19
+
20
+ def see_no_failure
21
+ assert_nothing_raised do
22
+ yield
23
+ end
24
+ end
25
+
26
+ #
27
+ # TESTS
28
+ #
29
+ def test_assert_schema_should_not_fail_if_schema_is_matched
30
+ see_no_failure do
31
+ assert_schema do |s|
32
+ s.table :dogs do |t|
33
+ t.column :id, :integer
34
+ t.column :tail, :string
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def test_should_fail_if_a_table_is_not_specified
41
+ see_failure 'wrong tables in schema.*dogs' do
42
+ assert_schema do |s|
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_should_fail_if_a_table_is_not_found
48
+ see_failure 'table <things> not found in schema' do
49
+ assert_schema do |s|
50
+ s.table :things do |t|
51
+ t.column :id, :integer
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def test_should_fail_if_a_column_is_not_specified
58
+ see_failure 'wrong columns for table.*dogs.*tail' do
59
+ assert_schema do |s|
60
+ s.table :dogs do |t|
61
+ t.column :id, :integer
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ def test_should_fail_if_a_column_is_not_found
68
+ see_failure 'column <legs> not found in table <dogs>' do
69
+ assert_schema do |s|
70
+ s.table :dogs do |t|
71
+ t.column :id, :integer
72
+ t.column :tail, :string
73
+ t.column :legs, :integer
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def test_should_drop_all_tables
80
+ assert_equal ['dogs','schema_info'].sort, ActiveRecord::Base.connection.tables.sort
81
+ drop_all_tables
82
+ assert_equal [], ActiveRecord::Base.connection.tables
83
+ drop_all_tables
84
+ assert_equal [], ActiveRecord::Base.connection.tables
85
+ end
86
+
87
+ def test_should_migrate_to_highest_version
88
+ drop_all_tables
89
+ assert_schema do |s|
90
+ end
91
+
92
+ migrate :version => 1
93
+
94
+ assert_schema do |s|
95
+ s.table :top_dogs do |t|
96
+ t.column :id, :integer
97
+ t.column :name, :string
98
+ end
99
+ end
100
+
101
+ migrate :version => 2
102
+
103
+ assert_schema do |s|
104
+ s.table :top_dogs do |t|
105
+ t.column :id, :integer
106
+ t.column :name, :string
107
+ end
108
+ s.table :bottom_dogs do |t|
109
+ t.column :id, :integer
110
+ t.column :name, :string
111
+ t.column :sick, :boolean
112
+ end
113
+ end
114
+
115
+ migrate :version => 3
116
+
117
+ assert_schema do |s|
118
+ s.table :top_dogs do |t|
119
+ t.column :id, :integer
120
+ t.column :name, :string
121
+ end
122
+ s.table :bottom_dogs do |t|
123
+ t.column :id, :integer
124
+ t.column :name, :string
125
+ t.column :sick, :boolean
126
+ end
127
+ s.table :cats do |t|
128
+ t.column :id, :integer
129
+ t.column :lives, :integer
130
+ end
131
+ end
132
+
133
+ migrate :version => 0
134
+
135
+ assert_schema do |s|
136
+ end
137
+
138
+ migrate
139
+
140
+ assert_schema do |s|
141
+ s.table :top_dogs do |t|
142
+ t.column :id, :integer
143
+ t.column :name, :string
144
+ end
145
+ s.table :bottom_dogs do |t|
146
+ t.column :id, :integer
147
+ t.column :name, :string
148
+ t.column :sick, :boolean
149
+ end
150
+ s.table :cats do |t|
151
+ t.column :id, :integer
152
+ t.column :lives, :integer
153
+ end
154
+ end
155
+ end
156
+
157
+ def test_should_have_default_migration_dir_set
158
+ MigrationTestHelper.migration_dir = nil
159
+ assert_equal File.expand_path(RAILS_ROOT + '/db/migrate'), MigrationTestHelper.migration_dir,
160
+ "wrong default migration dir"
161
+
162
+ end
163
+ end
164
+
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: migration_test_helper
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-01-30 00:00:00 -05:00
8
+ summary: A Rails plugin for testing migrations
9
+ require_paths:
10
+ - lib
11
+ email: micah@atomicobject.com
12
+ homepage: http://migrationtest.rubyforge.org
13
+ rubyforge_project: migrationtest
14
+ description: migration_test_helper makes testing your migrations easier by adding helper methods to Test::Unit::TestCase for asserting the current state of the schema and executing migrations against the test database.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Micah Alles
30
+ files:
31
+ - test/config
32
+ - test/db
33
+ - test/helper.rb
34
+ - test/log
35
+ - test/migration_test_helper_test.rb
36
+ - test/config/database.yml
37
+ - test/db/migrate_good
38
+ - test/db/schema.rb
39
+ - test/db/migrate_good/001_create_top_dogs.rb
40
+ - test/db/migrate_good/002_create_bottom_dogs.rb
41
+ - test/db/migrate_good/003_create_cats.rb
42
+ - lib/migration_test_helper.rb
43
+ - tasks/migration_test_helper_tasks.rake
44
+ - LICENSE
45
+ - README
46
+ - Rakefile
47
+ - init.rb
48
+ - install.rb
49
+ - uninstall.rb
50
+ test_files: []
51
+
52
+ rdoc_options: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ requirements: []
61
+
62
+ dependencies:
63
+ - !ruby/object:Gem::Dependency
64
+ name: hoe
65
+ version_requirement:
66
+ version_requirements: !ruby/object:Gem::Version::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.1.7
71
+ version: