rocking_chair 0.0.2
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.
- data/lib/rocking_chair.rb +33 -0
- data/lib/rocking_chair/couch_rest_http_adapter.rb +80 -0
- data/lib/rocking_chair/database.rb +176 -0
- data/lib/rocking_chair/error.rb +44 -0
- data/lib/rocking_chair/helper.rb +15 -0
- data/lib/rocking_chair/server.rb +137 -0
- data/lib/rocking_chair/view.rb +284 -0
- data/test/couch_rest_test.rb +350 -0
- data/test/database_test.rb +447 -0
- data/test/extended_couch_rest_test.rb +49 -0
- data/test/fixtures/extended_couch_rest_fixtures.rb +23 -0
- data/test/fixtures/simply_stored_fixtures.rb +36 -0
- data/test/simply_stored_test.rb +214 -0
- data/test/test_helper.rb +36 -0
- data/test/view_test.rb +372 -0
- metadata +77 -0
@@ -0,0 +1,23 @@
|
|
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
|
+
timestamps!
|
9
|
+
|
10
|
+
view_by :post_id
|
11
|
+
end
|
12
|
+
|
13
|
+
class Post < CouchRest::ExtendedDocument
|
14
|
+
use_database SERVER.default_database
|
15
|
+
property :title
|
16
|
+
property :body
|
17
|
+
timestamps!
|
18
|
+
|
19
|
+
def comments
|
20
|
+
Comment.by_post_id :key => id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,36 @@
|
|
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
|
+
|
10
|
+
enable_soft_delete
|
11
|
+
|
12
|
+
view :by_name, :key => :name
|
13
|
+
end
|
14
|
+
|
15
|
+
class Project
|
16
|
+
include SimplyStored::Couch
|
17
|
+
|
18
|
+
property :title
|
19
|
+
has_many :users
|
20
|
+
belongs_to :manager
|
21
|
+
end
|
22
|
+
|
23
|
+
class Manager
|
24
|
+
include SimplyStored::Couch
|
25
|
+
|
26
|
+
property :firstname
|
27
|
+
property :lastname
|
28
|
+
has_one :project
|
29
|
+
end
|
30
|
+
|
31
|
+
class CustomViewUser
|
32
|
+
include SimplyStored::Couch
|
33
|
+
|
34
|
+
property :tags
|
35
|
+
view :by_tags, :type => SimplyStored::Couch::Views::ArrayPropertyViewSpec, :key => :tags
|
36
|
+
end
|
@@ -0,0 +1,214 @@
|
|
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
|
+
context "Extended use cases for SimplyStored" do
|
10
|
+
setup do
|
11
|
+
RockingChair::Server.reset
|
12
|
+
CouchPotato::Config.database_name = 'fake_simply_stored'
|
13
|
+
recreate_db
|
14
|
+
end
|
15
|
+
|
16
|
+
context "storing and loading documents" do
|
17
|
+
should "save and find Projects" do
|
18
|
+
p = Project.new(:title => 'The title')
|
19
|
+
assert p.save
|
20
|
+
assert_not_nil p.id, p.inspect
|
21
|
+
project = Project.find(p.id)
|
22
|
+
assert_equal 'The title', project.title
|
23
|
+
end
|
24
|
+
|
25
|
+
should "save and find Users" do
|
26
|
+
p = Project.new(:title => 'The title')
|
27
|
+
assert p.save
|
28
|
+
u = User.new(:firstname => 'Doc', :lastname => 'Holiday', :project => p)
|
29
|
+
assert u.save
|
30
|
+
user = User.find(u.id)
|
31
|
+
assert_equal 'Doc', user.firstname
|
32
|
+
assert_equal 'Holiday', user.lastname
|
33
|
+
assert_equal p.id, user.project_id
|
34
|
+
assert_equal p, user.project
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "Views" do
|
39
|
+
setup do
|
40
|
+
@manager = Manager.new(:firstname => 'Michael')
|
41
|
+
assert @manager.save
|
42
|
+
|
43
|
+
@project = Project.new(:title => 'The title', :manager => @manager)
|
44
|
+
assert @project.save
|
45
|
+
|
46
|
+
@user = User.new(:firstname => 'Michael', :project => @project)
|
47
|
+
assert @user.save
|
48
|
+
end
|
49
|
+
|
50
|
+
context "by_attribute views" do
|
51
|
+
|
52
|
+
should "load first" do
|
53
|
+
user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
|
54
|
+
user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
|
55
|
+
assert_equal 'J', User.find_by_firstname('Homer').lastname
|
56
|
+
end
|
57
|
+
|
58
|
+
should "load all" do
|
59
|
+
user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
|
60
|
+
user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
|
61
|
+
user_2 = User.create(:firstname => 'Homer', :lastname => 'S')
|
62
|
+
assert_equal ['S', 'J'].sort, User.find_all_by_firstname('Homer').map(&:lastname).sort
|
63
|
+
end
|
64
|
+
|
65
|
+
should "only load objects from the correct class" do
|
66
|
+
user = User.create(:firstname => 'Bart', :lastname => 'S')
|
67
|
+
manager = Manager.create(:firstname => 'Bart', :lastname => 'J')
|
68
|
+
assert_equal ['S'], User.find_all_by_firstname('Bart').map(&:lastname)
|
69
|
+
end
|
70
|
+
|
71
|
+
should "support multiple attributes" do
|
72
|
+
user_1 = User.create(:firstname => 'Bart', :lastname => 'S')
|
73
|
+
user_2 = User.create(:firstname => 'Homer', :lastname => 'J')
|
74
|
+
user_2 = User.create(:firstname => 'Homer', :lastname => 'S')
|
75
|
+
assert_equal ['J'].sort, User.find_all_by_firstname_and_lastname('Homer', 'J').map(&:lastname).sort
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "belongs_to" do
|
80
|
+
should "load the parent object" do
|
81
|
+
assert_equal @project.id, @user.project.id
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "has_one" do
|
86
|
+
should "load the child object" do
|
87
|
+
assert_equal @project.id, @manager.project.id
|
88
|
+
assert_equal @manager.id, @project.manager.id
|
89
|
+
end
|
90
|
+
|
91
|
+
should "re-use existing views" do
|
92
|
+
Manager.create(:firstname => 'Jochen', :lastname => 'Peter')
|
93
|
+
Manager.create(:firstname => 'Another', :lastname => 'Bert')
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "has_many" do
|
98
|
+
setup do
|
99
|
+
RockingChair::Server.reset
|
100
|
+
CouchPotato::Config.database_name = 'fake_simply_stored'
|
101
|
+
recreate_db
|
102
|
+
end
|
103
|
+
|
104
|
+
should "load all has_many objects" do
|
105
|
+
user = User.new(:firstname => 'Michael', :project => @project)
|
106
|
+
assert user.save
|
107
|
+
assert_equal [user.id], @project.users.map(&:id)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "when querying the all_documents view" do
|
112
|
+
setup do
|
113
|
+
RockingChair::Server.reset
|
114
|
+
CouchPotato::Config.database_name = 'fake_simply_stored'
|
115
|
+
recreate_db
|
116
|
+
end
|
117
|
+
|
118
|
+
should "load all from the same class" do
|
119
|
+
User.create(:firstname => 'Michael Mann', :project => @project)
|
120
|
+
User.create(:firstname => 'Peter', :project => @project)
|
121
|
+
assert_equal ['Peter', 'Michael Mann'].sort, User.all.map(&:firstname).sort
|
122
|
+
|
123
|
+
User.create(:firstname => 'Hulk', :project => @project)
|
124
|
+
assert_equal ['Peter', 'Michael Mann', 'Hulk'].sort, User.all.map(&:firstname).sort
|
125
|
+
end
|
126
|
+
|
127
|
+
should "load first" do
|
128
|
+
User.create(:firstname => 'Michael Mann', :project => @project)
|
129
|
+
User.create(:firstname => 'Peter', :project => @project)
|
130
|
+
assert User.first.respond_to?(:firstname)
|
131
|
+
end
|
132
|
+
|
133
|
+
should "count" do
|
134
|
+
User.create(:firstname => 'Michael the first')
|
135
|
+
|
136
|
+
assert_equal 0, Project.count
|
137
|
+
assert_equal 0, Manager.count
|
138
|
+
assert_equal 1, User.count
|
139
|
+
|
140
|
+
Manager.create(:firstname => 'Jochen', :lastname => 'Peter')
|
141
|
+
Project.create(:title => 'The title', :manager => @manager)
|
142
|
+
Project.create(:title => 'another title', :manager => @manager)
|
143
|
+
User.create(:firstname => 'Michael Mann', :project => @project)
|
144
|
+
User.create(:firstname => 'Peter', :project => @project)
|
145
|
+
|
146
|
+
assert_equal 2, Project.count
|
147
|
+
assert_equal 1, Manager.count
|
148
|
+
assert_equal 3, User.count
|
149
|
+
end
|
150
|
+
|
151
|
+
context "with deleted" do
|
152
|
+
setup do
|
153
|
+
RockingChair::Server.reset
|
154
|
+
CouchPotato::Config.database_name = 'fake_simply_stored'
|
155
|
+
recreate_db
|
156
|
+
end
|
157
|
+
|
158
|
+
should "ignore deleted in find all but load them in all with deleted" do
|
159
|
+
user = User.new(:firstname => 'Bart', :lastname => 'S')
|
160
|
+
assert user.save
|
161
|
+
|
162
|
+
deleted_user = User.new(:firstname => 'Pete', :lastname => 'S')
|
163
|
+
assert deleted_user.save
|
164
|
+
|
165
|
+
deleted_user.destroy
|
166
|
+
|
167
|
+
assert_equal ['Bart'], User.all.map(&:firstname)
|
168
|
+
assert_equal ['Bart', 'Pete'].sort, User.find(:all, :with_deleted => true).map(&:firstname).sort
|
169
|
+
end
|
170
|
+
|
171
|
+
should "ignore deleted in find first" do
|
172
|
+
user = User.new(:firstname => 'Bart', :lastname => 'S')
|
173
|
+
assert user.save
|
174
|
+
assert_equal 'Bart', User.find(:first).firstname
|
175
|
+
user.destroy
|
176
|
+
assert_nil User.first
|
177
|
+
assert_equal 'Bart', User.find(:first, :with_deleted => true).firstname
|
178
|
+
end
|
179
|
+
|
180
|
+
should "ignore deleted in belongs_to/has_many" do
|
181
|
+
project = Project.new(:title => 'secret')
|
182
|
+
assert project.save
|
183
|
+
|
184
|
+
user = User.new(:firstname => 'Bart', :lastname => 'S', :project => project)
|
185
|
+
assert user.save
|
186
|
+
|
187
|
+
assert_equal [user.id], project.users.map(&:id)
|
188
|
+
user.destroy
|
189
|
+
|
190
|
+
assert_equal [], project.users(:force_reload => true, :with_deleted => false).map(&:id)
|
191
|
+
assert_equal [user.id], project.users(:force_reload => true, :with_deleted => true).map(&:id)
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context "With array views" do
|
198
|
+
|
199
|
+
should "find objects with one match of the array" do
|
200
|
+
CustomViewUser.create(:tags => ["agile", "cool", "extreme"])
|
201
|
+
CustomViewUser.create(:tags => ["agile"])
|
202
|
+
assert_equal 2, CustomViewUser.find_all_by_tags("agile").size
|
203
|
+
end
|
204
|
+
|
205
|
+
should "find the object when the property is not an array" do
|
206
|
+
CustomViewUser.create(:tags => "agile")
|
207
|
+
assert_equal 1, CustomViewUser.find_all_by_tags("agile").size
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/rocking_chair"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'mocha'
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures"
|
8
|
+
require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures"
|
9
|
+
|
10
|
+
RockingChair.enable
|
11
|
+
|
12
|
+
def assert_error_code(code, &blk)
|
13
|
+
ex = nil
|
14
|
+
begin
|
15
|
+
blk.call
|
16
|
+
rescue Exception => e
|
17
|
+
ex = e
|
18
|
+
ensure
|
19
|
+
assert_not_nil ex, "No Exception raised!"
|
20
|
+
assert_equal RockingChair::Error, ex.class, "The raised exception is not a RockingChair::Error: #{e.class}: #{e.message} - #{e.backtrace.join("\n")}"
|
21
|
+
assert_equal code, ex.code
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def dump_RockingChair
|
26
|
+
puts "No datases set yet!" if RockingChair::Server.databases.empty?
|
27
|
+
RockingChair::Server.databases.each do |db_name, db|
|
28
|
+
puts "Content of Database #{db_name}: \n\n#{db.inspect}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def with_debug(&blk)
|
33
|
+
HttpAbstraction.instance_variable_set("@_rocking_chair_debug", true)
|
34
|
+
blk.call
|
35
|
+
HttpAbstraction.instance_variable_set("@_rocking_chair_debug", false)
|
36
|
+
end
|
data/test/view_test.rb
ADDED
@@ -0,0 +1,372 @@
|
|
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) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
|
18
|
+
}
|
19
|
+
}}.to_json
|
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) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
|
33
|
+
}
|
34
|
+
}}.to_json
|
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' => "function(key, values){ return values.length }",
|
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' => "function(key, values){ return values.length }",
|
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' => "function(key, values){ return values.length }",
|
58
|
+
"map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
|
59
|
+
}
|
60
|
+
}}.to_json
|
61
|
+
|
62
|
+
@db.stubs(:rev).returns('the-rev')
|
63
|
+
end
|
64
|
+
|
65
|
+
should "respond to defined views" do
|
66
|
+
assert_nothing_raised do
|
67
|
+
@db.view('user', 'by_firstname', 'key' => 'abc')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
should "raise a 404 on undefined views" do
|
72
|
+
assert_error_code 404 do
|
73
|
+
@db.view('user', 'by_other_name', 'key' => 'abc')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when querying by_attr_and_attr views" do
|
78
|
+
|
79
|
+
should "return all keys if no key is given" do
|
80
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
81
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
82
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
83
|
+
|
84
|
+
assert_equal({
|
85
|
+
"total_rows" => 3,
|
86
|
+
"offset" => 0,
|
87
|
+
"rows" => [{
|
88
|
+
"id" => "user_1",
|
89
|
+
"key" => nil,
|
90
|
+
"value" => nil,
|
91
|
+
},{
|
92
|
+
"id" => "user_3",
|
93
|
+
"key" => nil,
|
94
|
+
"value" => nil,
|
95
|
+
},{
|
96
|
+
"id" => "user_2",
|
97
|
+
"key" => nil,
|
98
|
+
"value" => nil
|
99
|
+
}
|
100
|
+
]}, JSON.parse(@db.view('user', 'by_firstname')))
|
101
|
+
end
|
102
|
+
|
103
|
+
should "return all docs if no key is given and we asked to include the docs" do
|
104
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
105
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
106
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
107
|
+
|
108
|
+
assert_equal({
|
109
|
+
"total_rows" => 3,
|
110
|
+
"offset" => 0,
|
111
|
+
"rows" => [{
|
112
|
+
"id" => "user_1",
|
113
|
+
"key" => nil,
|
114
|
+
"value" => nil,
|
115
|
+
"doc" => {
|
116
|
+
"firstname" => "Alf",
|
117
|
+
"lastname" => "Bert",
|
118
|
+
'ruby_class' => 'User',
|
119
|
+
'_rev' => 'the-rev',
|
120
|
+
'_id' => 'user_1' }
|
121
|
+
},{
|
122
|
+
"id" => "user_3",
|
123
|
+
"key" => nil,
|
124
|
+
"value" => nil,
|
125
|
+
"doc" => {
|
126
|
+
"firstname" => "Alf",
|
127
|
+
"lastname" => "Horst",
|
128
|
+
'ruby_class' => 'User',
|
129
|
+
'_rev' => 'the-rev',
|
130
|
+
'_id' => 'user_3' }
|
131
|
+
}, {
|
132
|
+
"id" => "user_2",
|
133
|
+
"key" => nil,
|
134
|
+
"value" => nil,
|
135
|
+
"doc" => {
|
136
|
+
"firstname" => "Carl",
|
137
|
+
"lastname" => "Alf",
|
138
|
+
'ruby_class' => 'User',
|
139
|
+
'_rev' => 'the-rev',
|
140
|
+
'_id' => 'user_2' }
|
141
|
+
}
|
142
|
+
]}.to_json, @db.view('user', 'by_firstname', 'include_docs' => 'true'))
|
143
|
+
end
|
144
|
+
|
145
|
+
should "return matching elements" do
|
146
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
147
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
148
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
149
|
+
|
150
|
+
assert_equal({
|
151
|
+
"total_rows" => 2,
|
152
|
+
"offset" => 0,
|
153
|
+
"rows" => [{
|
154
|
+
"id" => "user_1",
|
155
|
+
"key" => "Alf",
|
156
|
+
"value" => nil,
|
157
|
+
"doc" => {
|
158
|
+
"firstname" => "Alf",
|
159
|
+
"lastname" => "Bert",
|
160
|
+
'ruby_class' => 'User',
|
161
|
+
'_rev' => 'the-rev',
|
162
|
+
'_id' => 'user_1' }
|
163
|
+
}, {
|
164
|
+
"id" => "user_3",
|
165
|
+
"key" => "Alf",
|
166
|
+
"value" => nil,
|
167
|
+
"doc" => {
|
168
|
+
"firstname" => "Alf",
|
169
|
+
"lastname" => "Horst",
|
170
|
+
'ruby_class' => 'User',
|
171
|
+
'_rev' => 'the-rev',
|
172
|
+
'_id' => 'user_3' }
|
173
|
+
}
|
174
|
+
]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
|
175
|
+
end
|
176
|
+
|
177
|
+
should "only return items with the correct klass matcher" do
|
178
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}.to_json
|
179
|
+
@db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}.to_json
|
180
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
181
|
+
|
182
|
+
assert_equal({
|
183
|
+
"total_rows" => 1,
|
184
|
+
"offset" => 0,
|
185
|
+
"rows" => [{
|
186
|
+
"id" => "user_3",
|
187
|
+
"key" => "Alf",
|
188
|
+
"value" => nil,
|
189
|
+
"doc" => {
|
190
|
+
"firstname" => "Alf",
|
191
|
+
"lastname" => "Horst",
|
192
|
+
'ruby_class' => 'User',
|
193
|
+
'_rev' => 'the-rev',
|
194
|
+
'_id' => 'user_3' }
|
195
|
+
}
|
196
|
+
]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
|
197
|
+
end
|
198
|
+
|
199
|
+
should "support multiple attributes" do
|
200
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
201
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
202
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
203
|
+
|
204
|
+
assert_equal({
|
205
|
+
"total_rows" => 1,
|
206
|
+
"offset" => 0,
|
207
|
+
"rows" => [{
|
208
|
+
"id" => "user_1",
|
209
|
+
"key" => ["Alf", "Bert"],
|
210
|
+
"value" => nil,
|
211
|
+
"doc" => {
|
212
|
+
"firstname" => "Alf",
|
213
|
+
"lastname" => "Bert",
|
214
|
+
'ruby_class' => 'User',
|
215
|
+
'_rev' => 'the-rev',
|
216
|
+
'_id' => 'user_1' }
|
217
|
+
}
|
218
|
+
]}.to_json, @db.view('user', 'by_firstname_and_lastname', 'key' => ["Alf", "Bert"].to_json, 'include_docs' => 'true'))
|
219
|
+
end
|
220
|
+
|
221
|
+
should "support startkey and endkey parameters" do
|
222
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
223
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
224
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
225
|
+
|
226
|
+
assert_equal(JSON.parse({
|
227
|
+
"total_rows" => 2,
|
228
|
+
"offset" => 0,
|
229
|
+
"rows" => [{
|
230
|
+
"id" => "user_1",
|
231
|
+
"endkey" => "Alf",
|
232
|
+
"value" => nil,
|
233
|
+
"startkey" => "Alf",
|
234
|
+
"doc" => {
|
235
|
+
"firstname" => "Alf",
|
236
|
+
"lastname" => "Bert",
|
237
|
+
'ruby_class' => 'User',
|
238
|
+
'_rev' => 'the-rev',
|
239
|
+
'_id' => 'user_1' }
|
240
|
+
}, {
|
241
|
+
"id" => "user_3",
|
242
|
+
"endkey" => "Alf",
|
243
|
+
"value" => nil,
|
244
|
+
"startkey" => "Alf",
|
245
|
+
"doc" => {
|
246
|
+
"firstname" => "Alf",
|
247
|
+
"lastname" => "Horst",
|
248
|
+
'ruby_class' => 'User',
|
249
|
+
'_rev' => 'the-rev',
|
250
|
+
'_id' => 'user_3' }
|
251
|
+
}
|
252
|
+
]}.to_json), JSON.parse(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'include_docs' => 'true')))
|
253
|
+
end
|
254
|
+
|
255
|
+
should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do
|
256
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
257
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
258
|
+
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
|
259
|
+
|
260
|
+
assert_equal(JSON.parse({
|
261
|
+
"total_rows" => 2,
|
262
|
+
"offset" => 0,
|
263
|
+
"rows" => [{
|
264
|
+
"id" => "user_3",
|
265
|
+
"startkey" => "Alf",
|
266
|
+
"endkey" => "Alf",
|
267
|
+
"startkey_docid" => "user_3",
|
268
|
+
"endkey_docid" => "user_3",
|
269
|
+
"value" => nil,
|
270
|
+
"doc" => {
|
271
|
+
"firstname" => "Alf",
|
272
|
+
"lastname" => "Horst",
|
273
|
+
'ruby_class' => 'User',
|
274
|
+
'_rev' => 'the-rev',
|
275
|
+
'_id' => 'user_3' }
|
276
|
+
}
|
277
|
+
]}.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')))
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
context "belongs_to" do
|
282
|
+
should "load parent" do
|
283
|
+
@db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}.to_json
|
284
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}.to_json
|
285
|
+
|
286
|
+
assert_equal({
|
287
|
+
"total_rows" => 1,
|
288
|
+
"offset" => 0,
|
289
|
+
"rows" => [{
|
290
|
+
"id" => "user_1",
|
291
|
+
"key" => "project_1",
|
292
|
+
"value" => nil,
|
293
|
+
"doc" => {
|
294
|
+
"firstname" => "Alf",
|
295
|
+
"lastname" => "Bert",
|
296
|
+
"project_id" => "project_1",
|
297
|
+
'ruby_class' => 'User',
|
298
|
+
'_rev' => 'the-rev',
|
299
|
+
'_id' => 'user_1' }
|
300
|
+
}
|
301
|
+
]}.to_json, @db.view('user', 'association_user_belongs_to_project', 'key' => "project_1".to_json, 'include_docs' => 'true'))
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
context "all_documents" do
|
306
|
+
should "load all documents of the matching class" do
|
307
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
308
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
309
|
+
@db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}.to_json
|
310
|
+
|
311
|
+
assert_equal({
|
312
|
+
"total_rows" => 2,
|
313
|
+
"offset" => 0,
|
314
|
+
"rows" => [{
|
315
|
+
"id" => "user_1",
|
316
|
+
"key" => nil,
|
317
|
+
"value" => nil,
|
318
|
+
"doc" => {
|
319
|
+
"firstname" => "Alf",
|
320
|
+
"lastname" => "Bert",
|
321
|
+
'ruby_class' => 'User',
|
322
|
+
'_rev' => 'the-rev',
|
323
|
+
'_id' => 'user_1' }
|
324
|
+
}, {
|
325
|
+
"id" => "user_2",
|
326
|
+
"key" => nil,
|
327
|
+
"value" => nil,
|
328
|
+
"doc" => {
|
329
|
+
"firstname" => "Carl",
|
330
|
+
"lastname" => "Alf",
|
331
|
+
'ruby_class' => 'User',
|
332
|
+
'_rev' => 'the-rev',
|
333
|
+
'_id' => 'user_2' }
|
334
|
+
}
|
335
|
+
]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true'))
|
336
|
+
end
|
337
|
+
|
338
|
+
should "limit the results if asked to" do
|
339
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
340
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
341
|
+
|
342
|
+
assert_equal({
|
343
|
+
"total_rows" => 2,
|
344
|
+
"offset" => 0,
|
345
|
+
"rows" => [{
|
346
|
+
"id" => "user_1",
|
347
|
+
"key" => nil,
|
348
|
+
"value" => nil,
|
349
|
+
"doc" => {
|
350
|
+
"firstname" => "Alf",
|
351
|
+
"lastname" => "Bert",
|
352
|
+
'ruby_class' => 'User',
|
353
|
+
'_rev' => 'the-rev',
|
354
|
+
'_id' => 'user_1' }
|
355
|
+
}
|
356
|
+
]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true', 'limit' => '1'))
|
357
|
+
end
|
358
|
+
|
359
|
+
should "count the objects with reduce" do
|
360
|
+
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
|
361
|
+
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
|
362
|
+
|
363
|
+
assert_equal({
|
364
|
+
"rows" => [{ "key" => nil, "value" => 2}]
|
365
|
+
}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'false', 'reduce' => 'true'))
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
end
|
372
|
+
end
|