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.
- data/Rakefile +23 -0
- data/bson.gemspec +1 -0
- data/ext/java/jar/bson.jar +0 -0
- data/ext/java/jar/jbson.jar +0 -0
- data/ext/java/jar/jruby.jar +0 -0
- data/ext/java/jar/mongo.jar +0 -0
- data/lib/bson.rb +32 -22
- data/lib/bson/bson_java.rb +21 -0
- data/lib/bson/bson_ruby.rb +7 -8
- data/lib/bson/ordered_hash.rb +2 -2
- data/lib/bson/types/code.rb +16 -3
- data/lib/bson/types/dbref.rb +4 -0
- data/lib/bson/types/object_id.rb +9 -0
- metadata +44 -63
- data/lib/bson/types/objectid.rb +0 -187
- data/test/mongo_bson/basic_test.rb +0 -99
- data/test/mongo_bson/binary_test.rb +0 -15
- data/test/mongo_bson/bson_test.rb +0 -543
- data/test/mongo_bson/byte_buffer_test.rb +0 -190
- data/test/mongo_bson/jruby_bson_test.rb +0 -227
- data/test/mongo_bson/jruby_encode_test.rb +0 -396
- data/test/mongo_bson/json_test.rb +0 -16
- data/test/mongo_bson/object_id_test.rb +0 -132
- data/test/mongo_bson/ordered_hash_test.rb +0 -197
@@ -1,16 +0,0 @@
|
|
1
|
-
require './test/test_helper'
|
2
|
-
require 'rubygems'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
class JSONTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
include Mongo
|
8
|
-
include BSON
|
9
|
-
|
10
|
-
def test_object_id_as_json
|
11
|
-
id = ObjectId.new
|
12
|
-
obj = {'_id' => id}
|
13
|
-
assert_equal "{\"_id\":{\"$oid\": \"#{id.to_s}\"}}", obj.to_json
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require './test/test_helper'
|
2
|
-
require 'rubygems'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
class ObjectIdTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
include Mongo
|
8
|
-
include BSON
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@o = ObjectId.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_hashcode
|
15
|
-
assert_equal @o.instance_variable_get(:@data).hash, @o.hash
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_array_uniq_for_equilavent_ids
|
19
|
-
a = ObjectId.new('123')
|
20
|
-
b = ObjectId.new('123')
|
21
|
-
assert_equal a, b
|
22
|
-
assert_equal 1, [a, b].uniq.size
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_create_pk_method
|
26
|
-
doc = {:name => 'Mongo'}
|
27
|
-
doc = ObjectId.create_pk(doc)
|
28
|
-
assert doc[:_id]
|
29
|
-
|
30
|
-
doc = {:name => 'Mongo', :_id => '12345'}
|
31
|
-
doc = ObjectId.create_pk(doc)
|
32
|
-
assert_equal '12345', doc[:_id]
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_different
|
36
|
-
a = ObjectId.new
|
37
|
-
b = ObjectId.new
|
38
|
-
assert_not_equal a.to_a, b.to_a
|
39
|
-
assert_not_equal a, b
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_eql?
|
43
|
-
o2 = ObjectId.new(@o.to_a)
|
44
|
-
assert_equal @o, o2
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_to_s
|
48
|
-
s = @o.to_s
|
49
|
-
assert_equal 24, s.length
|
50
|
-
s =~ /^([0-9a-f]+)$/
|
51
|
-
assert_equal 24, $1.length
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_method
|
55
|
-
assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_inspect
|
59
|
-
assert_equal "BSON::ObjectId('#{@o.to_s}')", @o.inspect
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_save_and_restore
|
63
|
-
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
64
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
65
|
-
db = Connection.new(host, port).db(MONGO_TEST_DB)
|
66
|
-
coll = db.collection('test')
|
67
|
-
|
68
|
-
coll.remove
|
69
|
-
coll << {'a' => 1, '_id' => @o}
|
70
|
-
|
71
|
-
row = coll.find().collect.first
|
72
|
-
assert_equal 1, row['a']
|
73
|
-
assert_equal @o, row['_id']
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_from_string
|
77
|
-
hex_str = @o.to_s
|
78
|
-
o2 = ObjectId.from_string(hex_str)
|
79
|
-
assert_equal hex_str, o2.to_s
|
80
|
-
assert_equal @o, o2
|
81
|
-
assert_equal @o.to_s, o2.to_s
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_illegal_from_string
|
85
|
-
assert_raise InvalidObjectId do
|
86
|
-
ObjectId.from_string("")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_legal
|
91
|
-
assert !ObjectId.legal?(nil)
|
92
|
-
assert !ObjectId.legal?("fred")
|
93
|
-
assert !ObjectId.legal?("0000")
|
94
|
-
assert !ObjectId.legal?('000102030405060708090A0')
|
95
|
-
assert ObjectId.legal?('000102030405060708090A0B')
|
96
|
-
assert ObjectId.legal?('abcdefABCDEF123456789012')
|
97
|
-
assert !ObjectId.legal?('abcdefABCDEF12345678901x')
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_from_string_leading_zeroes
|
101
|
-
hex_str = '000000000000000000000000'
|
102
|
-
o = ObjectId.from_string(hex_str)
|
103
|
-
assert_equal hex_str, o.to_s
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_byte_order
|
107
|
-
hex_str = '000102030405060708090A0B'
|
108
|
-
o = ObjectId.from_string(hex_str)
|
109
|
-
assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_generation_time
|
113
|
-
time = Time.now
|
114
|
-
id = ObjectId.new
|
115
|
-
generated_time = id.generation_time
|
116
|
-
|
117
|
-
assert_in_delta time.to_i, generated_time.to_i, 2
|
118
|
-
assert_equal "UTC", generated_time.zone
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_from_time
|
122
|
-
time = Time.now.utc
|
123
|
-
id = ObjectId.from_time(time)
|
124
|
-
|
125
|
-
assert_equal time.to_i, id.generation_time.to_i
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_json
|
129
|
-
id = ObjectId.new
|
130
|
-
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
131
|
-
end
|
132
|
-
end
|
@@ -1,197 +0,0 @@
|
|
1
|
-
require './test/test_helper'
|
2
|
-
|
3
|
-
class OrderedHashTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@oh = BSON::OrderedHash.new
|
7
|
-
@oh['c'] = 1
|
8
|
-
@oh['a'] = 2
|
9
|
-
@oh['z'] = 3
|
10
|
-
@ordered_keys = %w(c a z)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_initialize
|
14
|
-
a = BSON::OrderedHash.new
|
15
|
-
a['x'] = 1
|
16
|
-
a['y'] = 2
|
17
|
-
|
18
|
-
b = BSON::OrderedHash['x' => 1, 'y' => 2]
|
19
|
-
assert_equal a, b
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_hash_code
|
23
|
-
o = BSON::OrderedHash.new
|
24
|
-
o['number'] = 50
|
25
|
-
assert o.hash
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_empty
|
29
|
-
assert_equal [], BSON::OrderedHash.new.keys
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_uniq
|
33
|
-
list = []
|
34
|
-
doc = BSON::OrderedHash.new
|
35
|
-
doc['_id'] = 'ab12'
|
36
|
-
doc['name'] = 'test'
|
37
|
-
|
38
|
-
same_doc = BSON::OrderedHash.new
|
39
|
-
same_doc['_id'] = 'ab12'
|
40
|
-
same_doc['name'] = 'test'
|
41
|
-
list << doc
|
42
|
-
list << same_doc
|
43
|
-
|
44
|
-
assert_equal 2, list.size
|
45
|
-
assert_equal 1, list.uniq.size
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_equality
|
49
|
-
a = BSON::OrderedHash.new
|
50
|
-
a['x'] = 1
|
51
|
-
a['y'] = 2
|
52
|
-
|
53
|
-
b = BSON::OrderedHash.new
|
54
|
-
b['y'] = 2
|
55
|
-
b['x'] = 1
|
56
|
-
|
57
|
-
c = BSON::OrderedHash.new
|
58
|
-
c['x'] = 1
|
59
|
-
c['y'] = 2
|
60
|
-
|
61
|
-
d = BSON::OrderedHash.new
|
62
|
-
d['x'] = 2
|
63
|
-
d['y'] = 3
|
64
|
-
|
65
|
-
e = BSON::OrderedHash.new
|
66
|
-
e['z'] = 1
|
67
|
-
e['y'] = 2
|
68
|
-
|
69
|
-
assert_equal a, c
|
70
|
-
assert_not_equal a, b
|
71
|
-
assert_not_equal a, d
|
72
|
-
assert_not_equal a, e
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_order_preserved
|
76
|
-
assert_equal @ordered_keys, @oh.keys
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_to_a_order_preserved
|
80
|
-
assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_order_preserved_after_replace
|
84
|
-
@oh['a'] = 42
|
85
|
-
assert_equal @ordered_keys, @oh.keys
|
86
|
-
@oh['c'] = 'foobar'
|
87
|
-
assert_equal @ordered_keys, @oh.keys
|
88
|
-
@oh['z'] = /huh?/
|
89
|
-
assert_equal @ordered_keys, @oh.keys
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_each
|
93
|
-
keys = []
|
94
|
-
@oh.each { |k, v| keys << k }
|
95
|
-
assert_equal keys, @oh.keys
|
96
|
-
|
97
|
-
@oh['z'] = 42
|
98
|
-
assert_equal keys, @oh.keys
|
99
|
-
|
100
|
-
assert_equal @oh, @oh.each {|k,v|}
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_values
|
104
|
-
assert_equal [1, 2, 3], @oh.values
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_merge
|
108
|
-
other = BSON::OrderedHash.new
|
109
|
-
other['f'] = 'foo'
|
110
|
-
noob = @oh.merge(other)
|
111
|
-
assert_equal @ordered_keys + ['f'], noob.keys
|
112
|
-
assert_equal [1, 2, 3, 'foo'], noob.values
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_merge_bang
|
116
|
-
other = BSON::OrderedHash.new
|
117
|
-
other['f'] = 'foo'
|
118
|
-
@oh.merge!(other)
|
119
|
-
assert_equal @ordered_keys + ['f'], @oh.keys
|
120
|
-
assert_equal [1, 2, 3, 'foo'], @oh.values
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_merge_bang_with_overlap
|
124
|
-
other = BSON::OrderedHash.new
|
125
|
-
other['a'] = 'apple'
|
126
|
-
other['c'] = 'crab'
|
127
|
-
other['f'] = 'foo'
|
128
|
-
@oh.merge!(other)
|
129
|
-
assert_equal @ordered_keys + ['f'], @oh.keys
|
130
|
-
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_merge_bang_with_hash_with_overlap
|
134
|
-
other = Hash.new
|
135
|
-
other['a'] = 'apple'
|
136
|
-
other['c'] = 'crab'
|
137
|
-
other['f'] = 'foo'
|
138
|
-
@oh.merge!(other)
|
139
|
-
assert_equal @ordered_keys + ['f'], @oh.keys
|
140
|
-
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_equality_with_hash
|
144
|
-
o = BSON::OrderedHash.new
|
145
|
-
o[:a] = 1
|
146
|
-
o[:b] = 2
|
147
|
-
o[:c] = 3
|
148
|
-
r = {:a => 1, :b => 2, :c => 3}
|
149
|
-
assert r == o
|
150
|
-
assert o == r
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_update
|
154
|
-
other = BSON::OrderedHash.new
|
155
|
-
other['f'] = 'foo'
|
156
|
-
noob = @oh.update(other)
|
157
|
-
assert_equal @ordered_keys + ['f'], noob.keys
|
158
|
-
assert_equal [1, 2, 3, 'foo'], noob.values
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_inspect_retains_order
|
162
|
-
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_clear
|
166
|
-
@oh.clear
|
167
|
-
assert @oh.keys.empty?
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_delete
|
171
|
-
assert @oh.keys.include?('z')
|
172
|
-
@oh.delete('z')
|
173
|
-
assert !@oh.keys.include?('z')
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_delete_if
|
177
|
-
assert @oh.keys.include?('z')
|
178
|
-
@oh.delete_if { |k,v| k == 'z' }
|
179
|
-
assert !@oh.keys.include?('z')
|
180
|
-
end
|
181
|
-
|
182
|
-
def test_reject
|
183
|
-
new = @oh.reject { |k, v| k == 'foo' }
|
184
|
-
assert new.keys == @oh.keys
|
185
|
-
|
186
|
-
new = @oh.reject { |k, v| k == 'z' }
|
187
|
-
assert !new.keys.include?('z')
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_clone
|
191
|
-
copy = @oh.clone
|
192
|
-
assert copy.keys == @oh.keys
|
193
|
-
|
194
|
-
copy[:foo] = 1
|
195
|
-
assert copy.keys != @oh.keys
|
196
|
-
end
|
197
|
-
end
|