Skip to content

Commit

Permalink
Added specs for numeric validator
Browse files Browse the repository at this point in the history
* Fixed bug when precision == scale
* Fix for ticket #300
  • Loading branch information
Dan Kubb committed May 26, 2008
1 parent 0ff796f commit 0765e3c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 24 deletions.
4 changes: 2 additions & 2 deletions dm-validations/lib/dm-validations/auto_validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def auto_generate_validations(property)
opts[:integer_only] = true
validates_is_number property.name, opts
elsif BigDecimal == property.type || Float == property.type
opts[:precision] = property.precision if property.precision > 0
opts[:scale] = property.scale if property.scale != 10
opts[:precision] = property.precision
opts[:scale] = property.scale
validates_is_number property.name, opts
end
end
Expand Down
8 changes: 7 additions & 1 deletion dm-validations/lib/dm-validations/numeric_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ def call(target)
error_message ||= '%s must be an integer'.t(DataMapper::Inflection.humanize(@field_name))
else
if scale && precision
return true if value =~ /\A(?:\d{1,#{scale - precision}}|\d{0,#{scale - precision}}\.\d{1,#{precision}})\z/
if scale == precision
return true if value =~ /\A(?:0\.\d{1,#{precision}})\z/
elsif precision == 0
return true if value =~ /\A(?:\d{1,#{scale}}(?:\.0)?)\z/
else
return true if value =~ /\A(?:\d{1,#{scale - precision}}|\d{0,#{scale - precision}}\.\d{1,#{precision}})\z/
end
else
return true if value =~ /\A(?:\d+|\d*\.\d+)\z/
end
Expand Down
129 changes: 108 additions & 21 deletions dm-validations/spec/integration/numeric_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,116 @@ class Bill
h.should be_valid
end

it "should validate with autovalidate" do

class RobotFish
include DataMapper::Resource
property :id, Integer, :serial => true
property :scales, Integer
property :average_weight, Float
describe 'auto validation' do
before :all do
class Fish
include DataMapper::Resource
property :id, Integer, :serial => true
property :scales, Integer
end
end

class PondFish
include DataMapper::Resource
property :id, Integer, :serial => true
property :scales, Integer
property :average_weight, Float, :scale => 10, :precision => 0, :auto_validation => false
validates_is_number :average_weight
end
describe 'Float' do
describe 'with default scale and precision' do
before :all do
class RobotFish < Fish
property :average_weight, Float
end
end

fish1 = PondFish.new
fish2 = RobotFish.new
fish1.scales = fish2.scales = 1
fish1.average_weight = fish2.average_weight = 20.22
fish1.valid?.should == true
fish2.valid?.should == true
end
before do
@robot_fish = RobotFish.new
end

it 'should allow up to 10 digits before the decimal' do
@robot_fish.average_weight = 0
@robot_fish.should be_valid

@robot_fish.average_weight = 9_999_999_999
@robot_fish.should be_valid

@robot_fish.average_weight = 10_000_000_000
@robot_fish.should_not be_valid
end

it 'should allow 0 digits of precision after the decimal' do
@robot_fish.average_weight = 0
@robot_fish.should be_valid
end

it 'should allow 1 digit of precision after the decimal if it is a zero' do
@robot_fish.average_weight = 0.0
@robot_fish.should be_valid

@robot_fish.average_weight = 9_999_999_999.0
@robot_fish.should be_valid

@robot_fish.average_weight = 0.1
@robot_fish.should_not be_valid
end
end

describe 'with a scale of 4 and a precision of 2' do
before :all do
class GoldFish < Fish
property :average_weight, Float, :scale => 4, :precision => 2
end
end

before do
@gold_fish = GoldFish.new
end

it 'should allow up to 2 digits before the decimal' do
@gold_fish.average_weight = 0
@gold_fish.should be_valid

@gold_fish.average_weight = 99
@gold_fish.should be_valid

@gold_fish.average_weight = 100
@gold_fish.should_not be_valid
end

it 'should allow 2 digits of precision after the decimal' do
@gold_fish.average_weight = 99.99
@gold_fish.should be_valid

@gold_fish.average_weight = 99.999
@gold_fish.should_not be_valid
end
end

describe 'with a scale of 2 and a precision of 2' do
before :all do
class SilverFish < Fish
property :average_weight, Float, :scale => 2, :precision => 2
end
end

before do
@silver_fish = SilverFish.new
end

it 'should allow a 0 before the decimal' do
@silver_fish.average_weight = 0
@silver_fish.should be_valid

@silver_fish.average_weight = 0.1
@silver_fish.should be_valid

@silver_fish.average_weight = 1
@silver_fish.should_not be_valid
end

it 'should allow 2 digits of precision after the decimal' do
@silver_fish.average_weight = 0.99
@silver_fish.should be_valid

@silver_fish.average_weight = 0.999
@silver_fish.should_not be_valid
end
end
end
end
end

0 comments on commit 0765e3c

Please sign in to comment.