bson 1.0.1 → 1.0.2

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.

data/Rakefile CHANGED
@@ -134,18 +134,18 @@ namespace :gem do
134
134
 
135
135
  desc "Install the gem locally"
136
136
  task :install do
137
+ sh "gem build bson.gemspec"
138
+ sh "gem install bson-*.gem"
137
139
  sh "gem build mongo-ruby-driver.gemspec"
138
140
  sh "gem install mongo-*.gem"
139
141
  sh "rm mongo-*.gem"
142
+ sh "rm bson-*.gem"
140
143
  end
141
144
 
142
145
  desc "Install the optional c extensions"
143
146
  task :install_extensions do
144
- sh "gem build bson.gemspec"
145
147
  sh "gem build bson_ext.gemspec"
146
- sh "gem install bson-*.gem"
147
148
  sh "gem install bson_ext-*.gem"
148
- sh "rm bson-*.gem"
149
149
  sh "rm bson_ext-*.gem"
150
150
  end
151
151
 
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: UTF-8
3
+
4
+ # --
5
+ # Copyright (C) 2008-2010 10gen Inc.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ # ++
19
+
20
+ require 'rubygems'
21
+ require 'bson'
22
+
23
+ begin
24
+ require 'yajl'
25
+ rescue LoadError
26
+ puts "This script requires yajl. Please install as follows:"
27
+ puts " gem install yajl-ruby"
28
+ Process.exit
29
+ end
30
+
31
+ # Convert all documents in an IO into JSON.
32
+ def print_b2json(io)
33
+ while not io.eof? do
34
+ bsonobj = BSON.read_bson_document(io)
35
+ Yajl::Encoder.encode(bsonobj, STDOUT)
36
+ STDOUT << "\n"
37
+ end
38
+ end
39
+
40
+ # print usage
41
+ def usage()
42
+ STDERR << <<END_OF_USAGE
43
+ usage: b2json.rb [-h] [file1 [file2]]
44
+
45
+ Converts a BSON file to JSON on STDOUT.
46
+ You can pass multiple filename.
47
+ If no filenames are passed, then STDIN is consumed.
48
+
49
+ END_OF_USAGE
50
+ exit
51
+ end
52
+
53
+ # no arg, use STDIN
54
+ # -h, print usage and exit
55
+ # otherwise loop of filenames
56
+ if ARGV.empty? then
57
+ print_b2json(STDIN)
58
+ exit
59
+ elsif ARGV[0] == "-h" then
60
+ usage()
61
+ else
62
+ ARGV.each { |fname| print_b2json(File.new(fname)) }
63
+ end
@@ -13,8 +13,11 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.files = ['Rakefile', 'bson.gemspec', 'LICENSE.txt']
15
15
  s.files += ['lib/bson.rb'] + Dir['lib/bson/**/*.rb']
16
+ s.files += ['bin/b2json']
16
17
  s.test_files = Dir['test/mongo_bson/*.rb']
17
18
 
19
+ s.executables = ['b2json']
20
+
18
21
  s.has_rdoc = true
19
22
 
20
23
  s.authors = ['Jim Menard', 'Mike Dirolf', 'Kyle Banker']
@@ -2,8 +2,10 @@
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
+ MINIMUM_BSON_EXT_VERSION = "1.0.1"
6
+
5
7
  module BSON
6
- VERSION = "1.0.1"
8
+ VERSION = "1.0.2"
7
9
  def self.serialize(obj, check_keys=false, move_id=false)
8
10
  BSON_CODER.serialize(obj, check_keys, move_id)
9
11
  end
@@ -12,13 +14,33 @@ module BSON
12
14
  def self.deserialize(buf=nil)
13
15
  BSON_CODER.deserialize(buf)
14
16
  end
17
+
18
+ # Reads a single BSON document from an IO object.
19
+ # This method is used in the executable b2json, bundled with
20
+ # the bson gem, for reading a file of bson documents.
21
+ #
22
+ # @param [IO] io an io object containing a bson object.
23
+ #
24
+ # @return [ByteBuffer]
25
+ def self.read_bson_document(io)
26
+ bytebuf = BSON::ByteBuffer.new
27
+ sz = io.read(4).unpack("V")[0]
28
+ bytebuf.put_int(sz)
29
+ bytebuf.put_array(io.read(sz-4).unpack("C*"))
30
+ bytebuf.rewind
31
+ return BSON.deserialize(bytebuf)
32
+ end
15
33
  end
16
34
 
17
35
  begin
18
36
  # Need this for running test with and without c ext in Ruby 1.9.
19
37
  raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
20
38
  require 'bson_ext/cbson'
21
- raise LoadError unless defined?(CBson::VERSION) && CBson::VERSION == BSON::VERSION
39
+ raise LoadError unless defined?(CBson::VERSION)
40
+ if CBson::VERSION < MINIMUM_BSON_EXT_VERSION
41
+ puts "Able to load bson_ext version #{CBson::VERSION}, but >= #{MINIMUM_BSON_EXT_VERSION} is required."
42
+ raise LoadError
43
+ end
22
44
  require 'bson/bson_c'
23
45
  module BSON
24
46
  BSON_CODER = BSON_C
@@ -1,5 +1,21 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ # --
4
+ # Copyright (C) 2008-2010 10gen Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ # ++
18
+
3
19
  # A thin wrapper for the CBson class
4
20
  module BSON
5
21
  class BSON_C
@@ -10,7 +26,7 @@ module BSON
10
26
 
11
27
  def self.deserialize(buf=nil)
12
28
  if buf.is_a? String
13
- to_deserialize = ByteBuffer.new(buf) if buf
29
+ buf = ByteBuffer.new(buf.unpack("C*")) if buf
14
30
  else
15
31
  buf = ByteBuffer.new(buf.to_a) if buf
16
32
  end
@@ -0,0 +1,123 @@
1
+ # encoding: UTF-8
2
+ # A thin wrapper for the CBson class
3
+ module BSON
4
+ module JMongo
5
+ import com.mongodb.BasicDBList
6
+ import com.mongodb.BasicDBObject
7
+ import com.mongodb.Bytes
8
+ import org.bson.BSONDecoder
9
+ import org.bson.BSONEncoder
10
+
11
+ import org.bson.BSONCallback
12
+ import org.bson.types.BasicBSONList
13
+ import org.bson.BasicBSONObject
14
+ import org.bson.types.Binary
15
+ import org.bson.types.ObjectId
16
+ import org.bson.types.Symbol
17
+ import org.bson.types.CodeWScope
18
+ end
19
+ end
20
+
21
+ module BSON
22
+ class BSON_JAVA
23
+ #ENC = JMongo::BSONEncoder.new
24
+ BSON::ObjectID
25
+
26
+ ENC = Java::OrgJbson::RubyBSONEncoder.new(JRuby.runtime)
27
+ DEC = JMongo::BSONDecoder.new
28
+
29
+
30
+ def self.deserialize(buf)
31
+ if buf.is_a? String
32
+ buf = ByteBuffer.new(buf.unpack("C*")) if buf
33
+ end
34
+ callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
35
+ DEC.decode(buf.to_a.to_java(Java::byte), callback)
36
+ callback.get
37
+ end
38
+
39
+ def deserialize(buf)
40
+ callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
41
+ DEC.decode(buf.to_a.to_java(Java::byte), callback)
42
+ callback.get
43
+ end
44
+
45
+ def self.serialize(obj, check=false, move=false)
46
+ ENC.encode(obj)
47
+ end
48
+
49
+ def to_dbobject obj
50
+ case obj
51
+ when Array
52
+ array_to_dblist obj
53
+ when Hash
54
+ hash_to_dbobject obj
55
+ #when BSON::Binary
56
+ # JMongo::Binary.new(obj.subtype, obj.to_a)
57
+ when BSON::ObjectID
58
+ JMongo::ObjectId.new(obj.to_s)
59
+ when Regexp
60
+ str = obj.source
61
+ options = obj.options
62
+ options_flag = 0
63
+ options_flag |= JavaPattern::CASE_INSENSITIVE if ((options & Regexp::IGNORECASE) != 0)
64
+ options_flag |= JavaPattern::MULTILINE if ((options & Regexp::MULTILINE) != 0)
65
+ #options_flag |= JavaPattern::EXTENDED if ((options & Regexp::EXTENDED) != 0)
66
+ Java::JavaUtilRegex::Pattern.compile(str, options_flag)
67
+ when Symbol
68
+ JMongo::Symbol.new(obj)
69
+ when BSON::Binary
70
+ obj.put_int(obj.size, 0)
71
+ b = JMongo::Binary.new(obj.subtype, obj.to_a.to_java(Java::byte))
72
+ obj.to_a.to_java(Java::byte)
73
+ when BSON::DBRef
74
+
75
+
76
+ else
77
+ # primitive value, no conversion necessary
78
+ #puts "Un-handled class type [#{obj.class}]"
79
+ obj
80
+ end
81
+ end
82
+
83
+ def from_java_object(obj)
84
+ case obj
85
+ when JMongo::BasicBSONList
86
+ obj
87
+ when JMongo::BasicBSONObject
88
+ hsh = {}
89
+ obj.toMap.keySet.each do |key|
90
+ value = obj.get(key)
91
+ hsh[key] = self.from_java_object(value)
92
+ end
93
+ hsh
94
+ when JMongo::ObjectId
95
+ BSON::ObjectID.from_string(obj.toStringMongod())
96
+ else
97
+ obj
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def hash_to_dbobject doc
104
+ obj = JMongo::BasicDBObject.new
105
+
106
+ doc.each_pair do |key, value|
107
+ obj.append(key, to_dbobject(value))
108
+ end
109
+
110
+ obj
111
+ end
112
+
113
+ def array_to_dblist ary
114
+ list = JMongo::BasicDBList.new
115
+
116
+ ary.each_with_index do |element, index|
117
+ list.put(index, to_dbobject(element))
118
+ end
119
+
120
+ list
121
+ end
122
+ end
123
+ end
@@ -3,26 +3,23 @@
3
3
  # --
4
4
  # Copyright (C) 2008-2010 10gen Inc.
5
5
  #
6
- # This program is free software: you can redistribute it and/or modify it
7
- # under the terms of the GNU Affero General Public License, version 3, as
8
- # published by the Free Software Foundation.
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
9
  #
10
- # This program is distributed in the hope that it will be useful, but WITHOUT
11
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
13
- # for more details.
10
+ # http://www.apache.org/licenses/LICENSE-2.0
14
11
  #
15
- # You should have received a copy of the GNU Affero General Public License
16
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
17
  # ++
18
18
 
19
19
  module BSON
20
20
  # A BSON seralizer/deserializer in pure Ruby.
21
21
  class BSON_RUBY
22
22
 
23
- # why was this necessary?
24
- #include Mongo
25
-
26
23
  MINKEY = -1
27
24
  EOO = 0
28
25
  NUMBER = 1
@@ -178,7 +175,7 @@ module BSON
178
175
  # If buf is nil, use @buf, assumed to contain already-serialized BSON.
179
176
  # This is only true during testing.
180
177
  if buf.is_a? String
181
- @buf = ByteBuffer.new(buf) if buf
178
+ @buf = ByteBuffer.new(buf.unpack("C*")) if buf
182
179
  else
183
180
  @buf = ByteBuffer.new(buf.to_a) if buf
184
181
  end
@@ -22,6 +22,10 @@ require 'digest/md5'
22
22
 
23
23
  module BSON
24
24
 
25
+ def BSON::ObjectID(s)
26
+ ObjectID.from_string(s)
27
+ end
28
+
25
29
  # Generates MongoDB object ids.
26
30
  #
27
31
  # @core objectids
@@ -129,11 +133,11 @@ module BSON
129
133
  end
130
134
 
131
135
  def inspect
132
- {"$oid" => to_s}
136
+ "BSON::ObjectID('#{to_s}')"
133
137
  end
134
138
 
135
139
  # Convert to MongoDB extended JSON format. Since JSON includes type information,
136
- # but lacks an ObjectID type, this JSON format encodes the type using an $id key.
140
+ # but lacks an ObjectID type, this JSON format encodes the type using an $oid key.
137
141
  #
138
142
  # @return [String] the object id represented as MongoDB extended JSON.
139
143
  def to_json(escaped=false)
@@ -0,0 +1,99 @@
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 test_string
55
+ doc = {'doc' => 'hello, world'}
56
+ bson = bson = BSON::BSON_CODER.serialize(doc)
57
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
58
+ end
59
+
60
+ def test_object
61
+ doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
62
+ bson = BSON::BSON_CODER.serialize(doc)
63
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
64
+ end
65
+
66
+ def test_oid
67
+ doc = {'doc' => ObjectID.new}
68
+ bson = BSON::BSON_CODER.serialize(doc)
69
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
70
+ end
71
+
72
+ def test_array
73
+ doc = {'doc' => [1, 2, "a", "b"]}
74
+ bson = BSON::BSON_CODER.serialize(doc)
75
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
76
+ end
77
+
78
+ def test_speed
79
+
80
+ Benchmark.bm do |x|
81
+ x.report('serialize obj') do
82
+ 1000.times do
83
+ BSON::BSON_CODER.serialize(LARGE)
84
+ end
85
+ end
86
+ end
87
+
88
+
89
+
90
+ Benchmark.bm do |x|
91
+ b = BSON::BSON_CODER.serialize(LARGE)
92
+ x.report('deserialize obj') do
93
+ 1000.times do
94
+ BSON::BSON_CODER.deserialize(b)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -22,12 +22,28 @@ class BSONTest < Test::Unit::TestCase
22
22
 
23
23
  include BSON
24
24
 
25
+ def test_read_bson_io_document
26
+ doc = {'doc' => 'hello, world'}
27
+ bson = BSON.serialize(doc)
28
+ io = StringIO.new
29
+ io.write(bson.to_s)
30
+ io.rewind
31
+ assert_equal BSON.deserialize(bson), BSON.read_bson_document(io)
32
+ end
33
+
25
34
  def test_serialize_returns_byte_buffer
26
35
  doc = {'doc' => 'hello, world'}
27
36
  bson = BSON.serialize(doc)
28
37
  assert bson.is_a?(ByteBuffer)
29
38
  end
30
39
 
40
+ def test_deserialize_from_string
41
+ doc = {'doc' => 'hello, world'}
42
+ bson = BSON.serialize(doc)
43
+ string = bson.to_s
44
+ assert_equal doc, BSON.deserialize(string)
45
+ end
46
+
31
47
  def test_deprecated_bson_module
32
48
  doc = {'doc' => 'hello, world'}
33
49
  bson = BSON.serialize(doc)
@@ -0,0 +1,242 @@
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_binary
102
+ # doc = {'foo' => BSON::Binary.new("A".unpack("C*"))}
103
+ # #doc = assert_doc_pass({'foo' => BSON::Binary.new("A".unpack("C*"))})
104
+ # puts "First"
105
+ # p doc.to_a
106
+ # bson = @coder.serialize(doc)
107
+ # de = @coder.deserialize(bson)
108
+ # puts "Second"
109
+ # p de.to_a
110
+ # puts "subtype"
111
+ # p de['foo'].subtype
112
+ # puts "data"
113
+ # p de['foo'].data
114
+ # assert_equal doc, de
115
+ # end
116
+
117
+ def test_valid_utf8_string
118
+ doc = {'doc' => 'aé'}
119
+ bson = bson = BSON::BSON_CODER.serialize(doc)
120
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
121
+ end
122
+
123
+ def test_valid_utf8_key
124
+ doc = {'aé' => 'hello'}
125
+ bson = bson = BSON::BSON_CODER.serialize(doc)
126
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
127
+ end
128
+
129
+ def test_number
130
+ doc = {'doc' => 41.99}
131
+ bson = BSON::BSON_CODER.serialize(doc)
132
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
133
+ end
134
+
135
+ def test_int
136
+ doc = {'doc' => 42}
137
+ bson = BSON::BSON_CODER.serialize(doc)
138
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
139
+
140
+ doc = {"doc" => -5600}
141
+ bson = BSON::BSON_CODER.serialize(doc)
142
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
143
+
144
+ doc = {"doc" => 2147483647}
145
+ bson = BSON::BSON_CODER.serialize(doc)
146
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
147
+
148
+ doc = {"doc" => -2147483648}
149
+ bson = BSON::BSON_CODER.serialize(doc)
150
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
151
+ end
152
+
153
+ def test_ordered_hash
154
+ doc = BSON::OrderedHash.new
155
+ doc["b"] = 1
156
+ doc["a"] = 2
157
+ doc["c"] = 3
158
+ doc["d"] = 4
159
+ bson = BSON::BSON_CODER.serialize(doc)
160
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
161
+ end
162
+
163
+ def test_object
164
+ doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
165
+ bson = BSON::BSON_CODER.serialize(doc)
166
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
167
+ end
168
+
169
+ def test_oid
170
+ doc = {'doc' => ObjectID.new}
171
+ bson = BSON::BSON_CODER.serialize(doc)
172
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
173
+ end
174
+
175
+ def test_array
176
+ doc = {'doc' => [1, 2, 'a', 'b']}
177
+ bson = BSON::BSON_CODER.serialize(doc)
178
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
179
+ end
180
+
181
+ def test_regex
182
+ doc = {'doc' => /foobar/i}
183
+ bson = BSON::BSON_CODER.serialize(doc)
184
+ doc2 = BSON::BSON_CODER.deserialize(bson)
185
+ assert_equal doc, doc2
186
+
187
+ r = doc2['doc']
188
+ assert_kind_of Regexp, r
189
+
190
+ doc = {'doc' => r}
191
+ bson_doc = BSON::BSON_CODER.serialize(doc)
192
+ doc2 = nil
193
+ doc2 = BSON::BSON_CODER.deserialize(bson_doc)
194
+ assert_equal doc, doc2
195
+ end
196
+
197
+ def test_boolean
198
+ doc = {'doc' => true}
199
+ bson = BSON::BSON_CODER.serialize(doc)
200
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
201
+ end
202
+
203
+ def test_date
204
+ doc = {'date' => Time.now}
205
+ bson = BSON::BSON_CODER.serialize(doc)
206
+ doc2 = BSON::BSON_CODER.deserialize(bson)
207
+ # Mongo only stores up to the millisecond
208
+ assert_in_delta doc['date'], doc2['date'], 0.001
209
+ end
210
+
211
+ def test_date_returns_as_utc
212
+ doc = {'date' => Time.now}
213
+ bson = BSON::BSON_CODER.serialize(doc)
214
+ doc2 = BSON::BSON_CODER.deserialize(bson)
215
+ assert doc2['date'].utc?
216
+ end
217
+
218
+ def test_dbref
219
+ oid = ObjectID.new
220
+ doc = {}
221
+ doc['dbref'] = DBRef.new('namespace', oid)
222
+ bson = BSON::BSON_CODER.serialize(doc)
223
+ doc2 = BSON::BSON_CODER.deserialize(bson)
224
+ assert_equal 'namespace', doc2['dbref'].namespace
225
+ assert_equal oid, doc2['dbref'].object_id
226
+ end
227
+
228
+ def test_symbol
229
+ doc = {'sym' => :foo}
230
+ bson = BSON::BSON_CODER.serialize(doc)
231
+ doc2 = BSON::BSON_CODER.deserialize(bson)
232
+ assert_equal :foo, doc2['sym']
233
+ end
234
+
235
+ # def test_regex_extended
236
+ # assert_doc_pass({'foo' => /^a/x})
237
+ # end
238
+ #
239
+ def test_object_id
240
+ assert_doc_pass({'_id' => BSON::ObjectID.new})
241
+ end
242
+ end
@@ -0,0 +1,269 @@
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
+ @coder = BSON::BSON_CODER
40
+ end
41
+
42
+ def assert_doc_pass(doc)
43
+ bson = @coder.serialize(doc)
44
+ assert_equal doc, @coder.deserialize(bson)
45
+ end
46
+
47
+ # def test_symbol_key
48
+ # doc = {:foo => "bar"}
49
+ # p doc.keys
50
+ # bson = @coder.serialize(doc)
51
+ # new_doc = {"foo" => "bar"}
52
+ # assert_equal new_doc, @coder.deserialize(bson)
53
+ # end
54
+ #
55
+ # def test_string
56
+ # assert_doc_pass({'doc' => 'hello, world'})
57
+ # end
58
+ #
59
+ require 'iconv'
60
+ def test_invalid_string
61
+ require 'iconv'
62
+ string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
63
+ doc = {'doc' => string}
64
+ bson = @coder.serialize(doc)
65
+ assert_equal doc, @coder.deserialize(bson)
66
+ end
67
+
68
+
69
+ # def test_nested_string
70
+ # assert_doc_pass({'doc' => {'text' => 'hello, world'}})
71
+ # end
72
+ #
73
+ # def test_array
74
+ # assert_doc_pass({'doc' => ['1', '2', '3']})
75
+ # end
76
+ #
77
+ # def test_number
78
+ # assert_doc_pass({'doc' => 1})
79
+ # end
80
+ #
81
+ # def test_object
82
+ # assert_doc_pass({'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}})
83
+ # end
84
+ #
85
+ # def test_boolean
86
+ # assert_doc_pass({'foo' => true})
87
+ # assert_doc_pass({'foo' => false})
88
+ # end
89
+ #
90
+ # def test_nil
91
+ # assert_doc_pass({'foo' => nil})
92
+ # end
93
+ #
94
+ # def test_time
95
+ # assert_doc_pass({'foo' => Time.now})
96
+ # end
97
+ #
98
+ # def test_simple_regex
99
+ # assert_doc_pass({'foo' => /^a/})
100
+ # end
101
+ #
102
+ # def test_regex_with_options
103
+ # assert_doc_pass({'foo' => /^a/imx})
104
+ # end
105
+ #
106
+ # def test_symbol
107
+ # assert_doc_pass({'foo' => :bar})
108
+ # end
109
+ #
110
+ # def test_binary
111
+ # doc = {'foo' => BSON::Binary.new("ABCDE".unpack("C*"))}
112
+ # bson = @coder.serialize(doc)
113
+ # de = @coder.deserialize(bson)
114
+ # assert_equal doc, de
115
+ # end
116
+ #
117
+ # def test_valid_utf8_string
118
+ # doc = {'doc' => 'aé'}
119
+ # bson = bson = BSON::BSON_CODER.serialize(doc)
120
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
121
+ # end
122
+ #
123
+ # def test_valid_utf8_key
124
+ # doc = {'aé' => 'hello'}
125
+ # bson = bson = BSON::BSON_CODER.serialize(doc)
126
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
127
+ # end
128
+ #
129
+ # def test_number
130
+ # doc = {'doc' => 41.99}
131
+ # bson = BSON::BSON_CODER.serialize(doc)
132
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
133
+ # end
134
+ #
135
+ # def test_int
136
+ # doc = {'doc' => 42}
137
+ # bson = BSON::BSON_CODER.serialize(doc)
138
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
139
+ #
140
+ # doc = {"doc" => -5600}
141
+ # bson = BSON::BSON_CODER.serialize(doc)
142
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
143
+ #
144
+ # doc = {"doc" => 2147483647}
145
+ # bson = BSON::BSON_CODER.serialize(doc)
146
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
147
+ #
148
+ # doc = {"doc" => -2147483648}
149
+ # bson = BSON::BSON_CODER.serialize(doc)
150
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
151
+ # end
152
+ #
153
+ # def test_ordered_hash
154
+ # doc = BSON::OrderedHash.new
155
+ # doc["b"] = 1
156
+ # doc["a"] = 2
157
+ # doc["c"] = 3
158
+ # doc["d"] = 4
159
+ # bson = BSON::BSON_CODER.serialize(doc)
160
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
161
+ # end
162
+ #
163
+ # def test_object
164
+ # doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
165
+ # bson = BSON::BSON_CODER.serialize(doc)
166
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
167
+ # end
168
+ #
169
+ ## def test_oid
170
+ ## doc = {'doc' => ObjectID.new}
171
+ ## bson = BSON::BSON_CODER.serialize(doc)
172
+ ## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
173
+ ## end
174
+ ##
175
+ ## def test_array
176
+ ## doc = {'doc' => [1, 2, 'a', 'b']}
177
+ ## bson = BSON::BSON_CODER.serialize(doc)
178
+ ## assert_equal doc, BSON::BSON_CODER.deserialize(bson)
179
+ ## end
180
+ ##
181
+ ## def test_invalid_object
182
+ ## doc = {'foo' => Object.new}
183
+ ## assert_raise InvalidDocument do
184
+ ## @coder.serialize(doc)
185
+ ## end
186
+ ##
187
+ ## assert_raise InvalidDocument do
188
+ ## @coder.serialize({'date' => Date.today})
189
+ ## end
190
+ ## end
191
+ ##
192
+ ##
193
+ ## def test_regex
194
+ ## doc = {'doc' => /foobar/i}
195
+ ## bson = BSON::BSON_CODER.serialize(doc)
196
+ ## doc2 = BSON::BSON_CODER.deserialize(bson)
197
+ ## assert_equal doc, doc2
198
+ ##
199
+ ## r = doc2['doc']
200
+ ## assert_kind_of Regexp, r
201
+ ##
202
+ ## doc = {'doc' => r}
203
+ ## bson_doc = BSON::BSON_CODER.serialize(doc)
204
+ ## doc2 = nil
205
+ ## doc2 = BSON::BSON_CODER.deserialize(bson_doc)
206
+ ## assert_equal doc, doc2
207
+ ## end
208
+ ##
209
+ # def test_boolean
210
+ # doc = {'doc' => true}
211
+ # bson = BSON::BSON_CODER.serialize(doc)
212
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
213
+ # end
214
+ #
215
+ # def test_date
216
+ # doc = {'date' => Time.now}
217
+ # bson = BSON::BSON_CODER.serialize(doc)
218
+ # doc2 = BSON::BSON_CODER.deserialize(bson)
219
+ # # Mongo only stores up to the millisecond
220
+ # assert_in_delta doc['date'], doc2['date'], 0.001
221
+ # end
222
+ #
223
+ # def test_date_returns_as_utc
224
+ # doc = {'date' => Time.now}
225
+ # bson = BSON::BSON_CODER.serialize(doc)
226
+ # doc2 = BSON::BSON_CODER.deserialize(bson)
227
+ # assert doc2['date'].utc?
228
+ # end
229
+ #
230
+ ## def test_dbref
231
+ ## oid = ObjectID.new
232
+ ## doc = {}
233
+ ## doc['dbref'] = DBRef.new('namespace', oid)
234
+ ## bson = BSON::BSON_CODER.serialize(doc)
235
+ ## doc2 = BSON::BSON_CODER.deserialize(bson)
236
+ ## assert_equal 'namespace', doc2['dbref'].namespace
237
+ ## assert_equal oid, doc2['dbref'].object_id
238
+ ## end
239
+ ##
240
+ # def test_symbol
241
+ # doc = {'sym' => :foo}
242
+ # bson = BSON::BSON_CODER.serialize(doc)
243
+ # doc2 = BSON::BSON_CODER.deserialize(bson)
244
+ # assert_equal :foo, doc2['sym']
245
+ # end
246
+ ##
247
+ ### def test_regex_extended
248
+ ### assert_doc_pass({'foo' => /^a/x})
249
+ ### end
250
+ ###
251
+ # def test_object_id
252
+ # assert_doc_pass({'_id' => BSON::ObjectID.new})
253
+ # end
254
+ #
255
+ # def test_where
256
+ # assert_doc_pass({'$where' => "function() { print('hello'); }"})
257
+ # end
258
+ #
259
+ # def test_min_max_keys
260
+ # assert_doc_pass({'doc' => BSON::MaxKey.new})
261
+ # assert_doc_pass({'doc' => BSON::MinKey.new})
262
+ # end
263
+ #
264
+ # def test_code
265
+ # code = BSON::Code.new("print('hello')")
266
+ # code.scope = {:foo => 2}
267
+ # assert_doc_pass({'doc' => code})
268
+ # end
269
+ end
@@ -1,4 +1,6 @@
1
1
  require 'test/test_helper'
2
+ require 'rubygems'
3
+ require 'json'
2
4
 
3
5
  class ObjectIDTest < Test::Unit::TestCase
4
6
 
@@ -49,9 +51,12 @@ class ObjectIDTest < Test::Unit::TestCase
49
51
  assert_equal 24, $1.length
50
52
  end
51
53
 
54
+ def test_method
55
+ assert_equal ObjectID.from_string(@o.to_s), BSON::ObjectID(@o.to_s)
56
+ end
57
+
52
58
  def test_inspect
53
- obj = {'$oid' => @o.to_s}
54
- assert_equal obj, @o.inspect
59
+ assert_equal "BSON::ObjectID('#{@o.to_s}')", @o.inspect
55
60
  end
56
61
 
57
62
  def test_save_and_restore
metadata CHANGED
@@ -1,43 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Jim Menard
8
- - Mike Dirolf
9
- - Kyle Banker
7
+ - Jim Menard
8
+ - Mike Dirolf
9
+ - Kyle Banker
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2010-05-07 00:00:00 -04:00
14
+ date: 2010-06-05 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
18
18
  description: A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.
19
19
  email: mongodb-dev@googlegroups.com
20
- executables: []
21
-
20
+ executables:
21
+ - b2json
22
22
  extensions: []
23
23
 
24
24
  extra_rdoc_files: []
25
25
 
26
26
  files:
27
- - Rakefile
28
- - bson.gemspec
29
- - LICENSE.txt
30
- - lib/bson.rb
31
- - lib/bson/bson_c.rb
32
- - lib/bson/bson_ruby.rb
33
- - lib/bson/byte_buffer.rb
34
- - lib/bson/exceptions.rb
35
- - lib/bson/ordered_hash.rb
36
- - lib/bson/types/binary.rb
37
- - lib/bson/types/code.rb
38
- - lib/bson/types/dbref.rb
39
- - lib/bson/types/min_max_keys.rb
40
- - lib/bson/types/objectid.rb
27
+ - Rakefile
28
+ - bson.gemspec
29
+ - LICENSE.txt
30
+ - lib/bson.rb
31
+ - lib/bson/bson_c.rb
32
+ - lib/bson/bson_java.rb
33
+ - lib/bson/bson_ruby.rb
34
+ - lib/bson/byte_buffer.rb
35
+ - lib/bson/exceptions.rb
36
+ - lib/bson/ordered_hash.rb
37
+ - lib/bson/types/binary.rb
38
+ - lib/bson/types/code.rb
39
+ - lib/bson/types/dbref.rb
40
+ - lib/bson/types/min_max_keys.rb
41
+ - lib/bson/types/objectid.rb
42
+ - bin/b2json
41
43
  has_rdoc: true
42
44
  homepage: http://www.mongodb.org
43
45
  licenses: []
@@ -46,18 +48,18 @@ post_install_message:
46
48
  rdoc_options: []
47
49
 
48
50
  require_paths:
49
- - lib
51
+ - lib
50
52
  required_ruby_version: !ruby/object:Gem::Requirement
51
53
  requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: "0"
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
55
57
  version:
56
58
  required_rubygems_version: !ruby/object:Gem::Requirement
57
59
  requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: "0"
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
61
63
  version:
62
64
  requirements: []
63
65
 
@@ -67,8 +69,11 @@ signing_key:
67
69
  specification_version: 3
68
70
  summary: Ruby implementation of BSON
69
71
  test_files:
70
- - test/mongo_bson/binary_test.rb
71
- - test/mongo_bson/bson_test.rb
72
- - test/mongo_bson/byte_buffer_test.rb
73
- - test/mongo_bson/objectid_test.rb
74
- - test/mongo_bson/ordered_hash_test.rb
72
+ - test/mongo_bson/basic_test.rb
73
+ - test/mongo_bson/binary_test.rb
74
+ - test/mongo_bson/bson_test.rb
75
+ - test/mongo_bson/byte_buffer_test.rb
76
+ - test/mongo_bson/jruby_bson_test.rb
77
+ - test/mongo_bson/jruby_encode_test.rb
78
+ - test/mongo_bson/objectid_test.rb
79
+ - test/mongo_bson/ordered_hash_test.rb