bson 2.0.0.beta → 2.0.0.rc

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.

Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +80 -0
  5. data/CONTRIBUTING.md +42 -0
  6. data/LICENSE +190 -0
  7. data/NOTICE +2 -0
  8. data/README.md +6 -1
  9. data/Rakefile +15 -1
  10. data/ext/bson/native.c +16 -0
  11. data/lib/bson.rb +12 -50
  12. data/lib/bson/array.rb +14 -1
  13. data/lib/bson/binary.rb +14 -1
  14. data/lib/bson/boolean.rb +14 -1
  15. data/lib/bson/code.rb +14 -1
  16. data/lib/bson/code_with_scope.rb +14 -1
  17. data/lib/bson/document.rb +15 -2
  18. data/lib/bson/encodable.rb +14 -1
  19. data/lib/bson/environment.rb +98 -0
  20. data/lib/bson/false_class.rb +14 -1
  21. data/lib/bson/float.rb +14 -1
  22. data/lib/bson/hash.rb +14 -1
  23. data/lib/bson/int32.rb +15 -2
  24. data/lib/bson/int64.rb +15 -2
  25. data/lib/bson/integer.rb +14 -1
  26. data/lib/bson/json.rb +14 -1
  27. data/lib/bson/max_key.rb +14 -1
  28. data/lib/bson/min_key.rb +14 -1
  29. data/lib/bson/nil_class.rb +14 -1
  30. data/lib/bson/object_id.rb +15 -2
  31. data/lib/bson/regexp.rb +14 -1
  32. data/lib/bson/registry.rb +14 -1
  33. data/lib/bson/specialized.rb +14 -1
  34. data/lib/bson/string.rb +14 -1
  35. data/lib/bson/symbol.rb +14 -1
  36. data/lib/bson/time.rb +14 -1
  37. data/lib/bson/timestamp.rb +14 -1
  38. data/lib/bson/true_class.rb +14 -1
  39. data/lib/bson/undefined.rb +15 -2
  40. data/lib/bson/version.rb +15 -2
  41. data/lib/native.bundle +0 -0
  42. data/spec/bson/array_spec.rb +31 -0
  43. data/spec/bson/binary_spec.rb +103 -0
  44. data/spec/bson/boolean_spec.rb +48 -0
  45. data/spec/bson/code_spec.rb +42 -0
  46. data/spec/bson/code_with_scope_spec.rb +70 -0
  47. data/spec/bson/document_spec.rb +778 -0
  48. data/spec/bson/false_class_spec.rb +28 -0
  49. data/spec/bson/float_spec.rb +29 -0
  50. data/spec/bson/hash_spec.rb +56 -0
  51. data/spec/bson/int32_spec.rb +28 -0
  52. data/spec/bson/int64_spec.rb +28 -0
  53. data/spec/bson/integer_spec.rb +76 -0
  54. data/spec/bson/json_spec.rb +53 -0
  55. data/spec/bson/max_key_spec.rb +75 -0
  56. data/spec/bson/min_key_spec.rb +75 -0
  57. data/spec/bson/nil_class_spec.rb +29 -0
  58. data/spec/bson/object_id_spec.rb +418 -0
  59. data/spec/bson/regexp_spec.rb +89 -0
  60. data/spec/bson/registry_spec.rb +55 -0
  61. data/spec/bson/string_spec.rb +271 -0
  62. data/spec/bson/symbol_spec.rb +45 -0
  63. data/spec/bson/time_spec.rb +43 -0
  64. data/spec/bson/timestamp_spec.rb +74 -0
  65. data/spec/bson/true_class_spec.rb +28 -0
  66. data/spec/bson/undefined_spec.rb +29 -0
  67. data/spec/bson_spec.rb +46 -0
  68. data/spec/spec_helper.rb +31 -0
  69. data/spec/support/shared_examples.rb +95 -0
  70. metadata +85 -5
  71. metadata.gz.sig +1 -0
  72. data/LICENSE.md +0 -13
@@ -0,0 +1,29 @@
1
+ # Copyright (C) 2013 10gen Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+
17
+ describe NilClass do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 10.chr }
22
+ let(:obj) { nil }
23
+ let(:bson) { BSON::NO_VALUE }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a serializable bson element"
27
+ it_behaves_like "a deserializable bson element"
28
+ end
29
+ end
@@ -0,0 +1,418 @@
1
+ # Copyright (C) 2013 10gen Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+ require "yaml"
17
+
18
+ describe BSON::ObjectId do
19
+
20
+ describe "#==" do
21
+
22
+ context "when data is identical" do
23
+
24
+ let(:time) do
25
+ Time.now
26
+ end
27
+
28
+ let(:object_id) do
29
+ described_class.from_time(time)
30
+ end
31
+
32
+ let(:other_id) do
33
+ described_class.from_time(time)
34
+ end
35
+
36
+ it "returns true" do
37
+ expect(object_id).to eq(other_id)
38
+ end
39
+ end
40
+
41
+ context "when the data is different" do
42
+
43
+ let(:time) do
44
+ Time.now
45
+ end
46
+
47
+ let(:object_id) do
48
+ described_class.from_time(time)
49
+ end
50
+
51
+ it "returns false" do
52
+ expect(object_id).to_not eq(described_class.new)
53
+ end
54
+ end
55
+
56
+ context "when other is not an object id" do
57
+
58
+ it "returns false" do
59
+ expect(described_class.new).to_not eq(nil)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#===" do
65
+
66
+ let(:object_id) do
67
+ described_class.new
68
+ end
69
+
70
+ context "when comparing with another object id" do
71
+
72
+ context "when the data is equal" do
73
+
74
+ let(:other) do
75
+ described_class.from_string(object_id.to_s)
76
+ end
77
+
78
+ it "returns true" do
79
+ expect(object_id === other).to be_true
80
+ end
81
+ end
82
+
83
+ context "when the data is not equal" do
84
+
85
+ let(:other) do
86
+ described_class.new
87
+ end
88
+
89
+ it "returns false" do
90
+ expect(object_id === other).to be_false
91
+ end
92
+ end
93
+ end
94
+
95
+ context "when comparing to an object id class" do
96
+
97
+ it "returns false" do
98
+ expect(object_id === BSON::ObjectId).to be_false
99
+ end
100
+ end
101
+
102
+ context "when comparing with a string" do
103
+
104
+ context "when the data is equal" do
105
+
106
+ let(:other) do
107
+ object_id.to_s
108
+ end
109
+
110
+ it "returns true" do
111
+ expect(object_id === other).to be_true
112
+ end
113
+ end
114
+
115
+ context "when the data is not equal" do
116
+
117
+ let(:other) do
118
+ described_class.new.to_s
119
+ end
120
+
121
+ it "returns false" do
122
+ expect(object_id === other).to be_false
123
+ end
124
+ end
125
+ end
126
+
127
+ context "when comparing with a non string or object id" do
128
+
129
+ it "returns false" do
130
+ expect(object_id === "test").to be_false
131
+ end
132
+ end
133
+
134
+ context "when comparing with a non object id class" do
135
+
136
+ it "returns false" do
137
+ expect(object_id === String).to be_false
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "#<" do
143
+
144
+ let(:object_id) do
145
+ described_class.from_time(Time.utc(2012, 1, 1))
146
+ end
147
+
148
+ let(:other_id) do
149
+ described_class.from_time(Time.utc(2012, 1, 30))
150
+ end
151
+
152
+ context "when the generation time before the other" do
153
+
154
+ it "returns true" do
155
+ expect(object_id < other_id).to be_true
156
+ end
157
+ end
158
+
159
+ context "when the generation time is after the other" do
160
+
161
+ it "returns false" do
162
+ expect(other_id < object_id).to be_false
163
+ end
164
+ end
165
+ end
166
+
167
+ describe "#>" do
168
+
169
+ let(:object_id) do
170
+ described_class.from_time(Time.utc(2012, 1, 1))
171
+ end
172
+
173
+ let(:other_id) do
174
+ described_class.from_time(Time.utc(2012, 1, 30))
175
+ end
176
+
177
+ context "when the generation time before the other" do
178
+
179
+ it "returns false" do
180
+ expect(object_id > other_id).to be_false
181
+ end
182
+ end
183
+
184
+ context "when the generation time is after the other" do
185
+
186
+ it "returns true" do
187
+ expect(other_id > object_id).to be_true
188
+ end
189
+ end
190
+ end
191
+
192
+ describe "#<=>" do
193
+
194
+ let(:object_id) do
195
+ described_class.from_time(Time.utc(2012, 1, 1))
196
+ end
197
+
198
+ let(:other_id) do
199
+ described_class.from_time(Time.utc(2012, 1, 30))
200
+ end
201
+
202
+ context "when the generation time before the other" do
203
+
204
+ it "returns -1" do
205
+ expect(object_id <=> other_id).to eq(-1)
206
+ end
207
+ end
208
+
209
+ context "when the generation time is after the other" do
210
+
211
+ it "returns false" do
212
+ expect(other_id <=> object_id).to eq(1)
213
+ end
214
+ end
215
+ end
216
+
217
+ describe "#as_json" do
218
+
219
+ let(:object) do
220
+ described_class.new
221
+ end
222
+
223
+ it "returns the object id with $oid key" do
224
+ expect(object.as_json).to eq({ "$oid" => object.to_s })
225
+ end
226
+
227
+ it_behaves_like "a JSON serializable object"
228
+ end
229
+
230
+ describe "::BSON_TYPE" do
231
+
232
+ it "returns 0x07" do
233
+ expect(BSON::ObjectId::BSON_TYPE).to eq(7.chr)
234
+ end
235
+ end
236
+
237
+ describe "#bson_type" do
238
+
239
+ let(:code) do
240
+ described_class.new
241
+ end
242
+
243
+ it "returns 0x0D" do
244
+ expect(code.bson_type).to eq(BSON::ObjectId::BSON_TYPE)
245
+ end
246
+ end
247
+
248
+ describe ".from_string" do
249
+
250
+ context "when the string is valid" do
251
+
252
+ let(:string) do
253
+ "4e4d66343b39b68407000001"
254
+ end
255
+
256
+ let(:object_id) do
257
+ described_class.from_string(string)
258
+ end
259
+
260
+ it "initializes with the string's bytes" do
261
+ expect(object_id.to_s).to eq(string)
262
+ end
263
+ end
264
+
265
+ context "when the string is not valid" do
266
+
267
+ it "raises an error" do
268
+ expect {
269
+ described_class.from_string("asadsf")
270
+ }.to raise_error(BSON::ObjectId::Invalid)
271
+ end
272
+ end
273
+ end
274
+
275
+ describe ".from_time" do
276
+
277
+ context "when no unique option is provided" do
278
+
279
+ let(:time) do
280
+ Time.at((Time.now.utc - 64800).to_i).utc
281
+ end
282
+
283
+ let(:object_id) do
284
+ described_class.from_time(time)
285
+ end
286
+
287
+ it "sets the generation time" do
288
+ expect(object_id.generation_time).to eq(time)
289
+ end
290
+
291
+ it "does not include process or sequence information" do
292
+ expect(object_id.to_s =~ /\A[0-9a-f]{8}[0]{16}\Z/).to be_true
293
+ end
294
+ end
295
+
296
+ context "when a unique option is provided" do
297
+
298
+ let(:time) do
299
+ Time.at((Time.now.utc - 64800).to_i).utc
300
+ end
301
+
302
+ let(:object_id) do
303
+ described_class.from_time(time, :unique => true)
304
+ end
305
+
306
+ let(:non_unique) do
307
+ described_class.from_time(time, :unique => true)
308
+ end
309
+
310
+ it "creates a new unique object id" do
311
+ expect(object_id).to_not eq(non_unique)
312
+ end
313
+ end
314
+ end
315
+
316
+ describe "#generation_time" do
317
+
318
+ let(:time) do
319
+ Time.utc(2013, 1, 1)
320
+ end
321
+
322
+ let(:object_id) do
323
+ described_class.from_time(time)
324
+ end
325
+
326
+ it "returns the generation time" do
327
+ expect(object_id.generation_time).to eq(time)
328
+ end
329
+ end
330
+
331
+ describe ".legal?" do
332
+
333
+ context "when the string is too short to be an object id" do
334
+
335
+ it "returns false" do
336
+ expect(described_class).to_not be_legal("a" * 23)
337
+ end
338
+ end
339
+
340
+ context "when the string contains invalid hex characters" do
341
+
342
+ it "returns false" do
343
+ expect(described_class).to_not be_legal("y" + "a" * 23)
344
+ end
345
+ end
346
+
347
+ context "when the string is a valid object id" do
348
+
349
+ it "returns true" do
350
+ expect(described_class).to be_legal("a" * 24)
351
+ end
352
+ end
353
+
354
+ context "when checking against another object id" do
355
+
356
+ let(:object_id) do
357
+ described_class.new
358
+ end
359
+
360
+ it "returns true" do
361
+ expect(described_class).to be_legal(object_id)
362
+ end
363
+ end
364
+ end
365
+
366
+ describe "#to_bson/#from_bson" do
367
+
368
+ let(:time) { Time.utc(2013, 1, 1) }
369
+ let(:type) { 7.chr }
370
+ let(:obj) { described_class.from_time(time) }
371
+ let(:bson) { obj.to_bson }
372
+
373
+ it_behaves_like "a bson element"
374
+ it_behaves_like "a serializable bson element"
375
+ it_behaves_like "a deserializable bson element"
376
+ end
377
+
378
+ describe "#to_s" do
379
+
380
+ let(:time) do
381
+ Time.utc(2013, 1, 1)
382
+ end
383
+
384
+ let(:expected) do
385
+ "50e227000000000000000000"
386
+ end
387
+
388
+ let(:object_id) do
389
+ described_class.from_time(time)
390
+ end
391
+
392
+ it "returns a hex string representation of the id" do
393
+ expect(object_id.to_s).to eq(expected)
394
+ end
395
+
396
+ unless BSON::Environment.ruby_18?
397
+
398
+ it "returns the string in UTF-8" do
399
+ expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
400
+ end
401
+ end
402
+
403
+ it "converts to a readable yaml string" do
404
+ expect(YAML.dump(object_id.to_s)).to include(expected)
405
+ end
406
+ end
407
+
408
+ context "when the class is loaded" do
409
+
410
+ let(:registered) do
411
+ BSON::Registry.get(BSON::ObjectId::BSON_TYPE)
412
+ end
413
+
414
+ it "registers the type" do
415
+ expect(registered).to eq(described_class)
416
+ end
417
+ end
418
+ end
@@ -0,0 +1,89 @@
1
+ # Copyright (C) 2013 10gen Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+
17
+ describe Regexp do
18
+
19
+ describe "#as_json" do
20
+
21
+ let(:object) do
22
+ /\W+/i
23
+ end
24
+
25
+ it "returns the binary data plus type" do
26
+ expect(object.as_json).to eq(
27
+ { "$regex" => "\\W+", "$options" => "i" }
28
+ )
29
+ end
30
+
31
+ it_behaves_like "a JSON serializable object"
32
+ end
33
+
34
+ describe "#to_bson/#from_bson" do
35
+
36
+ let(:type) { 11.chr }
37
+ let(:obj) { /test/ }
38
+
39
+ it_behaves_like "a bson element"
40
+
41
+ context "when the regexp has no options" do
42
+
43
+ let(:obj) { /\d+/ }
44
+ let(:bson) { "#{obj.source}#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}" }
45
+
46
+ it_behaves_like "a serializable bson element"
47
+ it_behaves_like "a deserializable bson element"
48
+ end
49
+
50
+ context "when the regexp has options" do
51
+
52
+ context "when ignoring case" do
53
+
54
+ let(:obj) { /\W+/i }
55
+ let(:bson) { "#{obj.source}#{BSON::NULL_BYTE}i#{BSON::NULL_BYTE}" }
56
+
57
+ it_behaves_like "a serializable bson element"
58
+ it_behaves_like "a deserializable bson element"
59
+ end
60
+
61
+ context "when matching multiline" do
62
+
63
+ let(:obj) { /\W+/m }
64
+ let(:bson) { "#{obj.source}#{BSON::NULL_BYTE}ms#{BSON::NULL_BYTE}" }
65
+
66
+ it_behaves_like "a serializable bson element"
67
+ it_behaves_like "a deserializable bson element"
68
+ end
69
+
70
+ context "when matching extended" do
71
+
72
+ let(:obj) { /\W+/x }
73
+ let(:bson) { "#{obj.source}#{BSON::NULL_BYTE}x#{BSON::NULL_BYTE}" }
74
+
75
+ it_behaves_like "a serializable bson element"
76
+ it_behaves_like "a deserializable bson element"
77
+ end
78
+
79
+ context "when all options are present" do
80
+
81
+ let(:obj) { /\W+/xim }
82
+ let(:bson) { "#{obj.source}#{BSON::NULL_BYTE}imsx#{BSON::NULL_BYTE}" }
83
+
84
+ it_behaves_like "a serializable bson element"
85
+ it_behaves_like "a deserializable bson element"
86
+ end
87
+ end
88
+ end
89
+ end