rocking_chair 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +0,0 @@
1
- SERVER = CouchRest.new
2
- SERVER.default_database = 'couchrest-extendeddoc-example'
3
-
4
- class Comment < CouchRest::ExtendedDocument
5
- use_database SERVER.default_database
6
- property :body
7
- property :post_id
8
-
9
- view_by :post_id
10
- end
11
-
12
- class Post < CouchRest::ExtendedDocument
13
- use_database SERVER.default_database
14
- property :title
15
- property :body
16
-
17
- def comments
18
- Comment.by_post_id :key => id
19
- end
20
- end
21
-
@@ -1,60 +0,0 @@
1
- require 'simply_stored/couch'
2
-
3
- class User
4
- include SimplyStored::Couch
5
-
6
- property :firstname
7
- property :lastname
8
- belongs_to :project
9
- has_and_belongs_to_many :groups, :storing_keys => true
10
-
11
- enable_soft_delete
12
-
13
- view :by_name, :key => :name
14
- end
15
-
16
- class Project
17
- include SimplyStored::Couch
18
-
19
- property :title
20
- has_many :users
21
- belongs_to :manager
22
- end
23
-
24
- class Manager
25
- include SimplyStored::Couch
26
-
27
- property :firstname
28
- property :lastname
29
- has_one :project
30
- end
31
-
32
- class CustomFiewUser
33
- include SimplyStored::Couch
34
-
35
- property :tags
36
- view :by_tags, :type => SimplyStored::Couch::Views::ArrayPropertyViewSpec, :key => :tags
37
- end
38
-
39
- class Group
40
- include SimplyStored::Couch
41
-
42
- property :name
43
- has_and_belongs_to_many :users, :storing_keys => false
44
- end
45
-
46
- class Server
47
- include SimplyStored::Couch
48
-
49
- property :hostname
50
-
51
- has_and_belongs_to_many :networks, :storing_keys => true
52
- end
53
-
54
- class Network
55
- include SimplyStored::Couch
56
-
57
- property :klass
58
-
59
- has_and_belongs_to_many :servers, :storing_keys => false
60
- end
@@ -1,403 +0,0 @@
1
- require File.dirname(__FILE__) + "/test_helper"
2
-
3
- def recreate_db
4
- CouchPotato.couchrest_database.delete! rescue nil
5
- CouchPotato.couchrest_database.server.create_db CouchPotato::Config.database_name
6
- end
7
-
8
- class SimplyStoredTest < Test::Unit::TestCase
9
- [false, true].each do |setting|
10
- CouchPotato::Config.split_design_documents_per_view = setting
11
-
12
- context "Extended use cases for SimplyStored with split_design_documents_per_view #{setting ? 'enabled' : 'disabled'}" do
13
- setup do
14
- RockingChair::Server.reset
15
- CouchPotato::Config.database_name = 'fake_simply_stored'
16
- recreate_db
17
- end
18
-
19
- context "storing and loading documents" do
20
- should "save and find Projects" do
21
- p = Project.new(:title => 'The title')
22
- assert p.save
23
- assert_not_nil p.id, p.inspect
24
- project = Project.find(p.id)
25
- assert_equal 'The title', project.title
26
- end
27
-
28
- should "save and find Users" do
29
- p = Project.new(:title => 'The title')
30
- assert p.save
31
- u = User.new(:firstname => 'Doc', :lastname => 'Holiday', :project => p)
32
- assert u.save
33
- user = User.find(u.id)
34
- assert_equal 'Doc', user.firstname
35
- assert_equal 'Holiday', user.lastname
36
- assert_equal p.id, user.project_id
37
- assert_equal p, user.project
38
- end
39
- end
40
-
41
- context "Views" do
42
- setup do
43
- @manager = Manager.new(:firstname => 'Michael')
44
- assert @manager.save
45
-
46
- @project = Project.new(:title => 'The title', :manager => @manager)
47
- assert @project.save
48
-
49
- @user = User.new(:firstname => 'Michael', :project => @project)
50
- assert @user.save
51
- end
52
-
53
- context "by_attribute views" do
54
-
55
- should "load first" do
56
- user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
57
- user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
58
- assert_equal 'J', User.find_by_firstname('Homer').lastname
59
- end
60
-
61
- should "load all" do
62
- user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
63
- user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
64
- user_2 = User.create(:firstname => 'Homer', :lastname => 'S')
65
- assert_equal ['S', 'J'].sort, User.find_all_by_firstname('Homer').map(&:lastname).sort
66
- end
67
-
68
- should "only load objects from the correct class" do
69
- user = User.create(:firstname => 'Bart', :lastname => 'S')
70
- manager = Manager.create(:firstname => 'Bart', :lastname => 'J')
71
- assert_equal ['S'], User.find_all_by_firstname('Bart').map(&:lastname)
72
- end
73
-
74
- should "support multiple attributes" do
75
- user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
76
- user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
77
- user_2 = User.create(:firstname => 'Homer', :lastname => 'S')
78
- assert_equal ['J'].sort, User.find_all_by_firstname_and_lastname('Homer', 'J').map(&:lastname).sort
79
- end
80
- end
81
-
82
- context "belongs_to" do
83
- should "load the parent object" do
84
- assert_equal @project.id, @user.project.id
85
- end
86
- end
87
-
88
- context "has_one" do
89
- should "load the child object" do
90
- assert_equal @project.id, @manager.project.id
91
- assert_equal @manager.id, @project.manager.id
92
- end
93
-
94
- should "re-use existing views" do
95
- Manager.create(:firstname => 'Jochen', :lastname => 'Peter')
96
- Manager.create(:firstname => 'Another', :lastname => 'Bert')
97
- end
98
- end
99
-
100
- context "has_many" do
101
- setup do
102
- RockingChair::Server.reset
103
- CouchPotato::Config.database_name = 'fake_simply_stored'
104
- recreate_db
105
- end
106
-
107
- should "load all has_many objects" do
108
- user = User.new(:firstname => 'Michael', :project => @project)
109
- assert user.save
110
- assert_equal [user.id], @project.users.map(&:id)
111
- end
112
-
113
- should "support counting associated" do
114
- assert_equal 0, @project.user_count
115
- user = User.create(:firstname => 'Michael', :project => @project)
116
- assert_equal 1, @project.user_count(:force_reload => true)
117
- end
118
-
119
- should "support limiting" do
120
- 3.times{ User.create!(:firstname => 'Michael', :project => @project) }
121
- assert_equal 3, @project.users.size
122
- assert_equal 2, @project.users(:limit => 2).size
123
- end
124
-
125
- should "support mixing order and limit" do
126
- michael = User.find(User.create!(:firstname => "michael", :project => @project).id)
127
- michael.created_at = Time.local(2001)
128
- michael.save!
129
-
130
- mickey = User.find(User.create!(:firstname => "mickey", :project => @project).id)
131
- mickey.created_at = Time.local(2002)
132
- mickey.save!
133
-
134
- mike = User.find(User.create!(:firstname => "mike", :project => @project).id)
135
- mike.created_at = Time.local(2003)
136
- mike.save!
137
-
138
- assert_equal ["michael", "mickey", "mike"], @project.users(:order => :asc).map(&:firstname)
139
- assert_equal ["michael", "mickey", "mike"].reverse, @project.users(:order => :desc).map(&:firstname)
140
- end
141
- end
142
-
143
- context "when querying the all_documents view" do
144
- setup do
145
- RockingChair::Server.reset
146
- CouchPotato::Config.database_name = 'fake_simply_stored'
147
- recreate_db
148
- end
149
-
150
- should "load all from the same class" do
151
- User.create(:firstname => 'Michael Mann', :project => @project)
152
- User.create(:firstname => 'Peter', :project => @project)
153
- assert_equal ['Peter', 'Michael Mann'].sort, User.all.map(&:firstname).sort
154
-
155
- User.create(:firstname => 'Hulk', :project => @project)
156
- assert_equal ['Peter', 'Michael Mann', 'Hulk'].sort, User.all.map(&:firstname).sort
157
- end
158
-
159
- should "support order" do
160
- 3.times{|i| User.create!(:firstname => "user #{i}") }
161
- assert_not_equal User.all(:order => :asc).map(&:id), User.all(:order => :desc).map(&:id)
162
- assert_equal User.all(:order => :asc).reverse, User.all(:order => :desc)
163
- end
164
-
165
- should "load first" do
166
- User.create(:firstname => 'Michael Mann', :project => @project)
167
- User.create(:firstname => 'Peter', :project => @project)
168
- assert User.first.respond_to?(:firstname)
169
- end
170
-
171
- should "count" do
172
- User.create(:firstname => 'Michael the first')
173
-
174
- assert_equal 0, Project.count
175
- assert_equal 0, Manager.count
176
- assert_equal 1, User.count
177
-
178
- Manager.create(:firstname => 'Jochen', :lastname => 'Peter')
179
- Project.create(:title => 'The title', :manager => @manager)
180
- Project.create(:title => 'another title', :manager => @manager)
181
- User.create(:firstname => 'Michael Mann', :project => @project)
182
- User.create(:firstname => 'Peter', :project => @project)
183
-
184
- assert_equal 2, Project.count
185
- assert_equal 1, Manager.count
186
- assert_equal 3, User.count
187
- end
188
-
189
- should "count_by" do
190
- User.create(:firstname => 'michael')
191
-
192
- assert_equal 1, User.count
193
- assert_equal 1, User.count_by_firstname('michael')
194
- end
195
-
196
- should "count_by with nil attributes" do
197
- Project.create(:title => nil)
198
-
199
- assert_equal 1, Project.count
200
- assert_equal 1, Project.count_by_title(nil)
201
-
202
- Project.create(:title => nil, :manager_id => 12)
203
-
204
- assert_equal 2, Project.count
205
- assert_equal 2, Project.count_by_title(nil)
206
- assert_equal 1, Project.count_by_manager_id(12)
207
- assert_equal 1, Project.count_by_manager_id_and_title(12, nil)
208
-
209
- Project.create(:title => 'Hi There')
210
-
211
- assert_equal 3, Project.count
212
- assert_equal 2, Project.count_by_title(nil)
213
- end
214
-
215
- context "with deleted" do
216
- setup do
217
- RockingChair::Server.reset
218
- CouchPotato::Config.database_name = 'fake_simply_stored'
219
- recreate_db
220
- end
221
-
222
- should "ignore deleted in find all but load them in all with deleted" do
223
- user = User.new(:firstname => 'Bart', :lastname => 'S')
224
- assert user.save
225
-
226
- deleted_user = User.new(:firstname => 'Pete', :lastname => 'S')
227
- assert deleted_user.save
228
-
229
- deleted_user.destroy
230
-
231
- assert_equal ['Bart'], User.all.map(&:firstname)
232
- assert_equal ['Bart', 'Pete'].sort, User.find(:all, :with_deleted => true).map(&:firstname).sort
233
- end
234
-
235
- should "ignore deleted in find first" do
236
- user = User.new(:firstname => 'Bart', :lastname => 'S')
237
- assert user.save
238
- assert_equal 'Bart', User.find(:first).firstname
239
- user.destroy
240
- assert_nil User.first
241
- assert_equal 'Bart', User.find(:first, :with_deleted => true).firstname
242
- end
243
-
244
- should "ignore deleted in belongs_to/has_many" do
245
- project = Project.new(:title => 'secret')
246
- assert project.save
247
-
248
- user = User.new(:firstname => 'Bart', :lastname => 'S', :project => project)
249
- assert user.save
250
-
251
- assert_equal [user.id], project.users.map(&:id)
252
- user.destroy
253
-
254
- assert_equal [], project.users(:force_reload => true, :with_deleted => false).map(&:id)
255
- assert_equal [user.id], project.users(:force_reload => true, :with_deleted => true).map(&:id)
256
- end
257
-
258
- end
259
- end
260
-
261
- context "With array views" do
262
-
263
- should "find objects with one match of the array" do
264
- CustomFiewUser.create(:tags => ["agile", "cool", "extreme"])
265
- CustomFiewUser.create(:tags => ["agile"])
266
- assert_equal 2, CustomFiewUser.find_all_by_tags("agile").size
267
- end
268
-
269
- should "find the object when the property is not an array" do
270
- CustomFiewUser.create(:tags => "agile")
271
- assert_equal 1, CustomFiewUser.find_all_by_tags("agile").size
272
- end
273
- end
274
-
275
- end
276
-
277
- context "when deleting" do
278
- should "delete the doc" do
279
- user = User.new(:firstname => 'Bart', :lastname => 'S')
280
- assert user.save
281
- assert user.delete
282
- end
283
- end
284
-
285
- context "when handling n:m relations using has_and_belongs_to_many" do
286
- should "work relations from both sides" do
287
- network_a = Network.create(:klass => "A")
288
- network_b = Network.create(:klass => "B")
289
- 3.times {
290
- server = Server.new
291
- server.add_network(network_a)
292
- server.add_network(network_b)
293
- }
294
- assert_equal 3, network_a.servers.size
295
- network_a.servers.each do |server|
296
- assert_equal 2, server.networks.size
297
- end
298
- assert_equal 3, network_b.servers.size
299
- network_b.servers.each do |server|
300
- assert_equal 2, server.networks.size
301
- end
302
- end
303
-
304
- should "work relations from both sides - regardless from where the add was called" do
305
- network_a = Network.create(:klass => "A")
306
- network_b = Network.create(:klass => "B")
307
- 3.times {
308
- server = Server.new
309
- network_a.add_server(server)
310
- network_b.add_server(server)
311
- }
312
- assert_equal 3, network_a.servers.size
313
- network_a.servers.each do |server|
314
- assert_equal 2, server.networks.size, server.network_ids.inspect
315
- end
316
- assert_equal 3, network_b.servers.size
317
- network_b.servers.each do |server|
318
- assert_equal 2, server.networks.size
319
- end
320
- end
321
-
322
- should "cound correctly - regardless of the side of the relation" do
323
- network_a = Network.create(:klass => "A")
324
- network_b = Network.create(:klass => "B")
325
- 3.times {
326
- server = Server.new
327
- network_a.add_server(server)
328
- network_b.add_server(server)
329
- }
330
- assert_equal 3, network_a.server_count
331
- assert_equal 3, network_b.server_count
332
- assert_equal 2, network_a.servers.first.network_count
333
- assert_equal 2, network_b.servers.first.network_count
334
- end
335
-
336
- should "support mixing order and limit" do
337
- network_1 = Network.find(Network.create!(:klass => "A").id)
338
- network_1.created_at = Time.local(2001)
339
- network_1.save!
340
-
341
- network_2 = Network.find(Network.create!(:klass => "B").id)
342
- network_2.created_at = Time.local(2002)
343
- network_2.save!
344
-
345
- server_1 = Server.find(Server.create!(:hostname => 'www.example.com').id)
346
- server_1.created_at = Time.local(2003)
347
- network_1.add_server(server_1)
348
- network_2.add_server(server_1)
349
-
350
- server_2 = Server.find(Server.create!(:hostname => 'foo.com').id)
351
- server_2.created_at = Time.local(2004)
352
- network_1.add_server(server_2)
353
- network_2.add_server(server_2)
354
-
355
- assert_equal ['www.example.com', 'foo.com'], network_1.servers(:order => :asc).map(&:hostname)
356
- assert_equal ['www.example.com', 'foo.com'].reverse, network_1.servers(:order => :desc).map(&:hostname)
357
-
358
- assert_equal ['A', 'B'], server_2.networks(:order => :asc).map(&:klass)
359
- assert_equal ['A', 'B'].reverse, server_2.networks(:order => :desc).map(&:klass)
360
-
361
- assert_equal ['www.example.com'], network_1.servers(:order => :asc, :limit => 1).map(&:hostname)
362
- assert_equal ['foo.com'], network_1.servers(:order => :desc, :limit => 1).map(&:hostname)
363
-
364
- assert_equal ['A'], server_2.networks(:order => :asc, :limit => 1).map(&:klass)
365
- assert_equal ['B'], server_2.networks(:order => :desc, :limit => 1).map(&:klass)
366
- end
367
-
368
- should "when counting cache the result" do
369
- @network = Network.create(:klass => "C")
370
- @server = Server.create
371
- assert_equal 0, @network.server_count
372
- Server.create(:network_ids => [@network.id])
373
- assert_equal 0, @network.server_count
374
- assert_equal 0, @network.instance_variable_get("@server_count")
375
- @network.instance_variable_set("@server_count", nil)
376
- assert_equal 1, @network.server_count
377
- end
378
-
379
- should "when counting cache the result - from both directions" do
380
- @network = Network.create(:klass => "C")
381
- @server = Server.create
382
- assert_equal 0, @server.network_count
383
- @server.network_ids = [@network.id]
384
- @server.save!
385
- assert_equal 0, @server.network_count
386
- assert_equal 0, @server.instance_variable_get("@network_count")
387
- @server.instance_variable_set("@network_count", nil)
388
- assert_equal 1, @server.network_count
389
- end
390
- end
391
-
392
- context "when deleting all design docs" do
393
- should "reset all design docs" do
394
- User.find_all_by_firstname('a')
395
- db = "http://127.0.0.1:5984/#{CouchPotato::Config.database_name}"
396
- assert_nothing_raised do
397
- assert_equal 1, SimplyStored::Couch.delete_all_design_documents(db)
398
- end
399
- end
400
- end
401
- end
402
- end
403
- end