/
validatable.rb
123 lines (100 loc) · 3.44 KB
/
validatable.rb
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
123
require "rubygems"
require "dm-core"
require "dm-validations"
require "spec"
DataMapper.setup(:default, "sqlite3::memory:")
class Person
include DataMapper::Resource
property :id, Serial
has 1, :profile
has n, :project_memberships
has n, :projects, :through => :project_memberships
end
class Profile
include DataMapper::Resource
property :id, Serial
property :person_id, Integer, :nullable => false
belongs_to :person
end
class Project
include DataMapper::Resource
property :id, Serial
has n, :tasks
end
class ProjectMembership
include DataMapper::Resource
property :id, Serial
property :person_id, Integer, :nullable => false
property :project_id, Integer, :nullable => false
belongs_to :person
belongs_to :project
end
class Task
include DataMapper::Resource
property :id, Serial
property :project_id, Integer, :nullable => false
belongs_to :project
end
describe "Resource#validatable?" do
before(:all) do
DataMapper.auto_migrate!
end
describe "when called directly on a Resource" do
it "should return true if dm-validations are present" do
Person.create.should be_validatable
Profile.create.should be_validatable
Project.create.should be_validatable
ProjectMembership.create.should be_validatable
end
end
describe "when called on an DataMapper::Associations::ManyToOne::Proxy" do
it "should return true if dm-validations are present" do
person = Person.create
project = Project.create
membership = ProjectMembership.create :person => person, :project => project
membership.person.should be_validatable
membership.project.should be_validatable
end
end
describe "when called on an DataMapper::Associations::OneToMany::Proxy" do
describe "that points to a Resource related with has(n)" do
it "should return true if dm-validations are present" do
p = Project.create
t = Task.create :project_id => p.id
p.reload
p.tasks.all? { |task| task.validatable? }.should be_true
end
end
describe "that points to a Resource related with has(n, :through)" do
it "should return true if dm-validations are present" do
person = Person.create
project = Project.create
membership = ProjectMembership.create :person => person, :project => project
person.reload
person.projects.all? { |p| p.validatable? }.should be_true
end
end
end
end
# mungo:Desktop snusnu$ spec -c -f -s validatable.rb
#
# Resource#validatable? when called directly on a Resource
# - should return true if dm-validations are present
#
# Resource#validatable? when called on an DataMapper::Associations::ManyToOne::Proxy
# - should return true if dm-validations are present (FAILED - 1)
#
# Resource#validatable? when called on an DataMapper::Associations::OneToMany::Proxy that points to a Resource related with has(n)
# - should return true if dm-validations are present
#
# Resource#validatable? when called on an DataMapper::Associations::OneToMany::Proxy that points to a Resource related with has(n, :through)
# - should return true if dm-validations are present
#
# 1)
# 'Resource#validatable? when called on an DataMapper::Associations::ManyToOne::Proxy should return true if dm-validations are present' FAILED
# expected validatable? to return true, got false
# ./validatable.rb:67:
#
# Finished in 0.028411 seconds
#
# 4 examples, 1 failure