xbar 0.0.1 → 0.4.0

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.
Files changed (80) hide show
  1. data/Appraisals +25 -0
  2. data/README.mkdn +215 -0
  3. data/Rakefile +337 -1
  4. data/examples/README +5 -0
  5. data/examples/config/simple.json +22 -0
  6. data/examples/example1.rb +34 -0
  7. data/examples/migrations/1_create_users.rb +10 -0
  8. data/examples/setup.rb +43 -0
  9. data/gemfiles/rails3.gemfile +8 -0
  10. data/gemfiles/rails3.gemfile.lock +74 -0
  11. data/gemfiles/rails31.gemfile +8 -0
  12. data/gemfiles/rails31.gemfile.lock +83 -0
  13. data/gemfiles/rails32.gemfile +7 -0
  14. data/gemfiles/rails32.gemfile.lock +117 -0
  15. data/gemfiles/rails4.gemfile +9 -0
  16. data/gemfiles/rails4.gemfile.lock +134 -0
  17. data/lib/migrations/1_create_usage_statistics.rb +23 -0
  18. data/lib/xbar/association.rb +49 -0
  19. data/lib/xbar/association_collection.rb +69 -0
  20. data/lib/xbar/colors.rb +32 -0
  21. data/lib/xbar/has_and_belongs_to_many_association.rb +17 -0
  22. data/lib/xbar/logger.rb +14 -0
  23. data/lib/xbar/mapper.rb +304 -0
  24. data/lib/xbar/migration.rb +76 -0
  25. data/lib/xbar/model.rb +165 -0
  26. data/lib/xbar/proxy.rb +249 -0
  27. data/lib/xbar/rails2/association.rb +133 -0
  28. data/lib/xbar/rails2/persistence.rb +39 -0
  29. data/lib/xbar/rails3/arel.rb +13 -0
  30. data/lib/xbar/rails3/association.rb +112 -0
  31. data/lib/xbar/rails3/persistence.rb +37 -0
  32. data/lib/xbar/rails3.1/singular_association.rb +34 -0
  33. data/lib/xbar/scope_proxy.rb +55 -0
  34. data/lib/xbar/shard.rb +95 -0
  35. data/lib/xbar/version.rb +2 -2
  36. data/lib/xbar.rb +121 -2
  37. data/run +27 -0
  38. data/spec/config/acme.json +53 -0
  39. data/spec/config/connection.rb +2 -0
  40. data/spec/config/default.json +160 -0
  41. data/spec/config/duplicate_shard.json +21 -0
  42. data/spec/config/missing_key.json +20 -0
  43. data/spec/config/new_shards.json +29 -0
  44. data/spec/config/no_master_shard.json +19 -0
  45. data/spec/config/not_entire_sharded.json +23 -0
  46. data/spec/config/octopus.json +27 -0
  47. data/spec/config/octopus_rails.json +25 -0
  48. data/spec/config/production_fully_replicated.json +21 -0
  49. data/spec/config/production_raise_error.json +17 -0
  50. data/spec/config/simple.json +22 -0
  51. data/spec/config/single_adapter.json +20 -0
  52. data/spec/console.rb +15 -0
  53. data/spec/migrations/10_create_users_using_replication.rb +12 -0
  54. data/spec/migrations/11_add_field_in_all_slaves.rb +11 -0
  55. data/spec/migrations/12_create_users_using_block.rb +23 -0
  56. data/spec/migrations/13_create_users_using_block_and_using.rb +15 -0
  57. data/spec/migrations/1_create_users_on_master.rb +9 -0
  58. data/spec/migrations/2_create_users_on_canada.rb +11 -0
  59. data/spec/migrations/3_create_users_on_both_shards.rb +11 -0
  60. data/spec/migrations/4_create_users_on_shards_of_a_group.rb +11 -0
  61. data/spec/migrations/5_create_users_on_multiples_groups.rb +11 -0
  62. data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +11 -0
  63. data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +11 -0
  64. data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +11 -0
  65. data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +11 -0
  66. data/spec/spec_helper.rb +25 -0
  67. data/spec/support/database_models.rb +78 -0
  68. data/spec/support/xbar_helper.rb +42 -0
  69. data/spec/xbar/association_spec.rb +660 -0
  70. data/spec/xbar/controller_spec.rb +40 -0
  71. data/spec/xbar/logger_spec.rb +22 -0
  72. data/spec/xbar/mapper_spec.rb +283 -0
  73. data/spec/xbar/migration_spec.rb +110 -0
  74. data/spec/xbar/model_spec.rb +434 -0
  75. data/spec/xbar/proxy_spec.rb +124 -0
  76. data/spec/xbar/replication_spec.rb +94 -0
  77. data/spec/xbar/scope_proxy_spec.rb +22 -0
  78. data/spec/xbar/shard_spec.rb +36 -0
  79. data/xbar.gemspec +13 -3
  80. metadata +231 -10
@@ -0,0 +1,660 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe XBar::Association do
4
+ before(:all) do
5
+ set_xbar_env("default", "test")
6
+ end
7
+ describe "when you have a 1 x 1 relationship" do
8
+ before(:each) do
9
+ @computer_brazil = Computer.using(:brazil).create!(:name => "Computer Brazil")
10
+ @computer_master = Computer.create!(:name => "Computer Brazil")
11
+ @keyboard_brazil = Keyboard.using(:brazil).create!(:name => "Keyboard Brazil", :computer => @computer_brazil)
12
+ @keyboard_master = Keyboard.create!(:name => "Keyboard Master", :computer => @computer_master)
13
+ end
14
+
15
+ it "should find the models" do
16
+ @keyboard_master.computer.should == @computer_master
17
+ @keyboard_brazil.computer.should == @computer_brazil
18
+ end
19
+
20
+ it "should read correctly the relationed model" do
21
+ new_computer_brazil = Computer.using(:brazil).create!(:name => "New Computer Brazil")
22
+ new_computer_master = Computer.create!(:name => "New Computer Brazil")
23
+ @keyboard_brazil.computer = new_computer_brazil
24
+ @keyboard_brazil.save
25
+ @keyboard_brazil.reload
26
+ @keyboard_brazil.computer_id.should == new_computer_brazil.id
27
+ @keyboard_brazil.computer.should == new_computer_brazil
28
+ new_computer_brazil.save
29
+ new_computer_brazil.reload
30
+ new_computer_brazil.keyboard.should == @keyboard_brazil
31
+ end
32
+
33
+ it "should work when using #build_computer or #build_keyboard" do
34
+ c = Computer.using(:brazil).create!(:name => "Computer Brazil")
35
+ k = c.build_keyboard(:name => "Building keyboard")
36
+ # c.save
37
+ #k.save
38
+ c.keyboard.should == k
39
+ k.computer_id.should == c.id
40
+ k.computer.should == c
41
+ end
42
+
43
+ it "should work when using #create_computer or #create_keyboard" do
44
+ c = Computer.using(:brazil).create!(:name => "Computer Brazil")
45
+ k = c.create_keyboard(:name => "Building keyboard")
46
+ # c.save
47
+ # k.save
48
+ c.keyboard.should == k
49
+ k.computer_id.should == c.id
50
+ k.computer.should == c
51
+ end
52
+ end
53
+
54
+ describe "when you have a N x N relationship" do
55
+ before(:each) do
56
+ @brazil_role = Role.using(:brazil).create!(:name => "Brazil Role")
57
+ @master_role = Role.create!(:name => "Master Role")
58
+ @permission_brazil = Permission.using(:brazil).create!(:name => "Brazil Permission")
59
+ @permission_master = Permission.using(:master).create!(:name => "Master Permission")
60
+ @brazil_role.permissions << @permission_brazil
61
+ @brazil_role.save
62
+ Client.using(:master).create!(:name => "teste")
63
+ end
64
+
65
+ it "should find all models in the specified shard" do
66
+ @brazil_role.permission_ids.should == [@permission_brazil.id]
67
+ @brazil_role.permissions.should == [@permission_brazil]
68
+ end
69
+
70
+ it "should finds the client that the item belongs" do
71
+ @permission_brazil.role_ids.should == [@brazil_role.id]
72
+ @permission_brazil.roles.should == [@brazil_role]
73
+ end
74
+
75
+ it "should update the attribute for the item" do
76
+ new_brazil_role = Role.using(:brazil).create!(:name => "new Role")
77
+ @permission_brazil.roles = [new_brazil_role]
78
+ @permission_brazil.roles.should == [new_brazil_role]
79
+ @permission_brazil.save
80
+ @permission_brazil.reload
81
+ @permission_brazil.role_ids.should == [new_brazil_role.id]
82
+ @permission_brazil.roles.should == [new_brazil_role]
83
+ end
84
+
85
+ it "should works for build method" do
86
+ new_brazil_role = Role.using(:brazil).create!(:name => "Brazil Role")
87
+ c = new_brazil_role.permissions.create(:name => "new Permission")
88
+ c.save()
89
+ new_brazil_role.save()
90
+ c.roles().should == [new_brazil_role]
91
+ new_brazil_role.permissions.should == [c]
92
+ end
93
+
94
+ describe "it should works when using" do
95
+ before(:each) do
96
+ @permission_brazil_2 = Permission.using(:brazil).create!(:name => "Brazil Item 2")
97
+ @role = Role.using(:brazil).create!(:name => "testes")
98
+ end
99
+
100
+ it "update_attributes" do
101
+ @permission_brazil_2.update_attributes(:role_ids => [@role.id])
102
+ @permission_brazil_2.roles.to_set.should == [@role].to_set
103
+ end
104
+
105
+ if !XBar.rails3?
106
+ it "update_attribute" do
107
+ @permission_brazil_2.update_attribute(:role_ids, [@role.id])
108
+ @permission_brazil_2.roles.to_set.should == [@role].to_set
109
+ end
110
+ end
111
+
112
+ it "<<" do
113
+ @permission_brazil_2.roles << @role
114
+ @role.save()
115
+ @permission_brazil_2.save()
116
+ @permission_brazil_2.reload
117
+ @permission_brazil_2.roles.to_set.should == [@role].to_set
118
+ end
119
+
120
+ it "build" do
121
+ role = @permission_brazil_2.roles.build(:name => "Builded Role")
122
+ @permission_brazil_2.save()
123
+ @permission_brazil_2.roles.to_set.should == [role].to_set
124
+ end
125
+
126
+ it "create" do
127
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
128
+ @permission_brazil_2.roles.to_set.should == [role].to_set
129
+ end
130
+
131
+ it "create" do
132
+ role = @permission_brazil_2.roles.create!(:name => "Builded Role")
133
+ @permission_brazil_2.roles.to_set.should == [role].to_set
134
+ end
135
+
136
+ it "count" do
137
+ @permission_brazil_2.roles.count.should == 0
138
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
139
+ @permission_brazil_2.roles.count.should == 1
140
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
141
+ @permission_brazil_2.roles.count.should == 2
142
+ end
143
+
144
+ it "size" do
145
+ @permission_brazil_2.roles.size.should == 0
146
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
147
+ @permission_brazil_2.roles.size.should == 1
148
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
149
+ @permission_brazil_2.roles.size.should == 2
150
+ end
151
+
152
+ it "length" do
153
+ @permission_brazil_2.roles.length.should == 0
154
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
155
+ @permission_brazil_2.roles.length.should == 1
156
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
157
+ @permission_brazil_2.roles.length.should == 2
158
+ end
159
+
160
+
161
+ it "empty?" do
162
+ @permission_brazil_2.roles.empty?.should be_true
163
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
164
+ @permission_brazil_2.roles.empty?.should be_false
165
+ end
166
+
167
+ it "delete_all" do
168
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
169
+ @permission_brazil_2.roles.empty?.should be_false
170
+ @permission_brazil_2.roles.delete_all
171
+ @permission_brazil_2.roles.empty?.should be_true
172
+ end
173
+
174
+ it "destroy_all" do
175
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
176
+ @permission_brazil_2.roles.empty?.should be_false
177
+ @permission_brazil_2.roles.destroy_all
178
+ @permission_brazil_2.roles.empty?.should be_true
179
+ end
180
+
181
+ it "find" do
182
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
183
+ @permission_brazil_2.roles.find(:first).should == role
184
+ @permission_brazil_2.roles.destroy_all
185
+ @permission_brazil_2.roles.find(:first).should be_nil
186
+ end
187
+
188
+ it "exists?" do
189
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
190
+ @permission_brazil_2.roles.exists?(role).should be_true
191
+ @permission_brazil_2.roles.destroy_all
192
+ @permission_brazil_2.roles.exists?(role).should be_false
193
+ end
194
+
195
+ it "clear" do
196
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
197
+ @permission_brazil_2.roles.empty?.should be_false
198
+ @permission_brazil_2.roles.clear
199
+ @permission_brazil_2.roles.empty?.should be_true
200
+ end
201
+
202
+ it "delete" do
203
+ role = @permission_brazil_2.roles.create(:name => "Builded Role")
204
+ @permission_brazil_2.roles.empty?.should be_false
205
+ @permission_brazil_2.roles.delete(role)
206
+ @permission_brazil_2.reload
207
+ @role.reload
208
+ @role.permissions.should == []
209
+ @permission_brazil_2.roles.should == []
210
+ end
211
+ end
212
+ end
213
+
214
+ describe "when you have has_many :through" do
215
+ before(:each) do
216
+ @programmer = Programmer.using(:brazil).create!(:name => "Thiago")
217
+ @project = Project.using(:brazil).create!(:name => "RubySoc")
218
+ @project2 = Project.using(:brazil).create!(:name => "Cobol Application")
219
+ @programmer.projects << @project
220
+ @programmer.save()
221
+ Project.using(:master).create!(:name => "Project Master")
222
+ end
223
+
224
+ it "should find all models in the specified shard" do
225
+ @programmer.project_ids().should == [ @project.id]
226
+ @programmer.projects().should == [@project]
227
+ end
228
+
229
+
230
+ it "should update the attribute for the item" do
231
+ new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Joao")
232
+ @project.programmers = [new_brazil_programmer]
233
+ @project.programmers.should == [new_brazil_programmer]
234
+ @project.save()
235
+ @project.reload
236
+ @project.programmer_ids.should == [new_brazil_programmer.id]
237
+ @project.programmers().should == [new_brazil_programmer]
238
+ end
239
+
240
+ it "should works for create method" do
241
+ new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Joao")
242
+ c = new_brazil_programmer.projects.create(:name => "new Project")
243
+ c.save()
244
+ new_brazil_programmer.save()
245
+ c.programmers().should == [new_brazil_programmer]
246
+ new_brazil_programmer.projects.should == [c]
247
+ end
248
+
249
+ describe "it should works when using" do
250
+ before(:each) do
251
+ @new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Jose")
252
+ @project = Project.using(:brazil).create!(:name => "VB Application :-(")
253
+ end
254
+
255
+ it "update_attributes" do
256
+ @new_brazil_programmer.update_attributes(:project_ids => [@project.id])
257
+ @new_brazil_programmer.projects.to_set.should == [@project].to_set
258
+ end
259
+
260
+ if !XBar.rails3?
261
+ it "update_attribute" do
262
+ @new_brazil_programmer.update_attribute(:project_ids, [@project.id])
263
+ @new_brazil_programmer.projects.to_set.should == [@project].to_set
264
+ end
265
+ end
266
+
267
+ it "<<" do
268
+ @new_brazil_programmer.projects << @project
269
+ @project.save()
270
+ @new_brazil_programmer.save()
271
+ @new_brazil_programmer.reload
272
+ @new_brazil_programmer.projects.to_set.should == [@project].to_set
273
+ end
274
+
275
+ it "build" do
276
+ role = @new_brazil_programmer.projects.build(:name => "New VB App :-/")
277
+ @new_brazil_programmer.save()
278
+ @new_brazil_programmer.projects.to_set.should == [role].to_set
279
+ end
280
+
281
+ it "create" do
282
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
283
+ @new_brazil_programmer.projects.to_set.should == [role].to_set
284
+ end
285
+
286
+ it "create" do
287
+ role = @new_brazil_programmer.projects.create!(:name => "New VB App :-/")
288
+ @new_brazil_programmer.projects.to_set.should == [role].to_set
289
+ end
290
+
291
+ it "count" do
292
+ @new_brazil_programmer.projects.count.should == 0
293
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
294
+ @new_brazil_programmer.projects.count.should == 1
295
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
296
+ @new_brazil_programmer.projects.count.should == 2
297
+ end
298
+
299
+ it "size" do
300
+ @new_brazil_programmer.projects.size.should == 0
301
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
302
+ @new_brazil_programmer.projects.size.should == 1
303
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
304
+ @new_brazil_programmer.projects.size.should == 2
305
+ end
306
+
307
+ it "length" do
308
+ @new_brazil_programmer.projects.length.should == 0
309
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
310
+ @new_brazil_programmer.projects.length.should == 1
311
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
312
+ @new_brazil_programmer.projects.length.should == 2
313
+ end
314
+
315
+
316
+ it "empty?" do
317
+ @new_brazil_programmer.projects.empty?.should be_true
318
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
319
+ @new_brazil_programmer.projects.empty?.should be_false
320
+ end
321
+
322
+ it "delete_all" do
323
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
324
+ @new_brazil_programmer.projects.empty?.should be_false
325
+ @new_brazil_programmer.projects.delete_all
326
+ @new_brazil_programmer.projects.empty?.should be_true
327
+ end
328
+
329
+ it "destroy_all" do
330
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
331
+ @new_brazil_programmer.projects.empty?.should be_false
332
+ @new_brazil_programmer.projects.destroy_all
333
+ @new_brazil_programmer.projects.empty?.should be_true
334
+ end
335
+
336
+ it "find" do
337
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
338
+ @new_brazil_programmer.projects.find(:first).should == role
339
+ @new_brazil_programmer.projects.destroy_all
340
+ @new_brazil_programmer.projects.find(:first).should be_nil
341
+ end
342
+
343
+ it "exists?" do
344
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
345
+ @new_brazil_programmer.projects.exists?(role).should be_true
346
+ @new_brazil_programmer.projects.destroy_all
347
+ @new_brazil_programmer.projects.exists?(role).should be_false
348
+ end
349
+
350
+ it "clear" do
351
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
352
+ @new_brazil_programmer.projects.empty?.should be_false
353
+ @new_brazil_programmer.projects.clear
354
+ @new_brazil_programmer.projects.empty?.should be_true
355
+ end
356
+
357
+ it "delete" do
358
+ role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
359
+ @new_brazil_programmer.projects.empty?.should be_false
360
+ @new_brazil_programmer.projects.delete(role)
361
+ @new_brazil_programmer.reload
362
+ @project.reload
363
+ @project.programmers.should == []
364
+ @new_brazil_programmer.projects.should == []
365
+ end
366
+ end
367
+ end
368
+
369
+ describe "when you have a 1 x N relationship" do
370
+ before(:each) do
371
+ @brazil_client = Client.using(:brazil).create!(:name => "Brazil Client")
372
+ @master_client = Client.create!(:name => "Master Client")
373
+ @item_brazil = Item.using(:brazil).create!(:name => "Brazil Item", :client => @brazil_client)
374
+ @item_master = Item.create!(:name => "Master Item", :client => @master_client)
375
+ @brazil_client = Client.using(:brazil).find_by_name("Brazil Client")
376
+ Client.using(:master).create!(:name => "teste")
377
+ end
378
+
379
+ it "should find all models in the specified shard" do
380
+ @brazil_client.item_ids.should == [@item_brazil.id]
381
+ @brazil_client.items().should == [@item_brazil]
382
+ end
383
+
384
+ it "should finds the client that the item belongs" do
385
+ @item_brazil.client.should == @brazil_client
386
+ end
387
+
388
+ it "should raise error if you try to add a record from a different shard" do
389
+ lambda do
390
+ @brazil_client.items << Item.using(:canada).create!(:name => "New User")
391
+ end.should raise_error("Association Error: Records are from different shards")
392
+ end
393
+
394
+ it "should update the attribute for the item" do
395
+ new_brazil_client = Client.using(:brazil).create!(:name => "new Client")
396
+ @item_brazil.client = new_brazil_client
397
+ @item_brazil.client.should == new_brazil_client
398
+ @item_brazil.save()
399
+ @item_brazil.reload
400
+ @item_brazil.client_id.should == new_brazil_client.id
401
+ @item_brazil.client().should == new_brazil_client
402
+ end
403
+
404
+ it "should works for build method" do
405
+ item2 = Item.using(:brazil).create!(:name => "Brazil Item")
406
+ c = item2.create_client(:name => "new Client")
407
+ c.save()
408
+ item2.save()
409
+ item2.client.should == c
410
+ c.items().should == [item2]
411
+ end
412
+
413
+ describe "it should works when using" do
414
+ before(:each) do
415
+ @item_brazil_2 = Item.using(:brazil).create!(:name => "Brazil Item 2")
416
+ @brazil_client.items.to_set.should == [@item_brazil].to_set
417
+ end
418
+
419
+
420
+ it "update_attributes" do
421
+ @brazil_client.update_attributes(:item_ids => [@item_brazil_2.id, @item_brazil.id])
422
+ @brazil_client.items.to_set.should == [@item_brazil, @item_brazil_2].to_set
423
+ end
424
+
425
+ if !XBar.rails3?
426
+ it "update_attribute" do
427
+ @brazil_client.update_attribute(:item_ids, [@item_brazil_2.id, @item_brazil.id])
428
+ @brazil_client.items.to_set.should == [@item_brazil, @item_brazil_2].to_set
429
+ end
430
+ end
431
+
432
+ it "<<" do
433
+ @brazil_client.items << @item_brazil_2
434
+ @brazil_client.items.to_set.should == [@item_brazil, @item_brazil_2].to_set
435
+ end
436
+
437
+ it "build" do
438
+ item = @brazil_client.items.build(:name => "Builded Item")
439
+ item.save()
440
+ @brazil_client.items.to_set.should == [@item_brazil, item].to_set
441
+ end
442
+
443
+ it "create" do
444
+ item = @brazil_client.items.create(:name => "Builded Item")
445
+ @brazil_client.items.to_set.should == [@item_brazil, item].to_set
446
+ end
447
+
448
+ it "count" do
449
+ @brazil_client.items.count.should == 1
450
+ item = @brazil_client.items.create(:name => "Builded Item")
451
+ @brazil_client.items.count.should == 2
452
+ end
453
+
454
+ it "size" do
455
+ @brazil_client.items.size.should == 1
456
+ item = @brazil_client.items.create(:name => "Builded Item")
457
+ @brazil_client.items.size.should == 2
458
+ end
459
+
460
+ it "create!" do
461
+ item = @brazil_client.items.create!(:name => "Builded Item")
462
+ @brazil_client.items.to_set.should == [@item_brazil, item].to_set
463
+ end
464
+
465
+ it "length" do
466
+ @brazil_client.items.length.should == 1
467
+ item = @brazil_client.items.create(:name => "Builded Item")
468
+ @brazil_client.items.length.should == 2
469
+ end
470
+
471
+ it "empty?" do
472
+ @brazil_client.items.empty?.should be_false
473
+ c = Client.create!(:name => "Client1")
474
+ c.items.empty?.should be_true
475
+ end
476
+
477
+ it "delete" do
478
+ @brazil_client.items.empty?.should be_false
479
+ @brazil_client.items.delete(@item_brazil)
480
+ @brazil_client.reload
481
+ @item_brazil.reload
482
+ @item_brazil.client.should be_nil
483
+ @brazil_client.items.should == []
484
+ @brazil_client.items.empty?.should be_true
485
+ end
486
+
487
+ it "delete_all" do
488
+ @brazil_client.items.empty?.should be_false
489
+ @brazil_client.items.delete_all
490
+ @brazil_client.items.empty?.should be_true
491
+ end
492
+
493
+ it "destroy_all" do
494
+ @brazil_client.items.empty?.should be_false
495
+ @brazil_client.items.destroy_all
496
+ @brazil_client.items.empty?.should be_true
497
+ end
498
+
499
+ it "find" do
500
+ @brazil_client.items.find(:first).should == @item_brazil
501
+ @brazil_client.items.destroy_all
502
+ @brazil_client.items.find(:first).should be_nil
503
+ end
504
+
505
+ it "exists?" do
506
+ @brazil_client.items.exists?(@item_brazil).should be_true
507
+ @brazil_client.items.destroy_all
508
+ @brazil_client.items.exists?(@item_brazil).should be_false
509
+ end
510
+
511
+ it "uniq" do
512
+ @brazil_client.items.uniq.should == [@item_brazil]
513
+ end
514
+
515
+ it "clear" do
516
+ @brazil_client.items.empty?.should be_false
517
+ @brazil_client.items.clear
518
+ @brazil_client.items.empty?.should be_true
519
+ end
520
+ end
521
+ end
522
+
523
+ describe "when you have a 1 x N polymorphic relationship" do
524
+ before(:each) do
525
+ @brazil_client = Client.using(:brazil).create!(:name => "Brazil Client")
526
+ @master_client = Client.create!(:name => "Master Client")
527
+ @comment_brazil = Comment.using(:brazil).create!(:name => "Brazil Comment", :commentable => @brazil_client)
528
+ @comment_master = Comment.create!(:name => "Master Comment", :commentable => @master_client)
529
+ @brazil_client = Client.using(:brazil).find_by_name("Brazil Client")
530
+ Client.using(:master).create!(:name => "teste")
531
+ end
532
+
533
+ it "should find all models in the specified shard" do
534
+ @brazil_client.comment_ids.should == [@comment_brazil.id]
535
+ @brazil_client.comments().should == [@comment_brazil]
536
+ end
537
+
538
+ it "should finds the client that the comment belongs" do
539
+ @comment_brazil.commentable.should == @brazil_client
540
+ end
541
+
542
+ it "should update the attribute for the comment" do
543
+ new_brazil_client = Client.using(:brazil).create!(:name => "new Client")
544
+ @comment_brazil.commentable = new_brazil_client
545
+ @comment_brazil.commentable.should == new_brazil_client
546
+ @comment_brazil.save()
547
+ @comment_brazil.reload
548
+ @comment_brazil.commentable_id.should == new_brazil_client.id
549
+ @comment_brazil.commentable().should == new_brazil_client
550
+ end
551
+
552
+ describe "it should works when using" do
553
+ before(:each) do
554
+ @comment_brazil_2 = Comment.using(:brazil).create!(:name => "Brazil Comment 2")
555
+ @brazil_client.comments.to_set.should == [@comment_brazil].to_set
556
+ end
557
+
558
+ it "update_attributes" do
559
+ @brazil_client.update_attributes(:comment_ids => [@comment_brazil_2.id, @comment_brazil.id])
560
+ @brazil_client.comments.to_set.should == [@comment_brazil, @comment_brazil_2].to_set
561
+ end
562
+
563
+ if !XBar.rails3?
564
+ it "update_attribute" do
565
+ @brazil_client.update_attribute(:comment_ids, [@comment_brazil_2.id, @comment_brazil.id])
566
+ @brazil_client.comments.to_set.should == [@comment_brazil, @comment_brazil_2].to_set
567
+ end
568
+ end
569
+
570
+ it "<<" do
571
+ @brazil_client.comments << @comment_brazil_2
572
+ @brazil_client.comments.to_set.should == [@comment_brazil, @comment_brazil_2].to_set
573
+ end
574
+
575
+ it "build" do
576
+ comment = @brazil_client.comments.build(:name => "Builded Comment")
577
+ comment.save()
578
+ @brazil_client.comments.to_set.should == [@comment_brazil, comment].to_set
579
+ end
580
+
581
+ it "create" do
582
+ comment = @brazil_client.comments.create(:name => "Builded Comment")
583
+ @brazil_client.comments.to_set.should == [@comment_brazil, comment].to_set
584
+ end
585
+
586
+ it "count" do
587
+ @brazil_client.comments.count.should == 1
588
+ comment = @brazil_client.comments.create(:name => "Builded Comment")
589
+ @brazil_client.comments.count.should == 2
590
+ end
591
+
592
+ it "size" do
593
+ @brazil_client.comments.size.should == 1
594
+ comment = @brazil_client.comments.create(:name => "Builded Comment")
595
+ @brazil_client.comments.size.should == 2
596
+ end
597
+
598
+ it "create!" do
599
+ comment = @brazil_client.comments.create!(:name => "Builded Comment")
600
+ @brazil_client.comments.to_set.should == [@comment_brazil, comment].to_set
601
+ end
602
+
603
+ it "length" do
604
+ @brazil_client.comments.length.should == 1
605
+ comment = @brazil_client.comments.create(:name => "Builded Comment")
606
+ @brazil_client.comments.length.should == 2
607
+ end
608
+
609
+ it "empty?" do
610
+ @brazil_client.comments.empty?.should be_false
611
+ c = Client.create!(:name => "Client1")
612
+ c.comments.empty?.should be_true
613
+ end
614
+
615
+ it "delete" do
616
+ @brazil_client.comments.empty?.should be_false
617
+ @brazil_client.comments.delete(@comment_brazil)
618
+ @brazil_client.reload
619
+ @comment_brazil.reload
620
+ @comment_brazil.commentable.should be_nil
621
+ @brazil_client.comments.should == []
622
+ @brazil_client.comments.empty?.should be_true
623
+ end
624
+
625
+ it "delete_all" do
626
+ @brazil_client.comments.empty?.should be_false
627
+ @brazil_client.comments.delete_all
628
+ @brazil_client.comments.empty?.should be_true
629
+ end
630
+
631
+ it "destroy_all" do
632
+ @brazil_client.comments.empty?.should be_false
633
+ @brazil_client.comments.destroy_all
634
+ @brazil_client.comments.empty?.should be_true
635
+ end
636
+
637
+ it "find" do
638
+ @brazil_client.comments.find(:first).should == @comment_brazil
639
+ @brazil_client.comments.destroy_all
640
+ @brazil_client.comments.find(:first).should be_nil
641
+ end
642
+
643
+ it "exists?" do
644
+ @brazil_client.comments.exists?(@comment_brazil).should be_true
645
+ @brazil_client.comments.destroy_all
646
+ @brazil_client.comments.exists?(@comment_brazil).should be_false
647
+ end
648
+
649
+ it "uniq" do
650
+ @brazil_client.comments.uniq.should == [@comment_brazil]
651
+ end
652
+
653
+ it "clear" do
654
+ @brazil_client.comments.empty?.should be_false
655
+ @brazil_client.comments.clear
656
+ @brazil_client.comments.empty?.should be_true
657
+ end
658
+ end
659
+ end
660
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Rails Controllers" do
4
+ before(:all) do
5
+ set_xbar_env('default', 'test')
6
+ end
7
+ it "should use #using method to in all requests" do
8
+ class UsersController < ActionController::Base
9
+ around_filter :select_shard
10
+ def create
11
+ User.create!(:name => "ActionController")
12
+ render :nothing => true
13
+ end
14
+
15
+ def select_shard(&block)
16
+ XBar.using(:brazil, &block)
17
+ end
18
+
19
+ def self._routes
20
+ ActionDispatch::Routing::RouteSet.new
21
+ end
22
+ end
23
+
24
+ UsersController.action_methods.include?("create").should be_true
25
+
26
+ if XBar.rails3?
27
+ a = UsersController.new
28
+ a.stub!(:request).and_return(
29
+ mock({:fullpath => "", :filtered_parameters => {}, :env => {},
30
+ :formats => [mock(:to_sym => :xml, :ref => "xml")],
31
+ :method => "GET", :format => nil, :content_mime_type => nil}))
32
+ a.instance_variable_set(:@_response, mock(:content_type => "xml", :body= => "", :status => 401))
33
+ a.process(:create)
34
+ User.using(:brazil).find_by_name("ActionController").should_not be_nil
35
+ User.using(:master).find_by_name("ActionController").should be_nil
36
+ else
37
+ pending()
38
+ end
39
+ end
40
+ end