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