/
has-bit-field_test.rb
217 lines (171 loc) · 5.8 KB
/
has-bit-field_test.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require File.join(File.dirname(__FILE__), 'test_helper')
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
#ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.connection.create_table(:people) do |t|
t.integer :bit_field, :null => true
end
class Person < ActiveRecord::Base
extend HasBitField
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
end
ActiveRecord::Base.connection.create_table(:skills) do |t|
t.integer :outdoor_bit_field, :default => 0, :null => false
t.integer :indoor_bit_field, :default => 0, :null => false
end
class Skill < ActiveRecord::Base
extend HasBitField
validates_acceptance_of :chops_trees, :message => "You have to be a lumberjack", :accept => true
validates_acceptance_of :plays_piano, :message => "You must be a pianist", :accept => true
has_bit_field :outdoor_bit_field, :chops_trees, :builds_fences, :cuts_hedges
has_bit_field :indoor_bit_field, :plays_piano, :mops_floors, :makes_soup
end
class HasBitFieldTest < Test::Unit::TestCase
def test_bit_field
p = Person.new
[:likes_ice_cream, :plays_golf, :watches_tv, :reads_books].each do |f|
assert p.respond_to?("#{f}?"), "Expected #{p.inspect} to respond to #{f}?"
assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
end
assert_equal Person.likes_ice_cream_bit, (1 << 0)
assert_equal Person.plays_golf_bit, (1 << 1)
assert_equal Person.watches_tv_bit, (1 << 2)
assert_equal Person.reads_books_bit, (1 << 3)
p.likes_ice_cream = true
assert p.likes_ice_cream?
assert !p.plays_golf?
assert !p.watches_tv?
assert !p.reads_books?
p.likes_ice_cream = true
assert p.likes_ice_cream?
assert !p.plays_golf?
assert !p.watches_tv?
assert !p.reads_books?
p.watches_tv = true
assert p.likes_ice_cream
assert !p.plays_golf
assert p.watches_tv
assert !p.reads_books
p.watches_tv = false
p.plays_golf = true
assert p.likes_ice_cream?
assert p.plays_golf?
assert !p.watches_tv?
assert !p.reads_books?
p.watches_tv = "1"
p.reads_books = "true"
assert p.likes_ice_cream?
assert p.plays_golf?
assert p.watches_tv?
assert p.reads_books?
p.likes_ice_cream = nil
p.plays_golf = 0
p.watches_tv = "0"
p.reads_books = "false"
assert !p.likes_ice_cream?
assert !p.plays_golf?
assert !p.watches_tv?
assert !p.reads_books?
end
def test_named_scopes_on_nullable_column
Person.delete_all
a = Person.new
a.plays_golf = true
a.reads_books = true
assert a.save
b = Person.new
b.likes_ice_cream = true
b.watches_tv = true
assert b.save
c = Person.create! :bit_field => 0
assert_equal [b], Person.likes_ice_cream.order("id").to_a
assert_equal [a,c], Person.not_likes_ice_cream.order("id").to_a
assert_equal [a], Person.plays_golf.order("id").to_a
assert_equal [b,c], Person.not_plays_golf.order("id").to_a
assert_equal [b], Person.watches_tv.order("id").to_a
assert_equal [a,c], Person.not_watches_tv.order("id").to_a
assert_equal [a], Person.reads_books.order("id").to_a
assert_equal [b,c], Person.not_reads_books.order("id").to_a
end
def test_named_scopes_on_non_nullable_column
Skill.delete_all
a = Skill.new
a.plays_piano = true
a.chops_trees = true
a.mops_floors = true
assert a.save, a.errors.full_messages.join(", ")
b = Skill.new
b.plays_piano = true
b.chops_trees = true
b.makes_soup = true
assert b.save, b.errors.full_messages.join(", ")
c = Skill.create! :plays_piano => true, :chops_trees => true
assert_equal [a,b,c], Skill.plays_piano.order("id").to_a
assert_equal [], Skill.not_plays_piano.order("id").to_a
assert_equal [a], Skill.mops_floors.order("id").to_a
assert_equal [b,c], Skill.not_mops_floors.order("id").to_a
assert_equal [b], Skill.makes_soup.order("id").to_a
assert_equal [a,c], Skill.not_makes_soup.order("id").to_a
end
def test_dirty_attributes
Person.delete_all
a = Person.new
a.plays_golf = true
a.reads_books = true
assert a.save
a.plays_golf = false
assert_equal false, a.plays_golf
assert_equal true, a.plays_golf_was
assert a.plays_golf_changed?
assert_equal true, a.reads_books
assert_equal true, a.reads_books_was
assert !a.reads_books_changed?
end
def test_ar_validations_no_default
s = Skill.new(:plays_piano => true, :builds_fences => true, :cuts_hedges => false)
assert_equal false, s.chops_trees?
assert_equal false, s.chops_trees
assert s.builds_fences?
assert !s.cuts_hedges?
assert !s.cuts_hedges
assert !s.valid?
assert s.errors[:chops_trees]
s.chops_trees = false
assert !s.chops_trees?
assert !s.valid?
assert s.errors[:chops_trees]
s.chops_trees = true
assert s.chops_trees?
assert s.valid?
assert s.errors[:chops_trees].blank?
assert s.save
end
def test_ar_validations_with_default
s = Skill.new(:chops_trees => true, :mops_floors => true, :makes_soup => false)
assert_equal false, s.plays_piano?
assert_equal false, s.plays_piano
assert s.mops_floors?
assert !s.makes_soup?
assert !s.makes_soup
assert !s.valid?
assert s.errors[:plays_piano]
s.plays_piano = false
assert !s.plays_piano?
assert !s.valid?
assert s.errors[:plays_piano]
s.plays_piano = true
assert s.plays_piano?
assert s.valid?
assert s.errors[:plays_piano].blank?
assert s.save
end
def test_environment_will_not_die_on_undefined_fields
assert_nothing_raised do
Person.class_eval do
has_bit_field :not_a_happy_field, :if_only_it_existed, :but_its_not_in_the_db, :we_had_hoped_for_it_but_its_not_there, :silence
end
end
end
end