Skip to content

Commit

Permalink
cucumber: More picky diffing of db-contents against table (so that un…
Browse files Browse the repository at this point in the history
…expected columns will flunk the test).

AR timestamp and id columns are stripped (or not) depending on the step used.

[github #2]
  • Loading branch information
nruth authored and ianwhite committed Aug 11, 2010
1 parent dffcb2b commit 0fe3c79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion features/dump_a_database.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Dump a database
| Jane | Heidie |

Scenario: test step works
Then the "users" table should consist of:
Then the "users" table should match exactly (ignoring ids and timestamps):
| name | surname |
| Fred | Bloggs |
| Ethel | Smith |
Expand Down
17 changes: 16 additions & 1 deletion features/step_definitions/database_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
table.diff!(table_contents(table_name))
end

Then /^the "([^"]*)" table should match exactly:$/ do |table_name, table|
table.diff!(table_contents(table_name), :surplus_col => true)
end

Then /^the "([^"]*)" table should match exactly \(ignoring ids and timestamps\):$/ do |table_name, table|
table.diff!(table_contents(table_name, :ids => false, :timestamps => false), :surplus_col => true)
end

module DatabaseHelpers
def create_table(name)
ActiveRecord::Base.connection.create_table name do
Expand Down Expand Up @@ -67,8 +75,15 @@ def insert_record_into_table(table_name, attrs)
class_for_table(table_name).create! attrs
end

def table_contents(table_name)
# table_contents 'users' # gives back everything
# table_contents 'users', :timestamps => false # without timestamps
# table_contents 'users', :ids => false # without ids
def table_contents(table_name, opts={:timestamps => true, :ids => true})
contents = class_for_table(table_name).all.map(&:attributes)
contents.tap do |contents|
contents.map{|c| c.delete('id')} unless opts[:ids]
contents.map{|c| c.delete('updated_at'); c.delete('created_at')} unless opts[:timestamps]
end
end
end

Expand Down

0 comments on commit 0fe3c79

Please sign in to comment.