mongo 1.0.9 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +8 -0
- data/Rakefile +23 -0
- data/lib/mongo.rb +1 -1
- data/lib/mongo/connection.rb +25 -38
- data/lib/mongo/cursor.rb +1 -21
- data/lib/mongo/db.rb +0 -27
- data/test/bson/basic_test.rb +99 -0
- data/test/bson/bench_test.rb +172 -0
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +528 -0
- data/test/bson/byte_buffer_test.rb +190 -0
- data/test/bson/c_encode_test.rb +51 -0
- data/test/bson/java_bson_test.rb +146 -0
- data/test/bson/jruby_bson_test.rb +24 -0
- data/test/bson/jruby_encode_test.rb +438 -0
- data/test/bson/json_test.rb +16 -0
- data/test/bson/object_id_test.rb +132 -0
- data/test/bson/ordered_hash_test.rb +197 -0
- data/test/collection_test.rb +1 -1
- data/test/connection_test.rb +23 -2
- data/test/db_api_test.rb +3 -10
- data/test/db_test.rb +0 -14
- metadata +118 -148
@@ -0,0 +1,190 @@
|
|
1
|
+
# encoding: binary
|
2
|
+
require './test/test_helper'
|
3
|
+
|
4
|
+
class ByteBufferTest < Test::Unit::TestCase
|
5
|
+
include BSON
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@buf = ByteBuffer.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initial_state
|
12
|
+
assert_equal 0, @buf.position
|
13
|
+
assert_equal [], @buf.to_a
|
14
|
+
assert_equal "", @buf.to_s
|
15
|
+
assert_equal 0, @buf.length
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_nil_get_returns_one_byte
|
19
|
+
@buf.put_array([1, 2, 3, 4])
|
20
|
+
@buf.rewind
|
21
|
+
assert_equal 1, @buf.get
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_one_get_returns_array_length_one
|
25
|
+
@buf.put_array([1, 2, 3, 4])
|
26
|
+
@buf.rewind
|
27
|
+
assert_equal [1], @buf.get(1)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_zero_get_returns_empty_array
|
31
|
+
@buf.put_array([1, 2, 3, 4])
|
32
|
+
@buf.rewind
|
33
|
+
assert_equal [], @buf.get(0)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_length
|
37
|
+
@buf.put_int 3
|
38
|
+
assert_equal 4, @buf.length
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_default_order
|
42
|
+
assert_equal :little_endian, @buf.order
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_long_length
|
46
|
+
@buf.put_long 1027
|
47
|
+
assert_equal 8, @buf.length
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_get_long
|
51
|
+
@buf.put_long 1027
|
52
|
+
@buf.rewind
|
53
|
+
assert_equal 1027, @buf.get_long
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_get_double
|
57
|
+
@buf.put_double 41.2
|
58
|
+
@buf.rewind
|
59
|
+
assert_equal 41.2, @buf.get_double
|
60
|
+
end
|
61
|
+
|
62
|
+
if defined?(Encoding)
|
63
|
+
def test_serialize_cstr_converts_encoding_to_utf8
|
64
|
+
theta = "hello \xC8".force_encoding("ISO-8859-7")
|
65
|
+
ByteBuffer.serialize_cstr(@buf, theta)
|
66
|
+
assert_equal "hello \xCE\x98\0", @buf.to_s
|
67
|
+
assert_equal Encoding.find('binary'), @buf.to_s.encoding
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_serialize_cstr_validates_data_as_utf8
|
71
|
+
assert_raises(Encoding::UndefinedConversionError) do
|
72
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
else
|
76
|
+
def test_serialize_cstr_forces_encoding_to_utf8
|
77
|
+
# Unicode snowman (\u2603)
|
78
|
+
ByteBuffer.serialize_cstr(@buf, "hello \342\230\203")
|
79
|
+
assert_equal "hello \342\230\203\0", @buf.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_serialize_cstr_validates_data_as_utf8
|
83
|
+
assert_raises(BSON::InvalidStringEncoding) do
|
84
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_put_negative_byte
|
90
|
+
@buf.put(-1)
|
91
|
+
@buf.rewind
|
92
|
+
assert_equal 255, @buf.get
|
93
|
+
assert_equal "\xFF", @buf.to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_put_with_offset
|
97
|
+
@buf.put(1)
|
98
|
+
@buf.put(2, 0)
|
99
|
+
@buf.put(3, 3)
|
100
|
+
assert_equal "\x02\x00\x00\x03", @buf.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_put_array_with_offset
|
104
|
+
@buf.put(1)
|
105
|
+
@buf.put_array([2, 3], 0)
|
106
|
+
@buf.put_array([4, 5], 4)
|
107
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_put_int_with_offset
|
111
|
+
@buf.put(1)
|
112
|
+
@buf.put_int(2, 0)
|
113
|
+
@buf.put_int(3, 5)
|
114
|
+
assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_put_long_with_offset
|
118
|
+
@buf.put(1)
|
119
|
+
@buf.put_long(2, 0)
|
120
|
+
@buf.put_long(3, 9)
|
121
|
+
assert_equal(
|
122
|
+
"\x02\x00\x00\x00\x00\x00\x00\x00" +
|
123
|
+
"\x00" +
|
124
|
+
"\x03\x00\x00\x00\x00\x00\x00\x00",
|
125
|
+
@buf.to_s)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_put_binary
|
129
|
+
@buf.put(1)
|
130
|
+
@buf.put_binary("\x02\x03", 0)
|
131
|
+
@buf.put_binary("\x04\x05", 4)
|
132
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_rewrite
|
136
|
+
@buf.put_int(0)
|
137
|
+
@buf.rewind
|
138
|
+
@buf.put_int(1027)
|
139
|
+
assert_equal 4, @buf.length
|
140
|
+
@buf.rewind
|
141
|
+
assert_equal 1027, @buf.get_int
|
142
|
+
assert_equal 4, @buf.position
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_prepend_byte_buffer
|
146
|
+
@buf.put_int(4)
|
147
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
148
|
+
@buf.prepend!(new_buf)
|
149
|
+
assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_append_byte_buffer
|
153
|
+
@buf.put_int(4)
|
154
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
155
|
+
@buf.append!(new_buf)
|
156
|
+
assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_array_as_initial_input
|
160
|
+
@buf = ByteBuffer.new([5, 0, 0, 0])
|
161
|
+
assert_equal 4, @buf.size
|
162
|
+
assert_equal "\x05\x00\x00\x00", @buf.to_s
|
163
|
+
assert_equal [5, 0, 0, 0], @buf.to_a
|
164
|
+
@buf.put_int(32)
|
165
|
+
@buf.rewind
|
166
|
+
assert_equal 5, @buf.get_int
|
167
|
+
assert_equal 32, @buf.get_int
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_binary_string_as_initial_input
|
171
|
+
str = "abcd"
|
172
|
+
str.force_encoding('binary') if str.respond_to?(:force_encoding)
|
173
|
+
@buf = ByteBuffer.new(str)
|
174
|
+
assert_equal "abcd", @buf.to_s
|
175
|
+
assert_equal [97, 98, 99, 100], @buf.to_a
|
176
|
+
@buf.put_int(0)
|
177
|
+
assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_more
|
181
|
+
assert !@buf.more?
|
182
|
+
@buf.put_int(5)
|
183
|
+
assert !@buf.more?
|
184
|
+
@buf.rewind
|
185
|
+
assert @buf.more?
|
186
|
+
@buf.get_int
|
187
|
+
assert !@buf.more?
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
require 'test/test_helper'
|
3
|
+
require 'complex'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'rational'
|
6
|
+
require 'benchmark'
|
7
|
+
|
8
|
+
MEDIUM = {
|
9
|
+
'integer' => 5,
|
10
|
+
'number' => 5.05,
|
11
|
+
'boolean' => false,
|
12
|
+
'array' => ['test', 'benchmark']
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
LARGE = {
|
17
|
+
'base_url' => 'http://www.example.com/test-me',
|
18
|
+
'total_word_count' => 6743,
|
19
|
+
'access_time' => 123, #Time.now,
|
20
|
+
'meta_tags' => {
|
21
|
+
'description' => 'i am a long description string',
|
22
|
+
'author' => 'Holly Man',
|
23
|
+
'dynamically_created_meta_tag' => 'who know\n what'
|
24
|
+
},
|
25
|
+
'page_structure' => {
|
26
|
+
'counted_tags' => 3450,
|
27
|
+
'no_of_js_attached' => 10,
|
28
|
+
'no_of_images' => 6
|
29
|
+
},
|
30
|
+
'harvested_words' => ['10gen','web','open','source','application','paas',
|
31
|
+
'platform-as-a-service','technology','helps',
|
32
|
+
'developers','focus','building','mongodb','mongo'] * 20
|
33
|
+
}
|
34
|
+
|
35
|
+
class CBSONTest < Test::Unit::TestCase
|
36
|
+
include BSON
|
37
|
+
|
38
|
+
def setup
|
39
|
+
@encoder = BSON::BSON_CODER
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_nested_string
|
43
|
+
t0 = Time.now
|
44
|
+
50000.times do
|
45
|
+
@encoder.serialize({'doc' => MEDIUM})
|
46
|
+
end
|
47
|
+
t1 = Time.now
|
48
|
+
puts t1 - t0
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
require './test/test_helper'
|
3
|
+
require 'complex'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'rational'
|
6
|
+
require 'benchmark'
|
7
|
+
|
8
|
+
MEDIUM = {
|
9
|
+
'integer' => 5,
|
10
|
+
'number' => 5.05,
|
11
|
+
'boolean' => false,
|
12
|
+
'array' => ['test', 'benchmark']
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
LARGE = {
|
17
|
+
'base_url' => 'http://www.example.com/test-me',
|
18
|
+
'total_word_count' => 6743,
|
19
|
+
'access_time' => 1,# Time.now,
|
20
|
+
'meta_tags' => {
|
21
|
+
'description' => 'i am a long description string',
|
22
|
+
'author' => 'Holly Man',
|
23
|
+
'dynamically_created_meta_tag' => 'who know\n what'
|
24
|
+
},
|
25
|
+
'page_structure' => {
|
26
|
+
'counted_tags' => 3450,
|
27
|
+
'no_of_js_attached' => 10,
|
28
|
+
'no_of_images' => 6
|
29
|
+
},
|
30
|
+
'harvested_words' => ['10gen','web','open','source','application','paas',
|
31
|
+
'platform-as-a-service','technology','helps',
|
32
|
+
'developers','focus','building','mongodb','mongo'] * 20
|
33
|
+
}
|
34
|
+
|
35
|
+
# Tests and benchmarks specific to the JRuby BSON serializer
|
36
|
+
if RUBY_PLATFORM =~ /java/
|
37
|
+
class BSONTest < Test::Unit::TestCase
|
38
|
+
include BSON
|
39
|
+
|
40
|
+
def setup
|
41
|
+
@encoder = BSON::BSON_RUBY
|
42
|
+
@decoder = BSON::BSON_RUBY
|
43
|
+
@con = Mongo::Connection.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def assert_doc_pass(doc, options={})
|
47
|
+
bson = @encoder.serialize(doc)
|
48
|
+
if options[:debug]
|
49
|
+
puts "DEBUGGIN DOC:"
|
50
|
+
p bson.to_a
|
51
|
+
puts "DESERIALIZES TO:"
|
52
|
+
p @decoder.deserialize(bson)
|
53
|
+
end
|
54
|
+
assert_equal @decoder.serialize(doc).to_a, bson.to_a
|
55
|
+
assert_equal doc, @decoder.deserialize(bson)
|
56
|
+
end
|
57
|
+
|
58
|
+
# def test_bench_big_string
|
59
|
+
# t0 = Time.now
|
60
|
+
# @con['foo']['bar'].remove
|
61
|
+
# doc = {'doc' => 'f' * 2_000_000}
|
62
|
+
# 10.times do
|
63
|
+
# @con['foo']['bar'].save({'d' => doc})
|
64
|
+
# @con['foo']['bar'].find.to_a
|
65
|
+
# end
|
66
|
+
# puts "Big String"
|
67
|
+
# puts Time.now - t0
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# def test_big_array
|
71
|
+
# t0 = Time.now
|
72
|
+
# @con['foo']['bar'].remove
|
73
|
+
# doc = {'doc' => 'f' * 2_000_000}
|
74
|
+
# 10.times do
|
75
|
+
# @con['foo']['bar'].save({'d' => doc})
|
76
|
+
# @con['foo']['bar'].find.to_a
|
77
|
+
# end
|
78
|
+
# puts "Big String"
|
79
|
+
# puts Time.now - t0
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# def test_string
|
83
|
+
# doc = {'doc' => "Hello world!", 'awesome' => true, 'a' => 1, 'b' => 4_333_433_232, 'c' => 2.33, 'd' => nil,
|
84
|
+
# 'f' => BSON::Code.new("function"), 'g' => BSON::ObjectId.new, 'h' => [1, 2, 3]}
|
85
|
+
# bson = @encoder.serialize(doc)
|
86
|
+
# d = @encoder.deserialize(bson)
|
87
|
+
# puts "Array"
|
88
|
+
# puts d
|
89
|
+
# puts d['h']
|
90
|
+
# puts "End Array"
|
91
|
+
# puts d['h'][0]
|
92
|
+
# puts d['h'][1]
|
93
|
+
# puts (d['h'][2] + 100).class
|
94
|
+
# puts "ObjecId Info"
|
95
|
+
# bson2 = @encoder.serialize(d)
|
96
|
+
# doc2 = @encoder.deserialize(bson2)
|
97
|
+
# assert_equal doc2, @encoder.deserialize(bson)
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
|
101
|
+
def test_eval
|
102
|
+
code = BSON::Code.new('f')
|
103
|
+
oh = BSON::OrderedHash.new
|
104
|
+
oh[:$eval] = code
|
105
|
+
oh[:args] = [1]
|
106
|
+
|
107
|
+
assert_equal BSON::BSON_RUBY.serialize(oh).to_a, BSON::BSON_JAVA.serialize(oh).to_a
|
108
|
+
end
|
109
|
+
|
110
|
+
# def test_oid
|
111
|
+
# b = Java::OrgBsonTypes::ObjectId.new.toByteArray
|
112
|
+
# o = ObjectId.new(b)
|
113
|
+
# p o
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# def test_speed
|
117
|
+
# @con['foo']['bar'].remove
|
118
|
+
#
|
119
|
+
# puts "Test OID"
|
120
|
+
# t0 = Time.now
|
121
|
+
# 5000.times do
|
122
|
+
# BSON::ObjectId.new
|
123
|
+
# end
|
124
|
+
# puts Time.now - t0
|
125
|
+
#
|
126
|
+
# puts "Test insert"
|
127
|
+
# t0 = Time.now
|
128
|
+
# 1000.times do |n|
|
129
|
+
# if n % 1000 == 0
|
130
|
+
# puts Time.now - t0
|
131
|
+
# t0 = Time.now
|
132
|
+
# end
|
133
|
+
# @con['foo']['bar'].insert({'doc' => MEDIUM})
|
134
|
+
# end
|
135
|
+
# puts Time.now - t0
|
136
|
+
#
|
137
|
+
# puts "Test query / deserialize"
|
138
|
+
# t0 = Time.now
|
139
|
+
# @con['foo']['bar'].find.to_a
|
140
|
+
# t1 = Time.now
|
141
|
+
# puts t1 - t0
|
142
|
+
# end
|
143
|
+
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
require './test/test_helper'
|
3
|
+
|
4
|
+
# Special tests for the JRuby encoder only
|
5
|
+
if RUBY_PLATFORM =~ /java/
|
6
|
+
|
7
|
+
class JRubyBSONTest < Test::Unit::TestCase
|
8
|
+
include BSON
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@encoder = BSON::BSON_CODER
|
12
|
+
@decoder = BSON::BSON_RUBY
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_object_id
|
16
|
+
oid = {'doc' => BSON::ObjectId.new}
|
17
|
+
p oid['doc'].data
|
18
|
+
bson = @encoder.serialize(oid)
|
19
|
+
assert_equal oid, @encoder.deserialize(bson)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,438 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
require 'test/test_helper'
|
3
|
+
require 'complex'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'rational'
|
6
|
+
require 'benchmark'
|
7
|
+
|
8
|
+
MEDIUM = {
|
9
|
+
'integer' => 5,
|
10
|
+
'number' => 5.05,
|
11
|
+
'boolean' => false,
|
12
|
+
'array' => ['test', 'benchmark']
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
LARGE = {
|
17
|
+
'base_url' => 'http://www.example.com/test-me',
|
18
|
+
'total_word_count' => 6743,
|
19
|
+
'access_time' => 123, #Time.now,
|
20
|
+
'meta_tags' => {
|
21
|
+
'description' => 'i am a long description string',
|
22
|
+
'author' => 'Holly Man',
|
23
|
+
'dynamically_created_meta_tag' => 'who know\n what'
|
24
|
+
},
|
25
|
+
'page_structure' => {
|
26
|
+
'counted_tags' => 3450,
|
27
|
+
'no_of_js_attached' => 10,
|
28
|
+
'no_of_images' => 6
|
29
|
+
},
|
30
|
+
'harvested_words' => ['10gen','web','open','source','application','paas',
|
31
|
+
'platform-as-a-service','technology','helps',
|
32
|
+
'developers','focus','building','mongodb','mongo'] * 20
|
33
|
+
}
|
34
|
+
|
35
|
+
class BSONTest < Test::Unit::TestCase
|
36
|
+
include BSON
|
37
|
+
|
38
|
+
def setup
|
39
|
+
@encoder = BSON::BSON_CODER
|
40
|
+
end
|
41
|
+
|
42
|
+
def assert_doc_pass(doc)
|
43
|
+
bson = @encoder.serialize(doc)
|
44
|
+
p bson.to_a
|
45
|
+
puts "#{doc.inspect} serializes to #{bson.inspect}"
|
46
|
+
|
47
|
+
assert_equal doc, BSON::BSON_RUBY.deserialize(bson)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_string
|
51
|
+
doc = {'doc' => 'the well-tempered clavier'}
|
52
|
+
BSON::BSON_RUBY.serialize(doc)
|
53
|
+
bson = @encoder.serialize(doc)
|
54
|
+
assert_equal doc, BSON::BSON_RUBY.deserialize(bson.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_nested_string
|
58
|
+
t0 = Time.now
|
59
|
+
500.times do
|
60
|
+
@encoder.serialize({'doc' => MEDIUM})
|
61
|
+
end
|
62
|
+
t1 = Time.now
|
63
|
+
puts t1 - t0
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_array
|
67
|
+
doc = {'doc' => ['1', '2', '3']}
|
68
|
+
assert_doc_pass(doc)
|
69
|
+
puts "Here's the doc"
|
70
|
+
p doc.class
|
71
|
+
bson = @encoder.serialize(doc)
|
72
|
+
assert_equal bson.to_s, BSON::BSON_RUBY.serialize(doc).to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_number
|
76
|
+
assert_doc_pass({'doc' => 1})
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_object
|
80
|
+
assert_doc_pass({'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}})
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_code
|
84
|
+
@reduce_function = "function (obj, prev) { prev.count += inc_value; }"
|
85
|
+
doc = {"a" => Code.new(@reduce_function, {"a" => 1})}
|
86
|
+
assert_doc_pass(doc)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_date
|
90
|
+
doc = {'date' => Time.now}
|
91
|
+
d = BSON::BSON_RUBY.serialize(doc)
|
92
|
+
p d.to_s
|
93
|
+
e = BSON::BSON_JAVA.serialize(doc)
|
94
|
+
p e.to_s
|
95
|
+
assert_doc_pass(doc)
|
96
|
+
#bson = @e.serialize(doc)
|
97
|
+
#doc2 = BSON::BSON_CODER.deserialize(bson)
|
98
|
+
# Mongo only stores up to the millisecond
|
99
|
+
#assert_in_delta doc['date'], doc2['date'], 0.001
|
100
|
+
end
|
101
|
+
|
102
|
+
# def test_float
|
103
|
+
# doc = {'a' => 1}
|
104
|
+
# assert_doc_pass(doc)
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# def test_group_cmd
|
108
|
+
# initial = {"c" => 0}
|
109
|
+
# reduce = "function (obj, prev) { prev.c += inc_value; }"
|
110
|
+
# group_command = {
|
111
|
+
# "group" => {
|
112
|
+
# "ns" => 'col',
|
113
|
+
# "$reduce" => reduce,
|
114
|
+
# "cond" => {},
|
115
|
+
# "initial" => initial
|
116
|
+
# }
|
117
|
+
# }
|
118
|
+
#
|
119
|
+
# assert_doc_pass(group_command)
|
120
|
+
# end
|
121
|
+
#
|
122
|
+
# context "Grouping" do
|
123
|
+
# setup do
|
124
|
+
# @@test.remove
|
125
|
+
# @@test.save("a" => 1)
|
126
|
+
# @@test.save("b" => 1)
|
127
|
+
# @initial = {"count" => 0}
|
128
|
+
# @reduce_function = "function (obj, prev) { prev.count += inc_value; }"
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# should "group results using eval form" do
|
132
|
+
# p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))
|
133
|
+
# p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))
|
134
|
+
# assert_equal 2, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))[0]["count"]
|
135
|
+
# assert_equal 4, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))[0]["count"]
|
136
|
+
# end
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# def test_find_and_modify
|
140
|
+
# @@test << { :a => 1, :processed => false }
|
141
|
+
# @@test << { :a => 2, :processed => false }
|
142
|
+
# @@test << { :a => 3, :processed => false }
|
143
|
+
# p @@test.find.to_a
|
144
|
+
#
|
145
|
+
# @@test.find_and_modify(:query => {}, :sort => [['a', -1]], :update => {"$set" => {"processed" => true}})
|
146
|
+
#
|
147
|
+
# p @@test.find.to_a
|
148
|
+
# assert @@test.find_one({:a => 3})['processed']
|
149
|
+
# end
|
150
|
+
#
|
151
|
+
# def test_invalid_string
|
152
|
+
# require 'iconv'
|
153
|
+
# string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
154
|
+
# doc = {'doc' => string}
|
155
|
+
# bson = @encoder.serialize(doc)
|
156
|
+
# assert_equal doc, @encoder.deserialize(bson)
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# def test_null
|
160
|
+
# #doc = {"\x00" => "foo"}
|
161
|
+
# #@encoder.serialize(doc)
|
162
|
+
# @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
|
163
|
+
# end
|
164
|
+
|
165
|
+
# def test_dbref
|
166
|
+
# oid = ObjectID.new
|
167
|
+
# doc = {}
|
168
|
+
# doc['dbref'] = DBRef.new('namespace', oid)
|
169
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
170
|
+
# doc2 = BSON::BSON_CODER.deserialize(bson)
|
171
|
+
# puts doc2.class
|
172
|
+
# puts doc2
|
173
|
+
# assert_equal 'namespace', doc2['dbref'].namespace
|
174
|
+
# assert_equal oid, doc2['dbref'].object_id
|
175
|
+
# end
|
176
|
+
|
177
|
+
# def test_null_character
|
178
|
+
# #assert_raise InvalidDocument do
|
179
|
+
# bson = @encoder.serialize({"\x00" => "a"})
|
180
|
+
# puts bson
|
181
|
+
# #end
|
182
|
+
# puts BSON_RUBY.deserialize(bson)
|
183
|
+
#
|
184
|
+
# #assert_raise InvalidDocument do
|
185
|
+
# # @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
|
186
|
+
# #end
|
187
|
+
# end
|
188
|
+
#
|
189
|
+
## def test_symbol_key
|
190
|
+
# doc = {:foo => "bar"}
|
191
|
+
# p doc.keys
|
192
|
+
# bson = @coder.serialize(doc)
|
193
|
+
# new_doc = {"foo" => "bar"}
|
194
|
+
# assert_equal new_doc, @coder.deserialize(bson)
|
195
|
+
# end
|
196
|
+
#
|
197
|
+
#
|
198
|
+
# def test_string
|
199
|
+
# assert_doc_pass({'doc' => 'hello, world'})
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
##
|
203
|
+
# require 'iconv'
|
204
|
+
# def test_invalid_string
|
205
|
+
# require 'iconv'
|
206
|
+
# string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
207
|
+
# doc = {'doc' => string}
|
208
|
+
# bson = @coder.serialize(doc)
|
209
|
+
# assert_equal doc, @coder.deserialize(bson)
|
210
|
+
# end
|
211
|
+
#
|
212
|
+
#
|
213
|
+
# def test_nested_string
|
214
|
+
# assert_doc_pass({'doc' => {'text' => 'hello, world'}})
|
215
|
+
# end
|
216
|
+
#
|
217
|
+
# def test_array
|
218
|
+
# assert_doc_pass({'doc' => ['1', '2', '3']})
|
219
|
+
# end
|
220
|
+
#
|
221
|
+
# def test_number
|
222
|
+
# assert_doc_pass({'doc' => 1})
|
223
|
+
# end
|
224
|
+
#
|
225
|
+
# def test_object
|
226
|
+
# assert_doc_pass({'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}})
|
227
|
+
# end
|
228
|
+
#
|
229
|
+
# def test_boolean
|
230
|
+
# assert_doc_pass({'foo' => true})
|
231
|
+
# assert_doc_pass({'foo' => false})
|
232
|
+
# end
|
233
|
+
#
|
234
|
+
# def test_nil
|
235
|
+
# assert_doc_pass({'foo' => nil})
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# def test_time
|
239
|
+
# assert_doc_pass({'foo' => Time.now})
|
240
|
+
# end
|
241
|
+
#
|
242
|
+
# def test_simple_regex
|
243
|
+
# assert_doc_pass({'foo' => /^a/})
|
244
|
+
# end
|
245
|
+
#
|
246
|
+
# def test_regex_with_options
|
247
|
+
# assert_doc_pass({'foo' => /^a/imx})
|
248
|
+
# end
|
249
|
+
#
|
250
|
+
# def test_symbol
|
251
|
+
# assert_doc_pass({'foo' => :bar})
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
# def test_binary
|
255
|
+
# doc = {'foo' => BSON::Binary.new("ABCDE".unpack("C*"))}
|
256
|
+
# bson = @coder.serialize(doc)
|
257
|
+
# de = @coder.deserialize(bson)
|
258
|
+
# assert_equal doc, de
|
259
|
+
# end
|
260
|
+
#
|
261
|
+
# def test_valid_utf8_string
|
262
|
+
# doc = {'doc' => 'aé'}
|
263
|
+
# bson = bson = BSON::BSON_CODER.serialize(doc)
|
264
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
265
|
+
# end
|
266
|
+
#
|
267
|
+
# def test_valid_utf8_key
|
268
|
+
# doc = {'aé' => 'hello'}
|
269
|
+
# bson = bson = BSON::BSON_CODER.serialize(doc)
|
270
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
271
|
+
# end
|
272
|
+
#
|
273
|
+
# def test_number
|
274
|
+
# doc = {'doc' => 41.99}
|
275
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
276
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
277
|
+
# end
|
278
|
+
#
|
279
|
+
# def test_int
|
280
|
+
# doc = {'doc' => 42}
|
281
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
282
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
283
|
+
#
|
284
|
+
# doc = {"doc" => -5600}
|
285
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
286
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
287
|
+
#
|
288
|
+
# doc = {"doc" => 2147483647}
|
289
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
290
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
291
|
+
#
|
292
|
+
# doc = {"doc" => -2147483648}
|
293
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
294
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
295
|
+
# end
|
296
|
+
#
|
297
|
+
# def test_obj
|
298
|
+
# doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
299
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
300
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
301
|
+
#
|
302
|
+
# doc = {"a" => 10, "b" => 20}# {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
303
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
304
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
305
|
+
# end
|
306
|
+
#
|
307
|
+
# def test_oh
|
308
|
+
# oh = BSON::OrderedHash.new
|
309
|
+
# oh["z"] = 10
|
310
|
+
# oh["a"] = 1
|
311
|
+
# bson = BSON::BSON_CODER.serialize(oh)
|
312
|
+
# p bson
|
313
|
+
# assert_equal oh, BSON::BSON_CODER.deserialize(bson)
|
314
|
+
# end
|
315
|
+
#
|
316
|
+
def test_ordered_hash
|
317
|
+
# doc = BSON::OrderedHash.new
|
318
|
+
# doc["b"] = 1
|
319
|
+
# doc["a"] = 2
|
320
|
+
# doc["c"] = 3
|
321
|
+
# doc["d"] = 4
|
322
|
+
# bson1 = BSON::BSON_CODER.serialize(doc)
|
323
|
+
# bson2 = BSON::BSON_RUBY.serialize({"b" => 1, "a" => 2, "c" => 3, "d" => 4})
|
324
|
+
# bson3 = BSON::BSON_RUBY.serialize({"b" => '1', "a" => '2', "c" => '3', "d" => '4'})
|
325
|
+
# p bson1
|
326
|
+
# p bson2
|
327
|
+
# p bson3
|
328
|
+
# p BSON::BSON_CODER.deserialize(bson3)
|
329
|
+
# assert_equal doc, BSON::BSON_RUBY.deserialize(bson3)
|
330
|
+
end
|
331
|
+
#
|
332
|
+
# def test_object
|
333
|
+
# doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
334
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
335
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
336
|
+
# end
|
337
|
+
#
|
338
|
+
## def test_oid
|
339
|
+
## doc = {'doc' => ObjectID.new}
|
340
|
+
## bson = BSON::BSON_CODER.serialize(doc)
|
341
|
+
## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
342
|
+
## end
|
343
|
+
##
|
344
|
+
## def test_array
|
345
|
+
## doc = {'doc' => [1, 2, 'a', 'b']}
|
346
|
+
## bson = BSON::BSON_CODER.serialize(doc)
|
347
|
+
## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
348
|
+
## end
|
349
|
+
##
|
350
|
+
## def test_invalid_object
|
351
|
+
## doc = {'foo' => Object.new}
|
352
|
+
## assert_raise InvalidDocument do
|
353
|
+
## @coder.serialize(doc)
|
354
|
+
## end
|
355
|
+
##
|
356
|
+
## assert_raise InvalidDocument do
|
357
|
+
## @coder.serialize({'date' => Date.today})
|
358
|
+
## end
|
359
|
+
## end
|
360
|
+
##
|
361
|
+
##
|
362
|
+
## def test_regex
|
363
|
+
## doc = {'doc' => /foobar/i}
|
364
|
+
## bson = BSON::BSON_CODER.serialize(doc)
|
365
|
+
## doc2 = BSON::BSON_CODER.deserialize(bson)
|
366
|
+
## assert_equal doc, doc2
|
367
|
+
##
|
368
|
+
## r = doc2['doc']
|
369
|
+
## assert_kind_of Regexp, r
|
370
|
+
##
|
371
|
+
## doc = {'doc' => r}
|
372
|
+
## bson_doc = BSON::BSON_CODER.serialize(doc)
|
373
|
+
## doc2 = nil
|
374
|
+
## doc2 = BSON::BSON_CODER.deserialize(bson_doc)
|
375
|
+
## assert_equal doc, doc2
|
376
|
+
## end
|
377
|
+
##
|
378
|
+
# def test_boolean
|
379
|
+
# doc = {'doc' => true}
|
380
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
381
|
+
# assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
382
|
+
# end
|
383
|
+
#
|
384
|
+
# def test_date
|
385
|
+
# doc = {'date' => Time.now}
|
386
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
387
|
+
# doc2 = BSON::BSON_CODER.deserialize(bson)
|
388
|
+
# # Mongo only stores up to the millisecond
|
389
|
+
# assert_in_delta doc['date'], doc2['date'], 0.001
|
390
|
+
# end
|
391
|
+
#
|
392
|
+
# def test_date_returns_as_utc
|
393
|
+
# doc = {'date' => Time.now}
|
394
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
395
|
+
# doc2 = BSON::BSON_CODER.deserialize(bson)
|
396
|
+
# assert doc2['date'].utc?
|
397
|
+
# end
|
398
|
+
#
|
399
|
+
## def test_dbref
|
400
|
+
## oid = ObjectID.new
|
401
|
+
## doc = {}
|
402
|
+
## doc['dbref'] = DBRef.new('namespace', oid)
|
403
|
+
## bson = BSON::BSON_CODER.serialize(doc)
|
404
|
+
## doc2 = BSON::BSON_CODER.deserialize(bson)
|
405
|
+
## assert_equal 'namespace', doc2['dbref'].namespace
|
406
|
+
## assert_equal oid, doc2['dbref'].object_id
|
407
|
+
## end
|
408
|
+
##
|
409
|
+
# def test_symbol
|
410
|
+
# doc = {'sym' => :foo}
|
411
|
+
# bson = BSON::BSON_CODER.serialize(doc)
|
412
|
+
# doc2 = BSON::BSON_CODER.deserialize(bson)
|
413
|
+
# assert_equal :foo, doc2['sym']
|
414
|
+
# end
|
415
|
+
##
|
416
|
+
### def test_regex_extended
|
417
|
+
### assert_doc_pass({'foo' => /^a/x})
|
418
|
+
### end
|
419
|
+
###
|
420
|
+
# def test_object_id
|
421
|
+
# assert_doc_pass({'_id' => BSON::ObjectID.new})
|
422
|
+
# end
|
423
|
+
#
|
424
|
+
# def test_where
|
425
|
+
# assert_doc_pass({'$where' => "function() { print('hello'); }"})
|
426
|
+
# end
|
427
|
+
#
|
428
|
+
# def test_min_max_keys
|
429
|
+
# assert_doc_pass({'doc' => BSON::MaxKey.new})
|
430
|
+
# assert_doc_pass({'doc' => BSON::MinKey.new})
|
431
|
+
# end
|
432
|
+
#
|
433
|
+
# def test_code
|
434
|
+
# code = BSON::Code.new("print('hello')")
|
435
|
+
# code.scope = {:foo => 2}
|
436
|
+
# assert_doc_pass({'doc' => code})
|
437
|
+
# end
|
438
|
+
end
|