Permalink
Browse files

added :scale option to have_db_column

  • Loading branch information...
1 parent 4ff07bc commit 76ff61e4d5c1c7aaf794b21326a3668387d5afab @croaky croaky committed with jferris Jan 24, 2009
@@ -416,23 +416,23 @@ def should_have_instance_methods(*methods)
#
# should_have_db_columns :id, :email, :name, :created_at
#
- # should_have_db_column :email, :type => "string",
- # :limit => 255,
- # :sql_type => 'varchar(255)'
- # should_have_db_column :money, :decimal, :precision => 15, :scale => 2
- # should_have_db_column :admin, :default => false, :null => false
+ # should_have_db_column :email, :type => "string",
+ # :limit => 255,
+ # :sql_type => 'varchar(255)'
+ # should_have_db_column :salary, :decimal, :precision => 15, :scale => 2
+ # should_have_db_column :admin, :default => false, :null => false
#
def should_have_db_columns(*columns)
column_type, precision, limit, default, null, scale, sql_type =
get_options!(columns, :type, :precision, :limit,
:default, :null, :scale, :sql_type)
- klass = model_class
+ klass = model_class
columns.each do |name|
matcher = have_db_column(name).
of_type(column_type).
- with_options(:precision => precision, :limit => limit,
- :default => default, :null => null,
- :scale => scale, :sql_type => sql_type)
+ with_options(:precision => precision, :limit => limit,
+ :default => default, :null => null,
+ :scale => scale, :sql_type => sql_type)
should matcher.description do
assert_accepts(matcher, klass.new)
end
@@ -18,6 +18,7 @@ def with_options(opts = {})
@limit = opts[:limit]
@default = opts[:default]
@null = opts[:null]
+ @scale = opts[:scale]
self
end
@@ -28,7 +29,8 @@ def matches?(subject)
correct_precision? &&
correct_limit? &&
correct_default? &&
- correct_null?
+ correct_null? &&
+ correct_scale?
end
def failure_message
@@ -47,6 +49,7 @@ def description
desc << " of default #{@default}" unless @default.nil?
desc << " of null #{@null}" unless @null.nil?
desc << " of primary #{@primary}" unless @primary.nil?
+ desc << " of scale #{@scale}" unless @scale.nil?
desc
end
@@ -120,6 +123,17 @@ def correct_null?
end
end
+ def correct_scale?
+ return true if @scale.nil?
+ if matched_column.scale.to_s == @scale.to_s
+ true
+ else
+ @missing = "#{model_class} has a db column named #{@column} " <<
+ "of scale #{matched_column.scale}, not #{@scale}."
+ false
+ end
+ end
+
def matched_column
model_class.columns.detect { |each| each.name == @column.to_s }
end
@@ -21,7 +21,7 @@ class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
end
end
- context "have_db_column with column_type option" do
+ context "have_db_column of type string" do
setup do
@matcher = have_db_column(:nickname).of_type(:string)
end
@@ -50,20 +50,20 @@ class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
context "have_db_column with precision option" do
setup do
- @matcher = have_db_column(:money).with_options(:precision => 15)
+ @matcher = have_db_column(:salary).with_options(:precision => 5)
end
should "accept a column of correct precision" do
create_table 'superheros' do |table|
- table.decimal :money, :precision => 15
+ table.decimal :salary, :precision => 5
end
define_model_class 'Superhero'
assert_accepts @matcher, Superhero.new
end
should "reject a column of wrong precision" do
create_table 'superheros' do |table|
- table.decimal :money, :precision => 30
+ table.decimal :salary, :precision => 15
end
define_model_class 'Superhero'
assert_rejects @matcher, Superhero.new
@@ -142,6 +142,30 @@ class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
end
end
- # :scale, and :sql_type
+ context "have_db_column with scale option" do
+ setup do
+ @matcher = have_db_column(:salary).
+ of_type(:decimal).
+ with_options(:scale => 2)
+ end
+
+ should "accept a column of correct scale" do
+ create_table 'superheros' do |table|
+ table.decimal :salary, :precision => 10, :scale => 2
+ end
+ define_model_class 'Superhero'
+ assert_accepts @matcher, Superhero.new
+ end
+
+ should "reject a column of wrong scale" do
+ create_table 'superheros' do |table|
+ table.decimal :salary, :precision => 10, :scale => 4
+ end
+ define_model_class 'Superhero'
+ assert_rejects @matcher, Superhero.new
+ end
+ end
+
+ # :sql_type
end
View
@@ -44,8 +44,8 @@ class UserTest < Test::Unit::TestCase
should_have_instance_methods :email, :age, :email=, :valid?
should_have_db_columns :name, :email, :age
should_have_db_column :id, :type => "integer"
- should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
- :null => true, :scale => nil, :sql_type => 'varchar(255)'
+ should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
+ :null => true, :scale => nil, :sql_type => 'varchar(255)'
should_require_acceptance_of :eula
should_require_unique_attributes :email, :scoped_to => :name, :case_sensitive => false

0 comments on commit 76ff61e

Please sign in to comment.