-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_factory_spec.rb
568 lines (495 loc) · 14.8 KB
/
model_factory_spec.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
require_relative 'spec_helper'
describe "factory" do
before :all do
module FactoryTest
class InitializeModel < Watirmark::Model::Factory
keywords :username, :password
end
end
end
specify "set a value on instantiation" do
login = FactoryTest::InitializeModel.new(:username => 'username', :password => 'password')
login.username.should == 'username'
login.password.should == 'password'
end
specify "set a value after initialized" do
login = FactoryTest::InitializeModel.new
login.username.should be_nil
login.password.should be_nil
login.username = 'username'
login.password = 'password'
login.username.should == 'username'
login.password.should == 'password'
end
# this is mostly for legacy :(
specify "should be able to act like an openstruct" do
login = FactoryTest::InitializeModel.new
login.foobar.should be_nil
login.foobar = 'test'
login.foobar.should == 'test'
end
specify 'to_h' do
login = FactoryTest::InitializeModel.new(username: 'foo', password: 'bar')
login.to_h.should == {:username=>"foo", :password=>"bar"}
end
end
describe "#update" do
before :all do
module FactoryTest
class UpdateModel < Watirmark::Model::Factory
keywords :username, :password
end
end
end
specify "model update should create methods if not in model" do
login = FactoryTest::UpdateModel.new
login.update(:foobar=>1)
login.foobar.should == 1
login.foobar = 'test'
login.foobar.should == 'test'
end
specify "keywords should not bleed across instances for defined methods" do
first = FactoryTest::UpdateModel.new
first.username = 'username'
first.username.should == 'username'
second = FactoryTest::UpdateModel.new
second.username.should be_nil
end
specify "keywords should not bleed across instances for auto-created methods" do
first = FactoryTest::UpdateModel.new
first.update(:foobar=>1)
second = FactoryTest::UpdateModel.new
second.respond_to?(:foobar).should be_false
second.respond_to?(:foobar).should be_false
end
specify "model update should remove empty keys" do
keys = FactoryTest::UpdateModel.new
lambda{keys.update(':'=>'') }.should_not raise_error NameError
lambda{keys.update(nil=>'') }.should_not raise_error NameError
lambda{keys.update(' '=>'') }.should_not raise_error NameError
lambda{keys.update(' '.to_sym=>'') }.should_not raise_error NameError
end
end
describe "defaults" do
before :all do
module FactoryTest
class DefaultModel < Watirmark::Model::Factory
keywords :first_name, :last_name, :middle_name, :nickname, :id, :desc
defaults do
first_name { 'my_first_name' }
last_name { 'my_last_name' }
middle_name { "#{model_name} middle_name".strip }
id { uuid }
desc { 'some description' }
end
end
end
end
specify "retrieve a default proc setting" do
m = FactoryTest::DefaultModel.new
m.middle_name.should == 'middle_name'
m.model_name = 'foo'
m.middle_name.should == 'foo middle_name'
end
specify "update a default setting" do
m = FactoryTest::DefaultModel.new
m.first_name.should == 'my_first_name'
m.first_name = 'fred'
m.first_name.should == 'fred'
end
specify "retrieve a default setting" do
FactoryTest::DefaultModel.new.first_name.should == 'my_first_name'
end
specify "workaround for desc as a default when run from rake" do
FactoryTest::DefaultModel.new.desc.should == 'some description'
end
specify "override default settings on instantiation" do
module FactoryTest
class ModelWithDefaults < Watirmark::Model::Factory
keywords :foo, :bar
defaults do
foo { "hello from proc" }
end
end
end
m = FactoryTest::ModelWithDefaults.new :foo => 'hello init'
m.foo.should == 'hello init'
end
specify "defaults can reference each other" do
module FactoryTest
class DefaultReference < Watirmark::Model::Factory
keywords :name, :sort_name
defaults do
name { "name" }
sort_name { name }
end
end
model = DefaultReference.new
model.name.should == 'name'
model.sort_name.should == 'name'
end
end
specify "should raise error unless a proc is defined" do
lambda {
module FactoryTest
class Test < Watirmark::Model::Factory
keywords :first_name, :last_name, :middle_name, :nickname, :id
defaults do
first_name 'my_first_name'
end
end
end
FactoryTest::Test.new
}.should raise_error ArgumentError
end
end
describe "model name" do
before :all do
class ModelName < Watirmark::Model::Factory
keywords :middle_name
defaults do
middle_name { "#@model_name middle_name".strip }
end
end
end
specify "can set the models name" do
m = ModelName.new
m.model_name = 'my_model'
m.model_name.should == 'my_model'
end
specify "can set the models at initialize (used by transforms)" do
m = ModelName.new(:model_name => 'my_model')
m.model_name.should == 'my_model'
end
specify "setting the models name changes the defaults" do
m = ModelName.new
m.model_name = 'my_model'
m.middle_name.should =~ /^my_model/
end
end
describe "parents" do
specify "ask for a parent" do
module FactoryTest
class ChildModel < Watirmark::Model::Factory
keywords :name, :value
defaults do
name { parent.name }
end
end
class ParentModel < Watirmark::Model::Factory
keywords :name
model ChildModel
defaults do
name { 'a' }
end
end
end
model = FactoryTest::ParentModel.new
model.child.parent.should == model
model.child.parent.name.should == 'a'
model.child.name.should == 'a'
end
end
describe "children" do
before :all do
module FactoryTest
class Camelize < Watirmark::Model::Factory
keywords :first_name, :last_name
end
class Login < Watirmark::Model::Factory
keywords :username, :password
defaults do
username { 'username' }
password { 'password' }
end
end
class User < Watirmark::Model::Factory
keywords :first_name, :last_name
model Login, Camelize
defaults do
first_name { 'my_first_name' }
last_name { 'my_last_name' }
end
end
class Donor < Watirmark::Model::Factory
keywords :credit_card
model User
end
class SDP < Watirmark::Model::Factory
keywords :name, :value
end
class Config < Watirmark::Model::Factory
keywords :name
end
end
end
specify "should be able to see the models" do
model = FactoryTest::User.new
model.login.username.should == 'username'
end
specify "should be able to see nested models" do
model = FactoryTest::Donor.new
model.user.login.username.should == 'username'
model.users.first.login.username.should == 'username'
end
specify "multiple models of the same class should form a collection" do
model = FactoryTest::Config.new
model.add_model FactoryTest::SDP.new(:name => 'a', :value => 1)
model.add_model FactoryTest::SDP.new(:name => 'b', :value => 2)
model.sdp.name.should == 'a'
model.sdps.size.should == 2
model.sdps.first.name.should == 'a'
model.sdps.last.name.should == 'b'
end
specify "should raise an exception if the model is not a constant" do
lambda {
class Test < Watirmark::Model::Factory
keywords :name
model :FactorySDP.new
end
}.should raise_error
end
specify "should always instantiate NEW instances of sub-models" do
module FactoryTest
class Item < Watirmark::Model::Factory
keywords :name, :sort_name
defaults do
name { "name" }
end
end
class Container < Watirmark::Model::Factory
keywords :name, :sort_name
search_term { name }
model Item
end
end
c = FactoryTest::Container.new
c.item.name.should == 'name'
c.item.name = 'foo'
c.item.name.should == 'foo'
d = FactoryTest::Container.new
d.item.name.should_not == 'foo'
end
specify "models containing models in modules should not break model_class_name" do
module Foo
module Bar
class Login < Watirmark::Model::Factory
keywords :username, :password
defaults do
username { 'username' }
password { 'password' }
end
end
class User < Watirmark::Model::Factory
keywords :first_name, :last_name
model Login
defaults do
first_name { 'my_first_name' }
last_name { 'my_last_name' }
end
end
end
end
model = Foo::Bar::User.new
model.login.username.should == 'username'
end
end
describe "search_term" do
specify "is a string" do
module FactoryTest
class SearchIsString < Watirmark::Model::Factory
keywords :name, :sort_name
search_term { "name" }
defaults do
name { "name" }
end
end
end
model = FactoryTest::SearchIsString.new
model.search_term.should == 'name'
end
specify "matches another default" do
module FactoryTest
class SearchIsDefault < Watirmark::Model::Factory
keywords :name, :sort_name
search_term { name }
defaults do
name { "name" }
end
end
end
model = FactoryTest::SearchIsDefault.new
model.search_term.should == 'name'
end
specify "is found in a parent" do
module FactoryTest
class SearchChild < Watirmark::Model::Factory
keywords :name, :sort_name
end
class SearchParent < Watirmark::Model::Factory
keywords :name, :sort_name
search_term { name }
model SearchChild
defaults do
name { "name" }
end
end
end
child = FactoryTest::SearchChild.new
child.search_term.should be_nil
parent = FactoryTest::SearchParent.new
parent.search_term.should == 'name'
parent.search_child.search_term.should == 'name'
end
end
describe "find" do
before :all do
module FactoryTest
class FirstModel < Watirmark::Model::Factory
keywords :x
end
class SecondModel < Watirmark::Model::Factory
keywords :x
end
class NoAddedModels < Watirmark::Model::Factory
keywords :x
end
class SingleModel < Watirmark::Model::Factory
keywords :x
end
class MultipleModels < Watirmark::Model::Factory
keywords :x
end
end
@first_model = FactoryTest::FirstModel.new
@second_model = FactoryTest::SecondModel.new
@no_added_models = FactoryTest::NoAddedModels.new
@single_model = FactoryTest::SingleModel.new
@single_model.add_model @first_model
@multiple_models = FactoryTest::MultipleModels.new
@multiple_models.add_model @first_model
@multiple_models.add_model @second_model
end
specify 'should find itself' do
@no_added_models.find(FactoryTest::NoAddedModels).should == @no_added_models
@single_model.find(FactoryTest::SingleModel).should == @single_model
@multiple_models.find(FactoryTest::MultipleModels).should == @multiple_models
end
specify 'should be able to see a sub_model' do
@single_model.find(FactoryTest::FirstModel).should == @first_model
@multiple_models.find(FactoryTest::FirstModel).should == @first_model
@multiple_models.find(FactoryTest::SecondModel).should == @second_model
end
specify 'should be return nil when no model is found' do
@no_added_models.find(FactoryTest::FirstModel).should be_nil
@single_model.find(FactoryTest::NoAddedModels).should be_nil
@multiple_models.find(FactoryTest::NoAddedModels).should be_nil
end
end
describe "methods in Enumerable should not collide with model defaults" do
it "#zip" do
module FactoryTest
class ZipModel < Watirmark::Model::Factory
keywords :zip
defaults do
zip { 78732 }
end
end
end
FactoryTest::ZipModel.new.zip.should == 78732
end
it "#zip not in model" do
module FactoryTest
class NoZipModel < Watirmark::Model::Factory
keywords :foo
defaults do
end
end
end
FactoryTest::NoZipModel.new.respond_to?(:zip).should_not be_true
end
end
describe "keywords" do
before :all do
module FactoryTest
class Element
attr_accessor :value
def initialize(x)
@value = x
end
end
class SomeView < Page
keyword(:first_name) { Element.new :a }
keyword(:middle_name) { Element.new :b }
keyword(:last_name) { Element.new :c }
end
class SomeModel < Watirmark::Model::Factory
keywords SomeView.keywords
defaults do
first_name { "First" }
middle_name { "Middle" }
last_name { "Last #{uuid}" }
end
end
class SomeOtherModel < Watirmark::Model::Factory
keywords SomeView.keywords
defaults do
first_name { "First" }
middle_name { "Middle" }
last_name { "Last #{uuid}" }
end
end
end
end
specify "should add unpacked keywords as keywords" do
a = FactoryTest::SomeModel.new
a.middle_name.should == "Middle"
a.first_name.should == "First"
a.last_name.should include "Last"
end
specify "keywords can be specified without the asterisk" do
a = FactoryTest::SomeOtherModel.new
a.middle_name.should == "Middle"
a.first_name.should == "First"
a.last_name.should include "Last"
end
specify "should be able to list keywords for a model" do
FactoryTest::SomeModel.new.keywords.sort.should == [:first_name, :middle_name, :last_name].sort
end
end
describe "subclassing" do
before :all do
module Watirmark::Model
trait :some_trait do
full_name { "full_name" }
end
end
module FactoryTest
class BaseModel < Watirmark::Model::Factory
keywords :first_name, :last_name, :full_name
defaults do
first_name { 'base_first_name' }
last_name { 'base_last_name' }
end
traits :some_trait
end
class SubModel < BaseModel
defaults do
first_name { 'sub_first_name' }
last_name { 'sub_last_name' }
end
end
class NoDefaultModel < BaseModel
end
end
end
specify "submodel should be able to inherit keywords" do
FactoryTest::SubModel.new.first_name.should == 'sub_first_name'
end
specify "submodel should be able to inherit defaults" do
FactoryTest::NoDefaultModel.new.first_name.should == 'base_first_name'
end
specify "submodel should be able to inherit defaults" do
FactoryTest::NoDefaultModel.new.full_name.should == 'full_name'
end
end