static_model 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/History.txt +6 -0
- data/README.rdoc +5 -0
- data/Rakefile +31 -0
- data/index.html +80 -0
- data/lib/static_model/base.rb +12 -2
- data/lib/static_model.rb +6 -6
- data/static_model.gemspec +85 -0
- data/test/data/authors.yml +10 -0
- data/test/data/books.yml +22 -0
- data/test/data/empty.yml +0 -0
- data/test/data/pages.yml +16 -0
- data/test/data/projects.yml +7 -0
- data/test/data/publishers.yml +7 -0
- data/test/data/stores.yml +5 -0
- data/test/test_static_model.rb +472 -427
- metadata +40 -17
- data/Manifest.txt +0 -17
data/test/test_static_model.rb
CHANGED
@@ -1,427 +1,472 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
-
|
3
|
-
class TestStaticModel < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "StaticModel" do
|
6
|
-
context "A class that inherits from Base" do
|
7
|
-
context "an instance" do
|
8
|
-
|
9
|
-
setup do
|
10
|
-
@book = Book.new(book_params)
|
11
|
-
end
|
12
|
-
|
13
|
-
context "initialization" do
|
14
|
-
|
15
|
-
should "set attributes with a hash" do
|
16
|
-
assert @book.id
|
17
|
-
end
|
18
|
-
|
19
|
-
should "raise error if passed anything but hash" do
|
20
|
-
assert_raise(StaticModel::BadOptions) do
|
21
|
-
Book.new("im bad")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context "attributes" do
|
27
|
-
should "get attributes by methods" do
|
28
|
-
assert_equal book_params[:title], @book.title
|
29
|
-
assert_equal book_params[:genre], @book.genre
|
30
|
-
end
|
31
|
-
|
32
|
-
should "set attributes by methods" do
|
33
|
-
@book.title = 'New Title'
|
34
|
-
assert_equal 'New Title', @book.title
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "to_s" do
|
39
|
-
should "inspect" do
|
40
|
-
assert_equal @book.inspect, @book.to_s
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "comparing" do
|
45
|
-
|
46
|
-
should "be equal to an instance of the same class with same id" do
|
47
|
-
assert_equal @book, Book.new(book_params)
|
48
|
-
end
|
49
|
-
|
50
|
-
should "not be equal to an instance with the same class with different ids" do
|
51
|
-
assert_not_equal Book[1], @book
|
52
|
-
end
|
53
|
-
|
54
|
-
should "not be equal to an instance with different classes and the same ids" do
|
55
|
-
assert_not_equal Book[1], Author[1]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
context "on the class" do
|
62
|
-
context "set data file" do
|
63
|
-
context "After the class is defined" do
|
64
|
-
|
65
|
-
setup do
|
66
|
-
@book = Book.find(1)
|
67
|
-
@author = Author.find(1)
|
68
|
-
@original_data_file = Book.data_file
|
69
|
-
@data_file = File.join(File.dirname(__FILE__), 'data', 'authors.yml')
|
70
|
-
Book.set_data_file @data_file
|
71
|
-
@new_book = Book.find(1)
|
72
|
-
end
|
73
|
-
|
74
|
-
should "set the @data_file" do
|
75
|
-
assert_equal @data_file, Book.data_file
|
76
|
-
end
|
77
|
-
|
78
|
-
should "reload with next find" do
|
79
|
-
assert @author.attributes, @new_book.attributes
|
80
|
-
end
|
81
|
-
|
82
|
-
teardown do
|
83
|
-
Book.set_data_file @original_data_file
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "next_id" do
|
89
|
-
context "if id's were automaticaly assigned" do
|
90
|
-
should "be length + 1" do
|
91
|
-
Store.load
|
92
|
-
assert_equal Store.count + 1, Store.next_id
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "if ids were manualy assigned" do
|
97
|
-
should "be 1 after the last id" do
|
98
|
-
Publisher.load
|
99
|
-
assert_equal 8, Publisher.next_id
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
context "where the records are defined without ids" do
|
105
|
-
setup do
|
106
|
-
Store.reload!
|
107
|
-
end
|
108
|
-
|
109
|
-
context "loading" do
|
110
|
-
setup do
|
111
|
-
@stores = Store.all
|
112
|
-
end
|
113
|
-
|
114
|
-
should "automaticaly assign ids" do
|
115
|
-
@stores.each do |store|
|
116
|
-
assert store.id
|
117
|
-
assert store.id.is_a?(Fixnum)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
should "assign next id" do
|
122
|
-
assert Store.next_id
|
123
|
-
assert_equal 3, Store.next_id
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "initializing without id" do
|
128
|
-
setup do
|
129
|
-
@store = Store.new({:name => 'Metro Comics', :city => 'New York'})
|
130
|
-
end
|
131
|
-
|
132
|
-
should "return instance" do
|
133
|
-
assert @store.is_a?(Store)
|
134
|
-
end
|
135
|
-
|
136
|
-
should "set attributes" do
|
137
|
-
assert_equal 'New York', @store.city
|
138
|
-
end
|
139
|
-
|
140
|
-
should "assign id from next id" do
|
141
|
-
assert_equal 3, @store.id
|
142
|
-
end
|
143
|
-
|
144
|
-
should "increment next_id" do
|
145
|
-
assert_equal 4, Store.next_id
|
146
|
-
end
|
147
|
-
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
context "loading a data_file with embedded erb" do
|
152
|
-
setup do
|
153
|
-
@projects = Project.all
|
154
|
-
end
|
155
|
-
|
156
|
-
should "evaluate erb expressions at load time" do
|
157
|
-
assert_equal 1.day.ago.strftime('%m/%d/%Y %I:%M%p'), @projects.first.created_at
|
158
|
-
end
|
159
|
-
|
160
|
-
should "evaluate erb in current context" do
|
161
|
-
assert_equal Author.first, @projects[1].author
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
context "find" do
|
166
|
-
|
167
|
-
context "with an integer" do
|
168
|
-
setup do
|
169
|
-
@book = Book.find(1)
|
170
|
-
end
|
171
|
-
|
172
|
-
should "set loaded?" do
|
173
|
-
assert Book.loaded?
|
174
|
-
end
|
175
|
-
|
176
|
-
should "load by id" do
|
177
|
-
assert_equal 1, @book.id
|
178
|
-
end
|
179
|
-
|
180
|
-
should "return instance of klass" do
|
181
|
-
assert @book.is_a?(Book)
|
182
|
-
end
|
183
|
-
|
184
|
-
should "raise error if cant find record with id" do
|
185
|
-
assert_raise(StaticModel::RecordNotFound) do
|
186
|
-
@book = Book.find(1000)
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
context "[]" do
|
193
|
-
should "be an alias for find by id" do
|
194
|
-
assert_equal Book.find(1), Book[1]
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
context "find(:all)" do
|
199
|
-
should "be an alias for find_all" do
|
200
|
-
assert_equal Book.find_all, Book.find(:all)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
context "find(:first)" do
|
204
|
-
should "be an alias for find_first" do
|
205
|
-
assert_equal Book.find_first, Book.find(:first)
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
context "
|
215
|
-
should "be an alias for
|
216
|
-
assert_equal Book.
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
should "
|
226
|
-
assert_equal Book.
|
227
|
-
end
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
end
|
238
|
-
|
239
|
-
should "return
|
240
|
-
assert @
|
241
|
-
end
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
end
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
end
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
end
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
end
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
should "
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestStaticModel < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "StaticModel" do
|
6
|
+
context "A class that inherits from Base" do
|
7
|
+
context "an instance" do
|
8
|
+
|
9
|
+
setup do
|
10
|
+
@book = Book.new(book_params)
|
11
|
+
end
|
12
|
+
|
13
|
+
context "initialization" do
|
14
|
+
|
15
|
+
should "set attributes with a hash" do
|
16
|
+
assert @book.id
|
17
|
+
end
|
18
|
+
|
19
|
+
should "raise error if passed anything but hash" do
|
20
|
+
assert_raise(StaticModel::BadOptions) do
|
21
|
+
Book.new("im bad")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "attributes" do
|
27
|
+
should "get attributes by methods" do
|
28
|
+
assert_equal book_params[:title], @book.title
|
29
|
+
assert_equal book_params[:genre], @book.genre
|
30
|
+
end
|
31
|
+
|
32
|
+
should "set attributes by methods" do
|
33
|
+
@book.title = 'New Title'
|
34
|
+
assert_equal 'New Title', @book.title
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "to_s" do
|
39
|
+
should "inspect" do
|
40
|
+
assert_equal @book.inspect, @book.to_s
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "comparing" do
|
45
|
+
|
46
|
+
should "be equal to an instance of the same class with same id" do
|
47
|
+
assert_equal @book, Book.new(book_params)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "not be equal to an instance with the same class with different ids" do
|
51
|
+
assert_not_equal Book[1], @book
|
52
|
+
end
|
53
|
+
|
54
|
+
should "not be equal to an instance with different classes and the same ids" do
|
55
|
+
assert_not_equal Book[1], Author[1]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
context "on the class" do
|
62
|
+
context "set data file" do
|
63
|
+
context "After the class is defined" do
|
64
|
+
|
65
|
+
setup do
|
66
|
+
@book = Book.find(1)
|
67
|
+
@author = Author.find(1)
|
68
|
+
@original_data_file = Book.data_file
|
69
|
+
@data_file = File.join(File.dirname(__FILE__), 'data', 'authors.yml')
|
70
|
+
Book.set_data_file @data_file
|
71
|
+
@new_book = Book.find(1)
|
72
|
+
end
|
73
|
+
|
74
|
+
should "set the @data_file" do
|
75
|
+
assert_equal @data_file, Book.data_file
|
76
|
+
end
|
77
|
+
|
78
|
+
should "reload with next find" do
|
79
|
+
assert @author.attributes, @new_book.attributes
|
80
|
+
end
|
81
|
+
|
82
|
+
teardown do
|
83
|
+
Book.set_data_file @original_data_file
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "next_id" do
|
89
|
+
context "if id's were automaticaly assigned" do
|
90
|
+
should "be length + 1" do
|
91
|
+
Store.load
|
92
|
+
assert_equal Store.count + 1, Store.next_id
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "if ids were manualy assigned" do
|
97
|
+
should "be 1 after the last id" do
|
98
|
+
Publisher.load
|
99
|
+
assert_equal 8, Publisher.next_id
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "where the records are defined without ids" do
|
105
|
+
setup do
|
106
|
+
Store.reload!
|
107
|
+
end
|
108
|
+
|
109
|
+
context "loading" do
|
110
|
+
setup do
|
111
|
+
@stores = Store.all
|
112
|
+
end
|
113
|
+
|
114
|
+
should "automaticaly assign ids" do
|
115
|
+
@stores.each do |store|
|
116
|
+
assert store.id
|
117
|
+
assert store.id.is_a?(Fixnum)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
should "assign next id" do
|
122
|
+
assert Store.next_id
|
123
|
+
assert_equal 3, Store.next_id
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "initializing without id" do
|
128
|
+
setup do
|
129
|
+
@store = Store.new({:name => 'Metro Comics', :city => 'New York'})
|
130
|
+
end
|
131
|
+
|
132
|
+
should "return instance" do
|
133
|
+
assert @store.is_a?(Store)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "set attributes" do
|
137
|
+
assert_equal 'New York', @store.city
|
138
|
+
end
|
139
|
+
|
140
|
+
should "assign id from next id" do
|
141
|
+
assert_equal 3, @store.id
|
142
|
+
end
|
143
|
+
|
144
|
+
should "increment next_id" do
|
145
|
+
assert_equal 4, Store.next_id
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context "loading a data_file with embedded erb" do
|
152
|
+
setup do
|
153
|
+
@projects = Project.all
|
154
|
+
end
|
155
|
+
|
156
|
+
should "evaluate erb expressions at load time" do
|
157
|
+
assert_equal 1.day.ago.strftime('%m/%d/%Y %I:%M%p'), @projects.first.created_at
|
158
|
+
end
|
159
|
+
|
160
|
+
should "evaluate erb in current context" do
|
161
|
+
assert_equal Author.first, @projects[1].author
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "find" do
|
166
|
+
|
167
|
+
context "with an integer" do
|
168
|
+
setup do
|
169
|
+
@book = Book.find(1)
|
170
|
+
end
|
171
|
+
|
172
|
+
should "set loaded?" do
|
173
|
+
assert Book.loaded?
|
174
|
+
end
|
175
|
+
|
176
|
+
should "load by id" do
|
177
|
+
assert_equal 1, @book.id
|
178
|
+
end
|
179
|
+
|
180
|
+
should "return instance of klass" do
|
181
|
+
assert @book.is_a?(Book)
|
182
|
+
end
|
183
|
+
|
184
|
+
should "raise error if cant find record with id" do
|
185
|
+
assert_raise(StaticModel::RecordNotFound) do
|
186
|
+
@book = Book.find(1000)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context "[]" do
|
193
|
+
should "be an alias for find by id" do
|
194
|
+
assert_equal Book.find(1), Book[1]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context "find(:all)" do
|
199
|
+
should "be an alias for find_all" do
|
200
|
+
assert_equal Book.find_all, Book.find(:all)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
context "find(:first)" do
|
204
|
+
should "be an alias for find_first" do
|
205
|
+
assert_equal Book.find_first, Book.find(:first)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
context "find(:last)" do
|
209
|
+
should "be an alias for find_last" do
|
210
|
+
assert_equal Book.find_last, Book.find(:last)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context "all" do
|
215
|
+
should "be an alias for find_all" do
|
216
|
+
assert_equal Book.all, Book.find(:all)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
context "first" do
|
220
|
+
should "be an alias for find_first" do
|
221
|
+
assert_equal Book.first, Book.find(:first)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
context "last" do
|
225
|
+
should "be an alias for find_last" do
|
226
|
+
assert_equal Book.last, Book.find(:last)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "find_first" do
|
231
|
+
setup do
|
232
|
+
@book = Book.find_first
|
233
|
+
end
|
234
|
+
|
235
|
+
should "return the first instance from all records" do
|
236
|
+
assert_equal Book.find_all.first, @book
|
237
|
+
end
|
238
|
+
|
239
|
+
should "return instance of klass" do
|
240
|
+
assert @book.is_a?(Book)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
context "find_last" do
|
245
|
+
setup do
|
246
|
+
@book = Book.find_last
|
247
|
+
end
|
248
|
+
|
249
|
+
should "return the last instance from all records" do
|
250
|
+
assert_equal Book.find_all.last, @book
|
251
|
+
end
|
252
|
+
|
253
|
+
should "return an instance of klass" do
|
254
|
+
assert @book.is_a?(Book)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context "find_all" do
|
259
|
+
setup do
|
260
|
+
@books = Book.find_all
|
261
|
+
end
|
262
|
+
|
263
|
+
should "return an array" do
|
264
|
+
assert @books.is_a?(Array)
|
265
|
+
end
|
266
|
+
|
267
|
+
should "return all records" do
|
268
|
+
assert_equal 4, @books.length
|
269
|
+
end
|
270
|
+
|
271
|
+
should "return set of klass instances" do
|
272
|
+
@books.each do |book|
|
273
|
+
assert book.is_a?(Book)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
context "with an empty data file" do
|
278
|
+
setup do
|
279
|
+
@original_data_file = Book.data_file
|
280
|
+
@data_file = File.join(File.dirname(__FILE__), 'data', 'empty.yml')
|
281
|
+
Book.set_data_file @data_file
|
282
|
+
end
|
283
|
+
|
284
|
+
should "return an empty array" do
|
285
|
+
assert_equal [], Book.find_all
|
286
|
+
end
|
287
|
+
|
288
|
+
teardown do
|
289
|
+
Book.set_data_file @original_data_file
|
290
|
+
end
|
291
|
+
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
context "find_first_by" do
|
296
|
+
setup do
|
297
|
+
@author = 'Michael Pollan'
|
298
|
+
@book = Book.find_first_by(:author,@author)
|
299
|
+
end
|
300
|
+
|
301
|
+
should "return an instance of klass" do
|
302
|
+
assert @book.is_a?(Book)
|
303
|
+
end
|
304
|
+
|
305
|
+
should "return record matching search" do
|
306
|
+
assert @author, @book.author
|
307
|
+
end
|
308
|
+
|
309
|
+
context "when there is no match" do
|
310
|
+
should "return nil" do
|
311
|
+
assert_nil Book.find_first_by(:author,'Aaron Quint')
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
context "find_last_by" do
|
317
|
+
setup do
|
318
|
+
@author = 'Chuck Palahniuk'
|
319
|
+
@book = Book.find_last_by(:author, @author)
|
320
|
+
end
|
321
|
+
|
322
|
+
should "return an instance of klass" do
|
323
|
+
assert @book.is_a?(Book)
|
324
|
+
end
|
325
|
+
|
326
|
+
should "return record matching search" do
|
327
|
+
assert @author, @book.author
|
328
|
+
end
|
329
|
+
|
330
|
+
context "when there is no match" do
|
331
|
+
should "return nil" do
|
332
|
+
assert_nil Book.find_last_by(:author, 'Michael R. Bernstein')
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
context "find_by" do
|
338
|
+
setup do
|
339
|
+
@author = 'Michael Pollan'
|
340
|
+
@book = Book.find_by(:author,@author)
|
341
|
+
end
|
342
|
+
|
343
|
+
should "return an instance of klass" do
|
344
|
+
assert @book.is_a?(Book)
|
345
|
+
end
|
346
|
+
|
347
|
+
should "return record matching search" do
|
348
|
+
assert @author, @book.author
|
349
|
+
end
|
350
|
+
|
351
|
+
context "when there is no match" do
|
352
|
+
should "return nil" do
|
353
|
+
assert_nil Book.find_by(:author,'Aaron Quint')
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
end
|
358
|
+
|
359
|
+
context "find_all_by" do
|
360
|
+
setup do
|
361
|
+
@author = 'Michael Pollan'
|
362
|
+
@books = Book.find_all_by(:author,@author)
|
363
|
+
end
|
364
|
+
|
365
|
+
should "return an array" do
|
366
|
+
assert @books.is_a?(Array)
|
367
|
+
end
|
368
|
+
|
369
|
+
should "return all records that match search" do
|
370
|
+
@books.each {|b| assert_equal @author, b.author}
|
371
|
+
end
|
372
|
+
|
373
|
+
should "return set of klass instances" do
|
374
|
+
@books.each {|b| assert b.is_a?(Book) }
|
375
|
+
end
|
376
|
+
|
377
|
+
context "when there is no match" do
|
378
|
+
should "return an empty array" do
|
379
|
+
assert_equal [], Book.find_all_by(:author,'Aaron Quint')
|
380
|
+
end
|
381
|
+
end
|
382
|
+
context "when there is only one match" do
|
383
|
+
should "return an array" do
|
384
|
+
assert_equal [Book.find(3)], Book.find_all_by(:author,'Piers Anthony')
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
context "dynamic finders" do
|
390
|
+
setup do
|
391
|
+
@book = Book.first
|
392
|
+
end
|
393
|
+
|
394
|
+
context "find_by_*attribute*" do
|
395
|
+
should "be equivalent to find_first_by(attribute,)" do
|
396
|
+
assert_equal @book, Book.find_first_by(:genre, 'Non-Fiction')
|
397
|
+
assert_equal Book.find_first_by(:genre, 'Non-Fiction'), Book.find_by_genre('Non-Fiction')
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
context "find_first_by_*attribute*" do
|
402
|
+
should "be equivalent to find_first_by(attribute,)" do
|
403
|
+
assert_equal @book, Book.find_first_by(:genre, 'Non-Fiction')
|
404
|
+
assert_equal Book.find_first_by(:genre, 'Non-Fiction'), Book.find_first_by_genre('Non-Fiction')
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
context "find_all_by_*attribute*" do
|
409
|
+
should "be equivalent to find_all_by(attribute,)" do
|
410
|
+
assert_equal Book.find_all_by(:genre, 'Non-Fiction'), Book.find_all_by_genre('Non-Fiction')
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
context "count" do
|
416
|
+
should "return the count of all records" do
|
417
|
+
assert_equal Book.all.length, Book.count
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
context "with a class with yaml class vars" do
|
422
|
+
setup do
|
423
|
+
@pages = Page.all
|
424
|
+
end
|
425
|
+
|
426
|
+
should "load :records into @records" do
|
427
|
+
assert_set_of Page, @pages
|
428
|
+
assert Page.loaded?
|
429
|
+
end
|
430
|
+
|
431
|
+
should "give access to top level attributes as class methods" do
|
432
|
+
assert_equal 'http://www.quirkey.com', Page.url
|
433
|
+
assert_equal 'The Best Ever', Page.title
|
434
|
+
end
|
435
|
+
|
436
|
+
should "return a hash for class attribute" do
|
437
|
+
assert Page.settings.is_a?(Hash)
|
438
|
+
assert_equal 'test', Page.settings['username']
|
439
|
+
end
|
440
|
+
|
441
|
+
should "have class attributes appear as record accessor defaults if none exist" do
|
442
|
+
assert_equal 'http://www.quirkey.com', Page[1].url
|
443
|
+
end
|
444
|
+
|
445
|
+
should "not overwrite record specific methods" do
|
446
|
+
assert_equal 'http://github.com', Page[2].url
|
447
|
+
end
|
448
|
+
|
449
|
+
context "class_attributes" do
|
450
|
+
setup do
|
451
|
+
@attributes = Page.class_attributes
|
452
|
+
end
|
453
|
+
|
454
|
+
should "return a hash of all top level settings" do
|
455
|
+
assert @attributes.is_a?(Hash)
|
456
|
+
assert_equal 3, @attributes.length
|
457
|
+
assert_equal 'http://www.quirkey.com', @attributes['url']
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
end
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
protected
|
467
|
+
def book_params
|
468
|
+
{:id => 15, :title => 'Lord of the Rings', :author => 'J.R. Tolkien', :genre => 'Fantasy'}
|
469
|
+
end
|
470
|
+
|
471
|
+
|
472
|
+
end
|