mongo 0.1.0 → 0.15
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.
- data/README.rdoc +268 -71
- data/Rakefile +27 -62
- data/bin/bson_benchmark.rb +59 -0
- data/bin/mongo_console +3 -3
- data/bin/run_test_script +19 -0
- data/bin/standard_benchmark +109 -0
- data/examples/admin.rb +41 -0
- data/examples/benchmarks.rb +42 -0
- data/examples/blog.rb +76 -0
- data/examples/capped.rb +23 -0
- data/examples/cursor.rb +47 -0
- data/examples/gridfs.rb +87 -0
- data/examples/index_test.rb +125 -0
- data/examples/info.rb +30 -0
- data/examples/queries.rb +69 -0
- data/examples/simple.rb +23 -0
- data/examples/strict.rb +34 -0
- data/examples/types.rb +35 -0
- data/lib/mongo.rb +9 -2
- data/lib/mongo/admin.rb +65 -68
- data/lib/mongo/collection.rb +379 -117
- data/lib/mongo/connection.rb +151 -0
- data/lib/mongo/cursor.rb +271 -216
- data/lib/mongo/db.rb +500 -315
- data/lib/mongo/errors.rb +26 -0
- data/lib/mongo/gridfs.rb +16 -0
- data/lib/mongo/gridfs/chunk.rb +92 -0
- data/lib/mongo/gridfs/grid_store.rb +464 -0
- data/lib/mongo/message.rb +16 -0
- data/lib/mongo/message/get_more_message.rb +24 -13
- data/lib/mongo/message/insert_message.rb +29 -11
- data/lib/mongo/message/kill_cursors_message.rb +23 -12
- data/lib/mongo/message/message.rb +74 -62
- data/lib/mongo/message/message_header.rb +35 -24
- data/lib/mongo/message/msg_message.rb +21 -9
- data/lib/mongo/message/opcodes.rb +26 -15
- data/lib/mongo/message/query_message.rb +63 -43
- data/lib/mongo/message/remove_message.rb +29 -12
- data/lib/mongo/message/update_message.rb +30 -13
- data/lib/mongo/query.rb +97 -89
- data/lib/mongo/types/binary.rb +25 -21
- data/lib/mongo/types/code.rb +30 -0
- data/lib/mongo/types/dbref.rb +19 -23
- data/lib/mongo/types/objectid.rb +130 -116
- data/lib/mongo/types/regexp_of_holding.rb +27 -31
- data/lib/mongo/util/bson.rb +273 -160
- data/lib/mongo/util/byte_buffer.rb +32 -28
- data/lib/mongo/util/ordered_hash.rb +88 -42
- data/lib/mongo/util/xml_to_ruby.rb +18 -15
- data/mongo-ruby-driver.gemspec +103 -0
- data/test/mongo-qa/_common.rb +8 -0
- data/test/mongo-qa/admin +26 -0
- data/test/mongo-qa/capped +22 -0
- data/test/mongo-qa/count1 +18 -0
- data/test/mongo-qa/dbs +22 -0
- data/test/mongo-qa/find +10 -0
- data/test/mongo-qa/find1 +15 -0
- data/test/mongo-qa/gridfs_in +16 -0
- data/test/mongo-qa/gridfs_out +17 -0
- data/test/mongo-qa/indices +49 -0
- data/test/mongo-qa/remove +25 -0
- data/test/mongo-qa/stress1 +35 -0
- data/test/mongo-qa/test1 +11 -0
- data/test/mongo-qa/update +18 -0
- data/{tests → test}/test_admin.rb +25 -16
- data/test/test_bson.rb +268 -0
- data/{tests → test}/test_byte_buffer.rb +0 -0
- data/test/test_chunk.rb +84 -0
- data/test/test_collection.rb +282 -0
- data/test/test_connection.rb +101 -0
- data/test/test_cursor.rb +321 -0
- data/test/test_db.rb +196 -0
- data/test/test_db_api.rb +798 -0
- data/{tests → test}/test_db_connection.rb +4 -3
- data/test/test_grid_store.rb +284 -0
- data/{tests → test}/test_message.rb +1 -1
- data/test/test_objectid.rb +105 -0
- data/{tests → test}/test_ordered_hash.rb +55 -0
- data/{tests → test}/test_round_trip.rb +13 -9
- data/test/test_threading.rb +37 -0
- metadata +74 -32
- data/bin/validate +0 -51
- data/lib/mongo/mongo.rb +0 -74
- data/lib/mongo/types/undefined.rb +0 -31
- data/tests/test_bson.rb +0 -135
- data/tests/test_cursor.rb +0 -66
- data/tests/test_db.rb +0 -51
- data/tests/test_db_api.rb +0 -349
- data/tests/test_objectid.rb +0 -88
@@ -8,12 +8,12 @@ require 'test/unit'
|
|
8
8
|
# OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations.
|
9
9
|
#
|
10
10
|
# There is a whole other project that includes similar tests
|
11
|
-
# (http://github.com/mongodb/mongo-qa). If the directory
|
12
|
-
#
|
13
|
-
#
|
11
|
+
# (http://github.com/mongodb/mongo-qa). If the directory ../../mongo-qa
|
12
|
+
# exists, (that is, the top-level dir of mongo-qa is next to the top-level dir
|
13
|
+
# of this project), then we find the BSON test files there and use those, too.
|
14
14
|
class RoundTripTest < Test::Unit::TestCase
|
15
15
|
|
16
|
-
include
|
16
|
+
include Mongo
|
17
17
|
|
18
18
|
@@ruby = nil
|
19
19
|
|
@@ -47,7 +47,7 @@ EOS
|
|
47
47
|
# Dynamically generate one test for each test file. This way, if one test
|
48
48
|
# fails the others will still run.
|
49
49
|
create_test_for_round_trip_files_in_dir(File.join(HERE, 'data'))
|
50
|
-
mongo_qa_dir = File.join(HERE, '
|
50
|
+
mongo_qa_dir = File.join(HERE, '../..', 'mongo-qa/modules/bson_tests/tests')
|
51
51
|
if File.exist?(mongo_qa_dir)
|
52
52
|
%w(basic_types complex single_types).each { |subdir_name|
|
53
53
|
create_test_for_round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
|
@@ -64,10 +64,14 @@ EOS
|
|
64
64
|
# generated)
|
65
65
|
def one_round_trip(dir, name)
|
66
66
|
obj = File.open(File.join(dir, "#{name}.xson")) { |f|
|
67
|
-
|
67
|
+
begin
|
68
|
+
XMLToRuby.new.xml_to_ruby(f)
|
69
|
+
rescue => ex # unsupported type
|
70
|
+
return
|
71
|
+
end
|
68
72
|
}
|
69
73
|
|
70
|
-
File.open(File.join(dir, "#{name}.bson"), '
|
74
|
+
File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|
|
71
75
|
# Read the BSON from the file
|
72
76
|
bson = f.read
|
73
77
|
bson = if RUBY_VERSION >= '1.9'
|
@@ -93,9 +97,9 @@ EOS
|
|
93
97
|
# Turn those BSON bytes back into a Ruby object.
|
94
98
|
#
|
95
99
|
# We're passing a nil db to the contructor here, but that's OK because
|
96
|
-
# the BSON
|
100
|
+
# the BSON DBRef bytes don't contain the db object in any case, and we
|
97
101
|
# don't care what the database is.
|
98
|
-
obj_from_bson = BSON.new
|
102
|
+
obj_from_bson = BSON.new.deserialize(ByteBuffer.new(bson_from_ruby))
|
99
103
|
assert_kind_of OrderedHash, obj_from_bson
|
100
104
|
|
101
105
|
# Turn that Ruby object into BSON and compare it to the original BSON
|
@@ -0,0 +1,37 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestThreading < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mongo
|
8
|
+
|
9
|
+
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
10
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
11
|
+
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
12
|
+
@@coll = @@db.collection('thread-test-collection')
|
13
|
+
|
14
|
+
def test_threading
|
15
|
+
@@coll.clear
|
16
|
+
|
17
|
+
1000.times do |i|
|
18
|
+
@@coll.insert("x" => i)
|
19
|
+
end
|
20
|
+
|
21
|
+
threads = []
|
22
|
+
|
23
|
+
10.times do |i|
|
24
|
+
threads[i] = Thread.new{
|
25
|
+
sum = 0
|
26
|
+
@@coll.find().each { |document|
|
27
|
+
sum += document["x"]
|
28
|
+
}
|
29
|
+
assert_equal 499500, sum
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
10.times do |i|
|
34
|
+
threads[i].join
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,33 +1,56 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.15"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
8
|
+
- Mike Dirolf
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2009-
|
13
|
+
date: 2009-10-05 00:00:00 -04:00
|
13
14
|
default_executable:
|
14
15
|
dependencies: []
|
15
16
|
|
16
|
-
description:
|
17
|
-
email:
|
18
|
-
executables:
|
19
|
-
- mongo_console
|
20
|
-
extensions: []
|
17
|
+
description: A Ruby driver for MongoDB. For more information about Mongo, see http://www.mongodb.org.
|
18
|
+
email: mongodb-dev@googlegroups.com
|
19
|
+
executables: []
|
21
20
|
|
22
|
-
|
21
|
+
extensions: []
|
23
22
|
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.rdoc
|
24
25
|
files:
|
26
|
+
- README.rdoc
|
27
|
+
- Rakefile
|
28
|
+
- mongo-ruby-driver.gemspec
|
29
|
+
- bin/bson_benchmark.rb
|
25
30
|
- bin/mongo_console
|
26
|
-
- bin/
|
31
|
+
- bin/run_test_script
|
32
|
+
- bin/standard_benchmark
|
33
|
+
- examples/admin.rb
|
34
|
+
- examples/benchmarks.rb
|
35
|
+
- examples/blog.rb
|
36
|
+
- examples/capped.rb
|
37
|
+
- examples/cursor.rb
|
38
|
+
- examples/gridfs.rb
|
39
|
+
- examples/index_test.rb
|
40
|
+
- examples/info.rb
|
41
|
+
- examples/queries.rb
|
42
|
+
- examples/simple.rb
|
43
|
+
- examples/strict.rb
|
44
|
+
- examples/types.rb
|
27
45
|
- lib/mongo/admin.rb
|
28
46
|
- lib/mongo/collection.rb
|
47
|
+
- lib/mongo/connection.rb
|
29
48
|
- lib/mongo/cursor.rb
|
30
49
|
- lib/mongo/db.rb
|
50
|
+
- lib/mongo/gridfs/chunk.rb
|
51
|
+
- lib/mongo/gridfs/grid_store.rb
|
52
|
+
- lib/mongo/gridfs.rb
|
53
|
+
- lib/mongo/errors.rb
|
31
54
|
- lib/mongo/message/get_more_message.rb
|
32
55
|
- lib/mongo/message/insert_message.rb
|
33
56
|
- lib/mongo/message/kill_cursors_message.rb
|
@@ -39,36 +62,26 @@ files:
|
|
39
62
|
- lib/mongo/message/remove_message.rb
|
40
63
|
- lib/mongo/message/update_message.rb
|
41
64
|
- lib/mongo/message.rb
|
42
|
-
- lib/mongo/mongo.rb
|
43
65
|
- lib/mongo/query.rb
|
44
66
|
- lib/mongo/types/binary.rb
|
67
|
+
- lib/mongo/types/code.rb
|
45
68
|
- lib/mongo/types/dbref.rb
|
46
69
|
- lib/mongo/types/objectid.rb
|
47
70
|
- lib/mongo/types/regexp_of_holding.rb
|
48
|
-
- lib/mongo/types/undefined.rb
|
49
71
|
- lib/mongo/util/bson.rb
|
50
72
|
- lib/mongo/util/byte_buffer.rb
|
51
73
|
- lib/mongo/util/ordered_hash.rb
|
52
74
|
- lib/mongo/util/xml_to_ruby.rb
|
53
75
|
- lib/mongo.rb
|
54
|
-
- tests/test_admin.rb
|
55
|
-
- tests/test_bson.rb
|
56
|
-
- tests/test_byte_buffer.rb
|
57
|
-
- tests/test_cursor.rb
|
58
|
-
- tests/test_db.rb
|
59
|
-
- tests/test_db_api.rb
|
60
|
-
- tests/test_db_connection.rb
|
61
|
-
- tests/test_message.rb
|
62
|
-
- tests/test_objectid.rb
|
63
|
-
- tests/test_ordered_hash.rb
|
64
|
-
- tests/test_round_trip.rb
|
65
|
-
- Rakefile
|
66
|
-
- README.rdoc
|
67
76
|
has_rdoc: true
|
68
77
|
homepage: http://www.mongodb.org
|
69
|
-
|
70
|
-
rdoc_options: []
|
78
|
+
licenses: []
|
71
79
|
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- --main
|
83
|
+
- README.rdoc
|
84
|
+
- --inline-source
|
72
85
|
require_paths:
|
73
86
|
- lib
|
74
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -85,10 +98,39 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
98
|
version:
|
86
99
|
requirements: []
|
87
100
|
|
88
|
-
rubyforge_project:
|
89
|
-
rubygems_version: 1.3.
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.3.5
|
90
103
|
signing_key:
|
91
|
-
specification_version:
|
92
|
-
summary:
|
93
|
-
test_files:
|
94
|
-
|
104
|
+
specification_version: 3
|
105
|
+
summary: Ruby driver for the MongoDB
|
106
|
+
test_files:
|
107
|
+
- test/mongo-qa/_common.rb
|
108
|
+
- test/mongo-qa/admin
|
109
|
+
- test/mongo-qa/capped
|
110
|
+
- test/mongo-qa/count1
|
111
|
+
- test/mongo-qa/dbs
|
112
|
+
- test/mongo-qa/find
|
113
|
+
- test/mongo-qa/find1
|
114
|
+
- test/mongo-qa/gridfs_in
|
115
|
+
- test/mongo-qa/gridfs_out
|
116
|
+
- test/mongo-qa/indices
|
117
|
+
- test/mongo-qa/remove
|
118
|
+
- test/mongo-qa/stress1
|
119
|
+
- test/mongo-qa/test1
|
120
|
+
- test/mongo-qa/update
|
121
|
+
- test/test_admin.rb
|
122
|
+
- test/test_bson.rb
|
123
|
+
- test/test_byte_buffer.rb
|
124
|
+
- test/test_chunk.rb
|
125
|
+
- test/test_collection.rb
|
126
|
+
- test/test_connection.rb
|
127
|
+
- test/test_cursor.rb
|
128
|
+
- test/test_db.rb
|
129
|
+
- test/test_db_api.rb
|
130
|
+
- test/test_db_connection.rb
|
131
|
+
- test/test_grid_store.rb
|
132
|
+
- test/test_message.rb
|
133
|
+
- test/test_objectid.rb
|
134
|
+
- test/test_ordered_hash.rb
|
135
|
+
- test/test_threading.rb
|
136
|
+
- test/test_round_trip.rb
|
data/bin/validate
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# usage: validate somefile.xson somefile.bson
|
4
|
-
#
|
5
|
-
# Reads somefile.xson file (XML that describes a Mongo-type document),
|
6
|
-
# converts it into a Ruby OrderedHash, runs that through the BSON
|
7
|
-
# serialization code, and writes the BSON bytes to somefile.bson.
|
8
|
-
#
|
9
|
-
# In addition, this script takes the generated BSON, reads it in then writes
|
10
|
-
# it back out to a temp BSON file. If they are different, we report that error
|
11
|
-
# to STDOUT.
|
12
|
-
#
|
13
|
-
# This script is used by the mongo-qa project
|
14
|
-
# (http://github.com/mongodb/mongo-qa).
|
15
|
-
|
16
|
-
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
17
|
-
require 'mongo'
|
18
|
-
require 'mongo/util/xml_to_ruby'
|
19
|
-
|
20
|
-
if ARGV.length < 2
|
21
|
-
$stderr.puts "usage: validate somefile.xson somefile.bson"
|
22
|
-
exit 1
|
23
|
-
end
|
24
|
-
|
25
|
-
# Translate the .xson XML into a Ruby object, turn that object into BSON, and
|
26
|
-
# write the BSON to the file as requested.
|
27
|
-
obj = File.open(ARGV[0], 'rb') { |f| XMLToRuby.new.xml_to_ruby(f) }
|
28
|
-
bson = BSON.new.serialize(obj).to_a
|
29
|
-
File.open(ARGV[1], 'wb') { |f| bson.each { |b| f.putc(b) } }
|
30
|
-
|
31
|
-
# Now the additional testing. Read the generated BSON back in, deserialize it,
|
32
|
-
# and re-serialize the results. Compare that BSON with the BSON from the file
|
33
|
-
# we output.
|
34
|
-
bson = File.open(ARGV[1], 'rb') { |f| f.read }
|
35
|
-
bson = if RUBY_VERSION >= '1.9'
|
36
|
-
bson.bytes.to_a
|
37
|
-
else
|
38
|
-
bson.split(//).collect { |c| c[0] }
|
39
|
-
end
|
40
|
-
|
41
|
-
# Turn the Ruby object into BSON bytes and compare with the BSON bytes from
|
42
|
-
# the file.
|
43
|
-
bson_from_ruby = BSON.new.serialize(obj).to_a
|
44
|
-
|
45
|
-
if bson.length != bson_from_ruby.length
|
46
|
-
$stderr.puts "error: round-trip BSON lengths differ when testing #{ARGV[0]}"
|
47
|
-
exit 1
|
48
|
-
elsif bson != bson_from_ruby
|
49
|
-
$stderr.puts "error: round-trip BSON contents differ when testing #{ARGV[0]}"
|
50
|
-
exit 1
|
51
|
-
end
|
data/lib/mongo/mongo.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
# --
|
2
|
-
# Copyright (C) 2008-2009 10gen Inc.
|
3
|
-
#
|
4
|
-
# This program is free software: you can redistribute it and/or modify it
|
5
|
-
# under the terms of the GNU Affero General Public License, version 3, as
|
6
|
-
# published by the Free Software Foundation.
|
7
|
-
#
|
8
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
9
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
11
|
-
# for more details.
|
12
|
-
#
|
13
|
-
# You should have received a copy of the GNU Affero General Public License
|
14
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15
|
-
# ++
|
16
|
-
|
17
|
-
require 'mongo/db'
|
18
|
-
|
19
|
-
module XGen
|
20
|
-
module Mongo
|
21
|
-
module Driver
|
22
|
-
|
23
|
-
# Represents a Mongo database server.
|
24
|
-
class Mongo
|
25
|
-
|
26
|
-
DEFAULT_PORT = 27017
|
27
|
-
|
28
|
-
# Either nodes_or_host is a host name string and port is an optional
|
29
|
-
# port number that defaults to DEFAULT_PORT, or nodes_or_host is an
|
30
|
-
# array of arrays, where each is a host/port pair (or a host with no
|
31
|
-
# port). Finally, if nodes_or_host is nil then host is 'localhost' and
|
32
|
-
# port is DEFAULT_PORT. Since that's so confusing, here are a few
|
33
|
-
# examples:
|
34
|
-
#
|
35
|
-
# Mongo.new # localhost, DEFAULT_PORT
|
36
|
-
# Mongo.new("localhost") # localhost, DEFAULT_PORT
|
37
|
-
# Mongo.new("localhost", 3000) # localhost, 3000
|
38
|
-
# Mongo.new([["localhost"]]) # localhost, DEFAULT_PORT
|
39
|
-
# Mongo.new([["localhost", 3000]]) # localhost, 3000
|
40
|
-
# Mongo.new([["db1.example.com", 3000], ["db2.example.com", 3000]]])
|
41
|
-
#
|
42
|
-
# When a DB object first connects, it tries nodes and stops at the
|
43
|
-
# first one it connects to.
|
44
|
-
def initialize(nodes_or_host=nil, port=nil)
|
45
|
-
@nodes = case nodes_or_host
|
46
|
-
when String
|
47
|
-
[[nodes_or_host, port || DEFAULT_PORT]]
|
48
|
-
when Array
|
49
|
-
nodes_or_host.collect { |nh| [nh[0], nh[1] || DEFAULT_PORT] }
|
50
|
-
when nil
|
51
|
-
[['localhost', DEFAULT_PORT]]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Return the XGen::Mongo::Driver::DB named +db_name+.
|
56
|
-
def db(db_name)
|
57
|
-
XGen::Mongo::Driver::DB.new(db_name, @nodes)
|
58
|
-
end
|
59
|
-
|
60
|
-
# Not implemented.
|
61
|
-
def clone_database(from)
|
62
|
-
raise "not implemented"
|
63
|
-
end
|
64
|
-
|
65
|
-
# Not implemented.
|
66
|
-
def copy_database(from_host, from_db, to_db)
|
67
|
-
raise "not implemented"
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# --
|
2
|
-
# Copyright (C) 2008-2009 10gen Inc.
|
3
|
-
#
|
4
|
-
# This program is free software: you can redistribute it and/or modify it
|
5
|
-
# under the terms of the GNU Affero General Public License, version 3, as
|
6
|
-
# published by the Free Software Foundation.
|
7
|
-
#
|
8
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
9
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
11
|
-
# for more details.
|
12
|
-
#
|
13
|
-
# You should have received a copy of the GNU Affero General Public License
|
14
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15
|
-
# ++
|
16
|
-
|
17
|
-
module XGen
|
18
|
-
module Mongo
|
19
|
-
module Driver
|
20
|
-
|
21
|
-
# A special "undefined" type to match Mongo's storage of UNKNOWN values.
|
22
|
-
# "UNKNOWN" comes from JavaScript.
|
23
|
-
#
|
24
|
-
# NOTE: this class does not attempt to provide ANY of the semantics an
|
25
|
-
# "unknown" object might need. It isn't nil, it isn't special in any
|
26
|
-
# way, and there isn't any singleton value.
|
27
|
-
class Undefined < Object; end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/tests/test_bson.rb
DELETED
@@ -1,135 +0,0 @@
|
|
1
|
-
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
-
require 'mongo'
|
3
|
-
require 'test/unit'
|
4
|
-
|
5
|
-
class BSONTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
include XGen::Mongo::Driver
|
8
|
-
|
9
|
-
def setup
|
10
|
-
# We don't pass a DB to the constructor, even though we are about to test
|
11
|
-
# deserialization. This means that when we deserialize, any DBRefs will
|
12
|
-
# have nil @db ivars. That's fine for now.
|
13
|
-
@b = BSON.new
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_string
|
17
|
-
doc = {'doc' => 'hello, world'}
|
18
|
-
@b.serialize(doc)
|
19
|
-
assert_equal doc, @b.deserialize
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_code
|
23
|
-
doc = {'$where' => 'this.a.b < this.b'}
|
24
|
-
@b.serialize(doc)
|
25
|
-
assert_equal doc, @b.deserialize
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_number
|
29
|
-
doc = {'doc' => 41.99}
|
30
|
-
@b.serialize(doc)
|
31
|
-
assert_equal doc, @b.deserialize
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_int
|
35
|
-
doc = {'doc' => 42}
|
36
|
-
@b.serialize(doc)
|
37
|
-
assert_equal doc, @b.deserialize
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_object
|
41
|
-
doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
42
|
-
@b.serialize(doc)
|
43
|
-
assert_equal doc, @b.deserialize
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_oid
|
47
|
-
doc = {'doc' => ObjectID.new}
|
48
|
-
@b.serialize(doc)
|
49
|
-
assert_equal doc, @b.deserialize
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_array
|
53
|
-
doc = {'doc' => [1, 2, 'a', 'b']}
|
54
|
-
@b.serialize(doc)
|
55
|
-
assert_equal doc, @b.deserialize
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_regex
|
59
|
-
doc = {'doc' => /foobar/i}
|
60
|
-
@b.serialize(doc)
|
61
|
-
doc2 = @b.deserialize
|
62
|
-
assert_equal doc, doc2
|
63
|
-
|
64
|
-
r = doc2['doc']
|
65
|
-
assert_kind_of XGen::Mongo::Driver::RegexpOfHolding, r
|
66
|
-
assert_equal '', r.extra_options_str
|
67
|
-
|
68
|
-
r.extra_options_str << 'zywcab'
|
69
|
-
assert_equal 'zywcab', r.extra_options_str
|
70
|
-
|
71
|
-
b = BSON.new
|
72
|
-
doc = {'doc' => r}
|
73
|
-
b.serialize(doc)
|
74
|
-
doc2 = nil
|
75
|
-
doc2 = b.deserialize
|
76
|
-
assert_equal doc, doc2
|
77
|
-
|
78
|
-
r = doc2['doc']
|
79
|
-
assert_kind_of XGen::Mongo::Driver::RegexpOfHolding, r
|
80
|
-
assert_equal 'abcwyz', r.extra_options_str # must be sorted
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_boolean
|
84
|
-
doc = {'doc' => true}
|
85
|
-
@b.serialize(doc)
|
86
|
-
assert_equal doc, @b.deserialize
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_date
|
90
|
-
doc = {'date' => Time.now}
|
91
|
-
@b.serialize(doc)
|
92
|
-
doc2 = @b.deserialize
|
93
|
-
# Mongo only stores seconds, so comparing raw Time objects will fail
|
94
|
-
# because the fractional seconds will be different.
|
95
|
-
assert_equal doc['date'].to_i, doc2['date'].to_i
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_null
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_dbref
|
102
|
-
oid = ObjectID.new
|
103
|
-
doc = {}
|
104
|
-
doc['dbref'] = DBRef.new(doc, 'dbref', nil, 'namespace', oid)
|
105
|
-
@b.serialize(doc)
|
106
|
-
doc2 = @b.deserialize
|
107
|
-
assert_equal 'namespace', doc2['dbref'].namespace
|
108
|
-
assert_equal oid, doc2['dbref'].object_id
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_symbol
|
112
|
-
doc = {'sym' => :foo}
|
113
|
-
@b.serialize(doc)
|
114
|
-
doc2 = @b.deserialize
|
115
|
-
assert_equal :foo, doc2['sym']
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_binary
|
119
|
-
bin = 'binstring'.to_mongo_binary
|
120
|
-
assert_kind_of Binary, bin
|
121
|
-
|
122
|
-
doc = {'bin' => bin}
|
123
|
-
@b.serialize(doc)
|
124
|
-
doc2 = @b.deserialize
|
125
|
-
assert_equal 'binstring', doc2['bin']
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_undefined
|
129
|
-
doc = {'undef' => Undefined.new}
|
130
|
-
@b.serialize(doc)
|
131
|
-
doc2 = @b.deserialize
|
132
|
-
assert_kind_of Undefined, doc2['undef']
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|