forked from cardmagic/dm-more
/
couchdb_adapter_spec.rb
241 lines (205 loc) · 7.2 KB
/
couchdb_adapter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'pathname'
require Pathname(__FILE__).dirname.parent.expand_path + 'lib/couchdb_adapter'
DataMapper.setup(
:default,
Addressable::URI.parse("couchdb://localhost:5984/test_cdb_adapter")
)
class User
include DataMapper::Resource
# required for CouchDB
property :id, String, :key => true, :field => :_id
property :rev, String, :field => :_rev
# regular properties
property :name, String
property :age, Integer
property :wealth, Float
property :created_at, DateTime
property :created_on, Date
property :location, JsonObject
# creates methods for accessing stored/indexed views in the CouchDB database
view :by_name, { "map" => "function(doc) { if (doc.type == 'user') { emit(doc.name, doc); } }" }
view :by_age, { "map" => "function(doc) { if (doc.type == 'user') { emit(doc.age, doc); } }" }
view :count, { "map" => "function(doc) { if (doc.type == 'user') { emit(null, 1); } }",
"reduce" => "function(keys, values) { return sum(values); }" }
before :create do
self.created_at = DateTime.now
self.created_on = Date.today
end
end
class Company
include DataMapper::Resource
# required for CouchDB
property :id, String, :key => true, :field => :_id
property :rev, String, :field => :_rev
# This class happens to have similar properties
property :name, String
property :age, Integer
end
describe DataMapper::Adapters::CouchdbAdapter do
before :all do
@adapter = DataMapper::Repository.adapters[:couchdb]
@no_connection = false
unless @no_connection
begin
@adapter.send(:http_put, "/#{@adapter.escaped_db_name}")
create_procedures
rescue Errno::ECONNREFUSED
@no_connection = true
end
end
end
after :all do
unless @no_connection
@adapter.send(:http_delete, "/#{@adapter.escaped_db_name}")
end
end
it "should create a record" do
pending("No CouchDB connection.") if @no_connection
user = new_user
user.save.should == true
user.id.should_not == nil
company = Company.new(:name => "IBM", :age => 119)
company.save.should == true
company.id.should_not == nil
end
it "should create a record with a specified id" do
pending("No CouchDB connection.") if @no_connection
user_with_id = new_user
user_with_id.id = 'user_id'
user_with_id.save.should == true
User.get!('user_id').should == user_with_id
user_with_id.destroy
end
it "should get a record" do
pending("No CouchDB connection.") if @no_connection
created_user = new_user
created_user.save
user = User.get!(created_user.id)
user.id.should_not be_nil
user.name.should == "Jamie"
user.age.should == 67
end
it "should not get records of the wrong type by id" do
pending("No CouchDB connection.") if @no_connection
Company.get(new_user.id).should == nil
lambda { Company.get!(new_user.id) }.should raise_error(DataMapper::ObjectNotFoundError)
end
it "should update a record" do
pending("No CouchDB connection.") if @no_connection
created_user = new_user
created_user.save
user = User.get!(created_user.id)
user.name = "Janet"
user.save
user.name.should_not == created_user.name
user.rev.should_not == created_user.rev
user.age.should == created_user.age
user.id.should == created_user.id
end
it "should destroy a record" do
pending("No CouchDB connection.") if @no_connection
created_user = new_user
created_user.save
created_user.destroy.should == true
end
it "should get all records" do
pending("No CouchDB connection.") if @no_connection
User.all.length.should == 3
end
it "should get records by eql matcher" do
pending("No CouchDB connection.") if @no_connection
new_user(:name => "John", :age => 50).save
User.all(:name => "John").size.should == 1
User.all(:age => 50).size.should == 1
User.all(:wealth => 11.5).size.should == 4
end
it "should get records by not matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:age.not => 50).size.should == 3
end
it "should get records by gt matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:age.gt => 50).size.should == 3
end
it "should get records by gte matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:age.gte => 50).size.should == 4
end
it "should get records by lt matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:age.lt => 50).size.should == 0
end
it "should get records by lte matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:age.lte => 50).size.should == 1
end
it "should get records by the like matcher" do
pending("No CouchDB connection.") if @no_connection
User.all(:name.like => "Jo").size.should == 0
User.all(:name.like => "Jo%").size.should == 1
User.all(:name.like => "%J%t%").size.should == 1
User.all(:name.like => /^Jam/).size.should == 2
end
it "should get records with multiple matchers" do
pending("No CouchDB connection.") if @no_connection
new_user(:name => "John", :age => 30).save
User.all(:name => "John", :age.lt => 50).size.should == 1
end
it "should order records" do
pending("No CouchDB connection.") if @no_connection
new_user(:name => "Aaron", :age => 30).save
new_user(:name => "Aaron").save
users = User.all(:order => [:age])
users[0].age.should == 30
users = User.all(:order => [:name, :age])
users[0].age.should == 30
users[1].age.should == 67
end
it "should handle DateTime" do
pending("No CouchDB connection.") if @no_connection
user = new_user
user.save
time = user.created_at
User.get!(user.id).created_at.should.eql? time
end
it "should handle Date" do
pending("No CouchDB connection.") if @no_connection
user = new_user
user.save
date = user.created_on
User.get!(user.id).created_on.should == date
end
it "should handle JsonObject" do
pending("No CouchDB connection.") if @no_connection
user = new_user
location = { 'city' => 'San Francisco', 'state' => 'California' }
user.location = location
user.save
User.get!(user.id).location.should == location
end
it "should be able to call stored views" do
pending("No CouchDB connection.") if @no_connection
User.by_name.first.should == User.all(:order => [:name]).first
User.by_age.first.should == User.all(:order => [:age]).first
end
it "should be able to call stored views with keys" do
pending("No CouchDB connection.") if @no_connection
User.by_name("Aaron").first == User.all(:name => "Aaron").first
User.by_age(30).first == User.all(:age => 30).first
User.by_name("Aaron").first == User.by_name(:key => "Aaron").first
User.by_age(30).first == User.by_age(:key => 30).first
end
it "should return a value from a view with reduce defined" do
pending("No CouchDB connection.") if @no_connection
User.count.should == User.all.length
end
def create_procedures
DataMapper.auto_migrate!
DataMapper.auto_migrate!
end
def new_user(options = {})
default_options = { :name => "Jamie", :age => 67, :wealth => 11.5 }
default_options.merge!(options)
User.new(default_options)
end
end