Skip to content

Commit

Permalink
uniqueness: Extract code to gen dummy values
Browse files Browse the repository at this point in the history
We need to use this in another part of the codebase: later, when we add
ignoring_interference_by_writer to all matchers, we will need a way of
overriding an attribute such that it produces another value that
invalidates the test. Some tests require us to generate dummy values.
  • Loading branch information
mcmire committed Jan 5, 2016
1 parent 80b791d commit 67d5189
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,20 +591,7 @@ def dummy_value_for(scope)
end

def dummy_scalar_value_for(column)
case column.type
when :integer
0
when :date
Date.today
when :datetime
DateTime.now
when :uuid
SecureRandom.uuid
when :boolean
true
else
'dummy value'
end
Shoulda::Matchers::Util.dummy_value_for(column.type)
end

def next_value_for(scope, previous_value)
Expand Down
23 changes: 23 additions & 0 deletions lib/shoulda/matchers/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ def self.inspect_values(values)
def self.inspect_range(range)
"#{inspect_value(range.first)} to #{inspect_value(range.last)}"
end

def self.dummy_value_for(column_type, array: false)
if array
[dummy_value_for(column_type, array: false)]
else
case column_type
when :integer
0
when :date
Date.new(2100, 1, 1)
when :datetime, :timestamp
DateTime.new(2100, 1, 1)
when :time
Time.new(2100, 1, 1)
when :uuid
SecureRandom.uuid
when :boolean
true
else
'dummy value'
end
end
end
end
end
end

0 comments on commit 67d5189

Please sign in to comment.