toystore 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -226,7 +226,7 @@ describe Toy::Reference do
226
226
 
227
227
  it "unmemoizes the list" do
228
228
  @game.user.should == @user
229
- @game.user.reset
229
+ @game.reset_user
230
230
  User.should_receive(:get).and_return(@user)
231
231
  @game.user.should == @user
232
232
  end
@@ -238,87 +238,97 @@ describe Toy::Reference do
238
238
  end
239
239
  end
240
240
 
241
- context "delegating" do
242
- describe "when target is nil" do
243
- before do
244
- @game = Game.create
245
- end
241
+ context "when target is nil" do
242
+ before do
243
+ @game = Game.create
244
+ end
246
245
 
247
- it "returns nil if nil" do
248
- @game.user.inspect.should == 'nil'
249
- end
246
+ it "returns nil if nil" do
247
+ @game.user.inspect.should == 'nil'
248
+ end
250
249
 
251
- it "delegates #kind_of?" do
252
- @game.user.kind_of?(NilClass).should be_true
253
- @game.user.kind_of?(User).should be_false
254
- end
250
+ it "returns true for nil?" do
251
+ @game.user.nil?.should be_true
252
+ end
255
253
 
256
- it "delegates #instance_of?" do
257
- @game.user.instance_of?(NilClass).should be_true
258
- @game.user.instance_of?(User).should be_false
259
- end
254
+ it "does not evaluate to true if used in conditional" do
255
+ lambda { raise if @game.user }.should_not raise_error
256
+ end
260
257
 
261
- it "delegates #is_a?" do
262
- @game.user.is_a?(NilClass).should be_true
263
- @game.user.is_a?(User).should be_false
264
- end
258
+ it "returns true when prefaced with !" do
259
+ (!@game.user).should == true
260
+ end
265
261
 
266
- it "delegates #eql?" do
267
- @game.user.should eql(nil)
268
- @game.user.should_not eql(User.create)
269
- @game.user.should_not eql(@game)
270
- end
262
+ it "delegates #kind_of?" do
263
+ @game.user.kind_of?(NilClass).should be_true
264
+ @game.user.kind_of?(User).should be_false
265
+ end
271
266
 
272
- it "delegates #equal?" do
273
- @game.user.should equal(nil)
274
- @game.user.should_not equal(User.create)
275
- @game.user.should_not equal(@game)
276
- end
267
+ it "delegates #instance_of?" do
268
+ @game.user.instance_of?(NilClass).should be_true
269
+ @game.user.instance_of?(User).should be_false
277
270
  end
278
271
 
279
- describe "when target is not nil" do
280
- before do
281
- @user = User.create
282
- @game = Game.create(:user => @user)
283
- end
272
+ it "delegates #is_a?" do
273
+ @game.user.is_a?(NilClass).should be_true
274
+ @game.user.is_a?(User).should be_false
275
+ end
284
276
 
285
- it "delegates #inspect" do
286
- @game.user = @user
287
- @game.user.inspect.should == %Q(#<User:#{@user.object_id} id: "#{@user.id}">)
288
- end
277
+ it "delegates #eql?" do
278
+ @game.user.should eql(nil)
279
+ @game.user.should_not eql(User.create)
280
+ @game.user.should_not eql(@game)
281
+ end
289
282
 
290
- it "delegates #==" do
291
- (@game.user == @user).should be_true
292
- (@user == @game.user).should be_true
293
- end
283
+ it "delegates #equal?" do
284
+ @game.user.should equal(nil)
285
+ @game.user.should_not equal(User.create)
286
+ @game.user.should_not equal(@game)
287
+ end
288
+ end
294
289
 
295
- it "delegates #is_a?" do
296
- @game.user.is_a?(User).should be_true
297
- @game.user.is_a?(Game).should be_false
298
- end
290
+ context "when target is not nil" do
291
+ before do
292
+ @user = User.create
293
+ @game = Game.create(:user => @user)
294
+ end
299
295
 
300
- it "delegates #kind_of?" do
301
- @game.user.kind_of?(User).should be_true
302
- @game.user.kind_of?(Game).should be_false
303
- end
296
+ it "delegates #inspect" do
297
+ @game.user = @user
298
+ @game.user.inspect.should == %Q(#<User:#{@user.object_id} id: "#{@user.id}">)
299
+ end
304
300
 
305
- it "delegates #instance_of?" do
306
- @game.user.instance_of?(User).should be_true
307
- @game.user.instance_of?(Game).should be_false
308
- end
301
+ it "delegates #==" do
302
+ (@game.user == @user).should be_true
303
+ (@user == @game.user).should be_true
304
+ end
309
305
 
310
- it "delegates #eql?" do
311
- @game.user.should eql(@user)
312
- @game.user.should_not eql(User.create)
313
- @game.user.should_not eql(@game)
314
- end
306
+ it "delegates #is_a?" do
307
+ @game.user.is_a?(User).should be_true
308
+ @game.user.is_a?(Game).should be_false
309
+ end
315
310
 
316
- it "delegates #equal?" do
317
- @game.user.should equal(@user)
318
- @user.should equal(@game.user)
319
- @game.user.should_not equal(User.create)
320
- @game.user.should_not equal(@game)
321
- end
311
+ it "delegates #kind_of?" do
312
+ @game.user.kind_of?(User).should be_true
313
+ @game.user.kind_of?(Game).should be_false
314
+ end
315
+
316
+ it "delegates #instance_of?" do
317
+ @game.user.instance_of?(User).should be_true
318
+ @game.user.instance_of?(Game).should be_false
319
+ end
320
+
321
+ it "delegates #eql?" do
322
+ @game.user.should eql(@user)
323
+ @game.user.should_not eql(User.create)
324
+ @game.user.should_not eql(@game)
325
+ end
326
+
327
+ it "delegates #equal?" do
328
+ @game.user.should equal(@user)
329
+ @user.should equal(@game.user)
330
+ @game.user.should_not equal(User.create)
331
+ @game.user.should_not equal(@game)
322
332
  end
323
333
  end
324
334
 
@@ -339,7 +349,7 @@ describe Toy::Reference do
339
349
  describe "reference#create" do
340
350
  before do
341
351
  @game = Game.create
342
- @user = @game.user.create
352
+ @user = @game.create_user
343
353
  end
344
354
 
345
355
  it_should_behave_like 'reference#create'
@@ -349,7 +359,7 @@ describe Toy::Reference do
349
359
  before do
350
360
  User.attribute(:name, String)
351
361
  @game = Game.create
352
- @user = @game.user.create(:name => 'John')
362
+ @user = @game.create_user(:name => 'John')
353
363
  end
354
364
 
355
365
  it_should_behave_like 'reference#create'
@@ -374,14 +384,14 @@ describe Toy::Reference do
374
384
 
375
385
  it "does not save owner" do
376
386
  @game.should_not_receive(:save)
377
- @game.user.build
387
+ @game.build_user
378
388
  end
379
389
  end
380
390
 
381
391
  describe "reference#build" do
382
392
  before do
383
393
  @game = Game.create
384
- @user = @game.user.build
394
+ @user = @game.build_user
385
395
  end
386
396
 
387
397
  it_should_behave_like 'reference#build'
@@ -391,7 +401,7 @@ describe Toy::Reference do
391
401
  before do
392
402
  User.attribute(:name, String)
393
403
  @game = Game.create
394
- @user = @game.user.build(:name => 'John')
404
+ @user = @game.build_user(:name => 'John')
395
405
  end
396
406
 
397
407
  it_should_behave_like 'reference#build'
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Toy::Serialization do
4
- uses_constants('User', 'Game', 'Move')
4
+ uses_objects('User', 'Move')
5
5
 
6
6
  before do
7
7
  User.attribute :name, String
@@ -10,7 +10,7 @@ describe Toy::Serialization do
10
10
 
11
11
  it "serializes to json" do
12
12
  doc = User.new(:name => 'John', :age => 28)
13
- ActiveSupport::JSON.decode(doc.to_json).should == {
13
+ MultiJson.load(doc.to_json).should == {
14
14
  'user' => {
15
15
  'name' => 'John',
16
16
  'id' => doc.id,
@@ -28,7 +28,6 @@ describe Toy::Serialization do
28
28
  'age' => 28
29
29
  }
30
30
  }
31
-
32
31
  end
33
32
 
34
33
  it "correctly serializes methods" do
@@ -38,124 +37,31 @@ describe Toy::Serialization do
38
37
  end
39
38
  end
40
39
  json = User.new.to_json(:methods => [:foo])
41
- ActiveSupport::JSON.decode(json)['user']['foo'].should == {'foo' => 'bar'}
40
+ MultiJson.load(json)['user']['foo'].should == {'foo' => 'bar'}
42
41
  end
43
42
 
44
43
  it "allows using :only" do
45
44
  user = User.new
46
45
  json = user.to_json(:only => :id)
47
- ActiveSupport::JSON.decode(json).should == {'user' => {'id' => user.id}}
46
+ MultiJson.load(json).should == {'user' => {'id' => user.id}}
48
47
  end
49
48
 
50
49
  it "allows using :only with strings" do
51
50
  user = User.new
52
51
  json = user.to_json(:only => 'id')
53
- ActiveSupport::JSON.decode(json).should == {'user' => {'id' => user.id}}
52
+ MultiJson.load(json).should == {'user' => {'id' => user.id}}
54
53
  end
55
54
 
56
55
  it "allows using :except" do
57
56
  user = User.new
58
57
  json = user.to_json(:except => :id)
59
- ActiveSupport::JSON.decode(json)['user'].should_not have_key('id')
58
+ MultiJson.load(json)['user'].should_not have_key('id')
60
59
  end
61
60
 
62
61
  it "allows using :except with strings" do
63
62
  user = User.new
64
63
  json = user.to_json(:except => 'id')
65
- ActiveSupport::JSON.decode(json)['user'].should_not have_key('id')
66
- end
67
-
68
- describe "serializing relationships" do
69
- before do
70
- User.list :games, :inverse_of => :user
71
- Game.reference :user
72
- end
73
-
74
- it "should include references" do
75
- user = User.create(:name => 'John', :age => 28)
76
- game = user.games.create
77
-
78
- ActiveSupport::JSON.decode(game.to_json(:include => [:user])).should == {
79
- 'game' => {
80
- 'id' => game.id,
81
- 'user_id' => user.id,
82
- 'user' => {
83
- 'name' => 'John',
84
- 'game_ids' => [game.id],
85
- 'id' => user.id,
86
- 'age' => 28,
87
- }
88
- }
89
- }
90
- end
91
-
92
- it "should include lists" do
93
- user = User.create(:name => 'John', :age => 28)
94
- game = user.games.create
95
- ActiveSupport::JSON.decode(user.to_json(:include => [:games])).should == {
96
- 'user' => {
97
- 'name' => 'John',
98
- 'game_ids' => [game.id],
99
- 'id' => user.id,
100
- 'age' => 28,
101
- 'games' => [{'id' => game.id, 'user_id' => user.id}],
102
- }
103
- }
104
- end
105
-
106
- it "should not cause circular reference JSON errors for references" do
107
- user = User.create(:name => 'John', :age => 28)
108
- game = user.games.create
109
-
110
- ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(game.user)).should == {
111
- 'user' => {
112
- 'name' => 'John',
113
- 'game_ids' => [game.id],
114
- 'id' => user.id,
115
- 'age' => 28
116
- }
117
- }
118
- end
119
-
120
- it "should not cause circular reference JSON errors for references when called indirectly" do
121
- user = User.create(:name => 'John', :age => 28)
122
- game = user.games.create
123
-
124
- ActiveSupport::JSON.decode(ActiveSupport::JSON.encode([game.user])).should == [
125
- 'user' => {
126
- 'name' => 'John',
127
- 'game_ids' => [game.id],
128
- 'id' => user.id,
129
- 'age' => 28
130
- }
131
- ]
132
- end
133
-
134
- it "should not cause circular reference JSON errors for lists" do
135
- user = User.create(:name => 'John', :age => 28)
136
- game = user.games.create
137
-
138
- ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(user.games)).should == [{
139
- 'game' => {
140
- 'id' => game.id,
141
- 'user_id' => user.id
142
- }
143
- }]
144
- end
145
-
146
- it "should not cause circular reference JSON errors for lists when called indirectly" do
147
- user = User.create(:name => 'John', :age => 28)
148
- game = user.games.create
149
-
150
- ActiveSupport::JSON.decode(ActiveSupport::JSON.encode({:games => user.games})).should == {
151
- 'games' => [{
152
- 'game' => {
153
- 'id' => game.id,
154
- 'user_id' => user.id
155
- }
156
- }]
157
- }
158
- end
64
+ MultiJson.load(json)['user'].should_not have_key('id')
159
65
  end
160
66
 
161
67
  describe "serializing specific attributes" do
@@ -191,7 +97,7 @@ describe Toy::Serialization do
191
97
  end
192
98
 
193
99
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
194
- ActiveSupport::JSON.decode(move.to_json).should == {
100
+ MultiJson.load(move.to_json).should == {
195
101
  'move' => {
196
102
  'id' => move.id,
197
103
  'points' => 15,
@@ -213,7 +119,7 @@ describe Toy::Serialization do
213
119
  end
214
120
 
215
121
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
216
- ActiveSupport::JSON.decode(move.to_json).should == {
122
+ MultiJson.load(move.to_json).should == {
217
123
  'move' => {
218
124
  'id' => move.id,
219
125
  'index' => 0,
@@ -228,18 +134,17 @@ describe Toy::Serialization do
228
134
  describe "#serializable_hash" do
229
135
  context "with method that is another toystore object" do
230
136
  before do
231
- Game.reference(:creator, User)
232
- @game = Game.create(:creator => User.create)
137
+ Move.class_eval { attr_accessor :creator }
233
138
  end
234
- let(:game) { @game }
139
+
140
+ let(:move) { Move.new(:creator => User.new) }
235
141
 
236
142
  it "returns serializable hash of object" do
237
- game.serializable_hash(:methods => [:creator]).should == {
238
- 'id' => game.id,
239
- 'creator_id' => game.creator_id,
240
- 'creator' => {'id' => game.creator.id}
143
+ move.serializable_hash(:methods => [:creator]).should == {
144
+ 'id' => move.id,
145
+ 'creator' => {'id' => move.creator.id}
241
146
  }
242
147
  end
243
148
  end
244
149
  end
245
- end
150
+ end
@@ -3,39 +3,25 @@ require 'helper'
3
3
  describe Toy::Store do
4
4
  uses_constants('User')
5
5
 
6
- describe "including" do
7
- it "adds model naming" do
8
- model_name = User.model_name
9
- model_name.should == 'User'
10
- model_name.singular.should == 'user'
11
- model_name.plural.should == 'users'
6
+ describe "#to_key" do
7
+ it "returns [id] if persisted" do
8
+ user = User.create
9
+ user.to_key.should == [user.id]
12
10
  end
13
11
 
14
- it "adds to_model" do
15
- user = User.new
16
- user.to_model.should == user
12
+ it "returns nil if not persisted" do
13
+ User.new.to_key.should be_nil
17
14
  end
15
+ end
18
16
 
19
- describe "#to_key" do
20
- it "returns [id] if persisted" do
21
- user = User.create
22
- user.to_key.should == [user.id]
23
- end
24
-
25
- it "returns nil if not persisted" do
26
- User.new.to_key.should be_nil
27
- end
17
+ describe "#to_param" do
18
+ it "returns key joined by - if to_key present" do
19
+ user = User.create
20
+ user.to_param.should == user.to_key.join('-')
28
21
  end
29
22
 
30
- describe "#to_param" do
31
- it "returns key joined by - if to_key present" do
32
- user = User.create
33
- user.to_param.should == user.to_key.join('-')
34
- end
35
-
36
- it "returns nil if to_key nil" do
37
- User.new.to_param.should be_nil
38
- end
23
+ it "returns nil if to_key nil" do
24
+ User.new.to_param.should be_nil
39
25
  end
40
26
  end
41
- end
27
+ end