thingtank 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.md +329 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/examples/bear_julius.rb +50 -0
- data/examples/first_marriage.rb +81 -0
- data/examples/immortal_julius.rb +25 -0
- data/examples/marriage_improvement.rb +120 -0
- data/examples/second_marriage.rb +78 -0
- data/lib/couchrest/extensions/view.rb +17 -0
- data/lib/thingtank/callbacks.rb +32 -0
- data/lib/thingtank/dependencies.rb +94 -0
- data/lib/thingtank/fakebase.rb +66 -0
- data/lib/thingtank/force_update.rb +24 -0
- data/lib/thingtank/instance_methods.rb +45 -0
- data/lib/thingtank/role.rb +110 -0
- data/lib/thingtank/role_handling.rb +198 -0
- data/lib/thingtank/shared_methods.rb +63 -0
- data/lib/thingtank/shortcuts.rb +36 -0
- data/lib/thingtank/thingtank.rb +21 -0
- data/lib/thingtank.rb +27 -0
- data/test/examples/test_bear_julius.rb +15 -0
- data/test/examples/test_first_marriage.rb +20 -0
- data/test/examples/test_immortal_julius.rb +16 -0
- data/test/examples/test_marriage_improvement.rb +22 -0
- data/test/examples/test_second_marriage.rb +18 -0
- data/test/helper.rb +118 -0
- data/test/test_fakebase.rb +389 -0
- data/test/test_thingtank.rb +282 -0
- data/test/test_views.rb +80 -0
- metadata +172 -0
@@ -0,0 +1,389 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "should role a CouchRest::Database" do
|
4
|
+
|
5
|
+
it "create a couchrest doc" do
|
6
|
+
|
7
|
+
doc = CouchRest::Document.new("_id" => 'test', "_rev" => 'hiho')
|
8
|
+
assert !doc.new?
|
9
|
+
end
|
10
|
+
|
11
|
+
it "create an couchrest model" do
|
12
|
+
|
13
|
+
doc = Testmodel.new({"_id" => 'test', "_rev" => 'hiho'}, :directly_set_attributes => true)
|
14
|
+
assert !doc.new?
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should role" do
|
18
|
+
doc = create :a => 'A', :b => 'B'
|
19
|
+
role = doc.to_role(Check)
|
20
|
+
assert_equal "pong", role.ping
|
21
|
+
assert_equal "A", role.a
|
22
|
+
assert_equal "A", role["a"]
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
it "should update a role" do
|
27
|
+
doc = create :a => 'A', :b => 'B', :roles => ['Check']
|
28
|
+
role = doc.to_role(Check)
|
29
|
+
assert_equal false, role.new?
|
30
|
+
role.c = "C"
|
31
|
+
role.save
|
32
|
+
doc.reload
|
33
|
+
assert_equal "ThingTank", doc["type"]
|
34
|
+
assert_equal "B", doc["b"]
|
35
|
+
assert_equal "C", doc["c"]
|
36
|
+
assert_equal true, doc["check_saved"]
|
37
|
+
assert_equal true, doc["check_updated"]
|
38
|
+
assert doc["check_created"].nil?
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should create a role, based on doc that did not have this role" do
|
42
|
+
doc = create :a => 'A', :b => 'B'
|
43
|
+
role = doc.add_role(Check)
|
44
|
+
assert role.new?, "role should be new when freshly added"
|
45
|
+
role.c = "C"
|
46
|
+
doc.save
|
47
|
+
doc.reload
|
48
|
+
assert_equal "ThingTank", doc["type"]
|
49
|
+
assert_equal "B", doc["b"]
|
50
|
+
assert_equal "C", doc["c"]
|
51
|
+
assert_equal true, doc["check_saved"]
|
52
|
+
assert_equal true, doc["check_created"]
|
53
|
+
assert doc["check_updated"].nil?
|
54
|
+
|
55
|
+
doc.reload
|
56
|
+
role = doc.to_role(Check)
|
57
|
+
assert_equal false, role.new?
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should create a role, based on a new doc" do
|
61
|
+
doc = new :a => 'A', :b => 'B', :roles => ['Check']
|
62
|
+
role = doc.to_role(Check)
|
63
|
+
assert role.new?
|
64
|
+
role.c = "C"
|
65
|
+
role.save
|
66
|
+
assert_equal false, role.changed?
|
67
|
+
doc.reload
|
68
|
+
assert_equal "ThingTank", doc["type"]
|
69
|
+
assert_equal "B", doc["b"]
|
70
|
+
assert_equal "C", doc["c"]
|
71
|
+
assert_equal true, doc["check_saved"]
|
72
|
+
assert_equal true, doc["check_created"]
|
73
|
+
assert doc["check_updated"].nil?
|
74
|
+
|
75
|
+
doc.reload
|
76
|
+
role = doc.to_role(Check)
|
77
|
+
assert_equal false, role.new?
|
78
|
+
assert_equal false, role.changed?
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should not be valid if the requirements aren't met" do
|
82
|
+
doc = new
|
83
|
+
role = doc.to_role(Check)
|
84
|
+
assert !role.valid?
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should be able to access other roles and autosave the accessed roles if they are not saved by themselves" do
|
88
|
+
doc = new :a => 'A'
|
89
|
+
role = doc.to_role(Check)
|
90
|
+
role.access_other_role
|
91
|
+
doc.save
|
92
|
+
doc.reload
|
93
|
+
assert_equal "I was here", doc["checker1"]
|
94
|
+
assert_equal true, doc["roles"].include?(Checker.to_s)
|
95
|
+
assert_equal true, doc["roles"].include?(Check.to_s)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should not be able to save properties we don't have" do
|
99
|
+
doc = new :a => 'A'
|
100
|
+
role = doc.to_role(Check)
|
101
|
+
role.set_properties_we_don_t_have
|
102
|
+
|
103
|
+
assert_raises RuntimeError do
|
104
|
+
role.save
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should be able to save properties of the doc" do
|
109
|
+
doc = new :a => 'A'
|
110
|
+
role = doc.to_role(Check)
|
111
|
+
role.set_properties_of_doc
|
112
|
+
role.save
|
113
|
+
doc.reload
|
114
|
+
assert_equal "got it", doc["ooops"]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should only destroy it's own properties" do
|
118
|
+
doc = create :a => 'A', :c => 'C', :b => 'B', :shared => 'keep me too', :checker1 => 'keep me!', :roles => ['Check', 'Checker']
|
119
|
+
role = doc.to_role(Check)
|
120
|
+
doc.save
|
121
|
+
doc.reload
|
122
|
+
assert 'A', doc["a"]
|
123
|
+
assert 'C', doc["c"]
|
124
|
+
assert_equal "keep me!", doc["checker1"]
|
125
|
+
assert_equal "keep me too", doc["shared"]
|
126
|
+
assert doc["roles"].include?(Check.to_s)
|
127
|
+
|
128
|
+
# now destroy Check
|
129
|
+
role = doc.to_role(Check)
|
130
|
+
role.destroy
|
131
|
+
doc.save
|
132
|
+
doc.reload
|
133
|
+
assert_equal ['Checker'], doc["roles"]
|
134
|
+
assert_equal "B", doc["b"]
|
135
|
+
assert_equal "keep me!", doc["checker1"]
|
136
|
+
assert_equal "keep me too", doc["shared"]
|
137
|
+
assert doc["a"].nil?
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should trying to make a role from a property" do
|
141
|
+
doc = create :simple => { :a => 'A', :c => 'C', :b => 'B', :roles => ['Check']}, :checker1 => 'keep me!', :roles => ['Checker']
|
142
|
+
role = doc.to_role(Check,'simple')
|
143
|
+
role.c = "BC"
|
144
|
+
doc.save
|
145
|
+
assert_equal "BC", doc["simple"]["c"]
|
146
|
+
assert doc["simple"]["check_saved"]
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should trying to make a array of roles from a property" do
|
150
|
+
doc = create :simple => [{ :a => 'A1', :c => 'C1', :b => 'B1', :roles => ['Check']}, { :a => 'A2', :c => 'C2', :b => 'B2', :roles => ['Check']}], :checker1 => 'keep me!', :roles => ['Checker']
|
151
|
+
roles = doc.to_role(Check, 'simple')
|
152
|
+
roles.each do |fk|
|
153
|
+
fk.c = fk.c.downcase
|
154
|
+
end
|
155
|
+
|
156
|
+
doc.save
|
157
|
+
assert_equal "c1", doc["simple"].first["c"]
|
158
|
+
assert doc["simple"].first["check_saved"]
|
159
|
+
assert_equal "c2", doc["simple"].last["c"]
|
160
|
+
assert doc["simple"].last["check_saved"]
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
it "should also possible to use a role as property of a role" do
|
165
|
+
checks = [{ :a => 'A1', :c => 'C1', :b => 'B1', :roles => ['Check']}, { :a => 'A2', :c => 'C2', :b => 'B2', :roles => ['Check']}]
|
166
|
+
doc = create :checker1 => checks, :roles => ['Checker']
|
167
|
+
role = doc.to_role(Checker)
|
168
|
+
|
169
|
+
role.to_role(Check, 'checker1').each do |fk|
|
170
|
+
fk.c = fk.c.downcase
|
171
|
+
end
|
172
|
+
|
173
|
+
doc.save
|
174
|
+
assert_equal "c1", doc["checker1"].first["c"]
|
175
|
+
assert_equal "c2", doc["checker1"].last["c"]
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should also possible to use a role as property of anotherone" do
|
179
|
+
doc = create :simple => { :a => 'AAAA', :c => {:checker1 => 'modify me!!!!!', :roles => ['Checker']}, :b => 'B', :roles => ['Check']}
|
180
|
+
role1 = doc.to_role(Check, 'simple')
|
181
|
+
role2 = role1.to_role(Checker, 'c')
|
182
|
+
role2.checker1 = "modified!!!!"
|
183
|
+
doc.save
|
184
|
+
assert_equal "modified!!!!", doc["simple"]["c"]["checker1"]
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should also possible to use a role as property of anotherone (array)" do
|
188
|
+
doc = create :simple => { :a => 'AAAA', :c => [{:checker1 => 'mod1', :roles => ['Checker']},{:checker1 => 'mod2', :roles => ['Checker']}], :b => 'B', :roles => ['Check']}
|
189
|
+
role1 = doc.to_role(Check, 'simple')
|
190
|
+
roles = role1.to_role(Checker, 'c')
|
191
|
+
roles.each do |fk|
|
192
|
+
fk.checker1 = fk.checker1.upcase
|
193
|
+
end
|
194
|
+
|
195
|
+
doc.save
|
196
|
+
assert_equal "MOD1", doc["simple"]["c"].first["checker1"]
|
197
|
+
assert_equal "MOD2", doc["simple"]["c"].last["checker1"]
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should also possible to use a role as property of anotherone (array.save)" do
|
201
|
+
doc = create :simple => { :a => 'AAAA', :c => [{:checker1 => 'mod1', :roles => ['Checker']},{:checker1 => 'mod2', :roles => ['Checker']}], :b => 'B', :roles => ['Check']}
|
202
|
+
role1 = doc.to_role(Check, 'simple')
|
203
|
+
roles = role1.to_role(Checker, 'c')
|
204
|
+
roles.each do |fk|
|
205
|
+
fk.checker1 = fk.checker1.upcase
|
206
|
+
end
|
207
|
+
|
208
|
+
# saves only the first
|
209
|
+
roles.first.save
|
210
|
+
assert_equal "MOD1", doc["simple"]["c"].first["checker1"]
|
211
|
+
assert_equal "mod2", doc["simple"]["c"].last["checker1"]
|
212
|
+
|
213
|
+
# saves all
|
214
|
+
doc.save
|
215
|
+
assert_equal "MOD1", doc["simple"]["c"].first["checker1"]
|
216
|
+
assert_equal "MOD2", doc["simple"]["c"].last["checker1"]
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should also possible to use a role as property of anotherone (destroy hook)" do
|
220
|
+
doc = create :simple => { :a => 'AAAA', :c => {:checker1 => 'modify me!!!!!', :roles => ['Checker'], :a => 'A'}, :b => 'B', :roles => ['Check', 'Checker']}
|
221
|
+
role1 = doc.to_role(Check, 'simple')
|
222
|
+
role2 = role1.to_role(Checker, 'c')
|
223
|
+
role2.destroy
|
224
|
+
doc.save
|
225
|
+
assert_equal 'A', doc["simple"]["c"]["a"]
|
226
|
+
assert doc["simple"]["c"]["checker1"].nil?
|
227
|
+
end
|
228
|
+
|
229
|
+
|
230
|
+
it "should also possible to use a role as property of anotherone (array. trigger destroy hook)" do
|
231
|
+
doc = create :simple => { :a => 'AAAA', :c => [{:checker1 => 'mod1', :a => 'A', :roles => ['Checker']},{:checker1 => 'mod2', :roles => ['Checker']}], :b => 'B', :roles => ['Check']}
|
232
|
+
role1 = doc.to_role(Check, 'simple')
|
233
|
+
f = role1.first_role(Checker, 'c')
|
234
|
+
|
235
|
+
assert_equal 'mod1', doc["simple"]["c"].first["checker1"]
|
236
|
+
assert_equal 'A', doc["simple"]["c"].first["a"]
|
237
|
+
|
238
|
+
|
239
|
+
assert_equal 'mod1', f.checker1
|
240
|
+
f.destroy
|
241
|
+
assert_equal nil, f.checker1
|
242
|
+
assert_equal 'A', f._role_doc["a"]
|
243
|
+
doc.save
|
244
|
+
doc.reload
|
245
|
+
|
246
|
+
assert_equal nil, doc["simple"]["c"].first["checker1"]
|
247
|
+
assert_equal 'A', doc["simple"]["c"].first["a"]
|
248
|
+
end
|
249
|
+
|
250
|
+
|
251
|
+
it "should allow adding of a role as property with properties directly set" do
|
252
|
+
doc = create
|
253
|
+
doc.add_role(Check, 'check') do |c|
|
254
|
+
c.a = 'A'
|
255
|
+
end
|
256
|
+
|
257
|
+
assert_equal 'A', doc["check"]["a"]
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should allow adding of a role as property without properties directly set" do
|
261
|
+
doc = create
|
262
|
+
doc.add_role(Check, 'check')
|
263
|
+
|
264
|
+
doc.last_role(Check, 'check') do |c|
|
265
|
+
c.a = 'A'
|
266
|
+
end
|
267
|
+
|
268
|
+
doc.last_role(Check, 'check') do |c|
|
269
|
+
c.c = 'C'
|
270
|
+
end
|
271
|
+
|
272
|
+
|
273
|
+
assert_equal 'A', doc["check"]["a"]
|
274
|
+
assert_equal 'C', doc["check"]["c"]
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should allow adding of a role as property multiple times transforming into an array by the way" do
|
278
|
+
doc = create
|
279
|
+
doc.add_role(Check, 'check') do |c|
|
280
|
+
c.a = 'A1'
|
281
|
+
end
|
282
|
+
|
283
|
+
assert_equal 'A1', doc["check"]["a"]
|
284
|
+
|
285
|
+
doc.add_role(Check, 'check') do |c|
|
286
|
+
c.a = 'A2'
|
287
|
+
end
|
288
|
+
|
289
|
+
doc.add_role(Check, 'check') do |c|
|
290
|
+
c.a = 'A3'
|
291
|
+
end
|
292
|
+
|
293
|
+
assert_equal 'A1', doc["check"].first["a"]
|
294
|
+
assert_equal 'A2', doc["check"][1]["a"]
|
295
|
+
assert_equal 'A3', doc["check"].last["a"]
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should allow adding of a role of a role as property with properties directly set 1" do
|
299
|
+
doc = create
|
300
|
+
doc.add_role(Check, 'check') do |c|
|
301
|
+
c.a = 'A'
|
302
|
+
end
|
303
|
+
|
304
|
+
role = doc.to_role(Check, 'check')
|
305
|
+
|
306
|
+
role.add_role(Checker, 'sub') do |ck|
|
307
|
+
ck.checker1 = 'hiho'
|
308
|
+
end
|
309
|
+
|
310
|
+
assert_equal 'hiho', doc["check"]["sub"]["checker1"]
|
311
|
+
|
312
|
+
role.add_role(Checker, 'sub') do |ck|
|
313
|
+
ck.checker1 = 'hoho'
|
314
|
+
end
|
315
|
+
|
316
|
+
|
317
|
+
assert_equal 'A', doc["check"]["a"]
|
318
|
+
assert_equal 'hiho', doc["check"]["sub"].first["checker1"]
|
319
|
+
assert_equal 'hoho', doc["check"]["sub"].last["checker1"]
|
320
|
+
end
|
321
|
+
|
322
|
+
|
323
|
+
it "should allow adding of a role of a role as property with properties directly set 2" do
|
324
|
+
doc = create
|
325
|
+
doc.add_role(Check, 'check') do |c|
|
326
|
+
c.a = 'A1'
|
327
|
+
end
|
328
|
+
|
329
|
+
doc.add_role(Check, 'check') do |c|
|
330
|
+
c.a = 'A2'
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
role1 = doc.first_role(Check, 'check')
|
335
|
+
|
336
|
+
role1.add_role(Checker, 'sub') do |ck|
|
337
|
+
ck.checker1 = 'A1sub1'
|
338
|
+
end
|
339
|
+
|
340
|
+
assert_equal 'A1sub1', doc["check"].first["sub"]["checker1"]
|
341
|
+
|
342
|
+
role1.add_role(Checker, 'sub') do |ck|
|
343
|
+
ck.checker1 = 'A1sub2'
|
344
|
+
end
|
345
|
+
|
346
|
+
role2 = doc.last_role(Check, 'check')
|
347
|
+
|
348
|
+
role2.add_role(Checker, 'sub') do |ck|
|
349
|
+
ck.checker1 = 'A2sub1'
|
350
|
+
end
|
351
|
+
|
352
|
+
assert_equal 'A2sub1', doc["check"].last["sub"]["checker1"]
|
353
|
+
|
354
|
+
role2.add_role(Checker, 'sub') do |ck|
|
355
|
+
ck.checker1 = 'A2sub2'
|
356
|
+
end
|
357
|
+
|
358
|
+
|
359
|
+
assert_equal 'A1sub1', doc["check"].first["sub"].first["checker1"]
|
360
|
+
assert_equal 'A1sub2', doc["check"].first["sub"].last["checker1"]
|
361
|
+
assert_equal 'A2sub1', doc["check"].last["sub"].first["checker1"]
|
362
|
+
assert_equal 'A2sub2', doc["check"].last["sub"].last["checker1"]
|
363
|
+
|
364
|
+
|
365
|
+
assert_equal 'A1', doc["check"].first["a"]
|
366
|
+
assert_equal 'A2', doc["check"].last["a"]
|
367
|
+
end
|
368
|
+
|
369
|
+
|
370
|
+
it "should allow adding of a role as property without properties directly set" do
|
371
|
+
doc = create
|
372
|
+
doc.add_role(Check, 'check') do |c|
|
373
|
+
c.a = 'A'
|
374
|
+
end
|
375
|
+
|
376
|
+
role1 = doc.last_role(Check, 'check')
|
377
|
+
|
378
|
+
role1.add_role(Checker, 'sub')
|
379
|
+
|
380
|
+
role1.last_role(Checker, 'sub') do |ck|
|
381
|
+
ck.checker1 = 'A1sub1'
|
382
|
+
end
|
383
|
+
|
384
|
+
assert_equal 'A', doc["check"]["a"]
|
385
|
+
assert_equal 'A1sub1', doc["check"]["sub"]["checker1"]
|
386
|
+
end
|
387
|
+
|
388
|
+
end
|
389
|
+
|
@@ -0,0 +1,282 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class DrivingLicense < ThingTank::Role
|
4
|
+
property :date_of_driving_license, String
|
5
|
+
property :driving_license_state
|
6
|
+
property :tester
|
7
|
+
|
8
|
+
def driving_license_valid?
|
9
|
+
self["tester"] = "huha"
|
10
|
+
self["driving_license_state"] == "valid"
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
before_save do
|
15
|
+
#p :before_save_licence
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Car < ThingTank::Role
|
21
|
+
property :color, String
|
22
|
+
property :model, String
|
23
|
+
|
24
|
+
wants DrivingLicense
|
25
|
+
|
26
|
+
validates_presence_of :model
|
27
|
+
validates_presence_of :color
|
28
|
+
|
29
|
+
before_save do
|
30
|
+
#p :before_save_car
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
describe "a general test" do
|
37
|
+
|
38
|
+
before do
|
39
|
+
reset_test_db!
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should work with with" do
|
43
|
+
doc = create
|
44
|
+
doc.with('check') do |c|
|
45
|
+
c.as(Check).a = 'A'
|
46
|
+
c.as(Checker).checker1 = 'C'
|
47
|
+
end
|
48
|
+
|
49
|
+
assert_equal 'A', doc['check']['a']
|
50
|
+
assert_equal 'C', doc['check']['checker1']
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should work with 'with' and doc ids" do
|
54
|
+
first = create :name => 'A thing'
|
55
|
+
doc = create 'my_thing' => first.id
|
56
|
+
doc.with 'my_thing' do |c|
|
57
|
+
assert_equal 'A thing', c["name"]
|
58
|
+
c["name"] = 'renamed'
|
59
|
+
end
|
60
|
+
|
61
|
+
first.reload
|
62
|
+
assert_equal 'renamed', first["name"]
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should work with with and subs" do
|
66
|
+
doc = create
|
67
|
+
doc.with('check') do |c|
|
68
|
+
c.with('sub') do |s|
|
69
|
+
s.as(Check).a = 'subA'
|
70
|
+
s.as(Checker).checker1 = 'subC'
|
71
|
+
end
|
72
|
+
c.as(Check).a = 'A'
|
73
|
+
c.as(Checker).checker1 = 'C'
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_equal 'A', doc['check']['a']
|
77
|
+
assert_equal 'C', doc['check']['checker1']
|
78
|
+
assert_equal 'subA', doc['check']['sub']['a']
|
79
|
+
assert_equal 'subC', doc['check']['sub']['checker1']
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should work with with and subs" do
|
83
|
+
doc = create 'check' => []
|
84
|
+
doc.with_first('check') do |c|
|
85
|
+
c.with('sub1') do |s|
|
86
|
+
s.as(Check).a = 'sub1A'
|
87
|
+
s.as(Checker).checker1 = 'sub1C'
|
88
|
+
end
|
89
|
+
c.as(Check).a = 'A1'
|
90
|
+
c.as(Checker).checker1 = 'C1'
|
91
|
+
end
|
92
|
+
|
93
|
+
doc["check"] << {}
|
94
|
+
|
95
|
+
doc.with_last('check') do |c|
|
96
|
+
c.with('sub2') do |s|
|
97
|
+
s.as(Check).a = 'sub2A'
|
98
|
+
s.as(Checker).checker1 = 'sub2C'
|
99
|
+
end
|
100
|
+
c.with('sub3') do |s|
|
101
|
+
s.as(Check).a = 'sub3A'
|
102
|
+
s.as(Checker).checker1 = 'sub3C'
|
103
|
+
end
|
104
|
+
c.as(Check).a = 'A2'
|
105
|
+
c.as(Checker).checker1 = 'C2'
|
106
|
+
end
|
107
|
+
|
108
|
+
doc.with_first('check') do |c|
|
109
|
+
c.as(Check).a = 'A-changed'
|
110
|
+
end
|
111
|
+
|
112
|
+
assert_equal 'A-changed', doc['check'].first['a']
|
113
|
+
assert_equal 'C1', doc['check'].first['checker1']
|
114
|
+
assert_equal 'sub1A', doc['check'].first['sub1']['a']
|
115
|
+
assert_equal 'sub1C', doc['check'].first['sub1']['checker1']
|
116
|
+
|
117
|
+
assert_equal 'A2', doc['check'].last['a']
|
118
|
+
assert_equal 'C2', doc['check'].last['checker1']
|
119
|
+
assert_equal 'sub2A', doc['check'].last['sub2']['a']
|
120
|
+
assert_equal 'sub2C', doc['check'].last['sub2']['checker1']
|
121
|
+
|
122
|
+
assert_equal 'sub3A', doc['check'].last['sub3']['a']
|
123
|
+
assert_equal 'sub3C', doc['check'].last['sub3']['checker1']
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
it "make the changed test" do
|
128
|
+
doc = create :a => "A", :roles => ["Check"]
|
129
|
+
assert doc.is? Check
|
130
|
+
assert !doc.as(Check).changed?
|
131
|
+
chk = doc.as(Check) do |c|
|
132
|
+
c.a = 'AAA'
|
133
|
+
end
|
134
|
+
|
135
|
+
assert_equal false, chk.changed?
|
136
|
+
assert_equal 'AAA', doc["a"]
|
137
|
+
|
138
|
+
doc["a"] = 'AA'
|
139
|
+
|
140
|
+
assert_equal 'AA', doc["a"]
|
141
|
+
assert_equal 'AA', chk.reload["a"]
|
142
|
+
assert_equal 'AA', doc.as(Check)["a"]
|
143
|
+
doc.save
|
144
|
+
doc.reload
|
145
|
+
assert_equal 'AA', doc["a"]
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should export a property to its doc" do
|
149
|
+
other = { "sub" => "hash" }
|
150
|
+
doc = create :weather => "fine", :other => other
|
151
|
+
assert_equal other, doc["other"]
|
152
|
+
doc.export "other"
|
153
|
+
doc2 = load doc["other_id"]
|
154
|
+
assert_equal "fine", doc["weather"]
|
155
|
+
assert_equal "hash", doc2["sub"]
|
156
|
+
assert_equal nil, doc["other"]
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should allow same access if property is a thing or if it is a array of things or if it is a Hash" do
|
160
|
+
doc = create "thingie" => "hiho"
|
161
|
+
assert_equal "hiho", doc["thingie"]
|
162
|
+
doc["thingie"] = ["huhu", "hiho"]
|
163
|
+
doc.save
|
164
|
+
doc.reload
|
165
|
+
assert_equal ["huhu", "hiho"], doc["thingie"]
|
166
|
+
doc["thingie"] = { "hi" => "ho" }
|
167
|
+
doc.save
|
168
|
+
doc.reload
|
169
|
+
assert_equal({ "hi" => "ho" }, doc["thingie"])
|
170
|
+
end
|
171
|
+
|
172
|
+
it "can play a car" do
|
173
|
+
doc = create :color => 'black', :model => 'Carrera', :roles => ['Car']
|
174
|
+
assert_equal true, doc.is?(Car)
|
175
|
+
end
|
176
|
+
|
177
|
+
it "cannot be a car" do
|
178
|
+
doc = create :model => 'Carrera'
|
179
|
+
doc["roles"] = ['Car']
|
180
|
+
assert_equal false, doc.is?(Car)
|
181
|
+
assert_equal ["Car"], doc.invalid_roles
|
182
|
+
#assert_raises RuntimeError do
|
183
|
+
# doc.save
|
184
|
+
#end
|
185
|
+
doc["color"] = 'blue'
|
186
|
+
assert_equal true, doc.is?(Car)
|
187
|
+
assert_equal [], doc.invalid_roles
|
188
|
+
doc.save
|
189
|
+
doc.reload
|
190
|
+
assert_equal true, doc.is?(Car)
|
191
|
+
|
192
|
+
doc = create :color => 'black', :model => 'Carrera'
|
193
|
+
assert_equal false, doc.is?(Car)
|
194
|
+
end
|
195
|
+
|
196
|
+
it "could be a car" do
|
197
|
+
doc = create :color => 'black', :model => 'Carrera'
|
198
|
+
assert_equal true, doc.could_be?(Car)
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should wish a driving license" do
|
202
|
+
assert_equal [DrivingLicense], Car.wishes
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should have a driving license" do
|
206
|
+
doc = create :date_of_driving_license => Time.now.to_s, :driving_license_state => 'valid'
|
207
|
+
assert !doc.has?(DrivingLicense)
|
208
|
+
doc.have(DrivingLicense) do |licence|
|
209
|
+
assert_equal true, licence.driving_license_valid?
|
210
|
+
end
|
211
|
+
assert doc.has?(DrivingLicense)
|
212
|
+
doc.save
|
213
|
+
assert_equal "huha", doc["tester"]
|
214
|
+
assert_equal [DrivingLicense], Car.wishes
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
#it "should try to validate something" do
|
219
|
+
# doc = CouchRest::Document.new :hi => "ho"
|
220
|
+
# p doc.valid?
|
221
|
+
#end
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
=begin
|
226
|
+
describe "a thingtank" do
|
227
|
+
|
228
|
+
before do
|
229
|
+
#skip "temporary disabled"
|
230
|
+
reset_test_db!
|
231
|
+
end
|
232
|
+
|
233
|
+
after do
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should allow same access if property is a thing or if it is a array of things" do
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should allow same access if property is a thing or if it is the doc_id of a thing" do
|
240
|
+
end
|
241
|
+
|
242
|
+
it "should allow same access if property is a array of things or if it is array of doc_ids that are things" do
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should be able to change the name of a property" do
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should be able to share properties with different roles" do
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should trigger all affected callbacks when a property is shared between roles" do
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should allow an alias for a property if the property needs disambiguation between the different meanings for the different roles" do
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should allow general handlers for roles that only are affected if a doc fits to a role and ignore the docs otherwise" do
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should allow views that handle only the chosen roles" do
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should recommend interesting new roles" do
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should validate with validatable" do
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should convert basic types as Time" do
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should handle pagination" do
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should have roles" do
|
276
|
+
end
|
277
|
+
|
278
|
+
end
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
=end
|