bson 1.0.9 → 1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

@@ -1,190 +0,0 @@
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
@@ -1,227 +0,0 @@
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' => 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
-
36
-
37
- begin
38
- require 'active_support/core_ext'
39
- require 'active_support/hash_with_indifferent_access'
40
- Time.zone = "Pacific Time (US & Canada)"
41
- Zone = Time.zone.now
42
- rescue LoadError
43
- warn 'Could not test BSON with HashWithIndifferentAccess.'
44
- module ActiveSupport
45
- class TimeWithZone
46
- end
47
- end
48
- Zone = ActiveSupport::TimeWithZone.new
49
- end
50
-
51
- class BSONTest < Test::Unit::TestCase
52
- include BSON
53
-
54
- def setup
55
- @coder = BSON::BSON_CODER
56
- end
57
-
58
- def assert_doc_pass(doc)
59
- bson = @coder.serialize(doc)
60
- assert_equal doc, @coder.deserialize(bson)
61
- end
62
-
63
- def test_string
64
- assert_doc_pass({'doc' => 'hello, world'})
65
- end
66
-
67
- def test_nested_string
68
- assert_doc_pass({'doc' => {'text' => 'hello, world'}})
69
- end
70
-
71
- def test_array
72
- assert_doc_pass({'doc' => ['1', 2, 3]})
73
- end
74
-
75
- def test_object
76
- assert_doc_pass({'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}})
77
- end
78
-
79
- def test_boolean
80
- doc = {'foo' => true}
81
- bson = @coder.serialize(doc)
82
- assert_equal doc, @coder.deserialize(bson)
83
- end
84
-
85
- def test_nil
86
- assert_doc_pass({'foo' => nil})
87
- end
88
-
89
- def test_time
90
- assert_doc_pass({'foo' => Time.now})
91
- end
92
-
93
- def test_regex
94
- assert_doc_pass({'foo' => /^a/im})
95
- end
96
-
97
- def test_symbol
98
- assert_doc_pass({'foo' => :bar})
99
- end
100
-
101
- def test_valid_utf8_string
102
- doc = {'doc' => 'aé'}
103
- bson = bson = BSON::BSON_CODER.serialize(doc)
104
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
105
- end
106
-
107
- def test_valid_utf8_key
108
- doc = {'aé' => 'hello'}
109
- bson = bson = BSON::BSON_CODER.serialize(doc)
110
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
111
- end
112
-
113
- def test_number
114
- doc = {'doc' => 41.99}
115
- bson = BSON::BSON_CODER.serialize(doc)
116
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
117
- end
118
-
119
- def test_int
120
- doc = {'doc' => 42}
121
- bson = BSON::BSON_CODER.serialize(doc)
122
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
123
-
124
- doc = {"doc" => -5600}
125
- bson = BSON::BSON_CODER.serialize(doc)
126
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
127
-
128
- doc = {"doc" => 2147483647}
129
- bson = BSON::BSON_CODER.serialize(doc)
130
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
131
-
132
- doc = {"doc" => -2147483648}
133
- bson = BSON::BSON_CODER.serialize(doc)
134
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
135
- end
136
-
137
- def test_ordered_hash
138
- doc = BSON::OrderedHash.new
139
- doc["b"] = 1
140
- doc["a"] = 2
141
- doc["c"] = 3
142
- doc["d"] = 4
143
- bson = BSON::BSON_CODER.serialize(doc)
144
- p bson
145
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
146
- end
147
-
148
- def test_object
149
- doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
150
- bson = BSON::BSON_CODER.serialize(doc)
151
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
152
- end
153
-
154
- def test_oid
155
- doc = {'doc' => ObjectID.new}
156
- bson = BSON::BSON_CODER.serialize(doc)
157
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
158
- end
159
-
160
- def test_array
161
- doc = {'doc' => [1, 2, 'a', 'b']}
162
- bson = BSON::BSON_CODER.serialize(doc)
163
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
164
- end
165
-
166
- def test_regex
167
- doc = {'doc' => /foobar/i}
168
- bson = BSON::BSON_CODER.serialize(doc)
169
- doc2 = BSON::BSON_CODER.deserialize(bson)
170
- assert_equal doc, doc2
171
-
172
- r = doc2['doc']
173
- assert_kind_of Regexp, r
174
-
175
- doc = {'doc' => r}
176
- bson_doc = BSON::BSON_CODER.serialize(doc)
177
- doc2 = nil
178
- doc2 = BSON::BSON_CODER.deserialize(bson_doc)
179
- assert_equal doc, doc2
180
- end
181
-
182
- def test_boolean
183
- doc = {'doc' => true}
184
- bson = BSON::BSON_CODER.serialize(doc)
185
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
186
- end
187
-
188
- def test_date
189
- doc = {'date' => Time.now}
190
- bson = BSON::BSON_CODER.serialize(doc)
191
- doc2 = BSON::BSON_CODER.deserialize(bson)
192
- # Mongo only stores up to the millisecond
193
- assert_in_delta doc['date'], doc2['date'], 0.001
194
- end
195
-
196
- def test_date_returns_as_utc
197
- doc = {'date' => Time.now}
198
- bson = BSON::BSON_CODER.serialize(doc)
199
- doc2 = BSON::BSON_CODER.deserialize(bson)
200
- assert doc2['date'].utc?
201
- end
202
-
203
- def test_dbref
204
- oid = ObjectID.new
205
- doc = {}
206
- doc['dbref'] = DBRef.new('namespace', oid)
207
- bson = BSON::BSON_CODER.serialize(doc)
208
- doc2 = BSON::BSON_CODER.deserialize(bson)
209
- assert_equal 'namespace', doc2['dbref'].namespace
210
- assert_equal oid, doc2['dbref'].object_id
211
- end
212
-
213
- def test_symbol
214
- doc = {'sym' => :foo}
215
- bson = BSON::BSON_CODER.serialize(doc)
216
- doc2 = BSON::BSON_CODER.deserialize(bson)
217
- assert_equal :foo, doc2['sym']
218
- end
219
-
220
- # def test_regex_extended
221
- # assert_doc_pass({'foo' => /^a/x})
222
- # end
223
- #
224
- def test_object_id
225
- assert_doc_pass({'_id' => BSON::ObjectID.new})
226
- end
227
- end
@@ -1,396 +0,0 @@
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
- con = Mongo::Connection.new
41
- db = con['mongo-ruby-test']
42
- @@test = db['col']
43
- @@test.remove
44
- end
45
-
46
- def assert_doc_pass(doc)
47
- bson = @encoder.serialize(doc)
48
- puts "#{doc.inspect} serializes to #{bson.inspect}"
49
- p bson.to_a
50
-
51
- assert_equal doc, @encoder.deserialize(bson)
52
- end
53
-
54
- def test_code
55
- @reduce_function = "1"#function (obj, prev) { prev.count += inc_value; }"
56
- doc = {"a" => Code.new(@reduce_function, {"a" => 1})}
57
- assert_doc_pass(doc)
58
- end
59
-
60
- def test_float
61
- doc = {'a' => 1}
62
- assert_doc_pass(doc)
63
- end
64
-
65
- def test_group_cmd
66
- initial = {"c" => 0}
67
- reduce = "function (obj, prev) { prev.c += inc_value; }"
68
- group_command = {
69
- "group" => {
70
- "ns" => 'col',
71
- "$reduce" => reduce,
72
- "cond" => {},
73
- "initial" => initial
74
- }
75
- }
76
-
77
- assert_doc_pass(group_command)
78
- end
79
-
80
- context "Grouping" do
81
- setup do
82
- @@test.remove
83
- @@test.save("a" => 1)
84
- @@test.save("b" => 1)
85
- @initial = {"count" => 0}
86
- @reduce_function = "function (obj, prev) { prev.count += inc_value; }"
87
- end
88
-
89
- should "group results using eval form" do
90
- p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))
91
- p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))
92
- assert_equal 2, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))[0]["count"]
93
- assert_equal 4, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))[0]["count"]
94
- end
95
- end
96
-
97
- # def test_find_and_modify
98
- # @@test << { :a => 1, :processed => false }
99
- # @@test << { :a => 2, :processed => false }
100
- # @@test << { :a => 3, :processed => false }
101
- # p @@test.find.to_a
102
- #
103
- # @@test.find_and_modify(:query => {}, :sort => [['a', -1]], :update => {"$set" => {"processed" => true}})
104
- #
105
- # p @@test.find.to_a
106
- # assert @@test.find_one({:a => 3})['processed']
107
- # end
108
- #
109
- # def test_invalid_string
110
- # require 'iconv'
111
- # string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
112
- # doc = {'doc' => string}
113
- # bson = @encoder.serialize(doc)
114
- # assert_equal doc, @encoder.deserialize(bson)
115
- # end
116
- #
117
- # def test_null
118
- # #doc = {"\x00" => "foo"}
119
- # #@encoder.serialize(doc)
120
- # @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
121
- # end
122
-
123
- # def test_dbref
124
- # oid = ObjectID.new
125
- # doc = {}
126
- # doc['dbref'] = DBRef.new('namespace', oid)
127
- # bson = BSON::BSON_CODER.serialize(doc)
128
- # doc2 = BSON::BSON_CODER.deserialize(bson)
129
- # puts doc2.class
130
- # puts doc2
131
- # assert_equal 'namespace', doc2['dbref'].namespace
132
- # assert_equal oid, doc2['dbref'].object_id
133
- # end
134
-
135
- # def test_null_character
136
- # #assert_raise InvalidDocument do
137
- # bson = @encoder.serialize({"\x00" => "a"})
138
- # puts bson
139
- # #end
140
- # puts BSON_RUBY.deserialize(bson)
141
- #
142
- # #assert_raise InvalidDocument do
143
- # # @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
144
- # #end
145
- # end
146
- #
147
- ## def test_symbol_key
148
- # doc = {:foo => "bar"}
149
- # p doc.keys
150
- # bson = @coder.serialize(doc)
151
- # new_doc = {"foo" => "bar"}
152
- # assert_equal new_doc, @coder.deserialize(bson)
153
- # end
154
- #
155
-
156
- def test_string
157
- assert_doc_pass({'doc' => 'hello, world'})
158
- end
159
-
160
- ##
161
- # require 'iconv'
162
- # def test_invalid_string
163
- # require 'iconv'
164
- # string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
165
- # doc = {'doc' => string}
166
- # bson = @coder.serialize(doc)
167
- # assert_equal doc, @coder.deserialize(bson)
168
- # end
169
- #
170
- #
171
- # def test_nested_string
172
- # assert_doc_pass({'doc' => {'text' => 'hello, world'}})
173
- # end
174
- #
175
- # def test_array
176
- # assert_doc_pass({'doc' => ['1', '2', '3']})
177
- # end
178
- #
179
- # def test_number
180
- # assert_doc_pass({'doc' => 1})
181
- # end
182
- #
183
- # def test_object
184
- # assert_doc_pass({'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}})
185
- # end
186
- #
187
- # def test_boolean
188
- # assert_doc_pass({'foo' => true})
189
- # assert_doc_pass({'foo' => false})
190
- # end
191
- #
192
- # def test_nil
193
- # assert_doc_pass({'foo' => nil})
194
- # end
195
- #
196
- # def test_time
197
- # assert_doc_pass({'foo' => Time.now})
198
- # end
199
- #
200
- # def test_simple_regex
201
- # assert_doc_pass({'foo' => /^a/})
202
- # end
203
- #
204
- # def test_regex_with_options
205
- # assert_doc_pass({'foo' => /^a/imx})
206
- # end
207
- #
208
- # def test_symbol
209
- # assert_doc_pass({'foo' => :bar})
210
- # end
211
- #
212
- # def test_binary
213
- # doc = {'foo' => BSON::Binary.new("ABCDE".unpack("C*"))}
214
- # bson = @coder.serialize(doc)
215
- # de = @coder.deserialize(bson)
216
- # assert_equal doc, de
217
- # end
218
- #
219
- # def test_valid_utf8_string
220
- # doc = {'doc' => 'aé'}
221
- # bson = bson = BSON::BSON_CODER.serialize(doc)
222
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
223
- # end
224
- #
225
- # def test_valid_utf8_key
226
- # doc = {'aé' => 'hello'}
227
- # bson = bson = BSON::BSON_CODER.serialize(doc)
228
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
229
- # end
230
- #
231
- # def test_number
232
- # doc = {'doc' => 41.99}
233
- # bson = BSON::BSON_CODER.serialize(doc)
234
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
235
- # end
236
- #
237
- # def test_int
238
- # doc = {'doc' => 42}
239
- # bson = BSON::BSON_CODER.serialize(doc)
240
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
241
- #
242
- # doc = {"doc" => -5600}
243
- # bson = BSON::BSON_CODER.serialize(doc)
244
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
245
- #
246
- # doc = {"doc" => 2147483647}
247
- # bson = BSON::BSON_CODER.serialize(doc)
248
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
249
- #
250
- # doc = {"doc" => -2147483648}
251
- # bson = BSON::BSON_CODER.serialize(doc)
252
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
253
- # end
254
- #
255
- # def test_obj
256
- # doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
257
- # bson = BSON::BSON_CODER.serialize(doc)
258
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
259
- #
260
- # doc = {"a" => 10, "b" => 20}# {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
261
- # bson = BSON::BSON_CODER.serialize(doc)
262
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
263
- # end
264
- #
265
- # def test_oh
266
- # oh = BSON::OrderedHash.new
267
- # oh["z"] = 10
268
- # oh["a"] = 1
269
- # bson = BSON::BSON_CODER.serialize(oh)
270
- # p bson
271
- # assert_equal oh, BSON::BSON_CODER.deserialize(bson)
272
- # end
273
- #
274
- def test_ordered_hash
275
- # doc = BSON::OrderedHash.new
276
- # doc["b"] = 1
277
- # doc["a"] = 2
278
- # doc["c"] = 3
279
- # doc["d"] = 4
280
- # bson1 = BSON::BSON_CODER.serialize(doc)
281
- # bson2 = BSON::BSON_RUBY.serialize({"b" => 1, "a" => 2, "c" => 3, "d" => 4})
282
- # bson3 = BSON::BSON_RUBY.serialize({"b" => '1', "a" => '2', "c" => '3', "d" => '4'})
283
- # p bson1
284
- # p bson2
285
- # p bson3
286
- # p BSON::BSON_CODER.deserialize(bson3)
287
- # assert_equal doc, BSON::BSON_RUBY.deserialize(bson3)
288
- end
289
- #
290
- # def test_object
291
- # doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
292
- # bson = BSON::BSON_CODER.serialize(doc)
293
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
294
- # end
295
- #
296
- ## def test_oid
297
- ## doc = {'doc' => ObjectID.new}
298
- ## bson = BSON::BSON_CODER.serialize(doc)
299
- ## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
300
- ## end
301
- ##
302
- ## def test_array
303
- ## doc = {'doc' => [1, 2, 'a', 'b']}
304
- ## bson = BSON::BSON_CODER.serialize(doc)
305
- ## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
306
- ## end
307
- ##
308
- ## def test_invalid_object
309
- ## doc = {'foo' => Object.new}
310
- ## assert_raise InvalidDocument do
311
- ## @coder.serialize(doc)
312
- ## end
313
- ##
314
- ## assert_raise InvalidDocument do
315
- ## @coder.serialize({'date' => Date.today})
316
- ## end
317
- ## end
318
- ##
319
- ##
320
- ## def test_regex
321
- ## doc = {'doc' => /foobar/i}
322
- ## bson = BSON::BSON_CODER.serialize(doc)
323
- ## doc2 = BSON::BSON_CODER.deserialize(bson)
324
- ## assert_equal doc, doc2
325
- ##
326
- ## r = doc2['doc']
327
- ## assert_kind_of Regexp, r
328
- ##
329
- ## doc = {'doc' => r}
330
- ## bson_doc = BSON::BSON_CODER.serialize(doc)
331
- ## doc2 = nil
332
- ## doc2 = BSON::BSON_CODER.deserialize(bson_doc)
333
- ## assert_equal doc, doc2
334
- ## end
335
- ##
336
- # def test_boolean
337
- # doc = {'doc' => true}
338
- # bson = BSON::BSON_CODER.serialize(doc)
339
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
340
- # end
341
- #
342
- # def test_date
343
- # doc = {'date' => Time.now}
344
- # bson = BSON::BSON_CODER.serialize(doc)
345
- # doc2 = BSON::BSON_CODER.deserialize(bson)
346
- # # Mongo only stores up to the millisecond
347
- # assert_in_delta doc['date'], doc2['date'], 0.001
348
- # end
349
- #
350
- # def test_date_returns_as_utc
351
- # doc = {'date' => Time.now}
352
- # bson = BSON::BSON_CODER.serialize(doc)
353
- # doc2 = BSON::BSON_CODER.deserialize(bson)
354
- # assert doc2['date'].utc?
355
- # end
356
- #
357
- ## def test_dbref
358
- ## oid = ObjectID.new
359
- ## doc = {}
360
- ## doc['dbref'] = DBRef.new('namespace', oid)
361
- ## bson = BSON::BSON_CODER.serialize(doc)
362
- ## doc2 = BSON::BSON_CODER.deserialize(bson)
363
- ## assert_equal 'namespace', doc2['dbref'].namespace
364
- ## assert_equal oid, doc2['dbref'].object_id
365
- ## end
366
- ##
367
- # def test_symbol
368
- # doc = {'sym' => :foo}
369
- # bson = BSON::BSON_CODER.serialize(doc)
370
- # doc2 = BSON::BSON_CODER.deserialize(bson)
371
- # assert_equal :foo, doc2['sym']
372
- # end
373
- ##
374
- ### def test_regex_extended
375
- ### assert_doc_pass({'foo' => /^a/x})
376
- ### end
377
- ###
378
- # def test_object_id
379
- # assert_doc_pass({'_id' => BSON::ObjectID.new})
380
- # end
381
- #
382
- # def test_where
383
- # assert_doc_pass({'$where' => "function() { print('hello'); }"})
384
- # end
385
- #
386
- # def test_min_max_keys
387
- # assert_doc_pass({'doc' => BSON::MaxKey.new})
388
- # assert_doc_pass({'doc' => BSON::MinKey.new})
389
- # end
390
- #
391
- # def test_code
392
- # code = BSON::Code.new("print('hello')")
393
- # code.scope = {:foo => 2}
394
- # assert_doc_pass({'doc' => code})
395
- # end
396
- end