Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add cucumber test for migrations

  • Loading branch information...
commit 76cbfb80239d6ef9434eb18758b899b3eb5d77bc 1 parent 90ea9da
@sikachu sikachu authored
View
70 features/migration.feature
@@ -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"
View
28 features/step_definitions/attachment_steps.rb
@@ -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
View
6 features/step_definitions/rails_steps.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.