Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add cucumber test for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
sikachu committed Jun 15, 2012
1 parent 90ea9da commit 76cbfb8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
70 changes: 70 additions & 0 deletions features/migration.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Feature: Migration

Background:
Given I generate a new rails application
And I write to "app/models/user.rb" with:
"""
class User < ActiveRecord::Base; end
"""

Scenario: Vintage syntax
When I write to "db/migrate/01_add_attachment_to_users.rb" with:
"""
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.has_attached_file :avatar
end
end
def self.down
drop_attached_file :users, :avatar
end
end
"""
And I run a migration
Then I should have attachment columns for "avatar"

When I rollback a migration
Then I should not have attachment columns for "avatar"

Scenario: New syntax with create_table
When I write to "db/migrate/01_add_attachment_to_users.rb" with:
"""
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.attachment :avatar
end
end
end
"""
And I run a migration
Then I should have attachment columns for "avatar"

Scenario: New syntax outside of create_table
When I write to "db/migrate/01_create_users.rb" with:
"""
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users
end
end
"""
And I write to "db/migrate/02_add_attachment_to_users.rb" with:
"""
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up
add_attachment :users, :avatar
end
def self.down
remove_attachment :users, :avatar
end
end
"""
And I run a migration
Then I should have attachment columns for "avatar"

When I rollback a migration
Then I should not have attachment columns for "avatar"
28 changes: 28 additions & 0 deletions features/step_definitions/attachment_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,31 @@ def attachment_path(filename)
check_file_presence([attachment_path(filename)], !not_exist)
end
end

Then /^I should have attachment columns for "([^"]*)"$/ do |attachment_name|
in_current_dir do
columns = eval(`bundle exec #{runner_command} "puts User.columns.map{ |column| [column.name, column.type] }.inspect"`.strip)
expect_columns = [
["#{attachment_name}_file_name", :string],
["#{attachment_name}_content_type", :string],
["#{attachment_name}_file_size", :integer],
["#{attachment_name}_updated_at", :datetime]
]

expect_columns.all?{ |column| columns.include? column }.should be_true
end
end

Then /^I should not have attachment columns for "([^"]*)"$/ do |attachment_name|
in_current_dir do
columns = eval(`bundle exec #{runner_command} "puts User.columns.map{ |column| [column.name, column.type] }.inspect"`.strip)
expect_columns = [
["#{attachment_name}_file_name", :string],
["#{attachment_name}_content_type", :string],
["#{attachment_name}_file_size", :integer],
["#{attachment_name}_updated_at", :datetime]
]

expect_columns.none?{ |column| columns.include? column }.should be_true
end
end
6 changes: 5 additions & 1 deletion features/step_definitions/rails_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
end

Given /^I run a migration$/ do
step %[I successfully run `bundle exec rake db:migrate`]
step %[I successfully run `bundle exec rake db:migrate --trace`]
end

When /^I rollback a migration$/ do
step %[I successfully run `bundle exec rake db:rollback STEPS=1 --trace`]
end

Given /^I update my new user view to include the file upload field$/ do
Expand Down

0 comments on commit 76cbfb8

Please sign in to comment.