ar-octopus-master 0.9.2.master → 0.9.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +46 -0
  5. data/.rubocop_todo.yml +56 -0
  6. data/.travis.yml +25 -0
  7. data/Appraisals +20 -0
  8. data/Gemfile +4 -0
  9. data/README.mkdn +242 -0
  10. data/Rakefile +172 -0
  11. data/TODO.txt +7 -0
  12. data/ar-octopus.gemspec +39 -0
  13. data/gemfiles/rails4.gemfile +7 -0
  14. data/gemfiles/rails41.gemfile +7 -0
  15. data/gemfiles/rails42.gemfile +7 -0
  16. data/gemfiles/rails5.gemfile +7 -0
  17. data/gemfiles/rails51.gemfile +7 -0
  18. data/lib/ar-octopus.rb +1 -0
  19. data/lib/octopus.rb +205 -0
  20. data/lib/octopus/abstract_adapter.rb +33 -0
  21. data/lib/octopus/association.rb +14 -0
  22. data/lib/octopus/association_shard_tracking.rb +74 -0
  23. data/lib/octopus/collection_association.rb +17 -0
  24. data/lib/octopus/collection_proxy.rb +16 -0
  25. data/lib/octopus/exception.rb +4 -0
  26. data/lib/octopus/finder_methods.rb +8 -0
  27. data/lib/octopus/has_and_belongs_to_many_association.rb +9 -0
  28. data/lib/octopus/load_balancing.rb +4 -0
  29. data/lib/octopus/load_balancing/round_robin.rb +20 -0
  30. data/lib/octopus/log_subscriber.rb +26 -0
  31. data/lib/octopus/migration.rb +195 -0
  32. data/lib/octopus/model.rb +223 -0
  33. data/lib/octopus/persistence.rb +45 -0
  34. data/lib/octopus/proxy.rb +346 -0
  35. data/lib/octopus/proxy_config.rb +252 -0
  36. data/lib/octopus/query_cache_for_shards.rb +25 -0
  37. data/lib/octopus/railtie.rb +11 -0
  38. data/lib/octopus/relation_proxy.rb +58 -0
  39. data/lib/octopus/result_patch.rb +19 -0
  40. data/lib/octopus/scope_proxy.rb +68 -0
  41. data/lib/octopus/shard_tracking.rb +46 -0
  42. data/lib/octopus/shard_tracking/attribute.rb +22 -0
  43. data/lib/octopus/shard_tracking/dynamic.rb +11 -0
  44. data/lib/octopus/singular_association.rb +9 -0
  45. data/lib/octopus/slave_group.rb +13 -0
  46. data/lib/octopus/version.rb +3 -0
  47. data/lib/tasks/octopus.rake +16 -0
  48. data/sample_app/.gitignore +4 -0
  49. data/sample_app/.rspec +1 -0
  50. data/sample_app/Gemfile +20 -0
  51. data/sample_app/README +3 -0
  52. data/sample_app/README.rdoc +261 -0
  53. data/sample_app/Rakefile +7 -0
  54. data/sample_app/app/assets/images/rails.png +0 -0
  55. data/sample_app/app/assets/javascripts/application.js +15 -0
  56. data/sample_app/app/assets/stylesheets/application.css +13 -0
  57. data/sample_app/app/controllers/application_controller.rb +4 -0
  58. data/sample_app/app/helpers/application_helper.rb +2 -0
  59. data/sample_app/app/mailers/.gitkeep +0 -0
  60. data/sample_app/app/models/.gitkeep +0 -0
  61. data/sample_app/app/models/item.rb +3 -0
  62. data/sample_app/app/models/user.rb +3 -0
  63. data/sample_app/app/views/layouts/application.html.erb +14 -0
  64. data/sample_app/autotest/discover.rb +2 -0
  65. data/sample_app/config.ru +4 -0
  66. data/sample_app/config/application.rb +62 -0
  67. data/sample_app/config/boot.rb +6 -0
  68. data/sample_app/config/cucumber.yml +8 -0
  69. data/sample_app/config/database.yml +28 -0
  70. data/sample_app/config/environment.rb +5 -0
  71. data/sample_app/config/environments/development.rb +37 -0
  72. data/sample_app/config/environments/production.rb +67 -0
  73. data/sample_app/config/environments/test.rb +37 -0
  74. data/sample_app/config/initializers/backtrace_silencers.rb +7 -0
  75. data/sample_app/config/initializers/inflections.rb +15 -0
  76. data/sample_app/config/initializers/mime_types.rb +5 -0
  77. data/sample_app/config/initializers/secret_token.rb +7 -0
  78. data/sample_app/config/initializers/session_store.rb +8 -0
  79. data/sample_app/config/initializers/wrap_parameters.rb +14 -0
  80. data/sample_app/config/locales/en.yml +5 -0
  81. data/sample_app/config/routes.rb +58 -0
  82. data/sample_app/config/shards.yml +28 -0
  83. data/sample_app/db/migrate/20100720172715_create_users.rb +15 -0
  84. data/sample_app/db/migrate/20100720172730_create_items.rb +16 -0
  85. data/sample_app/db/migrate/20100720210335_create_sample_users.rb +11 -0
  86. data/sample_app/db/schema.rb +29 -0
  87. data/sample_app/db/seeds.rb +16 -0
  88. data/sample_app/doc/README_FOR_APP +2 -0
  89. data/sample_app/features/migrate.feature +45 -0
  90. data/sample_app/features/seed.feature +15 -0
  91. data/sample_app/features/step_definitions/seeds_steps.rb +13 -0
  92. data/sample_app/features/step_definitions/web_steps.rb +218 -0
  93. data/sample_app/features/support/database.rb +13 -0
  94. data/sample_app/features/support/env.rb +57 -0
  95. data/sample_app/features/support/paths.rb +33 -0
  96. data/sample_app/lib/assets/.gitkeep +0 -0
  97. data/sample_app/lib/tasks/.gitkeep +0 -0
  98. data/sample_app/lib/tasks/cucumber.rake +64 -0
  99. data/sample_app/log/.gitkeep +0 -0
  100. data/sample_app/public/404.html +26 -0
  101. data/sample_app/public/422.html +26 -0
  102. data/sample_app/public/500.html +26 -0
  103. data/sample_app/public/favicon.ico +0 -0
  104. data/sample_app/public/images/rails.png +0 -0
  105. data/sample_app/public/index.html +279 -0
  106. data/sample_app/public/javascripts/application.js +2 -0
  107. data/sample_app/public/javascripts/controls.js +965 -0
  108. data/sample_app/public/javascripts/dragdrop.js +974 -0
  109. data/sample_app/public/javascripts/effects.js +1123 -0
  110. data/sample_app/public/javascripts/prototype.js +4874 -0
  111. data/sample_app/public/javascripts/rails.js +118 -0
  112. data/sample_app/public/robots.txt +5 -0
  113. data/sample_app/public/stylesheets/.gitkeep +0 -0
  114. data/sample_app/script/cucumber +10 -0
  115. data/sample_app/script/rails +6 -0
  116. data/sample_app/spec/models/item_spec.rb +5 -0
  117. data/sample_app/spec/models/user_spec.rb +5 -0
  118. data/sample_app/spec/spec_helper.rb +27 -0
  119. data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
  120. data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
  121. data/sample_app/vendor/plugins/.gitkeep +0 -0
  122. data/spec/config/shards.yml +229 -0
  123. data/spec/migrations/10_create_users_using_replication.rb +9 -0
  124. data/spec/migrations/11_add_field_in_all_slaves.rb +11 -0
  125. data/spec/migrations/12_create_users_using_block.rb +23 -0
  126. data/spec/migrations/13_create_users_using_block_and_using.rb +15 -0
  127. data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
  128. data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +9 -0
  129. data/spec/migrations/1_create_users_on_master.rb +9 -0
  130. data/spec/migrations/2_create_users_on_canada.rb +11 -0
  131. data/spec/migrations/3_create_users_on_both_shards.rb +11 -0
  132. data/spec/migrations/4_create_users_on_shards_of_a_group.rb +11 -0
  133. data/spec/migrations/5_create_users_on_multiples_groups.rb +11 -0
  134. data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +11 -0
  135. data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +11 -0
  136. data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +11 -0
  137. data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +11 -0
  138. data/spec/octopus/association_shard_tracking_spec.rb +1036 -0
  139. data/spec/octopus/collection_proxy_spec.rb +16 -0
  140. data/spec/octopus/load_balancing/round_robin_spec.rb +15 -0
  141. data/spec/octopus/log_subscriber_spec.rb +19 -0
  142. data/spec/octopus/migration_spec.rb +134 -0
  143. data/spec/octopus/model_spec.rb +754 -0
  144. data/spec/octopus/octopus_spec.rb +123 -0
  145. data/spec/octopus/proxy_spec.rb +303 -0
  146. data/spec/octopus/query_cache_for_shards_spec.rb +17 -0
  147. data/spec/octopus/relation_proxy_spec.rb +124 -0
  148. data/spec/octopus/replicated_slave_grouped_spec.rb +91 -0
  149. data/spec/octopus/replication_spec.rb +196 -0
  150. data/spec/octopus/scope_proxy_spec.rb +97 -0
  151. data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
  152. data/spec/octopus/sharded_spec.rb +33 -0
  153. data/spec/spec_helper.rb +18 -0
  154. data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +15 -0
  155. data/spec/support/database_connection.rb +4 -0
  156. data/spec/support/database_models.rb +118 -0
  157. data/spec/support/octopus_helper.rb +54 -0
  158. data/spec/support/query_count.rb +17 -0
  159. data/spec/support/shared_contexts.rb +18 -0
  160. data/spec/tasks/octopus.rake_spec.rb +32 -0
  161. metadata +203 -5
@@ -0,0 +1,23 @@
1
+ class CreateUsersUsingBlock < BaseOctopusMigrationClass
2
+ def self.up
3
+ Octopus.using(:brazil) do
4
+ User.create!(:name => 'UsingBlock1')
5
+ User.create!(:name => 'UsingBlock2')
6
+ end
7
+
8
+ Octopus.using(:canada) do
9
+ User.create!(:name => 'UsingCanada')
10
+ User.create!(:name => 'UsingCanada2')
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ Octopus.using(:brazil) do
16
+ User.delete_all
17
+ end
18
+
19
+ Octopus.using(:canada) do
20
+ User.delete_all
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class CreateUsersUsingBlockAndUsing < BaseOctopusMigrationClass
2
+ using(:brazil)
3
+
4
+ def self.up
5
+ Octopus.using(:canada) do
6
+ User.create!(:name => 'Canada')
7
+ end
8
+
9
+ User.create!(:name => 'Brazil')
10
+ end
11
+
12
+ def self.down
13
+ User.delete_all
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersOnShardsOfAGroupWithVersions < BaseOctopusMigrationClass
2
+ using_group(:country_shards)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Group')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class CreateUserOnShardsOfDefaultGroupWithVersions < BaseOctopusMigrationClass
2
+ def self.up
3
+ User.create!(:name => 'Default Group')
4
+ end
5
+
6
+ def self.down
7
+ User.delete_all
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateUsersOnMaster < BaseOctopusMigrationClass
2
+ def self.up
3
+ User.create!(:name => 'Master')
4
+ end
5
+
6
+ def self.down
7
+ User.delete_all
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersOnCanada < BaseOctopusMigrationClass
2
+ using(:canada)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Sharding')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersOnBothShards < BaseOctopusMigrationClass
2
+ using(:brazil, :canada)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Both')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersOnShardsOfAGroup < BaseOctopusMigrationClass
2
+ using_group(:country_shards)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Group')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersOnMultiplesGroups < BaseOctopusMigrationClass
2
+ using_group('country_shards', 'history_shards')
3
+
4
+ def self.up
5
+ User.create!(:name => 'MultipleGroup')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class RaiseExceptionWithInvalidShardName < BaseOctopusMigrationClass
2
+ using(:amazing_shard)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Error')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class RaiseExceptionWithInvalidMultipleShardNames < BaseOctopusMigrationClass
2
+ using(:brazil, :invalid_shard)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Error')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class RaiseExceptionWithInvalidGroupName < BaseOctopusMigrationClass
2
+ using_group(:invalid_group)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Error')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class RaiseExceptionWithMultipleInvalidGroupNames < BaseOctopusMigrationClass
2
+ using_group(:country_shards, :invalid_group)
3
+
4
+ def self.up
5
+ User.create!(:name => 'Error')
6
+ end
7
+
8
+ def self.down
9
+ User.delete_all
10
+ end
11
+ end
@@ -0,0 +1,1036 @@
1
+ require 'spec_helper'
2
+
3
+ describe Octopus::AssociationShardTracking, :shards => [:brazil, :master, :canada] do
4
+ describe 'when you have a 1 x 1 relationship' do
5
+ before(:each) do
6
+ @computer_brazil = Computer.using(:brazil).create!(:name => 'Computer Brazil')
7
+ @computer_master = Computer.create!(:name => 'Computer Brazil')
8
+ @keyboard_brazil = Keyboard.using(:brazil).create!(:name => 'Keyboard Brazil', :computer => @computer_brazil)
9
+ @keyboard_master = Keyboard.create!(:name => 'Keyboard Master', :computer => @computer_master)
10
+ end
11
+
12
+ it 'should find the models' do
13
+ expect(@keyboard_master.computer).to eq(@computer_master)
14
+ expect(@keyboard_brazil.computer).to eq(@computer_brazil)
15
+ end
16
+
17
+ it 'should read correctly the relationed model' do
18
+ new_computer_brazil = Computer.using(:brazil).create!(:name => 'New Computer Brazil')
19
+ _new_computer_hello = Computer.create!(:name => 'New Computer Brazil')
20
+ @keyboard_brazil.computer = new_computer_brazil
21
+ @keyboard_brazil.save
22
+ @keyboard_brazil.reload
23
+ expect(@keyboard_brazil.computer_id).to eq(new_computer_brazil.id)
24
+ expect(@keyboard_brazil.computer).to eq(new_computer_brazil)
25
+ new_computer_brazil.save
26
+ new_computer_brazil.reload
27
+ expect(new_computer_brazil.keyboard).to eq(@keyboard_brazil)
28
+ end
29
+
30
+ it 'should work when using #build_computer or #build_keyboard' do
31
+ c = Computer.using(:brazil).create!(:name => 'Computer Brazil')
32
+ k = c.build_keyboard(:name => 'Building keyboard')
33
+ c.save
34
+ k.save
35
+ expect(c.keyboard).to eq(k)
36
+ expect(k.computer_id).to eq(c.id)
37
+ expect(k.computer).to eq(c)
38
+ end
39
+
40
+ it 'should work when using #create_computer or #create_keyboard' do
41
+ c = Computer.using(:brazil).create!(:name => 'Computer Brazil')
42
+ k = c.create_keyboard(:name => 'Building keyboard')
43
+ c.save
44
+ k.save
45
+ expect(c.keyboard).to eq(k)
46
+ expect(k.computer_id).to eq(c.id)
47
+ expect(k.computer).to eq(c)
48
+ end
49
+
50
+ it 'should include models' do
51
+ c = Computer.using(:brazil).create!(:name => 'Computer Brazil')
52
+ k = c.create_keyboard(:name => 'Building keyboard')
53
+ c.save
54
+ k.save
55
+
56
+ expect(Computer.using(:brazil).includes(:keyboard).find(c.id)).to eq(c)
57
+ end
58
+ end
59
+
60
+ describe 'when you have a N x N relationship' do
61
+ before(:each) do
62
+ @brazil_role = Role.using(:brazil).create!(:name => 'Brazil Role')
63
+ @master_role = Role.create!(:name => 'Master Role')
64
+ @permission_brazil = Permission.using(:brazil).create!(:name => 'Brazil Permission')
65
+ @permission_master = Permission.using(:master).create!(:name => 'Master Permission')
66
+ @brazil_role.permissions << @permission_brazil
67
+ @brazil_role.save
68
+ Client.using(:master).create!(:name => 'teste')
69
+ end
70
+
71
+ it 'should find all models in the specified shard' do
72
+ expect(@brazil_role.permission_ids).to eq([@permission_brazil.id])
73
+ expect(@brazil_role.permissions).to eq([@permission_brazil])
74
+
75
+ expect(@brazil_role.permissions.first).to eq(@permission_brazil)
76
+ expect(@brazil_role.permissions.first!).to eq(@permission_brazil)
77
+ expect(@brazil_role.permissions.last).to eq(@permission_brazil)
78
+ end
79
+
80
+ it 'should finds the client that the item belongs' do
81
+ expect(@permission_brazil.role_ids).to eq([@brazil_role.id])
82
+ expect(@permission_brazil.roles).to eq([@brazil_role])
83
+
84
+ expect(@permission_brazil.roles.first).to eq(@brazil_role)
85
+ expect(@permission_brazil.roles.first!).to eq(@brazil_role)
86
+ expect(@permission_brazil.roles.last).to eq(@brazil_role)
87
+ end
88
+
89
+ it 'should update the attribute for the item' do
90
+ new_brazil_role = Role.using(:brazil).create!(:name => 'new Role')
91
+ @permission_brazil.roles = [new_brazil_role]
92
+ expect(@permission_brazil.roles).to eq([new_brazil_role])
93
+ @permission_brazil.save
94
+ @permission_brazil.reload
95
+ expect(@permission_brazil.role_ids).to eq([new_brazil_role.id])
96
+ expect(@permission_brazil.roles).to eq([new_brazil_role])
97
+ end
98
+
99
+ it 'should work for build method' do
100
+ new_brazil_role = Role.using(:brazil).create!(:name => 'Brazil Role')
101
+ c = new_brazil_role.permissions.create(:name => 'new Permission')
102
+ c.save
103
+ new_brazil_role.save
104
+ expect(c.roles).to eq([new_brazil_role])
105
+ expect(new_brazil_role.permissions).to eq([c])
106
+ end
107
+
108
+ describe 'it should work when using' do
109
+ before(:each) do
110
+ @permission_brazil_2 = Permission.using(:brazil).create!(:name => 'Brazil Item 2')
111
+ @role = Role.using(:brazil).create!(:name => 'testes')
112
+ end
113
+
114
+ it 'update_attributes' do
115
+ @permission_brazil_2.update_attributes(:role_ids => [@role.id])
116
+ expect(@permission_brazil_2.roles.to_set).to eq([@role].to_set)
117
+ end
118
+
119
+ it 'update_attribute' do
120
+ @permission_brazil_2.update_attribute(:role_ids, [@role.id])
121
+ expect(@permission_brazil_2.roles.to_set).to eq([@role].to_set)
122
+ end
123
+
124
+ it '<<' do
125
+ @permission_brazil_2.roles << @role
126
+ @role.save
127
+ @permission_brazil_2.save
128
+ @permission_brazil_2.reload
129
+ expect(@permission_brazil_2.roles.to_set).to eq([@role].to_set)
130
+ end
131
+
132
+ it 'build' do
133
+ role = @permission_brazil_2.roles.build(:name => 'Builded Role')
134
+ @permission_brazil_2.save
135
+ expect(@permission_brazil_2.roles.to_set).to eq([role].to_set)
136
+ end
137
+
138
+ it 'create' do
139
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
140
+ expect(@permission_brazil_2.roles.to_set).to eq([role].to_set)
141
+ end
142
+
143
+ it 'create' do
144
+ role = @permission_brazil_2.roles.create!(:name => 'Builded Role')
145
+ expect(@permission_brazil_2.roles.to_set).to eq([role].to_set)
146
+ end
147
+
148
+ it 'count' do
149
+ expect(@permission_brazil_2.roles.count).to eq(0)
150
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
151
+ expect(@permission_brazil_2.roles.count).to eq(1)
152
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
153
+ expect(@permission_brazil_2.roles.count).to eq(2)
154
+ end
155
+
156
+ it 'size' do
157
+ expect(@permission_brazil_2.roles.size).to eq(0)
158
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
159
+ expect(@permission_brazil_2.roles.size).to eq(1)
160
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
161
+ expect(@permission_brazil_2.roles.size).to eq(2)
162
+ end
163
+
164
+ it 'length' do
165
+ expect(@permission_brazil_2.roles.length).to eq(0)
166
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
167
+ expect(@permission_brazil_2.roles.length).to eq(1)
168
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
169
+ expect(@permission_brazil_2.roles.length).to eq(2)
170
+ end
171
+
172
+ it 'empty?' do
173
+ expect(@permission_brazil_2.roles.empty?).to be true
174
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
175
+ expect(@permission_brazil_2.roles.empty?).to be false
176
+ end
177
+
178
+ it 'delete_all' do
179
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
180
+ expect(@permission_brazil_2.roles.empty?).to be false
181
+ @permission_brazil_2.roles.delete_all
182
+ expect(@permission_brazil_2.roles.empty?).to be true
183
+ end
184
+
185
+ it 'destroy_all' do
186
+ _role = @permission_brazil_2.roles.create(:name => 'Builded Role')
187
+ expect(@permission_brazil_2.roles.empty?).to be false
188
+ @permission_brazil_2.roles.destroy_all
189
+ expect(@permission_brazil_2.roles.empty?).to be true
190
+ end
191
+
192
+ it 'find' do
193
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
194
+ expect(@permission_brazil_2.roles.first).to eq(role)
195
+ @permission_brazil_2.roles.destroy_all
196
+ expect(@permission_brazil_2.roles.first).to be_nil
197
+ end
198
+
199
+ it 'where' do
200
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
201
+ expect(@permission_brazil_2.roles.where('1=1')).to eq([role])
202
+ @permission_brazil_2.roles.destroy_all
203
+ expect(@permission_brazil_2.roles.where('1=1')).to be_empty
204
+ end
205
+
206
+ it 'map' do
207
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
208
+ expect(@permission_brazil_2.roles.map(&:id)).to eq([role.id])
209
+ @permission_brazil_2.roles.destroy_all
210
+ expect(@permission_brazil_2.roles.map(&:id)).to be_empty
211
+ end
212
+
213
+ it 'where + map' do
214
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
215
+ expect(@permission_brazil_2.roles.where('1=1').map(&:id)).to eq([role.id])
216
+ @permission_brazil_2.roles.destroy_all
217
+ expect(@permission_brazil_2.roles.where('1=1').map(&:id)).to be_empty
218
+ end
219
+
220
+ # each_with_index is not listed in active_record/relation/delegation.rb
221
+ it 'where + each_with_index + map (enum method chain)' do
222
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
223
+ expect(@permission_brazil_2.roles.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to eq([[role.id, 0]])
224
+ @permission_brazil_2.roles.destroy_all
225
+ expect(@permission_brazil_2.roles.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to be_empty
226
+ end
227
+
228
+ # sum & index_by is specialized in active_support/core_ext/enumerable.rb
229
+ it 'where + sum' do
230
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
231
+ expect(@permission_brazil_2.roles.where('1=1').sum(&:id)).to eq(role.id)
232
+ @permission_brazil_2.roles.destroy_all
233
+ expect(@permission_brazil_2.roles.where('1=1').sum(&:id)).to eq(0)
234
+ end
235
+
236
+ it 'where + index_by' do
237
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
238
+ expect(@permission_brazil_2.roles.where('1=1').index_by(&:id)).to eq(role.id => role)
239
+ @permission_brazil_2.roles.destroy_all
240
+ expect(@permission_brazil_2.roles.where('1=1').index_by(&:id)).to be_empty
241
+ end
242
+
243
+ it 'where + find' do
244
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
245
+ expect(@permission_brazil_2.roles.where('1=1').find([role.id])).to eq([role])
246
+ @permission_brazil_2.roles.destroy_all
247
+ expect { @permission_brazil_2.roles.where('1=1').find([role.id]) }.to raise_error ActiveRecord::RecordNotFound
248
+ end
249
+
250
+ it 'where + find with block' do
251
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
252
+ expect(@permission_brazil_2.roles.where('1=1').find { |r| r.id == role.id }).to eq(role)
253
+ @permission_brazil_2.roles.destroy_all
254
+ expect(@permission_brazil_2.roles.where('1=1').find { |r| r.id == role.id }).to be_nil
255
+ end
256
+
257
+ it 'where + select' do
258
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
259
+ expect(@permission_brazil_2.roles.where('1=1').select(:name).first.name).to eq(role.name)
260
+ @permission_brazil_2.roles.destroy_all
261
+ expect(@permission_brazil_2.roles.where('1=1').select(:name)).to be_empty
262
+ end
263
+
264
+ it 'where + select with block' do
265
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
266
+ expect(@permission_brazil_2.roles.where('1=1').select { |r| r.id == role.id }).to eq([role])
267
+ @permission_brazil_2.roles.destroy_all
268
+ expect(@permission_brazil_2.roles.where('1=1').select { |r| r.id == role.id }).to be_empty
269
+ end
270
+
271
+ it 'where + any?' do
272
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
273
+ expect(@permission_brazil_2.roles.where('1=1').any?).to be true
274
+ @permission_brazil_2.roles.destroy_all
275
+ expect(@permission_brazil_2.roles.where('1=1').any?).to be false
276
+ end
277
+
278
+ it 'where + any? with block' do
279
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
280
+ expect(@permission_brazil_2.roles.where('1=1').any? { |r| r.id == role.id }).to be true
281
+ @permission_brazil_2.roles.destroy_all
282
+ expect(@permission_brazil_2.roles.where('1=1').any? { |r| r.id == role.id }).to be false
283
+ end
284
+
285
+ it 'exists?' do
286
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
287
+ expect(@permission_brazil_2.roles.exists?(role.id)).to be true
288
+ @permission_brazil_2.roles.destroy_all
289
+ expect(@permission_brazil_2.roles.exists?(role.id)).to be false
290
+ end
291
+
292
+ it 'clear' do
293
+ _rol = @permission_brazil_2.roles.create(:name => 'Builded Role')
294
+ expect(@permission_brazil_2.roles.empty?).to be false
295
+ @permission_brazil_2.roles.clear
296
+ expect(@permission_brazil_2.roles.empty?).to be true
297
+ end
298
+
299
+ it 'delete' do
300
+ role = @permission_brazil_2.roles.create(:name => 'Builded Role')
301
+ expect(@permission_brazil_2.roles.empty?).to be false
302
+ @permission_brazil_2.roles.delete(role)
303
+ @permission_brazil_2.reload
304
+ @role.reload
305
+ expect(@role.permissions).to eq([])
306
+ expect(@permission_brazil_2.roles).to eq([])
307
+ end
308
+ end
309
+ end
310
+
311
+ describe 'when you have has_many :through' do
312
+ before(:each) do
313
+ @programmer = Programmer.using(:brazil).create!(:name => 'Thiago')
314
+ @project = Project.using(:brazil).create!(:name => 'RubySoc')
315
+ @project2 = Project.using(:brazil).create!(:name => 'Cobol Application')
316
+ @programmer.projects << @project
317
+ @programmer.save
318
+ Project.using(:master).create!(:name => 'Project Master')
319
+ end
320
+
321
+ it 'should find all models in the specified shard' do
322
+ expect(@programmer.project_ids).to eq([@project.id])
323
+ expect(@programmer.projects).to eq([@project])
324
+
325
+ expect(@programmer.projects.first).to eq(@project)
326
+ expect(@programmer.projects.last).to eq(@project)
327
+ end
328
+
329
+ it 'should update the attribute for the item' do
330
+ new_brazil_programmer = Programmer.using(:brazil).create!(:name => 'Joao')
331
+ @project.programmers = [new_brazil_programmer]
332
+ expect(@project.programmers).to eq([new_brazil_programmer])
333
+ @project.save
334
+ @project.reload
335
+ expect(@project.programmer_ids).to eq([new_brazil_programmer.id])
336
+ expect(@project.programmers).to eq([new_brazil_programmer])
337
+ end
338
+
339
+ it 'should work for create method' do
340
+ new_brazil_programmer = Programmer.using(:brazil).create!(:name => 'Joao')
341
+ c = new_brazil_programmer.projects.create(:name => 'new Project')
342
+ c.save
343
+ new_brazil_programmer.save
344
+ expect(c.programmers).to eq([new_brazil_programmer])
345
+ expect(new_brazil_programmer.projects).to eq([c])
346
+ end
347
+
348
+ describe 'it should work when using' do
349
+ before(:each) do
350
+ @new_brazil_programmer = Programmer.using(:brazil).create!(:name => 'Jose')
351
+ @project = Project.using(:brazil).create!(:name => 'VB Application :-(')
352
+ end
353
+
354
+ it 'update_attributes' do
355
+ @new_brazil_programmer.update_attributes(:project_ids => [@project.id])
356
+ expect(@new_brazil_programmer.projects.to_set).to eq([@project].to_set)
357
+ end
358
+
359
+ it 'update_attribute' do
360
+ @new_brazil_programmer.update_attribute(:project_ids, [@project.id])
361
+ expect(@new_brazil_programmer.projects.to_set).to eq([@project].to_set)
362
+ end
363
+
364
+ it '<<' do
365
+ @new_brazil_programmer.projects << @project
366
+ @project.save
367
+ @new_brazil_programmer.save
368
+ @new_brazil_programmer.reload
369
+ expect(@new_brazil_programmer.projects.to_set).to eq([@project].to_set)
370
+ end
371
+
372
+ it 'build' do
373
+ role = @new_brazil_programmer.projects.build(:name => 'New VB App :-/')
374
+ @new_brazil_programmer.save
375
+ expect(@new_brazil_programmer.projects.to_set).to eq([role].to_set)
376
+ end
377
+
378
+ it 'create' do
379
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
380
+ expect(@new_brazil_programmer.projects.to_set).to eq([role].to_set)
381
+ end
382
+
383
+ it 'create' do
384
+ role = @new_brazil_programmer.projects.create!(:name => 'New VB App :-/')
385
+ expect(@new_brazil_programmer.projects.to_set).to eq([role].to_set)
386
+ end
387
+
388
+ it 'count' do
389
+ expect(@new_brazil_programmer.projects.count).to eq(0)
390
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
391
+ expect(@new_brazil_programmer.projects.count).to eq(1)
392
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
393
+ expect(@new_brazil_programmer.projects.count).to eq(2)
394
+ end
395
+
396
+ it 'size' do
397
+ expect(@new_brazil_programmer.projects.size).to eq(0)
398
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
399
+ expect(@new_brazil_programmer.projects.size).to eq(1)
400
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
401
+ expect(@new_brazil_programmer.projects.size).to eq(2)
402
+ end
403
+
404
+ it 'length' do
405
+ expect(@new_brazil_programmer.projects.length).to eq(0)
406
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
407
+ expect(@new_brazil_programmer.projects.length).to eq(1)
408
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
409
+ expect(@new_brazil_programmer.projects.length).to eq(2)
410
+ end
411
+
412
+ it 'empty?' do
413
+ expect(@new_brazil_programmer.projects.empty?).to be true
414
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
415
+ expect(@new_brazil_programmer.projects.empty?).to be false
416
+ end
417
+
418
+ it 'delete_all' do
419
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
420
+ expect(@new_brazil_programmer.projects.empty?).to be false
421
+ @new_brazil_programmer.projects.delete_all
422
+ expect(@new_brazil_programmer.projects.empty?).to be true
423
+ end
424
+
425
+ it 'destroy_all' do
426
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
427
+ expect(@new_brazil_programmer.projects.empty?).to be false
428
+ @new_brazil_programmer.projects.destroy_all
429
+ expect(@new_brazil_programmer.projects.empty?).to be true
430
+ end
431
+
432
+ it 'find' do
433
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
434
+ expect(@new_brazil_programmer.projects.first).to eq(role)
435
+ @new_brazil_programmer.projects.destroy_all
436
+ expect(@new_brazil_programmer.projects.first).to be_nil
437
+ end
438
+
439
+ it 'where' do
440
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
441
+ expect(@new_brazil_programmer.projects.where('1=1')).to eq([role])
442
+ @new_brazil_programmer.projects.destroy_all
443
+ expect(@new_brazil_programmer.projects.where('1=1')).to be_empty
444
+ end
445
+
446
+ it 'map' do
447
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
448
+ expect(@new_brazil_programmer.projects.map(&:id)).to eq([role.id])
449
+ @new_brazil_programmer.projects.destroy_all
450
+ expect(@new_brazil_programmer.projects.map(&:id)).to be_empty
451
+ end
452
+
453
+ it 'where + map' do
454
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
455
+ expect(@new_brazil_programmer.projects.where('1=1').map(&:id)).to eq([role.id])
456
+ @new_brazil_programmer.projects.destroy_all
457
+ expect(@new_brazil_programmer.projects.where('1=1').map(&:id)).to be_empty
458
+ end
459
+
460
+ it 'where + each_with_index + map (enum method chain)' do
461
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
462
+ expect(@new_brazil_programmer.projects.where('1=1').each_with_index.map { |r, i| [r.id, i] }).to eq([[role.id, 0]])
463
+ @new_brazil_programmer.projects.destroy_all
464
+ expect(@new_brazil_programmer.projects.where('1=1').each_with_index.map { |r, i| [r.id, i] }).to be_empty
465
+ end
466
+
467
+ it 'where + sum' do
468
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
469
+ expect(@new_brazil_programmer.projects.where('1=1').sum(&:id)).to eq(role.id)
470
+ @new_brazil_programmer.projects.destroy_all
471
+ expect(@new_brazil_programmer.projects.where('1=1').sum(&:id)).to eq(0)
472
+ end
473
+
474
+ it 'where + index_by' do
475
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
476
+ expect(@new_brazil_programmer.projects.where('1=1').index_by(&:id)).to eq(role.id => role)
477
+ @new_brazil_programmer.projects.destroy_all
478
+ expect(@new_brazil_programmer.projects.where('1=1').index_by(&:id)).to be_empty
479
+ end
480
+
481
+ it 'where + find' do
482
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
483
+ expect(@new_brazil_programmer.projects.where('1=1').find(role.id)).to eq(role)
484
+ @new_brazil_programmer.projects.destroy_all
485
+ expect { @new_brazil_programmer.projects.where('1=1').find(role.id) }.to raise_error ActiveRecord::RecordNotFound
486
+ end
487
+
488
+ it 'where + find with block' do
489
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
490
+ expect(@new_brazil_programmer.projects.where('1=1').find { |r| r.id == role.id }).to eq(role)
491
+ @new_brazil_programmer.projects.destroy_all
492
+ expect(@new_brazil_programmer.projects.where('1=1').find { |r| r.id == role.id }).to be_nil
493
+ end
494
+
495
+ it 'where + select' do
496
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
497
+ expect(@new_brazil_programmer.projects.where('1=1').select(:name).first.name).to eq(role.name)
498
+ @new_brazil_programmer.projects.destroy_all
499
+ expect(@new_brazil_programmer.projects.where('1=1').select(:name)).to be_empty
500
+ end
501
+
502
+ it 'where + select with block' do
503
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
504
+ expect(@new_brazil_programmer.projects.where('1=1').select { |r| r.id == role.id }).to eq([role])
505
+ @new_brazil_programmer.projects.destroy_all
506
+ expect(@new_brazil_programmer.projects.where('1=1').select { |r| r.id == role.id }).to be_empty
507
+ end
508
+
509
+ it 'where + any?' do
510
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
511
+ expect(@new_brazil_programmer.projects.where('1=1').any?).to be true
512
+ @new_brazil_programmer.projects.destroy_all
513
+ expect(@new_brazil_programmer.projects.where('1=1').any?).to be false
514
+ end
515
+
516
+ it 'where + any? with block' do
517
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
518
+ expect(@new_brazil_programmer.projects.where('1=1').any? { |r| r.id == role.id }).to be true
519
+ @new_brazil_programmer.projects.destroy_all
520
+ expect(@new_brazil_programmer.projects.where('1=1').any? { |r| r.id == role.id }).to be false
521
+ end
522
+
523
+ it 'exists?' do
524
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
525
+ expect(@new_brazil_programmer.projects.exists?(role.id)).to be true
526
+ @new_brazil_programmer.projects.destroy_all
527
+ expect(@new_brazil_programmer.projects.exists?(role.id)).to be false
528
+ end
529
+
530
+ it 'clear' do
531
+ _role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
532
+ expect(@new_brazil_programmer.projects.empty?).to be false
533
+ @new_brazil_programmer.projects.clear
534
+ expect(@new_brazil_programmer.projects.empty?).to be true
535
+ end
536
+
537
+ it 'delete' do
538
+ role = @new_brazil_programmer.projects.create(:name => 'New VB App :-/')
539
+ expect(@new_brazil_programmer.projects.empty?).to be false
540
+ @new_brazil_programmer.projects.delete(role)
541
+ @new_brazil_programmer.reload
542
+ @project.reload
543
+ expect(@project.programmers).to eq([])
544
+ expect(@new_brazil_programmer.projects).to eq([])
545
+ end
546
+ end
547
+ end
548
+
549
+ describe 'when you have a 1 x N relationship' do
550
+ before(:each) do
551
+ @brazil_client = Client.using(:brazil).create!(:name => 'Brazil Client')
552
+ @master_client = Client.create!(:name => 'Master Client')
553
+ @item_brazil = Item.using(:brazil).create!(:name => 'Brazil Item', :client => @brazil_client)
554
+ @item_master = Item.create!(:name => 'Master Item', :client => @master_client)
555
+ @brazil_client = Client.using(:brazil).find_by_name('Brazil Client')
556
+ Client.using(:master).create!(:name => 'teste')
557
+ end
558
+
559
+ it 'should find all models in the specified shard' do
560
+ expect(@brazil_client.item_ids).to eq([@item_brazil.id])
561
+ expect(@brazil_client.items).to eq([@item_brazil])
562
+
563
+ expect(@brazil_client.items.last).to eq(@item_brazil)
564
+ expect(@brazil_client.items.first).to eq(@item_brazil)
565
+ end
566
+
567
+ it 'should finds the client that the item belongs' do
568
+ expect(@item_brazil.client).to eq(@brazil_client)
569
+ end
570
+
571
+ it 'should raise error if you try to add a record from a different shard' do
572
+ expect do
573
+ @brazil_client.items << Item.using(:canada).create!(:name => 'New User')
574
+ end.to raise_error(Octopus::AssociationShardTracking::MismatchedShards)
575
+ end
576
+
577
+ it 'should make difference if the shard is a symbol or a string for raising Octopus::AssociationShardTracking::MismatchedShards' do
578
+ expect do
579
+ @brazil_client.items << Item.using('brazil').create!(:name => 'New Brazil Item')
580
+ end.not_to raise_error
581
+ end
582
+
583
+ it 'should update the attribute for the item' do
584
+ new_brazil_client = Client.using(:brazil).create!(:name => 'new Client')
585
+ @item_brazil.client = new_brazil_client
586
+ expect(@item_brazil.client).to eq(new_brazil_client)
587
+ @item_brazil.save
588
+ @item_brazil.reload
589
+ expect(@item_brazil.client_id).to eq(new_brazil_client.id)
590
+ expect(@item_brazil.client).to eq(new_brazil_client)
591
+ end
592
+
593
+ it 'should work for build method' do
594
+ item2 = Item.using(:brazil).create!(:name => 'Brazil Item')
595
+ c = item2.create_client(:name => 'new Client')
596
+ c.save
597
+ item2.save
598
+ expect(item2.client).to eq(c)
599
+ expect(c.items).to eq([item2])
600
+ end
601
+
602
+ context 'when calling methods on a collection generated by an association' do
603
+ let(:collection) { @brazil_client.items }
604
+ before :each do
605
+ @brazil_client.items.create(:name => 'Brazil Item #2')
606
+ end
607
+
608
+ it "can call collection indexes directly without resetting the collection's current_shard" do
609
+ last_item = collection[1]
610
+ expect(collection.length).to eq(2)
611
+ expect(collection).to eq([collection[0], last_item])
612
+ end
613
+
614
+ it "can call methods on the collection without resetting the collection's current_shard" do
615
+ last_item = collection[collection.size - 1]
616
+ expect(collection.length).to eq(2)
617
+ expect(collection).to eq([collection[0], last_item])
618
+ end
619
+ end
620
+
621
+ describe 'it should work when using' do
622
+ before(:each) do
623
+ @item_brazil_2 = Item.using(:brazil).create!(:name => 'Brazil Item 2')
624
+ expect(@brazil_client.items.to_set).to eq([@item_brazil].to_set)
625
+ end
626
+
627
+ it 'update_attributes' do
628
+ @brazil_client.update_attributes(:item_ids => [@item_brazil_2.id, @item_brazil.id])
629
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, @item_brazil_2].to_set)
630
+ end
631
+
632
+ it 'update_attribute' do
633
+ @brazil_client.update_attribute(:item_ids, [@item_brazil_2.id, @item_brazil.id])
634
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, @item_brazil_2].to_set)
635
+ end
636
+
637
+ it '<<' do
638
+ @brazil_client.items << @item_brazil_2
639
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, @item_brazil_2].to_set)
640
+ end
641
+
642
+ it 'all' do
643
+ item = @brazil_client.items.build(:name => 'Builded Item')
644
+ item.save
645
+ i = @brazil_client.items
646
+ expect(i.to_set).to eq([@item_brazil, item].to_set)
647
+ expect(i.reload.all.to_set).to eq([@item_brazil, item].to_set)
648
+ end
649
+
650
+ it 'build' do
651
+ item = @brazil_client.items.build(:name => 'Builded Item')
652
+ item.save
653
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, item].to_set)
654
+ end
655
+
656
+ it 'create' do
657
+ item = @brazil_client.items.create(:name => 'Builded Item')
658
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, item].to_set)
659
+ end
660
+
661
+ it 'count' do
662
+ expect(@brazil_client.items.count).to eq(1)
663
+ _itm = @brazil_client.items.create(:name => 'Builded Item')
664
+ expect(@brazil_client.items.count).to eq(2)
665
+ end
666
+
667
+ it 'size' do
668
+ expect(@brazil_client.items.size).to eq(1)
669
+ _itm = @brazil_client.items.create(:name => 'Builded Item')
670
+ expect(@brazil_client.items.size).to eq(2)
671
+ end
672
+
673
+ it 'create!' do
674
+ item = @brazil_client.items.create!(:name => 'Builded Item')
675
+ expect(@brazil_client.items.to_set).to eq([@item_brazil, item].to_set)
676
+ end
677
+
678
+ it 'length' do
679
+ expect(@brazil_client.items.length).to eq(1)
680
+ _itm = @brazil_client.items.create(:name => 'Builded Item')
681
+ expect(@brazil_client.items.length).to eq(2)
682
+ end
683
+
684
+ it 'empty?' do
685
+ expect(@brazil_client.items.empty?).to be false
686
+ c = Client.create!(:name => 'Client1')
687
+ expect(c.items.empty?).to be true
688
+ end
689
+
690
+ it 'delete' do
691
+ expect(@brazil_client.items.empty?).to be false
692
+ @brazil_client.items.delete(@item_brazil)
693
+ @brazil_client.reload
694
+ @item_brazil.reload
695
+ expect(@item_brazil.client).to be_nil
696
+ expect(@brazil_client.items).to eq([])
697
+ expect(@brazil_client.items.empty?).to be true
698
+ end
699
+
700
+ it 'delete_all' do
701
+ expect(@brazil_client.items.empty?).to be false
702
+ @brazil_client.items.delete_all
703
+ expect(@brazil_client.items.empty?).to be true
704
+ end
705
+
706
+ it 'destroy_all' do
707
+ expect(@brazil_client.items.empty?).to be false
708
+ @brazil_client.items.destroy_all
709
+ expect(@brazil_client.items.empty?).to be true
710
+ end
711
+
712
+ it 'find' do
713
+ expect(@brazil_client.items.first).to eq(@item_brazil)
714
+ @brazil_client.items.destroy_all
715
+ expect(@brazil_client.items.first).to be_nil
716
+ end
717
+
718
+ it 'where' do
719
+ expect(@brazil_client.items.where('1=1')).to eq([@item_brazil])
720
+ @brazil_client.items.destroy_all
721
+ expect(@brazil_client.items.where('1=1')).to be_empty
722
+ end
723
+
724
+ it 'map' do
725
+ expect(@brazil_client.items.map(&:id)).to eq([@item_brazil.id])
726
+ @brazil_client.items.destroy_all
727
+ expect(@brazil_client.items.map(&:id)).to be_empty
728
+ end
729
+
730
+ it 'where + map' do
731
+ expect(@brazil_client.items.where('1=1').map(&:id)).to eq([@item_brazil.id])
732
+ @brazil_client.items.destroy_all
733
+ expect(@brazil_client.items.where('1=1').map(&:id)).to be_empty
734
+ end
735
+
736
+ it 'where + each_with_index + map (enum method chain)' do
737
+ expect(@brazil_client.items.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to eq([[@item_brazil.id, 0]])
738
+ @brazil_client.items.destroy_all
739
+ expect(@brazil_client.items.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to be_empty
740
+ end
741
+
742
+ it 'where + sum' do
743
+ expect(@brazil_client.items.where('1=1').sum(&:id)).to eq(@item_brazil.id)
744
+ @brazil_client.items.destroy_all
745
+ expect(@brazil_client.items.where('1=1').sum(&:id)).to eq(0)
746
+ end
747
+
748
+ it 'where + index_by' do
749
+ expect(@brazil_client.items.where('1=1').index_by(&:id)).to eq(@item_brazil.id => @item_brazil)
750
+ @brazil_client.items.destroy_all
751
+ expect(@brazil_client.items.where('1=1').index_by(&:id)).to be_empty
752
+ end
753
+
754
+ it 'where + find' do
755
+ expect(@brazil_client.items.where('1=1').find(@item_brazil.id)).to eq(@item_brazil)
756
+ @brazil_client.items.destroy_all
757
+ expect { @brazil_client.items.where('1=1').find(@item_brazil.id) }.to raise_error ActiveRecord::RecordNotFound
758
+ end
759
+
760
+ it 'where + find with block' do
761
+ expect(@brazil_client.items.where('1=1').find { |i| i.id == @item_brazil.id }).to eq(@item_brazil)
762
+ @brazil_client.items.destroy_all
763
+ expect(@brazil_client.items.where('1=1').find { |i| i.id == @item_brazil.id }).to be_nil
764
+ end
765
+
766
+ it 'where + select' do
767
+ expect(@brazil_client.items.where('1=1').select(:name).first.name).to eq(@item_brazil.name)
768
+ @brazil_client.items.destroy_all
769
+ expect(@brazil_client.items.where('1=1').select(:name)).to be_empty
770
+ end
771
+
772
+ it 'where + select with block' do
773
+ expect(@brazil_client.items.where('1=1').select { |i| i.id == @item_brazil.id }).to eq([@item_brazil])
774
+ @brazil_client.items.destroy_all
775
+ expect(@brazil_client.items.where('1=1').select { |i| i.id == @item_brazil.id }).to be_empty
776
+ end
777
+
778
+ it 'where + any?' do
779
+ expect(@brazil_client.items.where('1=1').any?).to be true
780
+ @brazil_client.items.destroy_all
781
+ expect(@brazil_client.items.where('1=1').any?).to be false
782
+ end
783
+
784
+ it 'where + any? with block' do
785
+ expect(@brazil_client.items.where('1=1').any? { |i| i.id == @item_brazil.id }).to be true
786
+ @brazil_client.items.destroy_all
787
+ expect(@brazil_client.items.where('1=1').any? { |i| i.id == @item_brazil.id }).to be false
788
+ end
789
+
790
+ it 'exists?' do
791
+ expect(@brazil_client.items.exists?(@item_brazil.id)).to be true
792
+ @brazil_client.items.destroy_all
793
+ expect(@brazil_client.items.exists?(@item_brazil.id)).to be false
794
+ end
795
+
796
+ it 'uniq' do
797
+ expect(@brazil_client.items.uniq).to eq([@item_brazil])
798
+ end
799
+
800
+ it 'clear' do
801
+ expect(@brazil_client.items.empty?).to be false
802
+ @brazil_client.items.clear
803
+ expect(@brazil_client.items.empty?).to be true
804
+ end
805
+ end
806
+ end
807
+
808
+ describe 'when you have a 1 x N polymorphic relationship' do
809
+ before(:each) do
810
+ @brazil_client = Client.using(:brazil).create!(:name => 'Brazil Client')
811
+ @master_client = Client.create!(:name => 'Master Client')
812
+ @comment_brazil = Comment.using(:brazil).create!(:name => 'Brazil Comment', :commentable => @brazil_client)
813
+ @comment_master = Comment.create!(:name => 'Master Comment', :commentable => @master_client)
814
+ @brazil_client = Client.using(:brazil).find_by_name('Brazil Client')
815
+ Client.using(:master).create!(:name => 'teste')
816
+ end
817
+
818
+ it 'should find all models in the specified shard' do
819
+ expect(@brazil_client.comment_ids).to eq([@comment_brazil.id])
820
+ expect(@brazil_client.comments).to eq([@comment_brazil])
821
+ end
822
+
823
+ it 'should finds the client that the comment belongs' do
824
+ expect(@comment_brazil.commentable).to eq(@brazil_client)
825
+ end
826
+
827
+ it 'should update the attribute for the comment' do
828
+ new_brazil_client = Client.using(:brazil).create!(:name => 'new Client')
829
+ @comment_brazil.commentable = new_brazil_client
830
+ expect(@comment_brazil.commentable).to eq(new_brazil_client)
831
+ @comment_brazil.save
832
+ @comment_brazil.reload
833
+ expect(@comment_brazil.commentable_id).to eq(new_brazil_client.id)
834
+ expect(@comment_brazil.commentable).to eq(new_brazil_client)
835
+ end
836
+
837
+ describe 'it should work when using' do
838
+ before(:each) do
839
+ @comment_brazil_2 = Comment.using(:brazil).create!(:name => 'Brazil Comment 2')
840
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil].to_set)
841
+ end
842
+
843
+ it 'update_attributes' do
844
+ @brazil_client.update_attributes(:comment_ids => [@comment_brazil_2.id, @comment_brazil.id])
845
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, @comment_brazil_2].to_set)
846
+ end
847
+
848
+ it 'update_attribute' do
849
+ @brazil_client.update_attribute(:comment_ids, [@comment_brazil_2.id, @comment_brazil.id])
850
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, @comment_brazil_2].to_set)
851
+ end
852
+
853
+ it '<<' do
854
+ @brazil_client.comments << @comment_brazil_2
855
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, @comment_brazil_2].to_set)
856
+ end
857
+
858
+ it 'all' do
859
+ comment = @brazil_client.comments.build(:name => 'Builded Comment')
860
+ comment.save
861
+ c = @brazil_client.comments
862
+ expect(c.to_set).to eq([@comment_brazil, comment].to_set)
863
+ expect(c.reload.all.to_set).to eq([@comment_brazil, comment].to_set)
864
+ end
865
+
866
+ it 'build' do
867
+ comment = @brazil_client.comments.build(:name => 'Builded Comment')
868
+ comment.save
869
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, comment].to_set)
870
+ end
871
+
872
+ it 'create' do
873
+ comment = @brazil_client.comments.create(:name => 'Builded Comment')
874
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, comment].to_set)
875
+ end
876
+
877
+ it 'count' do
878
+ expect(@brazil_client.comments.count).to eq(1)
879
+ _cmt = @brazil_client.comments.create(:name => 'Builded Comment')
880
+ expect(@brazil_client.comments.count).to eq(2)
881
+ end
882
+
883
+ it 'group + count' do
884
+ expect(@brazil_client.comments.group(:id).count.length).to eq(1)
885
+ _cmt = @brazil_client.comments.create(:name => 'Builded Comment')
886
+ expect(@brazil_client.comments.group(:id).count.length).to eq(2)
887
+ end
888
+
889
+ it 'size' do
890
+ expect(@brazil_client.comments.size).to eq(1)
891
+ _cmt = @brazil_client.comments.create(:name => 'Builded Comment')
892
+ expect(@brazil_client.comments.size).to eq(2)
893
+ end
894
+
895
+ it 'create!' do
896
+ comment = @brazil_client.comments.create!(:name => 'Builded Comment')
897
+ expect(@brazil_client.comments.to_set).to eq([@comment_brazil, comment].to_set)
898
+ end
899
+
900
+ it 'length' do
901
+ expect(@brazil_client.comments.length).to eq(1)
902
+ _cmt = @brazil_client.comments.create(:name => 'Builded Comment')
903
+ expect(@brazil_client.comments.length).to eq(2)
904
+ end
905
+
906
+ it 'empty?' do
907
+ expect(@brazil_client.comments.empty?).to be false
908
+ c = Client.create!(:name => 'Client1')
909
+ expect(c.comments.empty?).to be true
910
+ end
911
+
912
+ it 'delete' do
913
+ expect(@brazil_client.comments.empty?).to be false
914
+ @brazil_client.comments.delete(@comment_brazil)
915
+ @brazil_client.reload
916
+ @comment_brazil.reload
917
+ expect(@comment_brazil.commentable).to be_nil
918
+ expect(@brazil_client.comments).to eq([])
919
+ expect(@brazil_client.comments.empty?).to be true
920
+ end
921
+
922
+ it 'delete_all' do
923
+ expect(@brazil_client.comments.empty?).to be false
924
+ @brazil_client.comments.delete_all
925
+ expect(@brazil_client.comments.empty?).to be true
926
+ end
927
+
928
+ it 'destroy_all' do
929
+ expect(@brazil_client.comments.empty?).to be false
930
+ @brazil_client.comments.destroy_all
931
+ expect(@brazil_client.comments.empty?).to be true
932
+ end
933
+
934
+ it 'find' do
935
+ expect(@brazil_client.comments.first).to eq(@comment_brazil)
936
+ @brazil_client.comments.destroy_all
937
+ expect(@brazil_client.comments.first).to be_nil
938
+ end
939
+
940
+ it 'where' do
941
+ expect(@brazil_client.comments.where('1=1')).to eq([@comment_brazil])
942
+ @brazil_client.comments.destroy_all
943
+ expect(@brazil_client.comments.where('1=1')).to be_empty
944
+ end
945
+
946
+ it 'map' do
947
+ expect(@brazil_client.comments.map(&:id)).to eq([@comment_brazil.id])
948
+ @brazil_client.comments.destroy_all
949
+ expect(@brazil_client.comments.map(&:id)).to be_empty
950
+ end
951
+
952
+ it 'where + map' do
953
+ expect(@brazil_client.comments.where('1=1').map(&:id)).to eq([@comment_brazil.id])
954
+ @brazil_client.comments.destroy_all
955
+ expect(@brazil_client.comments.where('1=1').map(&:id)).to be_empty
956
+ end
957
+
958
+ it 'where + each_with_index + map (enum method chain)' do
959
+ expect(@brazil_client.comments.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to eq([[@comment_brazil.id, 0]])
960
+ @brazil_client.comments.destroy_all
961
+ expect(@brazil_client.comments.where('1=1').each_with_index.map { |r, i| [r.id, i]}).to be_empty
962
+ end
963
+
964
+ it 'where + sum' do
965
+ expect(@brazil_client.comments.where('1=1').sum(&:id)).to eq(@comment_brazil.id)
966
+ @brazil_client.comments.destroy_all
967
+ expect(@brazil_client.comments.where('1=1').sum(&:id)).to eq(0)
968
+ end
969
+
970
+ it 'where + index_by' do
971
+ expect(@brazil_client.comments.where('1=1').index_by(&:id)).to eq(@comment_brazil.id => @comment_brazil)
972
+ @brazil_client.comments.destroy_all
973
+ expect(@brazil_client.comments.where('1=1').index_by(&:id)).to be_empty
974
+ end
975
+
976
+ it 'where + find' do
977
+ expect(@brazil_client.comments.where('1=1').find(@comment_brazil.id)).to eq(@comment_brazil)
978
+ @brazil_client.comments.destroy_all
979
+ expect { @brazil_client.comments.where('1=1').find(@comment_brazil.id) }.to raise_error ActiveRecord::RecordNotFound
980
+ end
981
+
982
+ it 'where + find with block' do
983
+ expect(@brazil_client.comments.where('1=1').find { |c| c.id == @comment_brazil.id }).to eq(@comment_brazil)
984
+ @brazil_client.comments.destroy_all
985
+ expect(@brazil_client.comments.where('1=1').find { |c| c.id == @comment_brazil.id }).to be_nil
986
+ end
987
+
988
+ it 'where + select' do
989
+ expect(@brazil_client.comments.where('1=1').select(:name).first.name).to eq(@comment_brazil.name)
990
+ @brazil_client.comments.destroy_all
991
+ expect(@brazil_client.comments.where('1=1').select(:name)).to be_empty
992
+ end
993
+
994
+ it 'where + select with block' do
995
+ expect(@brazil_client.comments.where('1=1').select { |c| c.id == @comment_brazil.id }).to eq([@comment_brazil])
996
+ @brazil_client.comments.destroy_all
997
+ expect(@brazil_client.comments.where('1=1').select { |c| c.id == @comment_brazil.id }).to be_empty
998
+ end
999
+
1000
+ it 'where + any?' do
1001
+ expect(@brazil_client.comments.where('1=1').any?).to be true
1002
+ @brazil_client.comments.destroy_all
1003
+ expect(@brazil_client.comments.where('1=1').any?).to be false
1004
+ end
1005
+
1006
+ it 'where + any? with block' do
1007
+ expect(@brazil_client.comments.where('1=1').any? { |c| c.id == @comment_brazil.id }).to be true
1008
+ @brazil_client.comments.destroy_all
1009
+ expect(@brazil_client.comments.where('1=1').any? { |c| c.id == @comment_brazil.id }).to be false
1010
+ end
1011
+
1012
+ it 'exists?' do
1013
+ expect(@brazil_client.comments.exists?(@comment_brazil.id)).to be true
1014
+ @brazil_client.comments.destroy_all
1015
+ expect(@brazil_client.comments.exists?(@comment_brazil.id)).to be false
1016
+ end
1017
+
1018
+ it 'uniq' do
1019
+ expect(@brazil_client.comments.uniq).to eq([@comment_brazil])
1020
+ end
1021
+
1022
+ it 'clear' do
1023
+ expect(@brazil_client.comments.empty?).to be false
1024
+ @brazil_client.comments.clear
1025
+ expect(@brazil_client.comments.empty?).to be true
1026
+ end
1027
+ end
1028
+ end
1029
+
1030
+ it 'block' do
1031
+ @brazil_role = Role.using(:brazil).create!(:name => 'Brazil Role')
1032
+ expect(@brazil_role.permissions.build(:name => 'ok').name).to eq('ok')
1033
+ expect(@brazil_role.permissions.create(:name => 'ok').name).to eq('ok')
1034
+ expect(@brazil_role.permissions.create!(:name => 'ok').name).to eq('ok')
1035
+ end
1036
+ end