bson 4.8.0 → 4.9.2

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.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +18 -6
  5. data/ext/bson/bson-endian.h +1 -1
  6. data/ext/bson/bson-native.h +1 -1
  7. data/ext/bson/bytebuf.c +1 -1
  8. data/ext/bson/endian.c +1 -1
  9. data/ext/bson/init.c +1 -1
  10. data/ext/bson/read.c +67 -19
  11. data/ext/bson/util.c +1 -1
  12. data/ext/bson/write.c +1 -1
  13. data/lib/bson.rb +1 -1
  14. data/lib/bson/active_support.rb +1 -1
  15. data/lib/bson/array.rb +14 -3
  16. data/lib/bson/binary.rb +1 -1
  17. data/lib/bson/boolean.rb +9 -2
  18. data/lib/bson/code.rb +1 -1
  19. data/lib/bson/code_with_scope.rb +16 -3
  20. data/lib/bson/config.rb +1 -1
  21. data/lib/bson/date.rb +1 -1
  22. data/lib/bson/date_time.rb +1 -1
  23. data/lib/bson/db_pointer.rb +7 -1
  24. data/lib/bson/decimal128.rb +1 -1
  25. data/lib/bson/decimal128/builder.rb +1 -1
  26. data/lib/bson/document.rb +1 -1
  27. data/lib/bson/environment.rb +2 -1
  28. data/lib/bson/error.rb +4 -0
  29. data/lib/bson/ext_json.rb +4 -3
  30. data/lib/bson/false_class.rb +1 -1
  31. data/lib/bson/float.rb +1 -1
  32. data/lib/bson/hash.rb +14 -3
  33. data/lib/bson/int32.rb +1 -1
  34. data/lib/bson/int64.rb +1 -1
  35. data/lib/bson/integer.rb +1 -1
  36. data/lib/bson/json.rb +1 -1
  37. data/lib/bson/max_key.rb +1 -1
  38. data/lib/bson/min_key.rb +1 -1
  39. data/lib/bson/nil_class.rb +1 -1
  40. data/lib/bson/object.rb +1 -1
  41. data/lib/bson/object_id.rb +1 -1
  42. data/lib/bson/open_struct.rb +1 -1
  43. data/lib/bson/regexp.rb +8 -2
  44. data/lib/bson/registry.rb +1 -1
  45. data/lib/bson/specialized.rb +1 -1
  46. data/lib/bson/string.rb +1 -1
  47. data/lib/bson/symbol.rb +1 -1
  48. data/lib/bson/time.rb +40 -6
  49. data/lib/bson/time_with_zone.rb +1 -1
  50. data/lib/bson/timestamp.rb +1 -1
  51. data/lib/bson/true_class.rb +1 -1
  52. data/lib/bson/undefined.rb +1 -1
  53. data/lib/bson/version.rb +2 -2
  54. data/spec/bson/array_spec.rb +1 -1
  55. data/spec/bson/binary_spec.rb +1 -1
  56. data/spec/bson/binary_uuid_spec.rb +1 -1
  57. data/spec/bson/boolean_spec.rb +1 -1
  58. data/spec/bson/code_spec.rb +1 -1
  59. data/spec/bson/code_with_scope_spec.rb +1 -1
  60. data/spec/bson/date_spec.rb +1 -1
  61. data/spec/bson/date_time_spec.rb +1 -1
  62. data/spec/bson/decimal128_spec.rb +1 -1
  63. data/spec/bson/document_spec.rb +1 -1
  64. data/spec/bson/ext_json_parse_spec.rb +34 -2
  65. data/spec/bson/false_class_spec.rb +1 -1
  66. data/spec/bson/float_spec.rb +1 -1
  67. data/spec/bson/hash_spec.rb +1 -1
  68. data/spec/bson/int32_spec.rb +1 -1
  69. data/spec/bson/int64_spec.rb +1 -1
  70. data/spec/bson/integer_spec.rb +1 -1
  71. data/spec/bson/json_spec.rb +1 -1
  72. data/spec/bson/max_key_spec.rb +1 -1
  73. data/spec/bson/min_key_spec.rb +1 -1
  74. data/spec/bson/nil_class_spec.rb +1 -1
  75. data/spec/bson/object_id_spec.rb +1 -1
  76. data/spec/bson/object_spec.rb +1 -1
  77. data/spec/bson/open_struct_spec.rb +1 -1
  78. data/spec/bson/raw_spec.rb +12 -1
  79. data/spec/bson/regexp_spec.rb +1 -1
  80. data/spec/bson/registry_spec.rb +1 -1
  81. data/spec/bson/string_spec.rb +1 -1
  82. data/spec/bson/symbol_spec.rb +1 -1
  83. data/spec/bson/time_spec.rb +205 -2
  84. data/spec/bson/time_with_zone_spec.rb +1 -1
  85. data/spec/bson/timestamp_spec.rb +1 -1
  86. data/spec/bson/true_class_spec.rb +1 -1
  87. data/spec/bson/undefined_spec.rb +1 -1
  88. data/spec/bson_spec.rb +1 -1
  89. data/spec/runners/common_driver.rb +1 -1
  90. data/spec/runners/corpus.rb +13 -10
  91. data/spec/runners/corpus_legacy.rb +1 -1
  92. data/spec/spec_helper.rb +1 -1
  93. data/spec/spec_tests/data/corpus/top.json +0 -4
  94. data/spec/support/shared_examples.rb +1 -1
  95. metadata +86 -87
  96. metadata.gz.sig +3 -2
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ module BSON
41
41
  # @return [ true, false ] If the Ruby version is 1.9.
42
42
  #
43
43
  # @since 4.2.0
44
+ # @deprecated
44
45
  def ruby_1_9?
45
46
  @ruby_1_9 ||= RUBY_VERSION < '2.0.0'
46
47
  end
@@ -13,5 +13,9 @@ module BSON
13
13
  # unsupported binary subtype.
14
14
  class UnsupportedBinarySubtype < Error
15
15
  end
16
+
17
+ # Exception raised when BSON decoding fails.
18
+ class BSONDecodeError < Error
19
+ end
16
20
  end
17
21
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2019 MongoDB Inc.
1
+ # Copyright (C) 2019-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -251,12 +251,13 @@ module BSON
251
251
  when '$date'
252
252
  case value
253
253
  when String
254
- ::Time.parse(value)
254
+ ::Time.parse(value).utc
255
255
  when Hash
256
256
  unless value.keys.sort == %w($numberLong)
257
257
  raise Error::ExtJSONParseError, "Invalid value for $date: #{value}"
258
258
  end
259
- ::Time.at(value.values.first.to_i.to_f / 1000)
259
+ sec, msec = value.values.first.to_i.divmod(1000)
260
+ ::Time.at(sec, msec*1000).utc
260
261
  else
261
262
  raise Error::ExtJSONParseError, "Invalid value for $date: #{value}"
262
263
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -108,13 +108,24 @@ module BSON
108
108
  buffer.get_hash(**options)
109
109
  else
110
110
  hash = Document.allocate
111
- buffer.get_int32 # Throw away the size.
111
+ start_position = buffer.read_position
112
+ expected_byte_size = buffer.get_int32
112
113
  while (type = buffer.get_byte) != NULL_BYTE
113
114
  field = buffer.get_cstring
114
115
  cls = BSON::Registry.get(type, field)
115
- value = cls.from_bson(buffer, **options)
116
+ value = if options.empty?
117
+ # Compatibility with the older Ruby driver versions which define
118
+ # a DBRef class with from_bson accepting a single argument.
119
+ cls.from_bson(buffer)
120
+ else
121
+ cls.from_bson(buffer, **options)
122
+ end
116
123
  hash.store(field, value)
117
124
  end
125
+ actual_byte_size = buffer.read_position - start_position
126
+ if actual_byte_size != expected_byte_size
127
+ raise Error::BSONDecodeError, "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
128
+ end
118
129
  hash
119
130
  end
120
131
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -165,7 +165,13 @@ module BSON
165
165
  #
166
166
  # @since 3.1.0
167
167
  def respond_to?(method, include_private = false)
168
- compile.respond_to?(method, include_private) || super
168
+ if defined?(@pattern)
169
+ compile.respond_to?(method, include_private) || super
170
+ else
171
+ # YAML calls #respond_to? during deserialization, before the object
172
+ # is initialized.
173
+ super
174
+ end
169
175
  end
170
176
 
171
177
  # Encode the Raw Regexp object to BSON.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
- # Copyright (C) 2009-2019 MongoDB Inc.
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -17,6 +17,20 @@ module BSON
17
17
  # Injects behaviour for encoding and decoding time values to
18
18
  # and from raw bytes as specified by the BSON spec.
19
19
  #
20
+ # @note
21
+ # Ruby time can have nanosecond precision:
22
+ # +Time.utc(2020, 1, 1, 0, 0, 0, 999_999_999/1000r)+
23
+ # +Time#usec+ returns the number of microseconds in the time, and
24
+ # if the time has nanosecond precision the sub-microsecond part is
25
+ # truncated (the value is floored to the nearest millisecond).
26
+ # MongoDB only supports millisecond precision; we truncate the
27
+ # sub-millisecond part of microseconds (floor to the nearest millisecond).
28
+ # Note that if a time is constructed from a floating point value,
29
+ # the microsecond value may round to the starting floating point value
30
+ # but due to flooring, the time after serialization may end up to
31
+ # be different than the starting floating point value.
32
+ # It is recommended that time calculations use integer math only.
33
+ #
20
34
  # @see http://bsonspec.org/#/specification
21
35
  #
22
36
  # @since 2.0.0
@@ -29,6 +43,8 @@ module BSON
29
43
 
30
44
  # Get the time as encoded BSON.
31
45
  #
46
+ # @note The time is floored to the nearest millisecond.
47
+ #
32
48
  # @example Get the time as encoded BSON.
33
49
  # Time.new(2012, 1, 1, 0, 0, 0).to_bson
34
50
  #
@@ -38,15 +54,15 @@ module BSON
38
54
  #
39
55
  # @since 2.0.0
40
56
  def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
41
- # A previous version of this method used the following implementation:
42
- # buffer.put_int64((to_i * 1000) + (usec / 1000))
43
- # Turns out, usec returned incorrect value - 999 for 1 millisecond.
44
- buffer.put_int64((to_f * 1000).round)
57
+ value = _bson_to_i * 1000 + usec.divmod(1000).first
58
+ buffer.put_int64(value)
45
59
  end
46
60
 
47
61
  # Converts this object to a representation directly serializable to
48
62
  # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
49
63
  #
64
+ # @note The time is floored to the nearest millisecond.
65
+ #
50
66
  # @option options [ true | false ] :relaxed Whether to produce relaxed
51
67
  # extended JSON representation.
52
68
  #
@@ -55,12 +71,30 @@ module BSON
55
71
  utc_time = utc
56
72
  if options[:mode] == :relaxed && (1970..9999).include?(utc_time.year)
57
73
  if utc_time.usec != 0
74
+ if utc_time.respond_to?(:floor)
75
+ # Ruby 2.7+
76
+ utc_time = utc_time.floor(3)
77
+ else
78
+ utc_time -= utc_time.usec.divmod(1000).last.to_r / 1000000
79
+ end
58
80
  {'$date' => utc_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')}
59
81
  else
60
82
  {'$date' => utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')}
61
83
  end
62
84
  else
63
- {'$date' => {'$numberLong' => (utc_time.to_f * 1000).round.to_s}}
85
+ sec = utc_time._bson_to_i
86
+ msec = utc_time.usec.divmod(1000).first
87
+ {'$date' => {'$numberLong' => (sec * 1000 + msec).to_s}}
88
+ end
89
+ end
90
+
91
+ def _bson_to_i
92
+ # Workaround for JRuby's #to_i rounding negative timestamps up
93
+ # rather than down (https://github.com/jruby/jruby/issues/6104)
94
+ if BSON::Environment.jruby?
95
+ (self - usec.to_r/1000000).to_i
96
+ else
97
+ to_i
64
98
  end
65
99
  end
66
100
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2018-2019 MongoDB Inc.
1
+ # Copyright (C) 2018-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.8.0".freeze
16
+ VERSION = "4.9.2".freeze
17
17
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2019 MongoDB Inc.
1
+ # Copyright (C) 2019-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (C) 2009-2019 MongoDB Inc.
3
+ # Copyright (C) 2009-2020 MongoDB Inc.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -50,14 +50,46 @@ describe "BSON::ExtJSON.parse" do
50
50
  end
51
51
  end
52
52
 
53
- context 'when input is a timestamp' do
53
+ context 'when input is a BSON timestamp' do
54
54
  let(:input) { {'$timestamp' => {'t' => 12345, 'i' => 42}} }
55
55
 
56
- it 'returns a timestamp object' do
56
+ it 'returns a BSON::Timestamp instance' do
57
57
  parsed.should == BSON::Timestamp.new(12345, 42)
58
58
  end
59
59
  end
60
60
 
61
+ context 'when input is an ISO time' do
62
+ let(:input) { {'$date' => '1970-01-01T00:00:04Z'} }
63
+
64
+ it 'returns a Time instance ' do
65
+ parsed.should be_a(Time)
66
+ end
67
+
68
+ it 'returns a Time instance with correct value' do
69
+ parsed.should == Time.at(4)
70
+ end
71
+
72
+ it 'returns a Time instance in UTC' do
73
+ parsed.zone.should == 'UTC'
74
+ end
75
+ end
76
+
77
+ context 'when input is a Unix timestamp' do
78
+ let(:input) { {'$date' => {'$numberLong' => '4000'}} }
79
+
80
+ it 'returns a Time instance ' do
81
+ parsed.should be_a(Time)
82
+ end
83
+
84
+ it 'returns a Time instance with correct value' do
85
+ parsed.should == Time.at(4)
86
+ end
87
+
88
+ it 'returns a Time instance in UTC' do
89
+ parsed.zone.should == 'UTC'
90
+ end
91
+ end
92
+
61
93
  context 'when input is an int32' do
62
94
  let(:input) do
63
95
  {'$numberInt' => '42'}