public this repo is viewable by everyone
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Splitting validation specs into separate files.

* Split specs into separate files for
  future ease-of-maintenance, merging,
  etc..
* Created spec_helper.rb.for shared,
  helper methods.
myabc (author)
4 days ago
commit  d030592d948d24d86511bc03af0026d7ebebce64
tree    06627dd67de4db6f2becbc5c8e1934721d682869
parent  53973fb564838e387c4721a46b3bba7d2fa10bcf
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,35 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::AbsentFieldValidator do
0
+ before(:all) do
0
+ class Kayak
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :salesman, String, :auto_validation => false
0
+
0
+ validates_absence_of :salesman, :when => :sold
0
+ end
0
+
0
+ class Pirogue
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :salesman, String, :default => 'Layfayette'
0
+ validates_absence_of :salesman, :when => :sold
0
+ end
0
+ end
0
+
0
+ it "should validate the absense of a value on an instance of a resource" do
0
+ kayak = Kayak.new
0
+ kayak.valid_for_sold?.should == true
0
+
0
+ kayak.salesman = 'Joe'
0
+ kayak.valid_for_sold?.should_not == true
0
+ end
0
+
0
+ it "should validate the absense of a value and ensure defaults" do
0
+ pirogue = Pirogue.new
0
+ pirogue.should_not be_valid_for_sold
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,76 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::AcceptanceValidator do
0
+ describe "with standard options" do
0
+ before :all do
0
+ class SkimBat
0
+ include DataMapper::Validate
0
+ validates_acceptance_of :sailyness
0
+ attr_accessor :sailyness
0
+ end
0
+ @s = SkimBat.new
0
+ end
0
+ it "should validate if a resource instance has accepted" do
0
+ @s.sailyness = "1"
0
+ @s.valid?.should == true
0
+ end
0
+ it "should not validate if a resource instance has not accepted" do
0
+ @s.sailyness = "0"
0
+ @s.valid?.should == false
0
+ end
0
+ it "should allow nil acceptance" do
0
+ @s.sailyness = nil
0
+ @s.valid?.should == true
0
+ end
0
+ it "should add the default message when invalid" do
0
+ @s.sailyness = "0"
0
+ @s.valid?.should == false
0
+ @s.errors.full_messages.join(" ").should =~ /#{DataMapper::Validate::AcceptanceValidator.default_message_for_field("sailyness")}/
0
+ end
0
+ end
0
+ describe "with :allow_nil => false" do
0
+ before :all do
0
+ SkimBat.class_eval do
0
+ validators.clear!
0
+ validates_acceptance_of :sailyness, :allow_nil => false
0
+ end
0
+ @s = SkimBat.new
0
+ end
0
+ it "should not allow nil acceptance" do
0
+ @s.sailyness = nil
0
+ @s.valid?.should == false
0
+ end
0
+ end
0
+ describe "with custom :accept" do
0
+ before :all do
0
+ SkimBat.class_eval do
0
+ validators.clear!
0
+ validates_acceptance_of :sailyness, :accept => "true"
0
+ end
0
+ @s = SkimBat.new
0
+ end
0
+ it "should validate if a resource instance has accepted" do
0
+ @s.sailyness = "true"
0
+ @s.valid?.should == true
0
+ end
0
+ it "should not validate if a resource instance has not accepted" do
0
+ @s.sailyness = "false"
0
+ @s.valid?.should == false
0
+ end
0
+ end
0
+ describe "with custom message" do
0
+ before :all do
0
+ SkimBat.class_eval do
0
+ validators.clear!
0
+ validates_acceptance_of :sailyness, :message => "hehu!"
0
+ end
0
+ @s = SkimBat.new
0
+ end
0
+ it "should append the custom message when invalid" do
0
+ @s.sailyness = "0"
0
+ @s.valid?.should == false
0
+ @s.errors.full_messages.join(" ").should =~ /hehu!/
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
0
@@ -0,0 +1,107 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+ describe "Automatic Validation from Property Definition" do
0
+ before(:all) do
0
+ class SailBoat
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :nullable => false , :validates => :presence_test
0
+ property :description, String, :length => 10, :validates => :length_test_1
0
+ property :notes, String, :length => 2..10, :validates => :length_test_2
0
+ property :no_validation, String, :auto_validation => false
0
+ property :salesman, String, :nullable => false, :validates => [:multi_context_1, :multi_context_2]
0
+ property :code, String, :format => Proc.new { |code| code =~ /A\d{4}/}, :validates => :format_test
0
+ property :allow_nil, String, :size => 5..10, :nullable => true, :validates => :nil_test
0
+ end
0
+ end
0
+
0
+ it "should have a hook for adding auto validations called from DataMapper::Property#new" do
0
+ SailBoat.should respond_to(:auto_generate_validations)
0
+ end
0
+
0
+ it "should auto add a validates_presence_of when property has option :nullable => false" do
0
+ validator = SailBoat.validators.context(:presence_test).first
0
+ validator.is_a?(DataMapper::Validate::RequiredFieldValidator).should == true
0
+ validator.field_name.should == :name
0
+
0
+ boat = SailBoat.new
0
+ boat.valid_for_presence_test?.should == false
0
+ boat.name = 'Float'
0
+ boat.valid_for_presence_test?.should == true
0
+ end
0
+
0
+ it "should auto add a validates_length_of for maximum size on String properties" do
0
+ # max length test max=10
0
+ boat = SailBoat.new
0
+ boat.valid_for_length_test_1?.should == true #no minimum length
0
+ boat.description = 'ABCDEFGHIJK' #11
0
+ boat.valid_for_length_test_1?.should == false
0
+ boat.description = 'ABCDEFGHIJ' #10
0
+ boat.valid_for_length_test_1?.should == true
0
+ end
0
+
0
+ it "should auto add validates_length_of within a range when option :length or :size is a range" do
0
+ # Range test notes = 2..10
0
+ boat = SailBoat.new
0
+ boat.valid_for_length_test_2?.should == false
0
+ boat.notes = 'AB' #2
0
+ boat.valid_for_length_test_2?.should == true
0
+ boat.notes = 'ABCDEFGHIJK' #11
0
+ boat.valid_for_length_test_2?.should == false
0
+ boat.notes = 'ABCDEFGHIJ' #10
0
+ boat.valid_for_length_test_2?.should == true
0
+ end
0
+
0
+ it "should auto add a validates_format_of if the :format option is given" do
0
+ # format test - format = /A\d{4}/ on code
0
+ boat = SailBoat.new
0
+ boat.valid_for_format_test?.should == false
0
+ boat.code = 'A1234'
0
+ boat.valid_for_format_test?.should == true
0
+ boat.code = 'BAD CODE'
0
+ boat.valid_for_format_test?.should == false
0
+ end
0
+
0
+ it "should auto validate all strings for max length" do
0
+ class Test
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String
0
+ end
0
+ Test.new().valid?().should == true
0
+ t = Test.new()
0
+ t.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
0
+ t.valid?().should == false
0
+ t.errors.full_messages.should include('Name must be less than 50 characters long')
0
+ end
0
+
0
+ it "should not auto add any validators if the option :auto_validation => false was given" do
0
+ class Test
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :nullable => false, :auto_validation => false
0
+ end
0
+ Test.new().valid?().should == true
0
+ end
0
+
0
+ it 'It should auto add range checking the length of a string while still allowing null values' do
0
+ boat = SailBoat.new()
0
+ boat.allow_nil = 'ABC'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+
0
+ boat.allow_nil = 'ABCDEFG'
0
+ boat.should be_valid_for_nil_test
0
+
0
+ boat.allow_nil = 'ABCDEFGHIJKLMNOP'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+
0
+ boat.allow_nil = nil
0
+ boat.should be_valid_for_nil_test
0
+
0
+ end
0
+
0
+ end
0
+
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,77 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::ConfirmationValidator do
0
+ before(:all) do
0
+ class Canoe
0
+ include DataMapper::Validate
0
+ def name=(value)
0
+ @name = value
0
+ end
0
+
0
+ def name
0
+ @name ||= nil
0
+ end
0
+
0
+ def name_confirmation=(value)
0
+ @name_confirmation = value
0
+ end
0
+
0
+ def name_confirmation
0
+ @name_confirmation ||= nil
0
+ end
0
+
0
+ validates_confirmation_of :name
0
+ end
0
+ end
0
+
0
+ it "should validate the confirmation of a value on an instance of a resource" do
0
+ canoe = Canoe.new
0
+ canoe.name = 'White Water'
0
+ canoe.name_confirmation = 'Not confirmed'
0
+ canoe.valid?.should_not == true
0
+ canoe.errors.full_messages.first.should == 'Name does not match the confirmation'
0
+
0
+ canoe.name_confirmation = 'White Water'
0
+ canoe.valid?.should == true
0
+ end
0
+
0
+ it "should default the name of the confirmation field to <field>_confirmation if one is not specified" do
0
+ canoe = Canoe.new
0
+ canoe.name = 'White Water'
0
+ canoe.name_confirmation = 'White Water'
0
+ canoe.valid?.should == true
0
+ end
0
+
0
+ it "should default to allowing nil values on the fields if not specified to" do
0
+ Canoe.new().valid?().should == true
0
+ end
0
+
0
+ it "should not pass validation with a nil value when specified to" do
0
+ class Canoe
0
+ validators.clear!
0
+ validates_confirmation_of :name, :allow_nil => false
0
+ end
0
+ Canoe.new().valid?().should_not == true
0
+ end
0
+
0
+ it "should allow the name of the confirmation field to be set" do
0
+ class Canoe
0
+ validators.clear!
0
+ validates_confirmation_of :name, :confirm => :name_check
0
+ def name_check=(value)
0
+ @name_check = value
0
+ end
0
+
0
+ def name_check
0
+ @name_confirmation ||= nil
0
+ end
0
+ end
0
+ canoe = Canoe.new
0
+ canoe.name = 'Float'
0
+ canoe.name_check = 'Float'
0
+ canoe.valid?.should == true
0
+
0
+ end
0
+
0
+end
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
0
@@ -0,0 +1,122 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+ describe DataMapper::Validate::FormatValidator do
0
+ before(:all) do
0
+ class BillOfLading
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :doc_no, String, :auto_validation => false
0
+ property :email, String, :auto_validation => false
0
+
0
+ # this is a trivial example
0
+ validates_format_of :doc_no, :with => lambda { |code|
0
+ (code =~ /A\d{4}/) || (code =~ /[B-Z]\d{6}X12/)
0
+ }
0
+
0
+ validates_format_of :email, :as => :email_address
0
+ end
0
+ end
0
+
0
+ it 'should validate the format of a value on an instance of a resource' do
0
+ bol = BillOfLading.new
0
+ bol.doc_no = 'BAD CODE :)'
0
+ bol.should_not be_valid
0
+ bol.errors.on(:doc_no).should include('Doc no has an invalid format')
0
+
0
+ bol.doc_no = 'A1234'
0
+ bol.valid?
0
+ bol.errors.on(:doc_no).should be_nil
0
+
0
+ bol.doc_no = 'B123456X12'
0
+ bol.valid?
0
+ bol.errors.on(:doc_no).should be_nil
0
+ end
0
+
0
+ it 'should have a pre-defined e-mail format' do
0
+ bad = [ '-- guy --@example.com', # spaces are invalid unless quoted
0
+ '[guy]@example.com', # square brackets are invalid unless quoted
0
+ '.guy@example.com', # local part cannot start with .
0
+ 'guy@example 10:10',
0
+ 'guy@ example dot com',
0
+ 'guy'
0
+ ]
0
+
0
+ good = [
0
+ '+1~1+@example.com',
0
+ '{_guy_}@example.com',
0
+ '"[[ guy ]]"@example.com',
0
+ 'guy."guy"@example.com',
0
+ 'guy@localhost',
0
+ 'guy@example.com',
0
+ 'guy@example.co.uk',
0
+ 'guy@example.co.za',
0
+ 'guy@[187.223.45.119]',
0
+ 'guy@123.com'
0
+ ]
0
+
0
+ bol = BillOfLading.new
0
+ bol.should_not be_valid
0
+ bol.errors.on(:email).should include('Email has an invalid format')
0
+
0
+ bad.map do |e|
0
+ bol.email = e
0
+ bol.valid?
0
+ bol.errors.on(:email).should include('Email has an invalid format')
0
+ end
0
+
0
+ good.map do |e|
0
+ bol.email = e
0
+ bol.valid?
0
+ bol.errors.on(:email).should be_nil
0
+ end
0
+
0
+ end
0
+
0
+ it 'should have pre-defined formats'
0
+ end
0
+
0
+
0
+
0
+
0
+
0
+=begin
0
+addresses = [
0
+ '-- dave --@example.com', # (spaces are invalid unless enclosed in quotation marks)
0
+ '[dave]@example.com', # (square brackets are invalid, unless contained within quotation marks)
0
+ '.dave@example.com', # (the local part of a domain name cannot start with a period)
0
+ 'Max@Job 3:14',
0
+ 'Job@Book of Job',
0
+ 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
0
+ ]
0
+addresses.each do |address|
0
+ if address =~ RFC2822::EmailAddress
0
+ puts "#{address} deveria ter sido rejeitado, ERRO"
0
+ else
0
+ puts "#{address} rejeitado, OK"
0
+ end
0
+end
0
+
0
+
0
+addresses = [
0
+ '+1~1+@example.com',
0
+ '{_dave_}@example.com',
0
+ '"[[ dave ]]"@example.com',
0
+ 'dave."dave"@example.com',
0
+ 'test@localhost',
0
+ 'test@example.com',
0
+ 'test@example.co.uk',
0
+ 'test@example.com.br',
0
+ '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
0
+ 'me@[187.223.45.119]',
0
+ 'someone@123.com',
0
+ 'simon&garfunkel@songs.com'
0
+ ]
0
+addresses.each do |address|
0
+ if address =~ RFC2822::EmailAddress
0
+ puts "#{address} aceito, OK"
0
+ else
0
+ puts "#{address} deveria ser aceito, ERRO"
0
+ end
0
+end
0
+=end
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
83
84
85
86
87
88
89
90
91
92
93
94
95
0
@@ -0,0 +1,95 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::LengthValidator do
0
+ before(:all) do
0
+ class MotorLaunch
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :auto_validation => false
0
+ end
0
+
0
+ class BoatDock
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String, :auto_validation => false, :default => "I'm a long string"
0
+ validates_length_of :name, :min => 3
0
+ end
0
+ end
0
+
0
+ it "should be able to set a minimum length of a string field" do
0
+ class MotorLaunch
0
+ validates_length_of :name, :min => 3
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Ab'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be more than 3 characters long'
0
+ end
0
+
0
+ it "should be able to alias :minimum for :min " do
0
+ class MotorLaunch
0
+ validators.clear!
0
+ validates_length_of :name, :minimum => 3
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Ab'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be more than 3 characters long'
0
+ end
0
+
0
+ it "should be able to set a maximum length of a string field" do
0
+ class MotorLaunch
0
+ validators.clear!
0
+ validates_length_of :name, :max => 5
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be less than 5 characters long'
0
+ end
0
+
0
+ it "should be able to alias :maximum for :max" do
0
+ class MotorLaunch
0
+ validators.clear!
0
+ validates_length_of :name, :maximum => 5
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be less than 5 characters long'
0
+ end
0
+
0
+ it "should be able to specify a length range of a string field" do
0
+ class MotorLaunch
0
+ validators.clear!
0
+ validates_length_of :name, :in => (3..5)
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be between 3 and 5 characters long'
0
+
0
+ launch.name = 'A'
0
+ launch.valid?.should == false
0
+ launch.errors.full_messages.first.should == 'Name must be between 3 and 5 characters long'
0
+
0
+ launch.name = 'Ride'
0
+ launch.valid?.should == true
0
+ end
0
+
0
+ it "should be able to alias :within for :in" do
0
+ class MotorLaunch
0
+ validators.clear!
0
+ validates_length_of :name, :within => (3..5)
0
+ end
0
+ launch = MotorLaunch.new
0
+ launch.name = 'Ride'
0
+ launch.valid?.should == true
0
+ end
0
+
0
+ it "should pass if a default fufills the requirements" do
0
+ doc = BoatDock.new
0
+ doc.should be_valid
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,32 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::MethodValidator do
0
+ before(:all) do
0
+ class Ship
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :name, String
0
+
0
+ validates_with_method :fail_validation, :when => [:testing_failure]
0
+ validates_with_method :pass_validation, :when => [:testing_success]
0
+
0
+ def fail_validation
0
+ return false, 'Validation failed'
0
+ end
0
+
0
+ def pass_validation
0
+ return true
0
+ end
0
+ end
0
+ end
0
+
0
+ it "should validate via a method on the resource" do
0
+ Ship.new().valid_for_testing_failure?().should == false
0
+ Ship.new().valid_for_testing_success?().should == true
0
+ ship = Ship.new()
0
+ ship.valid_for_testing_failure?().should == false
0
+ ship.errors.full_messages.include?('Validation failed').should == true
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,61 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::NumericValidator do
0
+ before(:all) do
0
+ class Bill
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :amount_1, String, :auto_validation => false
0
+ property :amount_2, Float, :auto_validation => false
0
+
0
+
0
+ validates_numericalnes_of :amount_1, :amount_2
0
+ end
0
+
0
+ class Hillary
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :amount_1, Float, :auto_validation => false, :default => 0.01
0
+ validates_numericalnes_of :amount_1
0
+
0
+ end
0
+ end
0
+
0
+ it "should validate a floating point value on the instance of a resource" do
0
+ b = Bill.new
0
+ b.valid?.should_not == true
0
+ b.amount_1 = 'ABC'
0
+ b.amount_2 = 27.343
0
+ b.valid?.should_not == true
0
+ b.amount_1 = '34.33'
0
+ b.valid?.should == true
0
+ end
0
+
0
+ it "should validate an integer value on the instance of a resource" do
0
+ class Bill
0
+ property :quantity_1, String, :auto_validation => false
0
+ property :quantity_2, Fixnum, :auto_validation => false
0
+
0
+ validators.clear!
0
+ validates_numericalnes_of :quantity_1, :quantity_2, :integer_only => true
0
+ end
0
+ b = Bill.new
0
+ b.valid?.should_not == true
0
+ b.quantity_1 = '12.334'
0
+ b.quantity_2 = 27.343
0
+ b.valid?.should_not == true
0
+ b.quantity_1 = '34.33'
0
+ b.quantity_2 = 22
0
+ b.valid?.should_not == true
0
+ b.quantity_1 = '34'
0
+ b.valid?.should == true
0
+
0
+ end
0
+
0
+ it "should validate if a default fufills the requirements" do
0
+ h = Hillary.new
0
+ h.should be_valid
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,75 @@
0
+require 'pathname'
0
+require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
+
0
+describe DataMapper::Validate::RequiredFieldValidator do
0
+ after do
0
+ repository(:sqlite3).adapter.execute('DROP TABLE "landscapers"');
0
+ repository(:sqlite3).adapter.execute('DROP TABLE "gardens"');
0
+ end
0
+
0
+ before do
0
+ repository(:sqlite3).adapter.execute(<<-EOS.compress_lines) rescue nil
0
+ CREATE TABLE "landscapers" (
0
+ "id" INTEGER PRIMARY KEY,
0
+ "name" VARCHAR(50)
0
+ )
0
+ EOS
0
+ repository(:sqlite3).adapter.execute(<<-EOS.compress_lines) rescue nil
0
+ CREATE TABLE "gardens" (
0
+ "id" INTEGER PRIMARY KEY,
0
+ "landscaper_id" INTEGER,
0
+ "name" VARCHAR(50)
0
+ )
0
+ EOS
0
+
0
+ class Landscaper
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :id, Fixnum, :key => true
0
+ property :name, String
0
+ end
0
+
0
+ class Garden
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :id, Fixnum, :key => true
0
+ property :landscaper_id, Fixnum
0
+ property :name, String, :auto_validation => false
0
+
0
+ belongs_to :landscaper #has :landscaper, 1..n
0
+
0
+ validates_presence_of :name, :when => :property_test
0
+ validates_presence_of :landscaper, :when => :association_test
0
+ end
0
+
0
+ class Fertilizer
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :id, Fixnum, :serial => true
0
+ property :brand, String, :auto_validation => false, :default => 'Scotts'
0
+ validates_presence_of :brand, :when => :property_test
0
+ end
0
+ end
0
+
0
+ it "should validate the presence of a property value on an instance of a resource" do
0
+ garden = Garden.new
0
+ garden.should_not be_valid_for_property_test
0
+ garden.errors.on(:name).should include('Name must not be blank')
0
+
0
+ garden.name = 'The Wilds'
0
+ garden.should be_valid_for_property_test
0
+ end
0
+
0
+ it "should validate the presence of an association value on an instance of a resource when dirty"
0
+ #do
0
+ # garden = Garden.new
0
+ # landscaper = garden.landscaper
0
+ # puts landscaper.children.length
0
+ # #puts "Gardens landscaper is #{garden.landscaper.child_key}"
0
+ #end
0
+
0
+ it "should pass when a default is available" do
0
+ fert = Fertilizer.new
0
+ fert.should be_valid_for_property_test
0
+ end
0
+end
...