/
conducer_test.rb
760 lines (619 loc) · 17.8 KB
/
conducer_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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# -*- encoding : utf-8 -*-
require_relative 'test_helper'
class Dao::ConducerTest < Dao::TestCase
##
#
context :teh_ctor do
#
test 'conducers have a POLS .new method' do
[
{:key => :val, :array => [0,1,2]},
{}
].each do |attributes|
c = new_conducer(attributes)
assert{ c.attributes =~ attributes }
end
end
#
test 'models passed to .new are automatically tracked' do
user = User.new
post = Post.new
comment = Comment.new
params = {}
args = [comment, post, user, params]
c = new_conducer(*args)
assert{ c.models == [comment, post, user] }
assert{ c.model == comment }
assert{ c.model == c.conduces }
end
#
test 'that the conduced model can be declared at the class level' do
user = User.new
post = Post.new
comment = Comment.new
params = {}
args = [comment, post, user, params]
c = new_conducer(*args){ conduces User }
assert{ c.models == [comment, post, user] }
assert{ c.model == user }
assert{ c.model == c.conduces }
end
#
test 'that the conduced model can be declared at the instance level' do
user = User.new
post = Post.new
comment = Comment.new
params = {}
args = [comment, post, user, params]
c = new_conducer(*args)
c.conduces(user)
assert{ c.models == [user, comment, post] }
assert{ c.model == user }
assert{ c.model == c.conduces }
end
end
##
#
context :teh_default_initialize do
#
test 'that the last mode determines the lifecycle state when a models are passed in' do
user = User.new
post = Post.new
comment = Comment.new
params = {}
args = [comment, post, user, params]
c = new_conducer(*args)
%w( new_record? persisted? destroyed? ).each do |state|
assert{ comment.send(state) == c.send(state) }
end
comment.new_record = false
comment.persisted = true
assert{ c.new_record? == false }
assert{ c.persisted == true }
end
#
test 'that passed in models/params are sanely ker-sploded onto the attributes' do
user = User.new :k => 1
post = Post.new :k => 2
comment = Comment.new :k => 3, :x => 4
params = {:x => 5}
args = [comment, post, user, params]
c = new_conducer(*args)
assert{ c.attributes[:user] =~ {:k => 1} }
assert{ c.instance_variable_get('@user') == user }
assert{ c.attributes[:post] =~ {:k => 2} }
assert{ c.instance_variable_get('@post') == post }
expected = Map.new
expected.add :user => user.attributes
expected.add :post => post.attributes
expected.add comment.attributes
expected.add params
assert{ c.attributes =~ expected }
assert{ c.instance_variable_get('@comment') == comment }
end
#
test 'that .new specialises based on current action' do
conducer_class =
new_conducer_class do
def initialize_for_new
attributes.update(:new => Time.now)
end
def initialize_for_create
attributes.update(:create => Time.now)
end
def initialize_for_edit
attributes.update(:edit => Time.now)
end
end
post = Post.new
%w( new create edit ).each do |action|
c = conducer_class.for(action, post)
assert{ c.action == action }
assert{ c.respond_to?("initialize_for_#{ action }") }
assert{ c.attributes[action].is_a?(Time) }
assert{ c.attributes.size == 1 }
end
end
#
test 'that conducers can build a highly specialized .new method based on action' do
c =
new_conducer_class do
def initialize(a, b, c, params)
@a, @b, @c = a, b, c
update_attributes(
:foo => :bar
)
case action
when 'new'
update_attributes(:action => :new)
when 'edit'
update_attributes(:action => :edit)
else
update_attributes(:action => nil)
end
update_attributes(params)
end
end
params = {:key => :val}
%w( new edit ).each do |action|
o = assert{ c.for(action, :a, :b, :c, params) }
assert{ o.action == action }
assert{ o.key == :val }
assert{ o.foo == :bar }
%w( a b c ).each{|var| assert{ o.instance_variable_get("@#{ var }") == var.to_sym } }
end
end
end
#
test 'that conducers *fold* in attributes' do
c = new_conducer
assert{ c.update_attributes :key => {:a => :b} }
assert{ c.update_attributes :key => {:nested => {:a => :b}} }
assert{ c.attributes =~ {:key => {:a => :b, :nested => {:a => :b}}} }
end
##
#
context :teh_default_save do
#
test 'is sane and based solely on the last model' do
user = User.new
post = Post.new
comment = Comment.new
params = {:text => 'hai!', :user => {:params => 'are ignored'}, :post => {:params => 'are ignored'}}
args = [comment, post, user, params]
c = new_conducer(*args)
assert{ comment[:text].nil? }
assert{ c.save }
assert{ comment.text == 'hai!' }
assert{ comment[:user].nil? }
assert{ comment[:post].nil? }
end
#
test 'halts when the conducer is invalid with errors' do
conducer_class =
new_conducer_class do
validates_presence_of(:foo)
end
c = conducer_class.new
assert{ !c.save }
assert{ !c.valid? }
assert{ !c.errors.empty? }
end
#
test 'halts when the model is invalid and relays errors' do
post = Post.new
post.errors[:foo] = 'is fucked'
c = new_conducer(post)
assert{ !c.save }
assert{ c.errors[:foo] == Array(post.errors[:foo]) }
end
#
test 'raises a validation error on #save!' do
post = Post.new
post.errors[:foo] = 'is fucked'
c = new_conducer(post)
error = assert{ begin; c.save!; rescue Object => e; e; end; }
assert{ error.errors == c.errors }
assert{ error.errors[:foo] = Array(post.errors[:foo]) }
assert{ c.errors[:foo] = Array(post.errors[:foo]) }
end
end
##
#
context :validations do
#
test 'that simple validations/errors work' do
c =
assert{
new_foo_conducer_class do
validates_presence_of :bar
validates_presence_of :foo, :bar
end
}
o = assert{ c.new }
assert{ !o.valid? }
assert{ Array(o.errors.get(:bar)).size == 1 }
assert{ Array(o.errors.get(:foo, :bar)).size == 1 }
o.attributes.set :foo, :bar, 42
assert{ !o.valid? }
assert{ Array(o.errors.get(:bar)).size == 1 }
assert{ Array(o.errors.get(:foo, :bar)).size == 0 }
assert{ Array(o.errors.get(:foo, :bar)).empty? }
end
#
test 'that validations are evaluated in the context of the object' do
c =
assert{
new_foo_conducer_class do
klass = self
validates(:a){ klass == self.class }
validates(:b){ value != 42.0 }
def value() 42 end
end
}
o = assert{ c.new }
assert{ !o.valid? }
assert{ o.errors.get(:a).empty? }
assert{ !o.errors.get(:b).empty? }
end
#
test 'that validates_each werks at the class and instance level' do
conducer_class =
new_conducer_class do
validates_each :a do |item|
validated.push(item)
true
end
def save
validates_each :b do |item|
validated.push(item)
true
end
return valid?
end
def validated
@validated ||= []
end
end
a = %w( a b c )
b = %w( 1 2 3 )
c = assert{ conducer_class.new(:a => a, :b => b) }
assert{ c.run_validations }
assert{ c.validated == %w( a b c ) }
c = conducer_class.new(:a => a, :b => b)
assert{ c.save }
assert{ c.validated == %w( a b c 1 2 3 ) }
end
end
##
#
context :forms do
#
test 'that basic form helpers work' do
c =
assert{
new_foo_conducer_class do
validates_presence_of :bar
end
}
o = assert{ c.new }
assert{ !o.valid? } # make validations run...
assert{ o.form }
assert{ o.form.input(:foo) =~ /\<input/ }
assert{ o.form.input(:foo) !~ /errors/ }
assert{ o.form.textarea(:bar) =~ /\<textarea/ }
assert{ o.form.textarea(:bar) =~ /errors/ }
end
end
##
#
context :class_methods do
#
test 'that base classes can be constructed and named' do
new_foo_conducer_class()
end
#
test '.new' do
c = assert{ new_foo_conducer_class }
controller = assert{ Dao.mock_controller }
check = proc do |args|
params = args.detect{|arg| arg.is_a?(Hash)} || {}
o = assert{ c.new(*args) }
assert{ o.is_a?(Dao::Conducer) }
assert{ o.attributes =~ params }
assert{ o.controller.is_a?(ActionController::Base) }
end
[
{},
{:k => :v},
nil,
].each do |params|
check[ [params] ]
check[ [controller, params] ]
check[ [params, controller] ]
end
end
#
test '.model_name' do
c = assert{ new_foo_conducer_class }
assert{ c.model_name }
o = assert{ c.new }
assert{ o.model_name }
end
end
##
#
context :instance_methods do
test '#id' do
[:_id, :id].each do |id_key|
o = assert{ new_foo_conducer() }
assert{ o.id.nil? }
o.attributes.update(id_key => 42)
assert{ o.id==42 }
assert{ o.id = nil; true }
assert{ !o.id }
assert{ o.attributes[:id].nil? }
assert{ o.attributes[:_id].nil? }
end
end
test '#to_param' do
o = assert{ new_foo_conducer() }
assert{ o.to_param.nil? }
o.id = 42
assert{ o.to_param }
end
test '#errors' do
o = assert{ new_foo_conducer() }
assert{ o.errors.respond_to?(:[]) }
end
=begin
test 'that conducers can register handlers for setting deeply nested attributes' do
c =
new_conducer_class do
def _update_attributes(attributes = {})
attributes.each do |key, value|
case Array(key).join('.')
when 'user.first_name'
set(key, value.to_s.upcase)
return true
else
return false
end
end
end
end
o = assert{ c.new :user => {:first_name => 'ara', :last_name => 'howard'} }
assert{ o.user.first_name == 'ARA' }
assert{ o.user.last_name == 'howard' }
o = assert{ c.new :name => 'ara howard' }
assert{ o.attributes.get(:name) == 'ara howard' }
end
=end
end
##
#
context :teh_mount do
#
test 'that mounted objects can be declared at the class level' do
conducer_class =
new_conducer_class do
mount Dao::Upload, :a, :b, :placeholder => '/images/foo.jpg'
end
assert{ !conducer_class.mounted.empty? }
c = conducer_class.new
assert{ c.mounted.first.is_a?(Dao::Upload) }
assert{ c.mounted.first._key.join('.') == 'a.b' }
assert{ c.mounted.first._value.nil? }
end
#
test 'that mounted objects replace their location in attributes' do
conducer_class =
new_conducer_class do
mount Dao::Upload, :a, :b, :placeholder => '/images/foo.jpg'
end
path = __FILE__
up = Upload.new(path)
c = conducer_class.new( :a => {:b => {:file => up}} )
upload = assert{ c.get(:a, :b) }
assert{ upload.is_a?(Dao::Upload) }
assert{ test(?f, upload._value) }
end
#
test 'that the default save uses the mounted _value and _clears it' do
begin
$pry=true
conducer_class =
new_conducer_class do
mount Dao::Upload, :up, :placeholder => '/images/foo.jpg'
end
path = File.join(File.dirname(__FILE__), 'data/han-solo.jpg')
assert{ test(?s, path) }
_up = Upload.new(path)
comment = Comment.new
c = conducer_class.new( comment, :up => {:file => Upload.new(path)} )
upload = assert{ c.get(:up) }
assert{ upload.is_a?(Dao::Upload) }
assert{ test(?f, upload.path) }
assert{ File.basename(upload.path) == File.basename(path) }
assert{ IO.read(upload.path) == IO.read(path) }
assert{ c.save }
_value_was_relayed = assert{ comment.attributes[:up] == upload._value }
_value_was_cleared = assert{ !test(?f, upload.path) }
assert{ test(?s, path) }
ensure
$pry=false
end
end
end
##
#
context :collections do
test 'can be created from page-y blessed arrays' do
paginated = Paginated[Post.new, Post.new, Post.new]
paginated.limit = 42
paginated.offset = 42.0
paginated.total_count = 420
conducer_class = new_conducer_class
conducer_class.collection_for(paginated)
collection = assert{ conducer_class.collection_for(paginated) }
assert{ collection.models == paginated }
assert{ collection.limit == 42 }
assert{ collection.offset == 42.0 }
assert{ collection.total_count == 420 }
user = User.new
collection = assert{ conducer_class.collection_for(paginated){|model| conducer_class.for(:show, user, model)} }
assert{ collection.all?{|conducer| conducer.action == :show} }
assert{ collection.all?{|conducer| conducer.models.first==user} }
assert{ collection.all?{|conducer| conducer.models.last.is_a?(Post)} }
end
end
##
#
context :callbacks do
test 'can be added lazily in an ad-hoc fashion' do
callbacks = []
conducer_class =
new_conducer_class do
before_initialize do
callbacks.push(:before_initialize)
end
after_initialize do
callbacks.push(:after_initialize)
end
define_method(:foobar){ 42 }
before :foobar do
callbacks.push(:before_foobar)
end
after :foobar do
callbacks.push(:after_foobar)
end
end
c = assert{ conducer_class.new }
assert{ callbacks == [:before_initialize, :after_initialize] }
assert{ c.foobar; true }
assert{ callbacks == [:before_initialize, :after_initialize, :before_foobar, :after_foobar] }
end
end
protected
def new_foo_conducer_class(&block)
const = :FooConducer
Object.send(:remove_const, const) if Object.send(:const_defined?, const)
name = const.to_s
c = assert{ Class.new(Dao::Conducer){ self.name = name } }
Object.send(:const_set, const, c)
assert{ c.name == 'FooConducer' }
assert{ c.model_name == 'Foo' }
assert{ c.table_name == 'foos' && c.collection_name == 'foos' }
assert{ c.class_eval(&block); true } if block
c
end
alias_method :new_conducer_class, :new_foo_conducer_class
def new_foo_conducer(*args, &block)
assert{ new_foo_conducer_class(&block).new(*args) }
end
alias_method :new_conducer, :new_foo_conducer
def setup
$db = Dao::Db.new(:path => 'test/db.yml')
Dao::Db.instance = $db
collection = $db['foos']
%w( a b c ).each do |name|
collection.save(
:name => name, :created_at => Time.now.to_f, :a => %w( x y z ), :h => {:k => :v}
)
end
end
def teardown
$db = Dao::Db.new(:path => 'test/db.yml')
$db.rm_f
end
def db
$db
end
def collection
$db[:foos]
end
class Mounted
def Mounted.mount(*args, &block)
new(*args, &block)
end
def _set
end
def _key
end
def _value
end
def _clear
end
end
class Upload < StringIO
attr_accessor :path
def initialize(path)
super(IO.read(@path = path))
end
def dup
self.class.new(path)
end
end
class Model
class << self
def model_name
name = self.name.split(/::/).last
ActiveModel::Name.new(Map[:name, name])
end
end
{
:new_record => true,
:persisted => false,
:destroyed => false,
}.each do |state, value|
class_eval <<-__
attr_writer :#{ state }
def #{ state }
@#{ state } = #{ value } unless defined?(@#{ state })
@#{ state }
end
def #{ state }?
#{ state }
end
__
end
def initialize(attributes = {})
self.attributes.update(attributes)
end
def attributes
@attributes ||= Map.new
end
def [](key)
attributes[key]
end
def []=(key, val)
attributes[key] = val
end
def update_attributes(hash = {})
hash.each{|k,v| attributes[k] = v }
end
def method_missing(method, *args, &block)
re = /^([^=!?]+)([=!?])?$/imox
_matched, key, suffix = re.match(method.to_s).to_a
case suffix
when '=' then attributes.set(key, args.first)
when '!' then attributes.set(key, args.size > 0 ? args.first : true)
when '?' then attributes.has?(key)
else
attributes.has?(key) ? attributes.get(key) : super
end
end
def inspect(*args, &block)
"#{ self.class.name }(#{ attributes.inspect.strip })"
end
def errors
@errors ||= Map.new
end
def valid?
errors.empty?
end
def save
return false unless valid?
self.new_record = false
self.persisted = true
return true
end
def destroy
true
ensure
self.new_record = false
self.destroyed = true
end
end
class Paginated < ::Array
attr_accessor :limit
attr_accessor :offset
attr_accessor :total_count
end
class User < Model
end
class Post < Model
end
class Comment < Model
end
end