sample_models 1.2.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -6
- data/Gemfile.lock +22 -7
- data/README.markdown +21 -42
- data/Rakefile +10 -14
- data/VERSION +1 -1
- data/lib/sample_models/attribute_sequence.rb +182 -0
- data/lib/sample_models/creation.rb +136 -89
- data/lib/sample_models/initializer.rb +48 -0
- data/lib/sample_models/model.rb +52 -143
- data/lib/sample_models/sampler.rb +64 -88
- data/lib/sample_models.rb +15 -95
- data/sample_models.gemspec +14 -18
- data/test/setup/models.rb +205 -0
- data/test/setup/schema.rb +108 -0
- data/test/test_helper.rb +42 -0
- data/test/unit/belongs_to_test.rb +143 -0
- data/test/unit/configuration_test.rb +85 -0
- data/test/unit/has_many_through_test.rb +46 -0
- data/test/unit/named_sample_test.rb +15 -0
- data/test/unit/polymorphic_belongs_to_test.rb +29 -0
- data/test/unit/sample_test.rb +134 -0
- metadata +16 -20
- data/lib/sample_models/finder.rb +0 -86
- data/spec/sample_models_spec.rb +0 -11
- data/spec_or_test/database.yml +0 -6
- data/spec_or_test/setup.rb +0 -288
- data/spec_or_test/specs_or_test_cases.rb +0 -586
- data/test/test_sample_models.rb +0 -28
- data/vendor/ar_query/MIT-LICENSE +0 -20
- data/vendor/ar_query/README +0 -0
- data/vendor/ar_query/ar_query.gemspec +0 -16
- data/vendor/ar_query/init.rb +0 -1
- data/vendor/ar_query/install.rb +0 -1
- data/vendor/ar_query/lib/ar_query.rb +0 -146
- data/vendor/ar_query/spec/ar_query_spec.rb +0 -318
- data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
- data/vendor/ar_query/uninstall.rb +0 -1
@@ -1,586 +0,0 @@
|
|
1
|
-
describe "Model.sample" do
|
2
|
-
it 'should fill non-validated, non-configured fields with a non-blank value' do
|
3
|
-
appt = Appointment.sample
|
4
|
-
assert appt.end_time.is_a?(Time)
|
5
|
-
blog_post = BlogPost.sample
|
6
|
-
assert blog_post.average_rating.is_a?(Float)
|
7
|
-
show = Show.sample
|
8
|
-
assert show.subscription_price.is_a?(Integer)
|
9
|
-
network = Network.sample
|
10
|
-
assert network.name.is_a?(String)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should allow overrides of all fields in sample' do
|
14
|
-
user = User.sample(
|
15
|
-
:homepage => 'http://mysite.com/', :password => 'myownpassword'
|
16
|
-
)
|
17
|
-
assert_equal 'http://mysite.com/', user.homepage
|
18
|
-
assert_equal 'myownpassword', user.password
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should pick the first value given in a validates_inclusion_of' do
|
22
|
-
user = User.sample
|
23
|
-
assert_equal 'f', user.gender
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should raise the standard validation error if you break the model's validates_inclusion_of validation" do
|
27
|
-
assert_raise(ActiveRecord::RecordInvalid) do
|
28
|
-
User.sample(:gender => 'x')
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should set emails based on a validation' do
|
33
|
-
user = User.sample
|
34
|
-
assert_match /^.*@.*\..*/, user.email
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should raise the standard validation error if you break the model's validates_email_format_of validation" do
|
38
|
-
unless ENV['ACTIVE_RECORD_VERSION'] =~ /^3\./
|
39
|
-
assert_raise(ActiveRecord::RecordInvalid) do
|
40
|
-
User.sample(:email => 'call.me')
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should set fields that are not validated to non-nil values' do
|
46
|
-
user = User.sample
|
47
|
-
assert_not_nil user.homepage
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'should not override a boolean default' do
|
51
|
-
assert !Comment.sample.flagged_as_spam
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'should return the same instance with two consecutive calls without arguments' do
|
55
|
-
user1 = User.sample
|
56
|
-
user2 = User.sample
|
57
|
-
assert_equal user1, user2
|
58
|
-
assert_equal user1.login, user2.login
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'should return the same instance with two consecutive calls with the same arguments' do
|
62
|
-
user1 = User.sample :homepage => 'http://john.doe.com/'
|
63
|
-
user2 = User.sample :homepage => 'http://john.doe.com/'
|
64
|
-
assert_equal user1, user2
|
65
|
-
assert_equal user1.login, user2.login
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'should return different instances with two consecutive calls with different arguments' do
|
69
|
-
user1 = User.sample :homepage => 'http://john.doe.com/'
|
70
|
-
user2 = User.sample :homepage => 'http://jane.doe.com/'
|
71
|
-
assert_not_equal user1, user2
|
72
|
-
assert_not_equal user1.login, user2.login
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'should return a different instance to a later call with more specific attributes' do
|
76
|
-
user1 = User.sample
|
77
|
-
user2 = User.sample :homepage => 'http://john.doe.com/'
|
78
|
-
assert_not_equal user1, user2
|
79
|
-
assert_not_equal user1.login, user2.login
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'should return the same instance to a later call with less specific attributes' do
|
83
|
-
User.destroy_all
|
84
|
-
user1 = User.sample(
|
85
|
-
:homepage => 'http://mysite.com/', :password => 'myownpassword'
|
86
|
-
)
|
87
|
-
user2 = User.sample :homepage => 'http://mysite.com/'
|
88
|
-
assert_equal user1, user2
|
89
|
-
user3 = User.sample
|
90
|
-
assert_equal user1, user3
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
describe 'Model with a belongs_to association' do
|
96
|
-
it 'should be associated with the belongs_to recipient by default' do
|
97
|
-
blog_post = BlogPost.sample
|
98
|
-
assert blog_post.user
|
99
|
-
assert blog_post.user.is_a?(User)
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'should set a custom value by the association name' do
|
103
|
-
user = User.sample
|
104
|
-
blog_post = BlogPost.sample :user => user
|
105
|
-
assert_equal user, blog_post.user
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'should return the same instance with two consecutive calls with the same associated value' do
|
109
|
-
user = User.sample
|
110
|
-
blog_post1 = BlogPost.sample :user => user
|
111
|
-
blog_post2 = BlogPost.sample :user => user
|
112
|
-
assert_equal blog_post1, blog_post2
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should set a custom value by the column name' do
|
116
|
-
user = User.sample
|
117
|
-
blog_post = BlogPost.sample :user_id => user.id
|
118
|
-
assert_equal user, blog_post.user
|
119
|
-
end
|
120
|
-
|
121
|
-
it 'should set a custom nil value by the association name' do
|
122
|
-
show = Show.sample :network => nil
|
123
|
-
assert_nil show.network
|
124
|
-
assert_nil show.network_id
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'should set a custom nil value by the association name, even when there has been a previously created record with default attributes' do
|
128
|
-
show1 = Show.sample
|
129
|
-
show2 = Show.sample :network => nil
|
130
|
-
assert_nil show2.network
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'should set a custom nil value by the association name, even when there has been a previously created record with that association assigned' do
|
134
|
-
show1 = Show.sample :network => Network.sample
|
135
|
-
show2 = Show.sample :network => nil
|
136
|
-
assert_nil show2.network
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'should set a custom nil value by the association ID' do
|
140
|
-
show = Show.sample :network_id => nil
|
141
|
-
assert_nil show.network
|
142
|
-
assert_nil show.network_id
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'should have no problem with circular associations' do
|
146
|
-
assert User.sample.favorite_blog_post.is_a?(BlogPost)
|
147
|
-
assert BlogPost.sample.user.is_a?(User)
|
148
|
-
end
|
149
|
-
|
150
|
-
it 'should allow creation of a custom associated instance with a hash' do
|
151
|
-
show = Show.sample(
|
152
|
-
:name => 'The Daily Show', :network => {:name => 'Comedy Central'}
|
153
|
-
)
|
154
|
-
assert_equal "The Daily Show", show.name
|
155
|
-
assert_equal 'Comedy Central', show.network.name
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'should return the same instance with two consecutive calls with the same association hash' do
|
159
|
-
show1 = Show.sample(
|
160
|
-
:name => 'The Daily Show', :network => {:name => 'Comedy Central'}
|
161
|
-
)
|
162
|
-
show2 = Show.sample(
|
163
|
-
:name => 'The Daily Show', :network => {:name => 'Comedy Central'}
|
164
|
-
)
|
165
|
-
assert_equal show1, show2
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'should return different instances when passed different association hashes' do
|
169
|
-
show1 = Show.sample :network => {:name => 'Comedy Central'}
|
170
|
-
show2 = Show.sample :network => {:name => 'MTV'}
|
171
|
-
assert_not_equal show1, show2
|
172
|
-
end
|
173
|
-
|
174
|
-
it 'should gracefully handle destruction of an associated value' do
|
175
|
-
blog_post1 = BlogPost.sample
|
176
|
-
assert blog_post1.user
|
177
|
-
User.destroy_all
|
178
|
-
blog_post2 = BlogPost.sample
|
179
|
-
assert blog_post2.user
|
180
|
-
blog_post2.reload
|
181
|
-
assert blog_post2.user
|
182
|
-
blog_post3 = BlogPost.sample :name => 'funny'
|
183
|
-
assert blog_post3.user
|
184
|
-
end
|
185
|
-
|
186
|
-
it "should just create a new instance after destruction even if the association is not validated to be present" do
|
187
|
-
show1 = Show.sample :name => "Oh no you didn't"
|
188
|
-
show1.network.destroy
|
189
|
-
show2 = Show.sample :name => "Don't go there"
|
190
|
-
assert_not_nil show2.network
|
191
|
-
end
|
192
|
-
|
193
|
-
it 'should assign that belongs-to association if a value is passed in as the first argument' do
|
194
|
-
user = User.create_sample
|
195
|
-
blog_post = BlogPost.sample(user, :title => 'some title')
|
196
|
-
assert_equal user, blog_post.user
|
197
|
-
assert_equal 'some title', blog_post.title
|
198
|
-
end
|
199
|
-
|
200
|
-
it 'should allow you to specify that associated records with shortcut associations' do
|
201
|
-
network = Network.sample
|
202
|
-
video = Video.sample(:show => [network, {:name => "Jersey Shore"}])
|
203
|
-
assert_equal network, video.show.network
|
204
|
-
assert_equal "Jersey Shore", video.show.name
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
describe 'Model with a belongs_to association of the same class' do
|
209
|
-
before :all do
|
210
|
-
@blog_post = BlogPost.sample
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'should be nil by default' do
|
214
|
-
assert_nil @blog_post.merged_into
|
215
|
-
end
|
216
|
-
|
217
|
-
it 'should not be itself by default' do
|
218
|
-
assert_not_equal @blog_post, @blog_post.merged_into
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
describe 'Model with a configured attribute' do
|
223
|
-
it 'should use that default' do
|
224
|
-
assert_equal 0, Video.sample.view_count
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
describe 'Model with a configured default association' do
|
229
|
-
it 'should use that default' do
|
230
|
-
cat = Category.sample
|
231
|
-
assert_nil cat.parent
|
232
|
-
end
|
233
|
-
|
234
|
-
it 'should allow that default to be overridden by name' do
|
235
|
-
sports = Category.sample :name => 'Sports'
|
236
|
-
soccer = Category.sample :name => 'Soccer', :parent => sports
|
237
|
-
assert_equal sports, soccer.parent
|
238
|
-
end
|
239
|
-
|
240
|
-
it 'should allow that default to be overridden by ID' do
|
241
|
-
sports = Category.sample :name => 'Sports'
|
242
|
-
soccer = Category.sample :name => 'Soccer', :parent_id => sports.id
|
243
|
-
assert_equal sports, soccer.parent
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
|
248
|
-
describe 'Model configuration with a bad field name' do
|
249
|
-
it 'should raise a useful error message' do
|
250
|
-
assert_raises(NoMethodError) do
|
251
|
-
SampleModels.configure Category do |category|
|
252
|
-
category.default.foobar ''
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe 'Model with a triangular belongs-to association' do
|
259
|
-
it 'should set unspecified association values to the same default instance' do
|
260
|
-
video = Video.sample :show => {:name => 'House'}
|
261
|
-
assert_equal 'House', video.show.name
|
262
|
-
assert video.show.network
|
263
|
-
assert video.network
|
264
|
-
assert_equal video.network, video.show.network
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe 'Model configured with .force_unique' do
|
269
|
-
it 'should return the same instance when called twice with no custom attrs' do
|
270
|
-
bp1 = BlogPost.sample
|
271
|
-
bp2 = BlogPost.sample
|
272
|
-
assert_equal bp1, bp2
|
273
|
-
assert_equal bp1.published_at, bp2.published_at
|
274
|
-
end
|
275
|
-
|
276
|
-
it 'should generated a new value for sample calls with custom attrs' do
|
277
|
-
bp1 = BlogPost.sample
|
278
|
-
bp2 = BlogPost.sample :user => {:login => 'francis'}
|
279
|
-
assert_not_equal bp1, bp2
|
280
|
-
assert_not_equal bp1.published_at, bp2.published_at
|
281
|
-
end
|
282
|
-
|
283
|
-
it 'should allow nil uniqued attribute if the model allows it' do
|
284
|
-
bp = BlogPost.sample :published_at => nil
|
285
|
-
assert_nil bp.published_at
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
describe 'Model with a redundant but validated association' do
|
290
|
-
it 'should use before_save to reconcile instance issues' do
|
291
|
-
video1 = Video.sample :episode => {:name => 'The one about the parents'}
|
292
|
-
assert_equal video1.show, video1.episode.show
|
293
|
-
video2 = Video.sample :show => {:name => 'South Park'}
|
294
|
-
assert_equal video2.show, video2.episode.show
|
295
|
-
assert_equal 'South Park', video2.show.name
|
296
|
-
end
|
297
|
-
|
298
|
-
it 'should not try to prefill the 2nd-hand association with another record' do
|
299
|
-
show = Show.sample(
|
300
|
-
:name => 'The Daily Show', :network => {:name => 'Comedy Central'}
|
301
|
-
)
|
302
|
-
video = Video.sample :show => {:name => 'House'}
|
303
|
-
assert_equal 'House', video.show.name
|
304
|
-
end
|
305
|
-
|
306
|
-
it 'should handle destroys gracefully' do
|
307
|
-
v1 = Video.sample
|
308
|
-
v1.show.destroy
|
309
|
-
v2 = Video.create_sample
|
310
|
-
assert_not_nil v2.show
|
311
|
-
assert_not_nil v2.episode.show
|
312
|
-
assert_equal v2.show, v2.episode.show
|
313
|
-
end
|
314
|
-
|
315
|
-
it 'should be able to use a before_save with only the first argument, and afterwards when a non-required association forces a 2nd save' do
|
316
|
-
appt = Appointment.sample
|
317
|
-
end
|
318
|
-
|
319
|
-
it 'should be able to recover from the associated value being destroyed' do
|
320
|
-
Appointment.sample
|
321
|
-
User.destroy_all
|
322
|
-
Appointment.sample
|
323
|
-
end
|
324
|
-
end
|
325
|
-
|
326
|
-
describe 'Model with a unique associated attribute' do
|
327
|
-
it 'should ensure that the attribute is unique every time you call create_sample' do
|
328
|
-
video_ids = {}
|
329
|
-
10.times do
|
330
|
-
created = VideoTakedownEvent.create_sample
|
331
|
-
assert_nil video_ids[created.video_id]
|
332
|
-
video_ids[created.video_id] = true
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
describe 'Model with a unique scoped associated attribute' do
|
338
|
-
it 'should create a new instance when you create_sample with the same scope variable as before' do
|
339
|
-
video_fav1 = VideoFavorite.sample
|
340
|
-
video_fav2 = VideoFavorite.create_sample :user => video_fav1.user
|
341
|
-
assert_not_equal video_fav1, video_fav2
|
342
|
-
assert_equal video_fav1.user, video_fav2.user
|
343
|
-
assert_not_equal video_fav1.video, video_fav2.video
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
describe 'Model with a unique string attribute' do
|
348
|
-
it 'should use sequences to ensure that the attribute is unique every time you call create_sample' do
|
349
|
-
ids = []
|
350
|
-
logins = []
|
351
|
-
10.times do
|
352
|
-
custom = User.create_sample
|
353
|
-
assert !ids.include?(custom.id)
|
354
|
-
ids << custom.id
|
355
|
-
assert !logins.include?(custom.login)
|
356
|
-
logins << custom.login
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
it 'should return the same instance if you use the same unique attribute each time' do
|
361
|
-
user1 = User.sample :login => 'john_doe'
|
362
|
-
user2 = User.sample :login => 'john_doe'
|
363
|
-
assert_equal user1, user2
|
364
|
-
end
|
365
|
-
|
366
|
-
it 'should raise an error if you try to make two different instances with the same string value' do
|
367
|
-
User.sample :login => 'john_doe'
|
368
|
-
assert_raise(ActiveRecord::RecordInvalid) do
|
369
|
-
User.sample(:login => 'john_doe', :homepage => 'http://john.doe.com/')
|
370
|
-
end
|
371
|
-
end
|
372
|
-
end
|
373
|
-
|
374
|
-
describe 'Model with a unique time attribute' do
|
375
|
-
it 'should use sequences to ensure that the attribute is unique every time you call create_sample' do
|
376
|
-
times = {}
|
377
|
-
10.times do
|
378
|
-
custom = Appointment.create_sample
|
379
|
-
assert times[custom.start_time].nil?
|
380
|
-
times[custom.start_time] = true
|
381
|
-
end
|
382
|
-
end
|
383
|
-
end
|
384
|
-
|
385
|
-
describe 'Model with email and uniqueness validations on the same field' do
|
386
|
-
it 'should be able to create a value that satisfies both validations' do
|
387
|
-
emails = {}
|
388
|
-
10.times do
|
389
|
-
custom = User.create_sample
|
390
|
-
assert emails[custom.email].nil?
|
391
|
-
emails[custom.email] = true
|
392
|
-
end
|
393
|
-
end
|
394
|
-
end
|
395
|
-
|
396
|
-
describe "Model when its default associated record has been deleted" do
|
397
|
-
it 'should just create a new one' do
|
398
|
-
ep1 = Episode.sample :name => 'funny'
|
399
|
-
ep1.show.destroy
|
400
|
-
ep2 = Episode.sample :name => 'funnier'
|
401
|
-
assert_not_nil ep2.show
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
describe 'Model with a has-many through association' do
|
406
|
-
it 'should not interfere with standard instance assignation' do
|
407
|
-
funny = Tag.sample :tag => 'funny'
|
408
|
-
bp = BlogPost.sample :tags => [funny]
|
409
|
-
assert_equal 1, bp.tags.size
|
410
|
-
assert_equal 'funny', bp.tags.first.tag
|
411
|
-
end
|
412
|
-
|
413
|
-
it 'should use the has-many through association to know that it needs to create a new instance' do
|
414
|
-
BlogPost.destroy_all
|
415
|
-
bp1 = BlogPost.sample
|
416
|
-
assert bp1.tags.empty?
|
417
|
-
funny = Tag.sample :tag => 'funny'
|
418
|
-
bp2 = BlogPost.sample :tags => [funny]
|
419
|
-
assert_equal %w(funny), bp2.tags.map(&:tag)
|
420
|
-
assert_not_equal bp1, bp2
|
421
|
-
assert_not_equal bp1.id, bp2.id
|
422
|
-
sad = Tag.sample :tag => 'sad'
|
423
|
-
bp3 = BlogPost.sample :tags => [sad]
|
424
|
-
assert_equal %w(sad), bp3.tags.map(&:tag)
|
425
|
-
[bp1, bp2].each do |other_bp|
|
426
|
-
assert_not_equal(
|
427
|
-
other_bp, bp3, "matched blog post with tags #{other_bp.tags.inspect}"
|
428
|
-
)
|
429
|
-
assert_not_equal other_bp.id, bp3.id
|
430
|
-
end
|
431
|
-
bp4 = BlogPost.sample :tags => [funny, sad]
|
432
|
-
[bp1, bp2, bp3].each do |other_bp|
|
433
|
-
assert_not_equal(
|
434
|
-
other_bp, bp4, "matched blog post with tags #{other_bp.tags.inspect}"
|
435
|
-
)
|
436
|
-
assert_not_equal other_bp.id, bp4.id
|
437
|
-
end
|
438
|
-
assert_equal 2, bp4.tags.size
|
439
|
-
%w(funny sad).each do |t|
|
440
|
-
assert bp4.tags.map(&:tag).include?(t)
|
441
|
-
end
|
442
|
-
bp5 = BlogPost.sample :tags => []
|
443
|
-
assert bp5.tags.empty?
|
444
|
-
end
|
445
|
-
|
446
|
-
it 'should create a later instance based on an empty has-many through association' do
|
447
|
-
BlogPost.destroy_all
|
448
|
-
funny = Tag.sample :tag => 'funny'
|
449
|
-
bp1 = BlogPost.sample :tags => [funny]
|
450
|
-
bp2 = BlogPost.sample :tags => []
|
451
|
-
assert_not_equal bp1, bp2
|
452
|
-
end
|
453
|
-
|
454
|
-
it "should create a later instance based on another attribute" do
|
455
|
-
BlogPost.destroy_all
|
456
|
-
funny = Tag.sample :tag => 'funny'
|
457
|
-
bp1 = BlogPost.sample :tags => [funny]
|
458
|
-
bp2 = BlogPost.sample :tags => [funny], :title => "really funny"
|
459
|
-
assert_not_equal bp1, bp2
|
460
|
-
end
|
461
|
-
|
462
|
-
it "should not match an earlier instance based on a has-many through array that's a subset of the earlier array" do
|
463
|
-
BlogPost.destroy_all
|
464
|
-
funny = Tag.sample :tag => 'funny'
|
465
|
-
sad = Tag.sample :tag => 'sad'
|
466
|
-
bp1 = BlogPost.sample :tags => [funny, sad]
|
467
|
-
bp2 = BlogPost.sample :tags => [funny]
|
468
|
-
assert_not_equal bp1, bp2
|
469
|
-
end
|
470
|
-
|
471
|
-
it 'should use the has-many through array to determine it already has a matching record' do
|
472
|
-
funny = Tag.sample :tag => 'funny'
|
473
|
-
bp1 = BlogPost.sample :tags => [funny]
|
474
|
-
bp2 = BlogPost.sample :tags => [funny]
|
475
|
-
assert_equal bp1, bp2
|
476
|
-
sad = Tag.sample :tag => 'sad'
|
477
|
-
bp3 = BlogPost.sample :tags => [funny, sad]
|
478
|
-
bp4 = BlogPost.sample :tags => [sad, funny]
|
479
|
-
assert_equal bp3, bp4
|
480
|
-
end
|
481
|
-
|
482
|
-
it 'should make it possible to assign and find as hashes' do
|
483
|
-
bp1 = BlogPost.sample :tags => [{:tag => 'funny'}]
|
484
|
-
assert_equal 1, bp1.tags.size
|
485
|
-
assert_equal 'funny', bp1.tags.first.tag
|
486
|
-
bp2 = BlogPost.sample :tags => [{:tag => 'funny'}]
|
487
|
-
assert_equal bp1, bp2
|
488
|
-
end
|
489
|
-
|
490
|
-
it 'should handle a mix of instances and hashes in the array' do
|
491
|
-
funny = Tag.sample :tag => 'funny'
|
492
|
-
bp = BlogPost.sample :tags => [{:tag => 'sad'}, funny]
|
493
|
-
assert_equal 2, bp.tags.size
|
494
|
-
%w(sad funny).each do |t|
|
495
|
-
assert bp.tags.map(&:tag).include?(t)
|
496
|
-
end
|
497
|
-
end
|
498
|
-
end
|
499
|
-
|
500
|
-
describe 'Model with an invalid default field' do
|
501
|
-
it "should raise an error when a bad configuration is attempted" do
|
502
|
-
assert_raise(RuntimeError) do
|
503
|
-
SampleModels.configure BlogPost do |b|
|
504
|
-
b.title.default ''
|
505
|
-
end
|
506
|
-
end
|
507
|
-
end
|
508
|
-
end
|
509
|
-
|
510
|
-
describe 'Model with a polymorphic belongs-to association' do
|
511
|
-
it 'should fill in that association with any old sample of another domain class' do
|
512
|
-
bookmark = Bookmark.sample
|
513
|
-
assert_not_nil bookmark.bookmarkable
|
514
|
-
assert_equal ActiveRecord::Base, bookmark.bookmarkable.class.superclass
|
515
|
-
assert_not_equal Bookmark, bookmark.bookmarkable.class
|
516
|
-
end
|
517
|
-
|
518
|
-
it 'should let you specify that association' do
|
519
|
-
blog_post = BlogPost.sample
|
520
|
-
bookmark = Bookmark.sample :bookmarkable => blog_post
|
521
|
-
assert_equal blog_post, bookmark.bookmarkable
|
522
|
-
end
|
523
|
-
|
524
|
-
it 'should let you specify that association with other leading associations' do
|
525
|
-
user = User.sample
|
526
|
-
blog_post = BlogPost.sample
|
527
|
-
sub = Subscription.sample user, :subscribable => blog_post
|
528
|
-
assert_equal user, sub.user
|
529
|
-
assert_equal blog_post, sub.subscribable
|
530
|
-
end
|
531
|
-
|
532
|
-
it 'should allow you to constrain which classes will be used by default for the polymorphic type' do
|
533
|
-
sub = Subscription.create_sample
|
534
|
-
assert_equal 'BlogPost', sub.subscribable_type
|
535
|
-
end
|
536
|
-
end
|
537
|
-
|
538
|
-
describe 'Model with a named sample' do
|
539
|
-
it 'should fill the default fields' do
|
540
|
-
bp = BlogPost.sample :funny
|
541
|
-
assert_equal 'Funny haha', bp.title
|
542
|
-
assert_equal 3.0, bp.average_rating
|
543
|
-
end
|
544
|
-
|
545
|
-
it 'should allow you to override fields' do
|
546
|
-
bp = BlogPost.sample :funny, :average_rating => 4.0
|
547
|
-
assert_equal 'Funny haha', bp.title
|
548
|
-
assert_equal 4.0, bp.average_rating
|
549
|
-
end
|
550
|
-
|
551
|
-
it 'should let you create_sample too' do
|
552
|
-
bp1 = BlogPost.sample :funny
|
553
|
-
bp2 = BlogPost.create_sample :funny
|
554
|
-
assert_not_equal bp1, bp2
|
555
|
-
end
|
556
|
-
end
|
557
|
-
|
558
|
-
describe 'Model which validates the presence of a date field' do
|
559
|
-
it 'should assign a value to that field' do
|
560
|
-
episode = Episode.sample
|
561
|
-
assert episode.original_air_date.is_a?(Date)
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
describe 'Model with a required accessor' do
|
566
|
-
it 'should assign that accessor' do
|
567
|
-
user_with_password = UserWithPassword.sample
|
568
|
-
assert user_with_password.password.present?
|
569
|
-
end
|
570
|
-
end
|
571
|
-
|
572
|
-
describe 'Model which validates the presence and uniqueness of a string field' do
|
573
|
-
it "should not get tripped up by a pre-existing record" do
|
574
|
-
User2.create!(:login => 'login 1')
|
575
|
-
User2.create_sample
|
576
|
-
end
|
577
|
-
end
|
578
|
-
|
579
|
-
describe 'Model that validates uniqueness with an allow-nil constraint' do
|
580
|
-
it 'should let you configure the default to be nil' do
|
581
|
-
User.sample
|
582
|
-
user = User.create_sample
|
583
|
-
assert_nil user.external_user
|
584
|
-
assert_nil user.external_user_id
|
585
|
-
end
|
586
|
-
end
|
data/test/test_sample_models.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require File.expand_path(File.dirname(__FILE__)) + "/../spec_or_test/setup"
|
3
|
-
|
4
|
-
# auto-convert specs into tests whoooooha
|
5
|
-
@@test_class_sequence = 1
|
6
|
-
|
7
|
-
def describe(desc_name, &block)
|
8
|
-
klass = Class.new Test::Unit::TestCase
|
9
|
-
class_name = "Test#{desc_name.gsub(/\W/, '_').camelize}"
|
10
|
-
Object.const_set class_name.to_sym, klass
|
11
|
-
@@test_class_sequence += 1
|
12
|
-
def klass.it(it_name, &block)
|
13
|
-
test_name = "test_" + it_name.gsub(/ /, '_')
|
14
|
-
if instance_methods.include?(test_name)
|
15
|
-
raise "redundant describe #{it_name.inspect}"
|
16
|
-
end
|
17
|
-
self.send(:define_method, test_name, &block)
|
18
|
-
end
|
19
|
-
def klass.before(before_name, &block)
|
20
|
-
self.send(:define_method, :setup, &block)
|
21
|
-
end
|
22
|
-
klass.instance_eval &block
|
23
|
-
end
|
24
|
-
|
25
|
-
initialize_db
|
26
|
-
|
27
|
-
require File.expand_path(File.dirname(__FILE__)) + "/../spec_or_test/specs_or_test_cases"
|
28
|
-
|
data/vendor/ar_query/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2008 [name of plugin creator]
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/vendor/ar_query/README
DELETED
File without changes
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
|
3
|
-
Gem::Specification.new do |s|
|
4
|
-
s.name = 'ar_query'
|
5
|
-
s.version = '0.0.1'
|
6
|
-
s.date = '2009-02-19'
|
7
|
-
s.author = 'Francis Hwang'
|
8
|
-
s.description = 'A utility class for building options for ActiveRecord.find.'
|
9
|
-
s.summary = 'A utility class for building options for ActiveRecord.find.'
|
10
|
-
s.email = 'sera@fhwang.net'
|
11
|
-
s.homepage = 'http://github.com/fhwang/ar_query'
|
12
|
-
s.files = FileList[
|
13
|
-
'lib/*.rb', 'MIT-LICENSE', 'README', '*.rb', 'spec/*.rb',
|
14
|
-
'tasks/*.rake'
|
15
|
-
].to_a
|
16
|
-
end
|
data/vendor/ar_query/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Include hook code here
|
data/vendor/ar_query/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Install hook code here
|