mongo 1.8.6 → 1.12.5
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/LICENSE +1 -1
- data/README.md +114 -282
- data/Rakefile +18 -4
- data/VERSION +1 -1
- data/bin/mongo_console +27 -5
- data/lib/mongo/bulk_write_collection_view.rb +387 -0
- data/lib/mongo/collection.rb +283 -222
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/{util → connection}/node.rb +58 -6
- data/lib/mongo/{util → connection}/pool.rb +61 -37
- data/lib/mongo/{util → connection}/pool_manager.rb +72 -22
- data/lib/mongo/{util → connection}/sharding_pool_manager.rb +13 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/cursor.rb +183 -57
- data/lib/mongo/db.rb +302 -138
- data/lib/mongo/exception.rb +145 -0
- data/lib/mongo/functional/authentication.rb +455 -0
- data/lib/mongo/{util → functional}/logging.rb +23 -7
- data/lib/mongo/functional/read_preference.rb +183 -0
- data/lib/mongo/functional/scram.rb +556 -0
- data/lib/mongo/functional/uri_parser.rb +409 -0
- data/lib/mongo/{util → functional}/write_concern.rb +21 -9
- data/lib/mongo/functional.rb +20 -0
- data/lib/mongo/gridfs/grid.rb +19 -8
- data/lib/mongo/gridfs/grid_ext.rb +14 -0
- data/lib/mongo/gridfs/grid_file_system.rb +17 -4
- data/lib/mongo/gridfs/grid_io.rb +21 -9
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/legacy.rb +76 -7
- data/lib/mongo/mongo_client.rb +246 -206
- data/lib/mongo/mongo_replica_set_client.rb +65 -15
- data/lib/mongo/mongo_sharded_client.rb +18 -3
- data/lib/mongo/networking.rb +47 -18
- data/lib/mongo/{util → utils}/conversions.rb +18 -3
- data/lib/mongo/{util → utils}/core_ext.rb +15 -32
- data/lib/mongo/{util → utils}/server_version.rb +15 -0
- data/lib/mongo/{util → utils}/support.rb +22 -55
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo.rb +44 -26
- data/mongo.gemspec +2 -2
- data/test/functional/authentication_test.rb +31 -10
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1198 -0
- data/test/functional/client_test.rb +627 -0
- data/test/functional/collection_test.rb +1419 -654
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +46 -2
- data/test/functional/cursor_fail_test.rb +17 -9
- data/test/functional/cursor_message_test.rb +28 -15
- data/test/functional/cursor_test.rb +300 -165
- data/test/functional/db_api_test.rb +294 -264
- data/test/functional/db_connection_test.rb +15 -3
- data/test/functional/db_test.rb +165 -99
- data/test/functional/grid_file_system_test.rb +124 -112
- data/test/functional/grid_io_test.rb +17 -3
- data/test/functional/grid_test.rb +16 -2
- data/test/functional/pool_test.rb +99 -10
- data/test/functional/safe_test.rb +18 -4
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +14 -0
- data/test/functional/timeout_test.rb +27 -27
- data/test/functional/uri_test.rb +268 -22
- data/test/functional/write_concern_test.rb +19 -5
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +476 -0
- data/test/replica_set/authentication_test.rb +28 -11
- data/test/replica_set/basic_test.rb +79 -23
- data/test/replica_set/client_test.rb +253 -124
- data/test/replica_set/connection_test.rb +59 -37
- data/test/replica_set/count_test.rb +18 -2
- data/test/replica_set/cursor_test.rb +30 -8
- data/test/replica_set/insert_test.rb +109 -2
- data/test/replica_set/max_values_test.rb +85 -10
- data/test/replica_set/pinning_test.rb +66 -2
- data/test/replica_set/query_test.rb +17 -3
- data/test/replica_set/read_preference_test.rb +115 -96
- data/test/replica_set/refresh_test.rb +59 -9
- data/test/replica_set/replication_ack_test.rb +32 -11
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +73 -25
- data/test/shared/authentication/basic_auth_shared.rb +260 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
- data/test/shared/authentication/gssapi_shared.rb +176 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/authentication/scram_shared.rb +92 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +47 -196
- data/test/threading/basic_test.rb +42 -2
- data/test/tools/mongo_config.rb +175 -35
- data/test/tools/mongo_config_test.rb +15 -1
- data/test/unit/client_test.rb +186 -57
- data/test/unit/collection_test.rb +44 -54
- data/test/unit/connection_test.rb +160 -71
- data/test/unit/cursor_test.rb +37 -3
- data/test/unit/db_test.rb +38 -14
- data/test/unit/grid_test.rb +15 -1
- data/test/unit/mongo_sharded_client_test.rb +30 -14
- data/test/unit/node_test.rb +16 -1
- data/test/unit/pool_manager_test.rb +21 -4
- data/test/unit/read_pref_test.rb +386 -1
- data/test/unit/read_test.rb +27 -13
- data/test/unit/safe_test.rb +22 -8
- data/test/unit/sharding_pool_manager_test.rb +25 -4
- data/test/unit/write_concern_test.rb +23 -9
- data.tar.gz.sig +0 -0
- metadata +80 -54
- metadata.gz.sig +0 -0
- data/lib/mongo/exceptions.rb +0 -65
- data/lib/mongo/util/read_preference.rb +0 -112
- data/lib/mongo/util/socket_util.rb +0 -20
- data/lib/mongo/util/ssl_socket.rb +0 -51
- data/lib/mongo/util/tcp_socket.rb +0 -62
- data/lib/mongo/util/thread_local_variable_manager.rb +0 -11
- data/lib/mongo/util/unix_socket.rb +0 -23
- data/lib/mongo/util/uri_parser.rb +0 -337
- data/test/functional/connection_test.rb +0 -449
- data/test/functional/threading_test.rb +0 -95
- data/test/replica_set/complex_connect_test.rb +0 -64
- data/test/shared/authentication.rb +0 -66
- data/test/unit/pool_test.rb +0 -9
- data/test/unit/util_test.rb +0 -55
|
@@ -1,45 +1,58 @@
|
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
1
15
|
require 'test_helper'
|
|
2
16
|
|
|
3
17
|
class DBAPITest < Test::Unit::TestCase
|
|
4
18
|
include Mongo
|
|
5
19
|
include BSON
|
|
6
20
|
|
|
7
|
-
@@client = standard_connection
|
|
8
|
-
@@db = @@client.db(MONGO_TEST_DB)
|
|
9
|
-
@@coll = @@db.collection('test')
|
|
10
|
-
@@version = @@client.server_version
|
|
11
|
-
|
|
12
21
|
def setup
|
|
13
|
-
|
|
14
|
-
@
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
@client = standard_connection
|
|
23
|
+
@db = @client.db(TEST_DB)
|
|
24
|
+
@coll = @db.collection('test')
|
|
25
|
+
@version = @client.server_version
|
|
26
|
+
@coll.remove
|
|
27
|
+
@r1 = {'a' => 1}
|
|
28
|
+
@coll.insert(@r1) # collection not created until it's used
|
|
29
|
+
@coll_full_name = "#{TEST_DB}.test"
|
|
17
30
|
end
|
|
18
31
|
|
|
19
32
|
def teardown
|
|
20
|
-
|
|
21
|
-
|
|
33
|
+
@coll.remove
|
|
34
|
+
@db.get_last_error
|
|
22
35
|
end
|
|
23
36
|
|
|
24
37
|
def test_clear
|
|
25
|
-
assert_equal 1,
|
|
26
|
-
|
|
27
|
-
assert_equal 0,
|
|
38
|
+
assert_equal 1, @coll.count
|
|
39
|
+
@coll.remove
|
|
40
|
+
assert_equal 0, @coll.count
|
|
28
41
|
end
|
|
29
42
|
|
|
30
43
|
def test_insert
|
|
31
|
-
assert_kind_of BSON::ObjectId,
|
|
32
|
-
assert_kind_of BSON::ObjectId,
|
|
44
|
+
assert_kind_of BSON::ObjectId, @coll.insert('a' => 2)
|
|
45
|
+
assert_kind_of BSON::ObjectId, @coll.insert('b' => 3)
|
|
33
46
|
|
|
34
|
-
assert_equal 3,
|
|
35
|
-
docs =
|
|
47
|
+
assert_equal 3, @coll.count
|
|
48
|
+
docs = @coll.find().to_a
|
|
36
49
|
assert_equal 3, docs.length
|
|
37
50
|
assert docs.detect { |row| row['a'] == 1 }
|
|
38
51
|
assert docs.detect { |row| row['a'] == 2 }
|
|
39
52
|
assert docs.detect { |row| row['b'] == 3 }
|
|
40
53
|
|
|
41
|
-
|
|
42
|
-
docs =
|
|
54
|
+
@coll << {'b' => 4}
|
|
55
|
+
docs = @coll.find().to_a
|
|
43
56
|
assert_equal 4, docs.length
|
|
44
57
|
assert docs.detect { |row| row['b'] == 4 }
|
|
45
58
|
end
|
|
@@ -49,23 +62,23 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
49
62
|
oh['a'] = -1
|
|
50
63
|
oh['b'] = 'foo'
|
|
51
64
|
|
|
52
|
-
oid =
|
|
53
|
-
assert_equal 'foo',
|
|
65
|
+
oid = @coll.save(oh)
|
|
66
|
+
assert_equal 'foo', @coll.find_one(oid)['b']
|
|
54
67
|
|
|
55
68
|
oh = BSON::OrderedHash['a' => 1, 'b' => 'foo']
|
|
56
|
-
oid =
|
|
57
|
-
assert_equal 'foo',
|
|
69
|
+
oid = @coll.save(oh)
|
|
70
|
+
assert_equal 'foo', @coll.find_one(oid)['b']
|
|
58
71
|
end
|
|
59
72
|
|
|
60
73
|
def test_insert_multiple
|
|
61
|
-
ids =
|
|
74
|
+
ids = @coll.insert([{'a' => 2}, {'b' => 3}])
|
|
62
75
|
|
|
63
76
|
ids.each do |i|
|
|
64
77
|
assert_kind_of BSON::ObjectId, i
|
|
65
78
|
end
|
|
66
79
|
|
|
67
|
-
assert_equal 3,
|
|
68
|
-
docs =
|
|
80
|
+
assert_equal 3, @coll.count
|
|
81
|
+
docs = @coll.find().to_a
|
|
69
82
|
assert_equal 3, docs.length
|
|
70
83
|
assert docs.detect { |row| row['a'] == 1 }
|
|
71
84
|
assert docs.detect { |row| row['a'] == 2 }
|
|
@@ -73,20 +86,20 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
73
86
|
end
|
|
74
87
|
|
|
75
88
|
def test_count_on_nonexisting
|
|
76
|
-
|
|
77
|
-
assert_equal 0,
|
|
89
|
+
@db.drop_collection('foo')
|
|
90
|
+
assert_equal 0, @db.collection('foo').count()
|
|
78
91
|
end
|
|
79
92
|
|
|
80
93
|
def test_find_simple
|
|
81
|
-
@r2 =
|
|
82
|
-
@r3 =
|
|
94
|
+
@r2 = @coll.insert('a' => 2)
|
|
95
|
+
@r3 = @coll.insert('b' => 3)
|
|
83
96
|
# Check sizes
|
|
84
|
-
docs =
|
|
97
|
+
docs = @coll.find().to_a
|
|
85
98
|
assert_equal 3, docs.size
|
|
86
|
-
assert_equal 3,
|
|
99
|
+
assert_equal 3, @coll.count
|
|
87
100
|
|
|
88
101
|
# Find by other value
|
|
89
|
-
docs =
|
|
102
|
+
docs = @coll.find('a' => @r1['a']).to_a
|
|
90
103
|
assert_equal 1, docs.size
|
|
91
104
|
doc = docs.first
|
|
92
105
|
# Can't compare _id values because at insert, an _id was added to @r1 by
|
|
@@ -97,52 +110,52 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
97
110
|
end
|
|
98
111
|
|
|
99
112
|
def test_find_advanced
|
|
100
|
-
|
|
101
|
-
|
|
113
|
+
@coll.insert('a' => 2)
|
|
114
|
+
@coll.insert('b' => 3)
|
|
102
115
|
|
|
103
116
|
# Find by advanced query (less than)
|
|
104
|
-
docs =
|
|
117
|
+
docs = @coll.find('a' => { '$lt' => 10 }).to_a
|
|
105
118
|
assert_equal 2, docs.size
|
|
106
119
|
assert docs.detect { |row| row['a'] == 1 }
|
|
107
120
|
assert docs.detect { |row| row['a'] == 2 }
|
|
108
121
|
|
|
109
122
|
# Find by advanced query (greater than)
|
|
110
|
-
docs =
|
|
123
|
+
docs = @coll.find('a' => { '$gt' => 1 }).to_a
|
|
111
124
|
assert_equal 1, docs.size
|
|
112
125
|
assert docs.detect { |row| row['a'] == 2 }
|
|
113
126
|
|
|
114
127
|
# Find by advanced query (less than or equal to)
|
|
115
|
-
docs =
|
|
128
|
+
docs = @coll.find('a' => { '$lte' => 1 }).to_a
|
|
116
129
|
assert_equal 1, docs.size
|
|
117
130
|
assert docs.detect { |row| row['a'] == 1 }
|
|
118
131
|
|
|
119
132
|
# Find by advanced query (greater than or equal to)
|
|
120
|
-
docs =
|
|
133
|
+
docs = @coll.find('a' => { '$gte' => 1 }).to_a
|
|
121
134
|
assert_equal 2, docs.size
|
|
122
135
|
assert docs.detect { |row| row['a'] == 1 }
|
|
123
136
|
assert docs.detect { |row| row['a'] == 2 }
|
|
124
137
|
|
|
125
138
|
# Find by advanced query (between)
|
|
126
|
-
docs =
|
|
139
|
+
docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
|
|
127
140
|
assert_equal 1, docs.size
|
|
128
141
|
assert docs.detect { |row| row['a'] == 2 }
|
|
129
142
|
|
|
130
143
|
# Find by advanced query (in clause)
|
|
131
|
-
docs =
|
|
144
|
+
docs = @coll.find('a' => {'$in' => [1,2]}).to_a
|
|
132
145
|
assert_equal 2, docs.size
|
|
133
146
|
assert docs.detect { |row| row['a'] == 1 }
|
|
134
147
|
assert docs.detect { |row| row['a'] == 2 }
|
|
135
148
|
end
|
|
136
149
|
|
|
137
150
|
def test_find_sorting
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
151
|
+
@coll.remove
|
|
152
|
+
@coll.insert('a' => 1, 'b' => 2)
|
|
153
|
+
@coll.insert('a' => 2, 'b' => 1)
|
|
154
|
+
@coll.insert('a' => 3, 'b' => 2)
|
|
155
|
+
@coll.insert('a' => 4, 'b' => 1)
|
|
143
156
|
|
|
144
157
|
# Sorting (ascending)
|
|
145
|
-
docs =
|
|
158
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', 1]]).to_a
|
|
146
159
|
assert_equal 4, docs.size
|
|
147
160
|
assert_equal 1, docs[0]['a']
|
|
148
161
|
assert_equal 2, docs[1]['a']
|
|
@@ -150,7 +163,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
150
163
|
assert_equal 4, docs[3]['a']
|
|
151
164
|
|
|
152
165
|
# Sorting (descending)
|
|
153
|
-
docs =
|
|
166
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', -1]]).to_a
|
|
154
167
|
assert_equal 4, docs.size
|
|
155
168
|
assert_equal 4, docs[0]['a']
|
|
156
169
|
assert_equal 3, docs[1]['a']
|
|
@@ -158,7 +171,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
158
171
|
assert_equal 1, docs[3]['a']
|
|
159
172
|
|
|
160
173
|
# Sorting using array of names; assumes ascending order.
|
|
161
|
-
docs =
|
|
174
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
|
162
175
|
assert_equal 4, docs.size
|
|
163
176
|
assert_equal 1, docs[0]['a']
|
|
164
177
|
assert_equal 2, docs[1]['a']
|
|
@@ -166,14 +179,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
166
179
|
assert_equal 4, docs[3]['a']
|
|
167
180
|
|
|
168
181
|
# Sorting using single name; assumes ascending order.
|
|
169
|
-
docs =
|
|
182
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
|
170
183
|
assert_equal 4, docs.size
|
|
171
184
|
assert_equal 1, docs[0]['a']
|
|
172
185
|
assert_equal 2, docs[1]['a']
|
|
173
186
|
assert_equal 3, docs[2]['a']
|
|
174
187
|
assert_equal 4, docs[3]['a']
|
|
175
188
|
|
|
176
|
-
docs =
|
|
189
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [['b', 'asc'], ['a', 'asc']]).to_a
|
|
177
190
|
assert_equal 4, docs.size
|
|
178
191
|
assert_equal 2, docs[0]['a']
|
|
179
192
|
assert_equal 4, docs[1]['a']
|
|
@@ -181,7 +194,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
181
194
|
assert_equal 3, docs[3]['a']
|
|
182
195
|
|
|
183
196
|
# Sorting using empty array; no order guarantee should not blow up.
|
|
184
|
-
docs =
|
|
197
|
+
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
|
|
185
198
|
assert_equal 4, docs.size
|
|
186
199
|
end
|
|
187
200
|
|
|
@@ -189,17 +202,17 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
189
202
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
|
190
203
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
|
191
204
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
205
|
+
@coll.remove
|
|
206
|
+
@coll.insert('a' => 1, 'b' => 2)
|
|
207
|
+
@coll.insert('a' => 2, 'b' => 1)
|
|
208
|
+
@coll.insert('a' => 3, 'b' => 2)
|
|
209
|
+
@coll.insert('a' => 4, 'b' => 1)
|
|
197
210
|
|
|
198
211
|
oh = BSON::OrderedHash.new
|
|
199
212
|
oh['a'] = -1
|
|
200
213
|
|
|
201
214
|
# Sort as a method
|
|
202
|
-
docs =
|
|
215
|
+
docs = @coll.find.sort(oh).to_a
|
|
203
216
|
assert_equal 4, docs.size
|
|
204
217
|
assert_equal 4, docs[0]['a']
|
|
205
218
|
assert_equal 3, docs[1]['a']
|
|
@@ -207,7 +220,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
207
220
|
assert_equal 1, docs[3]['a']
|
|
208
221
|
|
|
209
222
|
# Sort as an option
|
|
210
|
-
docs =
|
|
223
|
+
docs = @coll.find({}, :sort => oh).to_a
|
|
211
224
|
assert_equal 4, docs.size
|
|
212
225
|
assert_equal 4, docs[0]['a']
|
|
213
226
|
assert_equal 3, docs[1]['a']
|
|
@@ -215,21 +228,21 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
215
228
|
assert_equal 1, docs[3]['a']
|
|
216
229
|
|
|
217
230
|
if RUBY_VERSION > '1.9'
|
|
218
|
-
docs =
|
|
231
|
+
docs = @coll.find({}, :sort => {:a => -1}).to_a
|
|
219
232
|
assert_equal 4, docs.size
|
|
220
233
|
assert_equal 4, docs[0]['a']
|
|
221
234
|
assert_equal 3, docs[1]['a']
|
|
222
235
|
assert_equal 2, docs[2]['a']
|
|
223
236
|
assert_equal 1, docs[3]['a']
|
|
224
237
|
|
|
225
|
-
docs =
|
|
238
|
+
docs = @coll.find.sort(:a => -1).to_a
|
|
226
239
|
assert_equal 4, docs.size
|
|
227
240
|
assert_equal 4, docs[0]['a']
|
|
228
241
|
assert_equal 3, docs[1]['a']
|
|
229
242
|
assert_equal 2, docs[2]['a']
|
|
230
243
|
assert_equal 1, docs[3]['a']
|
|
231
244
|
|
|
232
|
-
docs =
|
|
245
|
+
docs = @coll.find.sort(:b => -1, :a => 1).to_a
|
|
233
246
|
assert_equal 4, docs.size
|
|
234
247
|
assert_equal 1, docs[0]['a']
|
|
235
248
|
assert_equal 3, docs[1]['a']
|
|
@@ -238,168 +251,183 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
238
251
|
else
|
|
239
252
|
# Sort as an option
|
|
240
253
|
assert_raise InvalidSortValueError do
|
|
241
|
-
|
|
254
|
+
@coll.find({}, :sort => {:a => -1}).to_a
|
|
242
255
|
end
|
|
243
256
|
# Sort as a method
|
|
244
257
|
assert_raise InvalidSortValueError do
|
|
245
|
-
|
|
258
|
+
@coll.find.sort(:a => -1).to_a
|
|
246
259
|
end
|
|
247
260
|
end
|
|
248
261
|
end
|
|
249
262
|
|
|
250
263
|
def test_find_limits
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
264
|
+
@coll.insert('b' => 2)
|
|
265
|
+
@coll.insert('c' => 3)
|
|
266
|
+
@coll.insert('d' => 4)
|
|
254
267
|
|
|
255
|
-
docs =
|
|
268
|
+
docs = @coll.find({}, :limit => 1).to_a
|
|
256
269
|
assert_equal 1, docs.size
|
|
257
|
-
docs =
|
|
270
|
+
docs = @coll.find({}, :limit => 2).to_a
|
|
258
271
|
assert_equal 2, docs.size
|
|
259
|
-
docs =
|
|
272
|
+
docs = @coll.find({}, :limit => 3).to_a
|
|
260
273
|
assert_equal 3, docs.size
|
|
261
|
-
docs =
|
|
274
|
+
docs = @coll.find({}, :limit => 4).to_a
|
|
262
275
|
assert_equal 4, docs.size
|
|
263
|
-
docs =
|
|
276
|
+
docs = @coll.find({}).to_a
|
|
264
277
|
assert_equal 4, docs.size
|
|
265
|
-
docs =
|
|
278
|
+
docs = @coll.find({}, :limit => 99).to_a
|
|
266
279
|
assert_equal 4, docs.size
|
|
267
280
|
end
|
|
268
281
|
|
|
269
282
|
def test_find_one_no_records
|
|
270
|
-
|
|
271
|
-
x =
|
|
283
|
+
@coll.remove
|
|
284
|
+
x = @coll.find_one('a' => 1)
|
|
272
285
|
assert_nil x
|
|
273
286
|
end
|
|
274
287
|
|
|
275
288
|
def test_drop_collection
|
|
276
|
-
assert
|
|
277
|
-
assert
|
|
289
|
+
assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed"
|
|
290
|
+
assert !@db.collection_names.include?(@coll.name)
|
|
278
291
|
end
|
|
279
292
|
|
|
280
293
|
def test_other_drop
|
|
281
|
-
assert
|
|
282
|
-
|
|
283
|
-
assert
|
|
294
|
+
assert @db.collection_names.include?(@coll.name)
|
|
295
|
+
@coll.drop
|
|
296
|
+
assert !@db.collection_names.include?(@coll.name)
|
|
284
297
|
end
|
|
285
298
|
|
|
286
299
|
def test_collection_names
|
|
287
|
-
names =
|
|
300
|
+
names = @db.collection_names
|
|
288
301
|
assert names.length >= 1
|
|
289
|
-
assert names.include?(
|
|
302
|
+
assert names.include?(@coll.name)
|
|
290
303
|
|
|
291
|
-
coll2 =
|
|
304
|
+
coll2 = @db.collection('test2')
|
|
292
305
|
coll2.insert('a' => 1) # collection not created until it's used
|
|
293
|
-
names =
|
|
306
|
+
names = @db.collection_names
|
|
294
307
|
assert names.length >= 2
|
|
295
|
-
assert names.include?(
|
|
308
|
+
assert names.include?(@coll.name)
|
|
296
309
|
assert names.include?('test2')
|
|
297
310
|
ensure
|
|
298
|
-
|
|
311
|
+
@db.drop_collection('test2')
|
|
299
312
|
end
|
|
300
313
|
|
|
301
314
|
def test_collections_info
|
|
302
|
-
cursor =
|
|
315
|
+
cursor = @db.collections_info
|
|
303
316
|
rows = cursor.to_a
|
|
304
317
|
assert rows.length >= 1
|
|
305
|
-
|
|
318
|
+
if @client.server_version < '2.7.6'
|
|
319
|
+
row = rows.detect { |r| r['name'] == @coll_full_name }
|
|
320
|
+
else
|
|
321
|
+
row = rows.detect { |r| r['name'] == @coll.name }
|
|
322
|
+
end
|
|
306
323
|
assert_not_nil row
|
|
307
324
|
end
|
|
308
325
|
|
|
326
|
+
def test_collections_info_with_name
|
|
327
|
+
cursor = @db.collections_info(@coll.name)
|
|
328
|
+
info = cursor.to_a
|
|
329
|
+
assert_equal 1, info.length
|
|
330
|
+
if @client.server_version < '2.7.6'
|
|
331
|
+
assert_equal "#{@db.name}.#{@coll.name}", info.first['name']
|
|
332
|
+
else
|
|
333
|
+
assert_equal @coll.name, info.first['name']
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
309
337
|
def test_collection_options
|
|
310
|
-
|
|
311
|
-
|
|
338
|
+
@db.drop_collection('foobar')
|
|
339
|
+
@db.strict = true
|
|
312
340
|
|
|
313
341
|
begin
|
|
314
|
-
coll =
|
|
342
|
+
coll = @db.create_collection('foobar', :capped => true, :size => 4096)
|
|
315
343
|
options = coll.options
|
|
316
|
-
assert_equal 'foobar', options['create']
|
|
344
|
+
assert_equal 'foobar', options['create'] if @client.server_version < '2.5.5'
|
|
317
345
|
assert_equal true, options['capped']
|
|
318
|
-
assert_equal
|
|
346
|
+
assert_equal 4096, options['size']
|
|
319
347
|
rescue => ex
|
|
320
|
-
|
|
321
|
-
fail "did not expect exception \"#{ex}\""
|
|
348
|
+
@db.drop_collection('foobar')
|
|
349
|
+
fail "did not expect exception \"#{ex.inspect}\""
|
|
322
350
|
ensure
|
|
323
|
-
|
|
351
|
+
@db.strict = false
|
|
324
352
|
end
|
|
325
353
|
end
|
|
326
354
|
|
|
327
355
|
def test_collection_options_are_passed_to_the_existing_ones
|
|
328
|
-
|
|
356
|
+
@db.drop_collection('foobar')
|
|
329
357
|
|
|
330
|
-
|
|
358
|
+
@db.create_collection('foobar')
|
|
331
359
|
|
|
332
|
-
coll =
|
|
360
|
+
coll = @db.create_collection('foobar')
|
|
333
361
|
assert_equal true, Mongo::WriteConcern.gle?(coll.write_concern)
|
|
334
362
|
end
|
|
335
363
|
|
|
336
364
|
|
|
337
365
|
def test_index_information
|
|
338
|
-
assert_equal
|
|
366
|
+
assert_equal @coll.index_information.length, 1
|
|
339
367
|
|
|
340
|
-
name =
|
|
341
|
-
info =
|
|
368
|
+
name = @coll.create_index('a')
|
|
369
|
+
info = @db.index_information(@coll.name)
|
|
342
370
|
assert_equal name, "a_1"
|
|
343
|
-
assert_equal
|
|
371
|
+
assert_equal @coll.index_information, info
|
|
344
372
|
assert_equal 2, info.length
|
|
345
373
|
|
|
346
374
|
assert info.has_key?(name)
|
|
347
375
|
assert_equal info[name]["key"], {"a" => 1}
|
|
348
376
|
ensure
|
|
349
|
-
|
|
377
|
+
@db.drop_index(@coll.name, name)
|
|
350
378
|
end
|
|
351
379
|
|
|
352
380
|
def test_index_create_with_symbol
|
|
353
|
-
assert_equal
|
|
381
|
+
assert_equal @coll.index_information.length, 1
|
|
354
382
|
|
|
355
|
-
name =
|
|
356
|
-
info =
|
|
383
|
+
name = @coll.create_index([['a', 1]])
|
|
384
|
+
info = @db.index_information(@coll.name)
|
|
357
385
|
assert_equal name, "a_1"
|
|
358
|
-
assert_equal
|
|
386
|
+
assert_equal @coll.index_information, info
|
|
359
387
|
assert_equal 2, info.length
|
|
360
388
|
|
|
361
389
|
assert info.has_key?(name)
|
|
362
390
|
assert_equal info[name]['key'], {"a" => 1}
|
|
363
391
|
ensure
|
|
364
|
-
|
|
392
|
+
@db.drop_index(@coll.name, name)
|
|
365
393
|
end
|
|
366
394
|
|
|
367
395
|
def test_multiple_index_cols
|
|
368
|
-
name =
|
|
369
|
-
info =
|
|
396
|
+
name = @coll.create_index([['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
|
|
397
|
+
info = @db.index_information(@coll.name)
|
|
370
398
|
assert_equal 2, info.length
|
|
371
399
|
|
|
372
400
|
assert_equal name, 'a_-1_b_1_c_-1'
|
|
373
401
|
assert info.has_key?(name)
|
|
374
402
|
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
|
|
375
403
|
ensure
|
|
376
|
-
|
|
404
|
+
@db.drop_index(@coll.name, name)
|
|
377
405
|
end
|
|
378
406
|
|
|
379
407
|
def test_multiple_index_cols_with_symbols
|
|
380
|
-
name =
|
|
381
|
-
info =
|
|
408
|
+
name = @coll.create_index([[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]])
|
|
409
|
+
info = @db.index_information(@coll.name)
|
|
382
410
|
assert_equal 2, info.length
|
|
383
411
|
|
|
384
412
|
assert_equal name, 'a_-1_b_1_c_-1'
|
|
385
413
|
assert info.has_key?(name)
|
|
386
414
|
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
|
|
387
415
|
ensure
|
|
388
|
-
|
|
416
|
+
@db.drop_index(@coll.name, name)
|
|
389
417
|
end
|
|
390
418
|
|
|
391
419
|
def test_unique_index
|
|
392
|
-
|
|
393
|
-
test =
|
|
420
|
+
@db.drop_collection("blah")
|
|
421
|
+
test = @db.collection("blah")
|
|
394
422
|
test.create_index("hello")
|
|
395
423
|
|
|
396
424
|
test.insert("hello" => "world")
|
|
397
425
|
test.insert("hello" => "mike")
|
|
398
426
|
test.insert("hello" => "world")
|
|
399
|
-
assert
|
|
427
|
+
assert !@db.error?
|
|
400
428
|
|
|
401
|
-
|
|
402
|
-
test =
|
|
429
|
+
@db.drop_collection("blah")
|
|
430
|
+
test = @db.collection("blah")
|
|
403
431
|
test.create_index("hello", :unique => true)
|
|
404
432
|
|
|
405
433
|
test.insert("hello" => "world")
|
|
@@ -410,16 +438,16 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
410
438
|
end
|
|
411
439
|
|
|
412
440
|
def test_index_on_subfield
|
|
413
|
-
|
|
414
|
-
test =
|
|
441
|
+
@db.drop_collection("blah")
|
|
442
|
+
test = @db.collection("blah")
|
|
415
443
|
|
|
416
444
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
|
417
445
|
test.insert("hello" => {"a" => 7, "b" => 2})
|
|
418
446
|
test.insert("hello" => {"a" => 4, "b" => 10})
|
|
419
|
-
assert
|
|
447
|
+
assert !@db.error?
|
|
420
448
|
|
|
421
|
-
|
|
422
|
-
test =
|
|
449
|
+
@db.drop_collection("blah")
|
|
450
|
+
test = @db.collection("blah")
|
|
423
451
|
test.create_index("hello.a", :unique => true)
|
|
424
452
|
|
|
425
453
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
|
@@ -430,19 +458,19 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
430
458
|
end
|
|
431
459
|
|
|
432
460
|
def test_array
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
rows =
|
|
461
|
+
@coll.remove({'$atomic' => true})
|
|
462
|
+
@coll.insert({'b' => [1, 2, 3]})
|
|
463
|
+
@coll.insert({'b' => [1, 2, 3]})
|
|
464
|
+
rows = @coll.find({}, {:fields => ['b']}).to_a
|
|
437
465
|
assert_equal 2, rows.length
|
|
438
466
|
assert_equal [1, 2, 3], rows[1]['b']
|
|
439
467
|
end
|
|
440
468
|
|
|
441
469
|
def test_regex
|
|
442
470
|
regex = /foobar/i
|
|
443
|
-
|
|
444
|
-
rows =
|
|
445
|
-
if
|
|
471
|
+
@coll << {'b' => regex}
|
|
472
|
+
rows = @coll.find({}, {:fields => ['b']}).to_a
|
|
473
|
+
if @version < "1.1.3"
|
|
446
474
|
assert_equal 1, rows.length
|
|
447
475
|
assert_equal regex, rows[0]['b']
|
|
448
476
|
else
|
|
@@ -452,14 +480,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
452
480
|
end
|
|
453
481
|
|
|
454
482
|
def test_regex_multi_line
|
|
455
|
-
if
|
|
456
|
-
doc = <<HERE
|
|
483
|
+
if @version >= "1.9.1"
|
|
484
|
+
doc = <<HERE
|
|
457
485
|
the lazy brown
|
|
458
486
|
fox
|
|
459
487
|
HERE
|
|
460
|
-
|
|
461
|
-
assert
|
|
462
|
-
|
|
488
|
+
@coll.save({:doc => doc})
|
|
489
|
+
assert @coll.find_one({:doc => /n.*x/m})
|
|
490
|
+
@coll.remove
|
|
463
491
|
end
|
|
464
492
|
end
|
|
465
493
|
|
|
@@ -467,135 +495,137 @@ HERE
|
|
|
467
495
|
# Note: can't use Time.new because that will include fractional seconds,
|
|
468
496
|
# which Mongo does not store.
|
|
469
497
|
t = Time.at(1234567890)
|
|
470
|
-
|
|
471
|
-
rows =
|
|
498
|
+
@coll << {'_id' => t}
|
|
499
|
+
rows = @coll.find({'_id' => t}).to_a
|
|
472
500
|
assert_equal 1, rows.length
|
|
473
501
|
assert_equal t, rows[0]['_id']
|
|
474
502
|
end
|
|
475
503
|
|
|
476
504
|
def test_strict
|
|
477
|
-
assert
|
|
478
|
-
|
|
479
|
-
assert
|
|
505
|
+
assert !@db.strict?
|
|
506
|
+
@db.strict = true
|
|
507
|
+
assert @db.strict?
|
|
480
508
|
ensure
|
|
481
|
-
|
|
509
|
+
@db.strict = false
|
|
482
510
|
end
|
|
483
511
|
|
|
484
512
|
def test_strict_access_collection
|
|
485
|
-
|
|
513
|
+
@db.strict = true
|
|
486
514
|
begin
|
|
487
|
-
|
|
515
|
+
@db.collection('does-not-exist')
|
|
488
516
|
fail "expected exception"
|
|
489
517
|
rescue => ex
|
|
490
518
|
assert_equal Mongo::MongoDBError, ex.class
|
|
491
519
|
assert_equal "Collection 'does-not-exist' doesn't exist. (strict=true)", ex.to_s
|
|
492
520
|
ensure
|
|
493
|
-
|
|
494
|
-
|
|
521
|
+
@db.strict = false
|
|
522
|
+
@db.drop_collection('does-not-exist')
|
|
495
523
|
end
|
|
496
524
|
end
|
|
497
525
|
|
|
498
526
|
def test_strict_create_collection
|
|
499
|
-
|
|
500
|
-
|
|
527
|
+
@db.drop_collection('foobar')
|
|
528
|
+
@db.strict = true
|
|
501
529
|
|
|
502
530
|
begin
|
|
503
|
-
assert
|
|
531
|
+
assert @db.create_collection('foobar')
|
|
504
532
|
rescue => ex
|
|
505
533
|
fail "did not expect exception \"#{ex}\""
|
|
506
534
|
end
|
|
507
535
|
|
|
508
536
|
# Now the collection exists. This time we should see an exception.
|
|
509
537
|
assert_raise Mongo::MongoDBError do
|
|
510
|
-
|
|
538
|
+
@db.create_collection('foobar')
|
|
511
539
|
end
|
|
512
|
-
|
|
513
|
-
|
|
540
|
+
@db.strict = false
|
|
541
|
+
@db.drop_collection('foobar')
|
|
514
542
|
|
|
515
543
|
# Now we're not in strict mode - should succeed
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
544
|
+
@db.create_collection('foobar')
|
|
545
|
+
@db.create_collection('foobar')
|
|
546
|
+
@db.drop_collection('foobar')
|
|
519
547
|
end
|
|
520
548
|
|
|
521
549
|
def test_where
|
|
522
|
-
|
|
523
|
-
|
|
550
|
+
@coll.insert('a' => 2)
|
|
551
|
+
@coll.insert('a' => 3)
|
|
524
552
|
|
|
525
|
-
assert_equal 3,
|
|
526
|
-
assert_equal 1,
|
|
527
|
-
assert_equal 2,
|
|
553
|
+
assert_equal 3, @coll.count
|
|
554
|
+
assert_equal 1, @coll.find('$where' => BSON::Code.new('this.a > 2')).count()
|
|
555
|
+
assert_equal 2, @coll.find('$where' => BSON::Code.new('this.a > i', {'i' => 1})).count()
|
|
528
556
|
end
|
|
529
557
|
|
|
530
558
|
def test_eval
|
|
531
|
-
|
|
559
|
+
grant_admin_user_eval_role(@client)
|
|
560
|
+
|
|
561
|
+
assert_equal 3, @db.eval('function (x) {return x;}', 3)
|
|
532
562
|
|
|
533
|
-
assert_equal nil,
|
|
534
|
-
assert_equal 5,
|
|
563
|
+
assert_equal nil, @db.eval("function (x) {db.test_eval.save({y:x});}", 5)
|
|
564
|
+
assert_equal 5, @db.collection('test_eval').find_one['y']
|
|
535
565
|
|
|
536
|
-
assert_equal 5,
|
|
537
|
-
assert_equal 5,
|
|
538
|
-
assert_equal 5,
|
|
566
|
+
assert_equal 5, @db.eval("function (x, y) {return x + y;}", 2, 3)
|
|
567
|
+
assert_equal 5, @db.eval("function () {return 5;}")
|
|
568
|
+
assert_equal 5, @db.eval("2 + 3;")
|
|
539
569
|
|
|
540
|
-
assert_equal 5,
|
|
541
|
-
assert_equal 2,
|
|
542
|
-
assert_equal 5,
|
|
570
|
+
assert_equal 5, @db.eval(Code.new("2 + 3;"))
|
|
571
|
+
assert_equal 2, @db.eval(Code.new("return i;", {"i" => 2}))
|
|
572
|
+
assert_equal 5, @db.eval(Code.new("i + 3;", {"i" => 2}))
|
|
543
573
|
|
|
544
574
|
assert_raise OperationFailure do
|
|
545
|
-
|
|
575
|
+
@db.eval("5 ++ 5;")
|
|
546
576
|
end
|
|
547
577
|
end
|
|
548
578
|
|
|
549
579
|
def test_hint
|
|
550
|
-
name =
|
|
580
|
+
name = @coll.create_index('a')
|
|
551
581
|
begin
|
|
552
|
-
assert_nil
|
|
553
|
-
assert_equal 1,
|
|
554
|
-
assert_equal 1,
|
|
555
|
-
assert_equal 1,
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
assert_equal({'a' => 1},
|
|
559
|
-
assert_equal 1,
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
assert_equal({'a' => 1},
|
|
563
|
-
assert_equal 1,
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
assert_equal({'a' => 1},
|
|
567
|
-
assert_equal 1,
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
assert_nil
|
|
571
|
-
assert_equal 1,
|
|
582
|
+
assert_nil @coll.hint
|
|
583
|
+
assert_equal 1, @coll.find({'a' => 1}, :hint => 'a').to_a.size
|
|
584
|
+
assert_equal 1, @coll.find({'a' => 1}, :hint => ['a']).to_a.size
|
|
585
|
+
assert_equal 1, @coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
|
|
586
|
+
|
|
587
|
+
@coll.hint = 'a'
|
|
588
|
+
assert_equal({'a' => 1}, @coll.hint)
|
|
589
|
+
assert_equal 1, @coll.find('a' => 1).to_a.size
|
|
590
|
+
|
|
591
|
+
@coll.hint = ['a']
|
|
592
|
+
assert_equal({'a' => 1}, @coll.hint)
|
|
593
|
+
assert_equal 1, @coll.find('a' => 1).to_a.size
|
|
594
|
+
|
|
595
|
+
@coll.hint = {'a' => 1}
|
|
596
|
+
assert_equal({'a' => 1}, @coll.hint)
|
|
597
|
+
assert_equal 1, @coll.find('a' => 1).to_a.size
|
|
598
|
+
|
|
599
|
+
@coll.hint = nil
|
|
600
|
+
assert_nil @coll.hint
|
|
601
|
+
assert_equal 1, @coll.find('a' => 1).to_a.size
|
|
572
602
|
ensure
|
|
573
|
-
|
|
603
|
+
@coll.drop_index(name)
|
|
574
604
|
end
|
|
575
605
|
end
|
|
576
606
|
|
|
577
607
|
def test_named_hint
|
|
578
|
-
name =
|
|
608
|
+
name = @coll.create_index('a', :name => 'named_index')
|
|
579
609
|
begin
|
|
580
|
-
assert_nil
|
|
581
|
-
assert_equal 1,
|
|
582
|
-
assert_equal 1,
|
|
610
|
+
assert_nil @coll.hint
|
|
611
|
+
assert_equal 1, @coll.find({'a' => 1}, :named_hint => 'named_index').to_a.size
|
|
612
|
+
assert_equal 1, @coll.find({'a' => 1}, :hint => 'a', :named_hint => "bad_hint").to_a.size
|
|
583
613
|
ensure
|
|
584
|
-
|
|
614
|
+
@coll.drop_index('named_index')
|
|
585
615
|
end
|
|
586
616
|
end
|
|
587
617
|
|
|
588
618
|
def test_hash_default_value_id
|
|
589
619
|
val = Hash.new(0)
|
|
590
620
|
val["x"] = 5
|
|
591
|
-
|
|
592
|
-
id =
|
|
621
|
+
@coll.insert val
|
|
622
|
+
id = @coll.find_one("x" => 5)["_id"]
|
|
593
623
|
assert id != 0
|
|
594
624
|
end
|
|
595
625
|
|
|
596
626
|
def test_group
|
|
597
|
-
|
|
598
|
-
test =
|
|
627
|
+
@db.drop_collection("test")
|
|
628
|
+
test = @db.collection("test")
|
|
599
629
|
|
|
600
630
|
assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
|
|
601
631
|
assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
|
|
@@ -605,26 +635,26 @@ HERE
|
|
|
605
635
|
test.insert("a" => 1)
|
|
606
636
|
|
|
607
637
|
assert_equal 3, test.group(:initial => {"count" => 0},
|
|
608
|
-
|
|
638
|
+
:reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
|
|
609
639
|
assert_equal 3, test.group(:initial => {"count" => 0},
|
|
610
|
-
|
|
640
|
+
:reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
|
|
611
641
|
assert_equal 1, test.group(:cond => {"a" => {"$gt" => 1}},
|
|
612
|
-
|
|
642
|
+
:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
|
|
613
643
|
assert_equal 1, test.group(:cond => {"a" => {"$gt" => 1}},
|
|
614
|
-
|
|
644
|
+
:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
|
|
615
645
|
|
|
616
646
|
finalize = "function (obj) { obj.f = obj.count - 1; }"
|
|
617
|
-
assert_equal 2, test.group(:initial => {"count" => 0},
|
|
618
|
-
|
|
647
|
+
assert_equal 2, test.group(:initial => {"count" => 0},
|
|
648
|
+
:reduce => "function (obj, prev) { prev.count++; }", :finalize => finalize)[0]["f"]
|
|
619
649
|
|
|
620
650
|
test.insert("a" => 2, "b" => 3)
|
|
621
651
|
expected = [{"a" => 2, "count" => 2},
|
|
622
652
|
{"a" => nil, "count" => 1},
|
|
623
653
|
{"a" => 1, "count" => 1}]
|
|
624
654
|
assert_equal expected, test.group(:key => ["a"], :initial => {"count" => 0},
|
|
625
|
-
|
|
655
|
+
:reduce => "function (obj, prev) { prev.count++; }")
|
|
626
656
|
assert_equal expected, test.group(:key => :a, :initial => {"count" => 0},
|
|
627
|
-
|
|
657
|
+
:reduce => "function (obj, prev) { prev.count++; }")
|
|
628
658
|
|
|
629
659
|
assert_raise OperationFailure do
|
|
630
660
|
test.group(:initial => {}, :reduce => "5 ++ 5")
|
|
@@ -632,109 +662,109 @@ HERE
|
|
|
632
662
|
end
|
|
633
663
|
|
|
634
664
|
def test_deref
|
|
635
|
-
|
|
665
|
+
@coll.remove
|
|
636
666
|
|
|
637
|
-
assert_equal nil,
|
|
638
|
-
|
|
639
|
-
key =
|
|
640
|
-
assert_equal "hello",
|
|
667
|
+
assert_equal nil, @db.dereference(DBRef.new("test", ObjectId.new))
|
|
668
|
+
@coll.insert({"x" => "hello"})
|
|
669
|
+
key = @coll.find_one()["_id"]
|
|
670
|
+
assert_equal "hello", @db.dereference(DBRef.new("test", key))["x"]
|
|
641
671
|
|
|
642
|
-
assert_equal nil,
|
|
672
|
+
assert_equal nil, @db.dereference(DBRef.new("test", 4))
|
|
643
673
|
obj = {"_id" => 4}
|
|
644
|
-
|
|
645
|
-
assert_equal obj,
|
|
674
|
+
@coll.insert(obj)
|
|
675
|
+
assert_equal obj, @db.dereference(DBRef.new("test", 4))
|
|
646
676
|
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
assert_equal nil,
|
|
677
|
+
@coll.remove
|
|
678
|
+
@coll.insert({"x" => "hello"})
|
|
679
|
+
assert_equal nil, @db.dereference(DBRef.new("test", nil))
|
|
650
680
|
end
|
|
651
681
|
|
|
652
682
|
def test_save
|
|
653
|
-
|
|
683
|
+
@coll.remove
|
|
654
684
|
|
|
655
685
|
a = {"hello" => "world"}
|
|
656
686
|
|
|
657
|
-
id =
|
|
687
|
+
id = @coll.save(a)
|
|
658
688
|
assert_kind_of ObjectId, id
|
|
659
|
-
assert_equal 1,
|
|
689
|
+
assert_equal 1, @coll.count
|
|
660
690
|
|
|
661
|
-
assert_equal id,
|
|
662
|
-
assert_equal 1,
|
|
691
|
+
assert_equal id, @coll.save(a)
|
|
692
|
+
assert_equal 1, @coll.count
|
|
663
693
|
|
|
664
|
-
assert_equal "world",
|
|
694
|
+
assert_equal "world", @coll.find_one()["hello"]
|
|
665
695
|
|
|
666
696
|
a["hello"] = "mike"
|
|
667
|
-
|
|
668
|
-
assert_equal 1,
|
|
697
|
+
@coll.save(a)
|
|
698
|
+
assert_equal 1, @coll.count
|
|
669
699
|
|
|
670
|
-
assert_equal "mike",
|
|
700
|
+
assert_equal "mike", @coll.find_one()["hello"]
|
|
671
701
|
|
|
672
|
-
|
|
673
|
-
assert_equal 2,
|
|
702
|
+
@coll.save({"hello" => "world"})
|
|
703
|
+
assert_equal 2, @coll.count
|
|
674
704
|
end
|
|
675
705
|
|
|
676
706
|
def test_save_long
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
assert_equal 9223372036854775807,
|
|
707
|
+
@coll.remove
|
|
708
|
+
@coll.insert("x" => 9223372036854775807)
|
|
709
|
+
assert_equal 9223372036854775807, @coll.find_one()["x"]
|
|
680
710
|
end
|
|
681
711
|
|
|
682
712
|
def test_find_by_oid
|
|
683
|
-
|
|
713
|
+
@coll.remove
|
|
684
714
|
|
|
685
|
-
|
|
686
|
-
id =
|
|
715
|
+
@coll.save("hello" => "mike")
|
|
716
|
+
id = @coll.save("hello" => "world")
|
|
687
717
|
assert_kind_of ObjectId, id
|
|
688
718
|
|
|
689
|
-
assert_equal "world",
|
|
690
|
-
|
|
719
|
+
assert_equal "world", @coll.find_one(:_id => id)["hello"]
|
|
720
|
+
@coll.find(:_id => id).to_a.each do |doc|
|
|
691
721
|
assert_equal "world", doc["hello"]
|
|
692
722
|
end
|
|
693
723
|
|
|
694
724
|
id = ObjectId.from_string(id.to_s)
|
|
695
|
-
assert_equal "world",
|
|
725
|
+
assert_equal "world", @coll.find_one(:_id => id)["hello"]
|
|
696
726
|
end
|
|
697
727
|
|
|
698
728
|
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
|
|
699
|
-
|
|
729
|
+
@coll.remove
|
|
700
730
|
|
|
701
731
|
a = {'_id' => '1', 'hello' => 'world'}
|
|
702
|
-
|
|
703
|
-
assert_equal(1,
|
|
704
|
-
assert_equal("world",
|
|
732
|
+
@coll.save(a)
|
|
733
|
+
assert_equal(1, @coll.count)
|
|
734
|
+
assert_equal("world", @coll.find_one()["hello"])
|
|
705
735
|
|
|
706
736
|
a["hello"] = "mike"
|
|
707
|
-
|
|
708
|
-
assert_equal(1,
|
|
709
|
-
assert_equal("mike",
|
|
737
|
+
@coll.save(a)
|
|
738
|
+
assert_equal(1, @coll.count)
|
|
739
|
+
assert_equal("mike", @coll.find_one()["hello"])
|
|
710
740
|
end
|
|
711
741
|
|
|
712
742
|
def test_collection_names_errors
|
|
713
743
|
assert_raise TypeError do
|
|
714
|
-
|
|
744
|
+
@db.collection(5)
|
|
715
745
|
end
|
|
716
746
|
assert_raise Mongo::InvalidNSName do
|
|
717
|
-
|
|
747
|
+
@db.collection("")
|
|
718
748
|
end
|
|
719
749
|
assert_raise Mongo::InvalidNSName do
|
|
720
|
-
|
|
750
|
+
@db.collection("te$t")
|
|
721
751
|
end
|
|
722
752
|
assert_raise Mongo::InvalidNSName do
|
|
723
|
-
|
|
753
|
+
@db.collection(".test")
|
|
724
754
|
end
|
|
725
755
|
assert_raise Mongo::InvalidNSName do
|
|
726
|
-
|
|
756
|
+
@db.collection("test.")
|
|
727
757
|
end
|
|
728
758
|
assert_raise Mongo::InvalidNSName do
|
|
729
|
-
|
|
759
|
+
@db.collection("tes..t")
|
|
730
760
|
end
|
|
731
761
|
end
|
|
732
762
|
|
|
733
763
|
def test_rename_collection
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
a =
|
|
737
|
-
b =
|
|
764
|
+
@db.drop_collection("foo")
|
|
765
|
+
@db.drop_collection("bar")
|
|
766
|
+
a = @db.collection("foo")
|
|
767
|
+
b = @db.collection("bar")
|
|
738
768
|
|
|
739
769
|
assert_raise TypeError do
|
|
740
770
|
a.rename(5)
|
|
@@ -770,9 +800,9 @@ HERE
|
|
|
770
800
|
|
|
771
801
|
# doesn't really test functionality, just that the option is set correctly
|
|
772
802
|
def test_snapshot
|
|
773
|
-
|
|
803
|
+
@db.collection("test").find({}, :snapshot => true).to_a
|
|
774
804
|
assert_raise OperationFailure do
|
|
775
|
-
|
|
805
|
+
@db.collection("test").find({}, :snapshot => true, :sort => 'a').to_a
|
|
776
806
|
end
|
|
777
807
|
end
|
|
778
808
|
|
|
@@ -793,9 +823,9 @@ HERE
|
|
|
793
823
|
assert_equal "UTF-8", utf8.encoding.name
|
|
794
824
|
assert_equal "ISO-8859-1", iso8859.encoding.name
|
|
795
825
|
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
doc =
|
|
826
|
+
@coll.remove
|
|
827
|
+
@coll.save("default" => default, "utf8" => utf8, "iso8859" => iso8859)
|
|
828
|
+
doc = @coll.find_one()
|
|
799
829
|
|
|
800
830
|
assert_equal "UTF-8", doc["default"].encoding.name
|
|
801
831
|
assert_equal "UTF-8", doc["utf8"].encoding.name
|