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.
data/test/test_helper.rb DELETED
@@ -1,40 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + "/../lib")
2
- require "rocking_chair"
3
-
4
- require 'test/unit'
5
- require 'shoulda'
6
- require 'mocha'
7
- require 'json/ext'
8
- require 'active_support/inflector'
9
- require 'couchrest_extended_document'
10
-
11
- require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures"
12
- require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures"
13
-
14
- RockingChair.enable
15
-
16
- def assert_error_code(code, &blk)
17
- ex = nil
18
- begin
19
- blk.call
20
- rescue Exception => e
21
- ex = e
22
- ensure
23
- assert_not_nil ex, "No Exception raised!"
24
- assert_equal RockingChair::Error, ex.class, "The raised exception is not a RockingChair::Error: #{e.class}: #{e.message} - #{e.backtrace.join("\n")}"
25
- assert_equal code, ex.code
26
- end
27
- end
28
-
29
- def dump_RockingChair
30
- puts "No datases set yet!" if RockingChair::Server.databases.empty?
31
- RockingChair::Server.databases.each do |db_name, db|
32
- puts "Content of Database #{db_name}: \n\n#{db.inspect}"
33
- end
34
- end
35
-
36
- def with_debug(&blk)
37
- HttpAbstraction.instance_variable_set("@_rocking_chair_debug", true)
38
- blk.call
39
- HttpAbstraction.instance_variable_set("@_rocking_chair_debug", false)
40
- end
data/test/view_test.rb DELETED
@@ -1,450 +0,0 @@
1
- require File.dirname(__FILE__) + "/test_helper"
2
-
3
- class ViewTest < Test::Unit::TestCase
4
- context "A database view" do
5
- setup do
6
- @db = RockingChair::Database.new
7
- end
8
-
9
- should "need a database, a design doc, and a view name" do
10
- assert_error_code 404 do
11
- RockingChair::View.new(@db, 'user', 'by_firstname', {})
12
- end
13
-
14
- @db['_design/user'] = { 'language' => 'javascript', 'views' => {
15
- 'by_firstname' => {
16
- 'reduce' => "function(key, values){ return values.length }",
17
- "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
18
- }
19
- }}
20
-
21
- @db.stubs(:rev).returns('the-rev')
22
-
23
- assert_nothing_raised do
24
- RockingChair::View.new(@db, 'user', 'by_firstname', {})
25
- end
26
- end
27
-
28
- should "be constructed out a database" do
29
- @db['_design/user'] = { 'language' => 'javascript', 'views' => {
30
- 'by_firstname' => {
31
- 'reduce' => "function(key, values){ return values.length }",
32
- "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
33
- }
34
- }}
35
-
36
- assert_nothing_raised do
37
- JSON.parse(@db.view('user', 'by_firstname', {}))
38
- end
39
- end
40
-
41
- context "when querying the views" do
42
- setup do
43
- @db['_design/user'] = { 'language' => 'javascript', 'views' => {
44
- 'all_documents' => {
45
- 'reduce' => nil,
46
- 'map' => "function(item){emit(item)}"
47
- },
48
- 'by_firstname' => {
49
- 'reduce' => "_sum",
50
- "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
51
- },
52
- 'by_firstname_and_lastname' => {
53
- 'reduce' => "_sum",
54
- "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
55
- },
56
- 'association_user_belongs_to_project' => {
57
- 'reduce' => "_sum",
58
- "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
59
- },
60
- 'association_user_has_and_belongs_to_many_groups' => {
61
- 'reduce' => "_sum",
62
- "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
63
- }
64
- }}
65
-
66
- @db.stubs(:rev).returns('the-rev')
67
- end
68
-
69
- should "respond to defined views" do
70
- assert_nothing_raised do
71
- @db.view('user', 'by_firstname', 'key' => 'abc')
72
- end
73
- end
74
-
75
- should "raise a 404 on undefined views" do
76
- assert_error_code 404 do
77
- @db.view('user', 'by_other_name', 'key' => 'abc')
78
- end
79
- end
80
-
81
- context "when querying by_attr_and_attr views" do
82
-
83
- should "return all keys if no key is given" do
84
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
85
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
86
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
87
-
88
- assert_equal({
89
- "total_rows" => 3,
90
- "offset" => 0,
91
- "rows" => [{
92
- "id" => "user_1",
93
- "key" => nil,
94
- "value" => nil,
95
- },{
96
- "id" => "user_3",
97
- "key" => nil,
98
- "value" => nil,
99
- },{
100
- "id" => "user_2",
101
- "key" => nil,
102
- "value" => nil
103
- }
104
- ]}, JSON.parse(@db.view('user', 'by_firstname')))
105
- end
106
-
107
- should "return all docs if no key is given and we asked to include the docs" do
108
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
109
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
110
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
111
-
112
- assert_equal({
113
- "total_rows" => 3,
114
- "offset" => 0,
115
- "rows" => [{
116
- "id" => "user_1",
117
- "key" => nil,
118
- "value" => nil,
119
- "doc" => {
120
- "firstname" => "Alf",
121
- "lastname" => "Bert",
122
- 'ruby_class' => 'User',
123
- '_rev' => 'the-rev',
124
- '_id' => 'user_1' }
125
- },{
126
- "id" => "user_3",
127
- "key" => nil,
128
- "value" => nil,
129
- "doc" => {
130
- "firstname" => "Alf",
131
- "lastname" => "Horst",
132
- 'ruby_class' => 'User',
133
- '_rev' => 'the-rev',
134
- '_id' => 'user_3' }
135
- }, {
136
- "id" => "user_2",
137
- "key" => nil,
138
- "value" => nil,
139
- "doc" => {
140
- "firstname" => "Carl",
141
- "lastname" => "Alf",
142
- 'ruby_class' => 'User',
143
- '_rev' => 'the-rev',
144
- '_id' => 'user_2' }
145
- }
146
- ]}.to_json, @db.view('user', 'by_firstname', 'include_docs' => 'true'))
147
- end
148
-
149
- should "return matching elements" do
150
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
151
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
152
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
153
-
154
- assert_equal({
155
- "total_rows" => 2,
156
- "offset" => 0,
157
- "rows" => [{
158
- "id" => "user_1",
159
- "key" => "Alf",
160
- "value" => nil,
161
- "doc" => {
162
- "firstname" => "Alf",
163
- "lastname" => "Bert",
164
- 'ruby_class' => 'User',
165
- '_rev' => 'the-rev',
166
- '_id' => 'user_1' }
167
- }, {
168
- "id" => "user_3",
169
- "key" => "Alf",
170
- "value" => nil,
171
- "doc" => {
172
- "firstname" => "Alf",
173
- "lastname" => "Horst",
174
- 'ruby_class' => 'User',
175
- '_rev' => 'the-rev',
176
- '_id' => 'user_3' }
177
- }
178
- ]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
179
- end
180
-
181
- should "only return items with the correct klass matcher" do
182
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}
183
- @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}
184
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
185
-
186
- assert_equal({
187
- "total_rows" => 1,
188
- "offset" => 0,
189
- "rows" => [{
190
- "id" => "user_3",
191
- "key" => "Alf",
192
- "value" => nil,
193
- "doc" => {
194
- "firstname" => "Alf",
195
- "lastname" => "Horst",
196
- 'ruby_class' => 'User',
197
- '_rev' => 'the-rev',
198
- '_id' => 'user_3' }
199
- }
200
- ]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
201
- end
202
-
203
- should "support multiple attributes" do
204
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
205
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
206
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
207
-
208
- assert_equal({
209
- "total_rows" => 1,
210
- "offset" => 0,
211
- "rows" => [{
212
- "id" => "user_1",
213
- "key" => ["Alf", "Bert"],
214
- "value" => nil,
215
- "doc" => {
216
- "firstname" => "Alf",
217
- "lastname" => "Bert",
218
- 'ruby_class' => 'User',
219
- '_rev' => 'the-rev',
220
- '_id' => 'user_1' }
221
- }
222
- ]}.to_json, @db.view('user', 'by_firstname_and_lastname', 'key' => ["Alf", "Bert"].to_json, 'include_docs' => 'true'))
223
- end
224
-
225
- should "support startkey and endkey parameters" do
226
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
227
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
228
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
229
-
230
- assert_equal(JSON.parse({
231
- "total_rows" => 2,
232
- "offset" => 0,
233
- "rows" => [{
234
- "id" => "user_1",
235
- "endkey" => "Alf",
236
- "value" => nil,
237
- "startkey" => "Alf",
238
- "doc" => {
239
- "firstname" => "Alf",
240
- "lastname" => "Bert",
241
- 'ruby_class' => 'User',
242
- '_rev' => 'the-rev',
243
- '_id' => 'user_1' }
244
- }, {
245
- "id" => "user_3",
246
- "endkey" => "Alf",
247
- "value" => nil,
248
- "startkey" => "Alf",
249
- "doc" => {
250
- "firstname" => "Alf",
251
- "lastname" => "Horst",
252
- 'ruby_class' => 'User',
253
- '_rev' => 'the-rev',
254
- '_id' => 'user_3' }
255
- }
256
- ]}.to_json), JSON.parse(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'include_docs' => 'true')))
257
- end
258
-
259
- should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do
260
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
261
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
262
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
263
-
264
- assert_equal(JSON.parse({
265
- "total_rows" => 2,
266
- "offset" => 0,
267
- "rows" => [{
268
- "id" => "user_3",
269
- "startkey" => "Alf",
270
- "endkey" => "Alf",
271
- "startkey_docid" => "user_3",
272
- "endkey_docid" => "user_3",
273
- "value" => nil,
274
- "doc" => {
275
- "firstname" => "Alf",
276
- "lastname" => "Horst",
277
- 'ruby_class' => 'User',
278
- '_rev' => 'the-rev',
279
- '_id' => 'user_3' }
280
- }
281
- ]}.to_json), JSON.parse(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'startkey_docid' => "user_3".to_json, "endkey_docid" => 'user_3'.to_json, 'include_docs' => 'true', 'limit' => '1')))
282
- end
283
- end
284
-
285
- context "belongs_to" do
286
- should "load parent" do
287
- @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}
288
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}
289
-
290
- assert_equal({
291
- "total_rows" => 1,
292
- "offset" => 0,
293
- "rows" => [{
294
- "id" => "user_1",
295
- "key" => "project_1",
296
- "value" => nil,
297
- "doc" => {
298
- "firstname" => "Alf",
299
- "lastname" => "Bert",
300
- "project_id" => "project_1",
301
- 'ruby_class' => 'User',
302
- '_rev' => 'the-rev',
303
- '_id' => 'user_1' }
304
- }
305
- ]}.to_json, @db.view('user', 'association_user_belongs_to_project', 'key' => "project_1".to_json, 'include_docs' => 'true'))
306
- end
307
- end
308
-
309
- context "all_documents" do
310
- should "load all documents of the matching class" do
311
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
312
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
313
- @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}
314
-
315
- assert_equal({
316
- "total_rows" => 2,
317
- "offset" => 0,
318
- "rows" => [{
319
- "id" => "user_1",
320
- "key" => nil,
321
- "value" => nil,
322
- "doc" => {
323
- "firstname" => "Alf",
324
- "lastname" => "Bert",
325
- 'ruby_class' => 'User',
326
- '_rev' => 'the-rev',
327
- '_id' => 'user_1' }
328
- }, {
329
- "id" => "user_2",
330
- "key" => nil,
331
- "value" => nil,
332
- "doc" => {
333
- "firstname" => "Carl",
334
- "lastname" => "Alf",
335
- 'ruby_class' => 'User',
336
- '_rev' => 'the-rev',
337
- '_id' => 'user_2' }
338
- }
339
- ]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true'))
340
- end
341
-
342
- should "limit the results if asked to" do
343
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
344
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
345
-
346
- assert_equal({
347
- "total_rows" => 2,
348
- "offset" => 0,
349
- "rows" => [{
350
- "id" => "user_1",
351
- "key" => nil,
352
- "value" => nil,
353
- "doc" => {
354
- "firstname" => "Alf",
355
- "lastname" => "Bert",
356
- 'ruby_class' => 'User',
357
- '_rev' => 'the-rev',
358
- '_id' => 'user_1' }
359
- }
360
- ]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true', 'limit' => '1'))
361
- end
362
-
363
- should "count the objects with reduce" do
364
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
365
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
366
-
367
- assert_equal({
368
- "rows" => [{ "key" => nil, "value" => 2}]
369
- }.to_json, @db.view('user', 'all_documents', 'include_docs' => 'false', 'reduce' => 'true'))
370
- end
371
- end
372
-
373
- context "has and belongs to many views" do
374
- setup do
375
- @db['group_1'] = {"name" => 'A', 'ruby_class' => 'Group'}
376
- @db['group_2'] = {"name" => 'B', 'ruby_class' => 'Group'}
377
- @db['user_1'] = {"group_ids" => ['group_1', 'group_2'], 'firstname' => 'Bert', 'ruby_class' => 'User'}
378
- @db['user_2'] = {"group_ids" => ['group_1'], 'firstname' => 'Alf', 'ruby_class' => 'User'}
379
- @db['_design/group'] = { 'language' => 'javascript', 'views' => {
380
- 'all_documents' => {
381
- 'reduce' => nil,
382
- 'map' => "function(item){emit(item)}"
383
- },
384
- 'association_group_has_and_belongs_to_many_users' => {
385
- 'reduce' => "function(key, values){ return values.length }",
386
- "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
387
- }
388
- }}
389
- end
390
-
391
- should "return all item not storing keys" do
392
- assert_equal(JSON.parse({
393
- "total_rows" => 2,
394
- "rows" => [
395
- {"doc" => {
396
- "_rev" => "the-rev",
397
- "group_ids" => ["group_1","group_2"],
398
- "_id" => "user_1",
399
- "firstname" => "Bert",
400
- "ruby_class" => "User"
401
- },
402
- "id" => "user_1",
403
- "value" => nil,
404
- "key" => "group_1"
405
- },{
406
- "doc" => {
407
- "_rev" => "the-rev",
408
- "group_ids" => ["group_1"],
409
- "_id" => "user_2",
410
- "firstname" => "Alf",
411
- "ruby_class" => "User"
412
- },
413
- "id" => "user_2",
414
- "value" => nil,
415
- "key" => "group_1"
416
- }],
417
- "offset" => 0}.to_json), JSON.parse(@db.view('user', 'association_user_has_and_belongs_to_many_groups', 'key' => "group_1".to_json, 'include_docs' => 'true')))
418
- end
419
-
420
- should "return all item storing keys" do
421
- assert_equal(JSON.parse({
422
- "total_rows" => 2,
423
- "rows" => [
424
- {"doc" => {
425
- "_rev" => "the-rev",
426
- "_id" => "group_1",
427
- "name" => "A",
428
- "ruby_class" => "Group"
429
- },
430
- "id" => "group_1",
431
- "value" => nil,
432
- "key" => "user_1"
433
- },{
434
- "doc" => {
435
- "_rev" => "the-rev",
436
- "_id" => "group_2",
437
- "name" => "B",
438
- "ruby_class" => "Group"
439
- },
440
- "id" => "group_2",
441
- "value" => nil,
442
- "key" => "user_1"
443
- }],
444
- "offset" => 0}.to_json), JSON.parse(@db.view('group', 'association_group_has_and_belongs_to_many_users', 'key' => "user_1".to_json, 'include_docs' => 'true')))
445
- end
446
- end
447
- end
448
-
449
- end
450
- end