/
dogs.rb
183 lines (135 loc) · 3.5 KB
/
dogs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
%w{rubygems active_record test/unit Shoulda}.each { |lib| require(lib) }
def database_name(db='test.db')
File.join(File.dirname(__FILE__), db)
end
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => database_name
)
File.unlink(database_name) if File.exists?(database_name)
ActiveRecord::Schema.define do
create_table :dogs, :force => true do |t|
t.string :name
t.integer :age
t.integer :at_vet, :at_foster,
:at_hospice, :at_forever_home
t.timestamps
end
create_table :vettings, :force => true do |t|
t.integer :heartworms, :fixed
t.timestamps
end
create_table :people, :force => true do |t|
t.string :name
t.string :type
t.timestamps
end
end
class Dog < ActiveRecord::Base
has_many :vettings
belongs_to :foster_parent
belongs_to :hospice_provider
has_one :adoptive_parent
def rescued?
!at_vet? && !at_foster? &&
!at_hospice? && !adopted?
end
def vetted?
at_vet? && !at_foster? &&
!at_hospice? && !adopted?
end
def fostered?
!at_vet? && at_foster? &&
!at_hospice? && !adopted?
end
def hospice?
!at_vet? && !at_foster? &&
at_hospice? && !adopted?
end
def adopted?
!at_vet? && !at_foster? &&
!at_hospice && at_forever_home
end
end
class Vetting < ActiveRecord::Base
belongs_to :dog
end
class Person < ActiveRecord::Base
end
class FosterParent < Person
has_many :dogs
end
class HospiceProvider < Person
has_many :dogs
end
class AdoptiveParent < Person
has_many :dogs
end
class TestDog < Test::Unit::TestCase
context "A dog" do
setup do
@dog = Dog.new(:name => 'Cooper', :age => 2)
end
context "that has just been rescued" do
should "have a name" do
assert_equal 'Cooper', @dog.name
end
should "have an age" do
assert_equal 2, @dog.age
end
should "be rescued" do
assert @dog.rescued?
end
end
context "that has been vetted" do
setup do
vetting = Vetting.new(:heartworms => false,
:fixed => true)
@dog.vettings << vetting
@dog.at_vet = true
end
should "have veterinary information" do
assert_equal 1, @dog.vettings.length
end
should "be vetted" do
assert @dog.vetted?
end
end
context "that is being fostered" do
setup do
@dog.at_foster = true
@dog.foster_parent = FosterParent.new(:name => 'Adam')
end
should "belong to a foster home" do
assert_not_nil @dog.foster_parent
end
should "be at a foster home" do
assert @dog.at_foster?
end
end
context "that is in hospice" do
setup do
@dog.at_hospice = true
@dog.hospice_provider = HospiceProvider.new(:name => 'Maggie')
end
should "belong to a hospice home" do
assert_not_nil @dog.hospice_provider
end
should "be at a hospice home" do
assert @dog.at_hospice?
end
end
context "that has been adopted" do
setup do
@dog.at_forever_home = true
@dog.adoptive_parent = AdoptiveParent.new(:name => 'Marcel')
end
should "have adopter information" do
assert_not_nil @dog.adoptive_parent
end
should "be adopted" do
assert @dog.adopted?
end
end
end
end