sam / dm-more

Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper

This URL has Read+Write access

myabc (author)
Thu May 22 05:32:30 -0700 2008
commit  c88cc4e939519922e2a45db629443b955204f966
tree    c751adcdeee6de89b93e12fdaecbce5848606537
parent  28e69c642b8a534837913e2e0b6ffb63a977348e
dm-more / dm-validations / spec / integration / numeric_validator_spec.rb
100644 82 lines (69 sloc) 2.285 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
 
class Bill # :nodoc:
  include DataMapper::Resource
  property :id, Integer, :serial => true
  property :amount_1, String, :auto_validation => false
  property :amount_2, Float, :auto_validation => false
  validates_is_number :amount_1, :amount_2
end
 
class Hillary # :nodoc:
  include DataMapper::Resource
  property :id, Integer, :serial => true
  property :amount_1, Float, :auto_validation => false, :default => 0.01
  validates_is_number :amount_1
end
 
describe DataMapper::Validate::NumericValidator do
  it "should validate a floating point value on the instance of a resource" do
    b = Bill.new
    b.should_not be_valid
    b.amount_1 = 'ABC'
    b.amount_2 = 27.343
    b.should_not be_valid
    b.amount_1 = '34.33'
    b.should be_valid
  end
 
  it "should validate an integer value on the instance of a resource" do
    class Bill
      property :quantity_1, String, :auto_validation => false
      property :quantity_2, Integer, :auto_validation => false
 
      validators.clear!
      validates_is_number :quantity_1, :quantity_2, :integer_only => true
    end
    b = Bill.new
    b.valid?.should_not == true
    b.quantity_1 = '12.334'
    b.quantity_2 = 27.343
    b.valid?.should_not == true
    b.quantity_1 = '34.33'
    b.quantity_2 = 22
    b.valid?.should_not == true
    b.quantity_1 = '34'
    b.valid?.should == true
 
  end
 
  it "should validate if a default fufills the requirements" do
    h = Hillary.new
    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
    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
 
    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
 
end