bson 4.11.1 → 4.14.0
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.
- checksums.yaml +4 -4
 - checksums.yaml.gz.sig +0 -0
 - data/README.md +4 -7
 - data/ext/bson/bson-native.h +2 -0
 - data/ext/bson/init.c +9 -0
 - data/ext/bson/read.c +29 -0
 - data/lib/bson/active_support.rb +1 -0
 - data/lib/bson/array.rb +2 -1
 - data/lib/bson/big_decimal.rb +67 -0
 - data/lib/bson/binary.rb +8 -5
 - data/lib/bson/boolean.rb +2 -1
 - data/lib/bson/code.rb +2 -1
 - data/lib/bson/code_with_scope.rb +2 -1
 - data/lib/bson/config.rb +1 -0
 - data/lib/bson/date.rb +1 -0
 - data/lib/bson/date_time.rb +1 -0
 - data/lib/bson/db_pointer.rb +2 -1
 - data/lib/bson/dbref.rb +125 -0
 - data/lib/bson/decimal128/builder.rb +27 -20
 - data/lib/bson/decimal128.rb +27 -12
 - data/lib/bson/document.rb +61 -18
 - data/lib/bson/environment.rb +1 -0
 - data/lib/bson/error.rb +7 -0
 - data/lib/bson/ext_json.rb +24 -11
 - data/lib/bson/false_class.rb +2 -1
 - data/lib/bson/float.rb +21 -32
 - data/lib/bson/hash.rb +15 -6
 - data/lib/bson/int32.rb +3 -2
 - data/lib/bson/int64.rb +3 -2
 - data/lib/bson/integer.rb +3 -2
 - data/lib/bson/json.rb +1 -0
 - data/lib/bson/max_key.rb +3 -2
 - data/lib/bson/min_key.rb +3 -2
 - data/lib/bson/nil_class.rb +2 -1
 - data/lib/bson/object.rb +1 -0
 - data/lib/bson/object_id.rb +4 -3
 - data/lib/bson/open_struct.rb +1 -0
 - data/lib/bson/regexp.rb +17 -6
 - data/lib/bson/registry.rb +1 -0
 - data/lib/bson/specialized.rb +1 -0
 - data/lib/bson/string.rb +3 -2
 - data/lib/bson/symbol.rb +2 -1
 - data/lib/bson/time.rb +4 -3
 - data/lib/bson/time_with_zone.rb +1 -0
 - data/lib/bson/timestamp.rb +3 -2
 - data/lib/bson/true_class.rb +2 -1
 - data/lib/bson/undefined.rb +2 -1
 - data/lib/bson/version.rb +2 -1
 - data/lib/bson.rb +8 -5
 - data/lib/bson_native.bundle +0 -0
 - data/spec/README.md +14 -0
 - data/spec/bson/big_decimal_spec.rb +316 -0
 - data/spec/bson/binary_spec.rb +1 -1
 - data/spec/bson/binary_uuid_spec.rb +12 -0
 - data/spec/bson/dbref_spec.rb +461 -0
 - data/spec/bson/decimal128_spec.rb +16 -0
 - data/spec/bson/document_as_spec.rb +46 -0
 - data/spec/bson/document_spec.rb +43 -1
 - data/spec/bson/ext_json_parse_spec.rb +37 -0
 - data/spec/bson/hash_as_spec.rb +57 -0
 - data/spec/bson/hash_spec.rb +32 -0
 - data/spec/bson/int64_spec.rb +4 -24
 - data/spec/bson/raw_spec.rb +7 -1
 - data/spec/bson/regexp_spec.rb +52 -0
 - data/spec/runners/common_driver.rb +1 -1
 - data/spec/shared/LICENSE +20 -0
 - data/spec/shared/bin/get-mongodb-download-url +17 -0
 - data/spec/shared/bin/s3-copy +45 -0
 - data/spec/shared/bin/s3-upload +69 -0
 - data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
 - data/spec/shared/lib/mrss/cluster_config.rb +231 -0
 - data/spec/shared/lib/mrss/constraints.rb +386 -0
 - data/spec/shared/lib/mrss/docker_runner.rb +271 -0
 - data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
 - data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
 - data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
 - data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
 - data/spec/shared/lib/mrss/utils.rb +15 -0
 - data/spec/shared/share/Dockerfile.erb +338 -0
 - data/spec/shared/share/haproxy-1.conf +16 -0
 - data/spec/shared/share/haproxy-2.conf +17 -0
 - data/spec/shared/shlib/distro.sh +74 -0
 - data/spec/shared/shlib/server.sh +367 -0
 - data/spec/shared/shlib/set_env.sh +131 -0
 - data/spec/spec_helper.rb +20 -0
 - data/spec/spec_tests/common_driver_spec.rb +2 -1
 - data/spec/spec_tests/data/corpus/binary.json +33 -0
 - data/spec/spec_tests/data/corpus/dbref.json +21 -1
 - data/spec/spec_tests/data/corpus/document.json +4 -0
 - data/spec/spec_tests/data/corpus/regex.json +2 -2
 - data/spec/spec_tests/data/corpus/top.json +20 -9
 - data/spec/support/spec_config.rb +2 -1
 - data.tar.gz.sig +0 -0
 - metadata +157 -106
 - metadata.gz.sig +0 -0
 
| 
         @@ -0,0 +1,461 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
            # encoding: utf-8
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'json'
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            describe BSON::DBRef do
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              let(:object_id) do
         
     | 
| 
      
 10 
     | 
    
         
            +
                BSON::ObjectId.new
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              describe '#as_json' do
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                context 'when the database is not provided' do
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 18 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id })
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  it 'returns the json document without database' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                    expect(dbref.as_json).to eq({ '$ref' => 'users', '$id' => object_id })
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                context 'when the database is provided' do
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 29 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
         
     | 
| 
      
 30 
     | 
    
         
            +
                  end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                  it 'returns the json document with database' do
         
     | 
| 
      
 33 
     | 
    
         
            +
                    expect(dbref.as_json).to eq({
         
     | 
| 
      
 34 
     | 
    
         
            +
                      '$ref' => 'users',
         
     | 
| 
      
 35 
     | 
    
         
            +
                      '$id' => object_id,
         
     | 
| 
      
 36 
     | 
    
         
            +
                      '$db' => 'database'
         
     | 
| 
      
 37 
     | 
    
         
            +
                    })
         
     | 
| 
      
 38 
     | 
    
         
            +
                  end
         
     | 
| 
      
 39 
     | 
    
         
            +
                end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                context 'when other keys are provided' do
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 44 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
         
     | 
| 
      
 45 
     | 
    
         
            +
                  end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                  it 'returns the json document with the other keys' do
         
     | 
| 
      
 48 
     | 
    
         
            +
                    expect(dbref.as_json).to eq({
         
     | 
| 
      
 49 
     | 
    
         
            +
                      '$ref' => 'users',
         
     | 
| 
      
 50 
     | 
    
         
            +
                      '$id' => object_id,
         
     | 
| 
      
 51 
     | 
    
         
            +
                      '$db' => 'database',
         
     | 
| 
      
 52 
     | 
    
         
            +
                      'x' => 'y'
         
     | 
| 
      
 53 
     | 
    
         
            +
                    })
         
     | 
| 
      
 54 
     | 
    
         
            +
                  end
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
              end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
              describe '#initialize' do
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                let(:dbref) do
         
     | 
| 
      
 61 
     | 
    
         
            +
                  described_class.new(hash)
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                let(:hash) do
         
     | 
| 
      
 65 
     | 
    
         
            +
                  { '$ref' => 'users', '$id' => object_id }
         
     | 
| 
      
 66 
     | 
    
         
            +
                end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
                it 'sets the collection' do
         
     | 
| 
      
 69 
     | 
    
         
            +
                  expect(dbref.collection).to eq('users')
         
     | 
| 
      
 70 
     | 
    
         
            +
                end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                it 'sets the id' do
         
     | 
| 
      
 73 
     | 
    
         
            +
                  expect(dbref.id).to eq(object_id)
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                context 'when a database is provided' do
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 79 
     | 
    
         
            +
                    { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
         
     | 
| 
      
 80 
     | 
    
         
            +
                  end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                  it 'sets the database' do
         
     | 
| 
      
 83 
     | 
    
         
            +
                    expect(dbref.database).to eq('db')
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
                end
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                context 'when not providing a collection' do
         
     | 
| 
      
 88 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 89 
     | 
    
         
            +
                    { '$id' => object_id, '$db' => 'db' }
         
     | 
| 
      
 90 
     | 
    
         
            +
                  end
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 93 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 94 
     | 
    
         
            +
                      dbref
         
     | 
| 
      
 95 
     | 
    
         
            +
                    end.to raise_error(ArgumentError, /DBRef must have \$ref/)
         
     | 
| 
      
 96 
     | 
    
         
            +
                  end
         
     | 
| 
      
 97 
     | 
    
         
            +
                end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
                context 'when not providing an id' do
         
     | 
| 
      
 100 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 101 
     | 
    
         
            +
                    { '$ref' => 'users', '$db' => 'db' }
         
     | 
| 
      
 102 
     | 
    
         
            +
                  end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 105 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 106 
     | 
    
         
            +
                      dbref
         
     | 
| 
      
 107 
     | 
    
         
            +
                    end.to raise_error(ArgumentError, /DBRef must have \$id/)
         
     | 
| 
      
 108 
     | 
    
         
            +
                  end
         
     | 
| 
      
 109 
     | 
    
         
            +
                end
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                context 'when providing an invalid type for ref' do
         
     | 
| 
      
 112 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 113 
     | 
    
         
            +
                    { '$ref' => 1, '$id' => object_id }
         
     | 
| 
      
 114 
     | 
    
         
            +
                  end
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 117 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 118 
     | 
    
         
            +
                      dbref
         
     | 
| 
      
 119 
     | 
    
         
            +
                    end.to raise_error(ArgumentError, /The value for key \$ref must be a string/)
         
     | 
| 
      
 120 
     | 
    
         
            +
                  end
         
     | 
| 
      
 121 
     | 
    
         
            +
                end
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
                context 'when providing an invalid type for database' do
         
     | 
| 
      
 124 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 125 
     | 
    
         
            +
                    { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
         
     | 
| 
      
 126 
     | 
    
         
            +
                  end
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
      
 128 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 129 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 130 
     | 
    
         
            +
                      dbref
         
     | 
| 
      
 131 
     | 
    
         
            +
                    end.to raise_error(ArgumentError, /The value for key \$db must be a string/)
         
     | 
| 
      
 132 
     | 
    
         
            +
                  end
         
     | 
| 
      
 133 
     | 
    
         
            +
                end
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
      
 135 
     | 
    
         
            +
                context 'when providing the fieds as symbols' do
         
     | 
| 
      
 136 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 137 
     | 
    
         
            +
                    { :$ref => 'users', :$id => object_id, :$db => 'db' }
         
     | 
| 
      
 138 
     | 
    
         
            +
                  end
         
     | 
| 
      
 139 
     | 
    
         
            +
             
     | 
| 
      
 140 
     | 
    
         
            +
                  it 'does not raise an error' do
         
     | 
| 
      
 141 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 142 
     | 
    
         
            +
                      dbref
         
     | 
| 
      
 143 
     | 
    
         
            +
                    end.to_not raise_error
         
     | 
| 
      
 144 
     | 
    
         
            +
                  end
         
     | 
| 
      
 145 
     | 
    
         
            +
                end
         
     | 
| 
      
 146 
     | 
    
         
            +
             
     | 
| 
      
 147 
     | 
    
         
            +
                context 'when testing the ordering of the fields' do
         
     | 
| 
      
 148 
     | 
    
         
            +
                  context 'when the fields are in order' do
         
     | 
| 
      
 149 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 150 
     | 
    
         
            +
                      { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
         
     | 
| 
      
 151 
     | 
    
         
            +
                    end
         
     | 
| 
      
 152 
     | 
    
         
            +
             
     | 
| 
      
 153 
     | 
    
         
            +
                    it 'has the correct order' do
         
     | 
| 
      
 154 
     | 
    
         
            +
                      expect(dbref.keys).to eq(['$ref', '$id', '$db'])
         
     | 
| 
      
 155 
     | 
    
         
            +
                    end
         
     | 
| 
      
 156 
     | 
    
         
            +
                  end
         
     | 
| 
      
 157 
     | 
    
         
            +
             
     | 
| 
      
 158 
     | 
    
         
            +
                  context 'when the fields are out of order' do
         
     | 
| 
      
 159 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 160 
     | 
    
         
            +
                      { '$db' => 'db', '$id' => object_id, '$ref' => 'users' }
         
     | 
| 
      
 161 
     | 
    
         
            +
                    end
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                    it 'has the correct order' do
         
     | 
| 
      
 164 
     | 
    
         
            +
                      expect(dbref.keys).to eq(['$ref', '$id', '$db'])
         
     | 
| 
      
 165 
     | 
    
         
            +
                    end
         
     | 
| 
      
 166 
     | 
    
         
            +
                  end
         
     | 
| 
      
 167 
     | 
    
         
            +
             
     | 
| 
      
 168 
     | 
    
         
            +
                  context 'when there is no db' do
         
     | 
| 
      
 169 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 170 
     | 
    
         
            +
                      { '$id' => object_id, '$ref' => 'users' }
         
     | 
| 
      
 171 
     | 
    
         
            +
                    end
         
     | 
| 
      
 172 
     | 
    
         
            +
             
     | 
| 
      
 173 
     | 
    
         
            +
                    it 'has the correct order' do
         
     | 
| 
      
 174 
     | 
    
         
            +
                      expect(dbref.keys).to eq(['$ref', '$id'])
         
     | 
| 
      
 175 
     | 
    
         
            +
                    end
         
     | 
| 
      
 176 
     | 
    
         
            +
                  end
         
     | 
| 
      
 177 
     | 
    
         
            +
             
     | 
| 
      
 178 
     | 
    
         
            +
                  context 'when the there are other fields in order' do
         
     | 
| 
      
 179 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 180 
     | 
    
         
            +
                      { '$ref' => 'users', '$id' => object_id, '$db' => 'db', 'x' => 'y', 'y' => 'z' }
         
     | 
| 
      
 181 
     | 
    
         
            +
                    end
         
     | 
| 
      
 182 
     | 
    
         
            +
             
     | 
| 
      
 183 
     | 
    
         
            +
                    it 'has the correct order' do
         
     | 
| 
      
 184 
     | 
    
         
            +
                      expect(dbref.keys).to eq(['$ref', '$id', '$db', 'x', 'y'])
         
     | 
| 
      
 185 
     | 
    
         
            +
                    end
         
     | 
| 
      
 186 
     | 
    
         
            +
                  end
         
     | 
| 
      
 187 
     | 
    
         
            +
             
     | 
| 
      
 188 
     | 
    
         
            +
                  context 'when the there are other fields out of order' do
         
     | 
| 
      
 189 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 190 
     | 
    
         
            +
                      { 'y' => 'z', '$db' => 'db', '$id' => object_id, 'x' => 'y', '$ref' => 'users' }
         
     | 
| 
      
 191 
     | 
    
         
            +
                    end
         
     | 
| 
      
 192 
     | 
    
         
            +
             
     | 
| 
      
 193 
     | 
    
         
            +
                    it 'has the correct order' do
         
     | 
| 
      
 194 
     | 
    
         
            +
                      expect(dbref.keys).to eq(['$ref', '$id', '$db', 'y', 'x'])
         
     | 
| 
      
 195 
     | 
    
         
            +
                    end
         
     | 
| 
      
 196 
     | 
    
         
            +
                  end
         
     | 
| 
      
 197 
     | 
    
         
            +
                end
         
     | 
| 
      
 198 
     | 
    
         
            +
              end
         
     | 
| 
      
 199 
     | 
    
         
            +
             
     | 
| 
      
 200 
     | 
    
         
            +
              describe '#to_bson' do
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
                let(:dbref) do
         
     | 
| 
      
 203 
     | 
    
         
            +
                  described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
         
     | 
| 
      
 204 
     | 
    
         
            +
                end
         
     | 
| 
      
 205 
     | 
    
         
            +
             
     | 
| 
      
 206 
     | 
    
         
            +
                it 'converts the underlying document to bson' do
         
     | 
| 
      
 207 
     | 
    
         
            +
                  expect(dbref.to_bson.to_s).to eq(dbref.as_json.to_bson.to_s)
         
     | 
| 
      
 208 
     | 
    
         
            +
                end
         
     | 
| 
      
 209 
     | 
    
         
            +
              end
         
     | 
| 
      
 210 
     | 
    
         
            +
             
     | 
| 
      
 211 
     | 
    
         
            +
              describe '#to_json' do
         
     | 
| 
      
 212 
     | 
    
         
            +
             
     | 
| 
      
 213 
     | 
    
         
            +
                context 'when the database is not provided' do
         
     | 
| 
      
 214 
     | 
    
         
            +
             
     | 
| 
      
 215 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 216 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id })
         
     | 
| 
      
 217 
     | 
    
         
            +
                  end
         
     | 
| 
      
 218 
     | 
    
         
            +
             
     | 
| 
      
 219 
     | 
    
         
            +
                  it 'returns the json document without database' do
         
     | 
| 
      
 220 
     | 
    
         
            +
                    expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json}}")
         
     | 
| 
      
 221 
     | 
    
         
            +
                  end
         
     | 
| 
      
 222 
     | 
    
         
            +
                end
         
     | 
| 
      
 223 
     | 
    
         
            +
             
     | 
| 
      
 224 
     | 
    
         
            +
                context 'when the database is provided' do
         
     | 
| 
      
 225 
     | 
    
         
            +
             
     | 
| 
      
 226 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 227 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
         
     | 
| 
      
 228 
     | 
    
         
            +
                  end
         
     | 
| 
      
 229 
     | 
    
         
            +
             
     | 
| 
      
 230 
     | 
    
         
            +
                  it 'returns the json document with database' do
         
     | 
| 
      
 231 
     | 
    
         
            +
                    expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\"}")
         
     | 
| 
      
 232 
     | 
    
         
            +
                  end
         
     | 
| 
      
 233 
     | 
    
         
            +
                end
         
     | 
| 
      
 234 
     | 
    
         
            +
             
     | 
| 
      
 235 
     | 
    
         
            +
                context 'when other keys are provided' do
         
     | 
| 
      
 236 
     | 
    
         
            +
             
     | 
| 
      
 237 
     | 
    
         
            +
                  let(:dbref) do
         
     | 
| 
      
 238 
     | 
    
         
            +
                    described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
         
     | 
| 
      
 239 
     | 
    
         
            +
                  end
         
     | 
| 
      
 240 
     | 
    
         
            +
             
     | 
| 
      
 241 
     | 
    
         
            +
                  it 'returns the json document with the other keys' do
         
     | 
| 
      
 242 
     | 
    
         
            +
                    expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\",\"x\":\"y\"}")
         
     | 
| 
      
 243 
     | 
    
         
            +
                  end
         
     | 
| 
      
 244 
     | 
    
         
            +
                end
         
     | 
| 
      
 245 
     | 
    
         
            +
              end
         
     | 
| 
      
 246 
     | 
    
         
            +
             
     | 
| 
      
 247 
     | 
    
         
            +
              describe '#from_bson' do
         
     | 
| 
      
 248 
     | 
    
         
            +
             
     | 
| 
      
 249 
     | 
    
         
            +
                let(:buffer) do
         
     | 
| 
      
 250 
     | 
    
         
            +
                  hash.to_bson
         
     | 
| 
      
 251 
     | 
    
         
            +
                end
         
     | 
| 
      
 252 
     | 
    
         
            +
             
     | 
| 
      
 253 
     | 
    
         
            +
                let(:decoded) do
         
     | 
| 
      
 254 
     | 
    
         
            +
                  BSON::Document.from_bson(BSON::ByteBuffer.new(buffer.to_s))
         
     | 
| 
      
 255 
     | 
    
         
            +
                end
         
     | 
| 
      
 256 
     | 
    
         
            +
             
     | 
| 
      
 257 
     | 
    
         
            +
                context 'when a database exists' do
         
     | 
| 
      
 258 
     | 
    
         
            +
             
     | 
| 
      
 259 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 260 
     | 
    
         
            +
                    { '$ref' => 'users', '$id' => object_id, '$db' => 'database' }
         
     | 
| 
      
 261 
     | 
    
         
            +
                  end
         
     | 
| 
      
 262 
     | 
    
         
            +
             
     | 
| 
      
 263 
     | 
    
         
            +
                  it 'decodes the ref' do
         
     | 
| 
      
 264 
     | 
    
         
            +
                    expect(decoded.collection).to eq('users')
         
     | 
| 
      
 265 
     | 
    
         
            +
                  end
         
     | 
| 
      
 266 
     | 
    
         
            +
             
     | 
| 
      
 267 
     | 
    
         
            +
                  it 'decodes the id' do
         
     | 
| 
      
 268 
     | 
    
         
            +
                    expect(decoded.id).to eq(object_id)
         
     | 
| 
      
 269 
     | 
    
         
            +
                  end
         
     | 
| 
      
 270 
     | 
    
         
            +
             
     | 
| 
      
 271 
     | 
    
         
            +
                  it 'decodes the database' do
         
     | 
| 
      
 272 
     | 
    
         
            +
                    expect(decoded.database).to eq('database')
         
     | 
| 
      
 273 
     | 
    
         
            +
                  end
         
     | 
| 
      
 274 
     | 
    
         
            +
             
     | 
| 
      
 275 
     | 
    
         
            +
                  it 'is of class DBRef' do
         
     | 
| 
      
 276 
     | 
    
         
            +
                    expect(decoded).to be_a described_class
         
     | 
| 
      
 277 
     | 
    
         
            +
                  end
         
     | 
| 
      
 278 
     | 
    
         
            +
                end
         
     | 
| 
      
 279 
     | 
    
         
            +
             
     | 
| 
      
 280 
     | 
    
         
            +
                context 'when no database exists' do
         
     | 
| 
      
 281 
     | 
    
         
            +
             
     | 
| 
      
 282 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 283 
     | 
    
         
            +
                    { '$ref' => 'users', '$id' => object_id }
         
     | 
| 
      
 284 
     | 
    
         
            +
                  end
         
     | 
| 
      
 285 
     | 
    
         
            +
             
     | 
| 
      
 286 
     | 
    
         
            +
                  it 'decodes the ref' do
         
     | 
| 
      
 287 
     | 
    
         
            +
                    expect(decoded.collection).to eq('users')
         
     | 
| 
      
 288 
     | 
    
         
            +
                  end
         
     | 
| 
      
 289 
     | 
    
         
            +
             
     | 
| 
      
 290 
     | 
    
         
            +
                  it 'decodes the id' do
         
     | 
| 
      
 291 
     | 
    
         
            +
                    expect(decoded.id).to eq(object_id)
         
     | 
| 
      
 292 
     | 
    
         
            +
                  end
         
     | 
| 
      
 293 
     | 
    
         
            +
             
     | 
| 
      
 294 
     | 
    
         
            +
                  it 'sets the database to nil' do
         
     | 
| 
      
 295 
     | 
    
         
            +
                    expect(decoded.database).to be_nil
         
     | 
| 
      
 296 
     | 
    
         
            +
                  end
         
     | 
| 
      
 297 
     | 
    
         
            +
             
     | 
| 
      
 298 
     | 
    
         
            +
                  it 'is of class DBRef' do
         
     | 
| 
      
 299 
     | 
    
         
            +
                    expect(decoded).to be_a described_class
         
     | 
| 
      
 300 
     | 
    
         
            +
                  end
         
     | 
| 
      
 301 
     | 
    
         
            +
                end
         
     | 
| 
      
 302 
     | 
    
         
            +
             
     | 
| 
      
 303 
     | 
    
         
            +
                context 'when other keys exist' do
         
     | 
| 
      
 304 
     | 
    
         
            +
             
     | 
| 
      
 305 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 306 
     | 
    
         
            +
                    { '$ref' => 'users', '$id' => object_id, 'x' => 'y' }
         
     | 
| 
      
 307 
     | 
    
         
            +
                  end
         
     | 
| 
      
 308 
     | 
    
         
            +
             
     | 
| 
      
 309 
     | 
    
         
            +
                  it 'decodes the key' do
         
     | 
| 
      
 310 
     | 
    
         
            +
                    expect(decoded['x']).to eq('y')
         
     | 
| 
      
 311 
     | 
    
         
            +
                  end
         
     | 
| 
      
 312 
     | 
    
         
            +
             
     | 
| 
      
 313 
     | 
    
         
            +
                  it 'is of class DBRef' do
         
     | 
| 
      
 314 
     | 
    
         
            +
                    expect(decoded).to be_a described_class
         
     | 
| 
      
 315 
     | 
    
         
            +
                  end
         
     | 
| 
      
 316 
     | 
    
         
            +
                end
         
     | 
| 
      
 317 
     | 
    
         
            +
             
     | 
| 
      
 318 
     | 
    
         
            +
                context 'when it is an invalid dbref' do
         
     | 
| 
      
 319 
     | 
    
         
            +
             
     | 
| 
      
 320 
     | 
    
         
            +
                  shared_examples 'bson document' do
         
     | 
| 
      
 321 
     | 
    
         
            +
                    it 'should not raise' do
         
     | 
| 
      
 322 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 323 
     | 
    
         
            +
                        decoded
         
     | 
| 
      
 324 
     | 
    
         
            +
                      end.to_not raise_error
         
     | 
| 
      
 325 
     | 
    
         
            +
                    end
         
     | 
| 
      
 326 
     | 
    
         
            +
             
     | 
| 
      
 327 
     | 
    
         
            +
                    it 'has the correct class' do
         
     | 
| 
      
 328 
     | 
    
         
            +
                      expect(decoded).to be_a BSON::Document
         
     | 
| 
      
 329 
     | 
    
         
            +
                      expect(decoded).to_not be_a described_class
         
     | 
| 
      
 330 
     | 
    
         
            +
                    end
         
     | 
| 
      
 331 
     | 
    
         
            +
                  end
         
     | 
| 
      
 332 
     | 
    
         
            +
             
     | 
| 
      
 333 
     | 
    
         
            +
                  context 'when the hash has invalid collection type' do
         
     | 
| 
      
 334 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 335 
     | 
    
         
            +
                      { '$ref' => 1, '$id' => object_id }
         
     | 
| 
      
 336 
     | 
    
         
            +
                    end
         
     | 
| 
      
 337 
     | 
    
         
            +
                    include_examples 'bson document'
         
     | 
| 
      
 338 
     | 
    
         
            +
                  end
         
     | 
| 
      
 339 
     | 
    
         
            +
             
     | 
| 
      
 340 
     | 
    
         
            +
                  context 'when the hash has an invalid database type' do
         
     | 
| 
      
 341 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 342 
     | 
    
         
            +
                      { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
         
     | 
| 
      
 343 
     | 
    
         
            +
                    end
         
     | 
| 
      
 344 
     | 
    
         
            +
                    include_examples 'bson document'
         
     | 
| 
      
 345 
     | 
    
         
            +
                  end
         
     | 
| 
      
 346 
     | 
    
         
            +
             
     | 
| 
      
 347 
     | 
    
         
            +
                  context 'when the hash is missing a collection' do
         
     | 
| 
      
 348 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 349 
     | 
    
         
            +
                      { '$id' => object_id }
         
     | 
| 
      
 350 
     | 
    
         
            +
                    end
         
     | 
| 
      
 351 
     | 
    
         
            +
                    include_examples 'bson document'
         
     | 
| 
      
 352 
     | 
    
         
            +
                  end
         
     | 
| 
      
 353 
     | 
    
         
            +
             
     | 
| 
      
 354 
     | 
    
         
            +
                  context 'when the hash is missing an id' do
         
     | 
| 
      
 355 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 356 
     | 
    
         
            +
                      { '$ref' => 'users' }
         
     | 
| 
      
 357 
     | 
    
         
            +
                    end
         
     | 
| 
      
 358 
     | 
    
         
            +
                    include_examples 'bson document'
         
     | 
| 
      
 359 
     | 
    
         
            +
                  end
         
     | 
| 
      
 360 
     | 
    
         
            +
                end
         
     | 
| 
      
 361 
     | 
    
         
            +
             
     | 
| 
      
 362 
     | 
    
         
            +
                context 'when nesting the dbref' do
         
     | 
| 
      
 363 
     | 
    
         
            +
             
     | 
| 
      
 364 
     | 
    
         
            +
                  context 'when it is a valid dbref' do
         
     | 
| 
      
 365 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 366 
     | 
    
         
            +
                      { 'dbref' => { '$ref' => 'users', '$id' => object_id } }
         
     | 
| 
      
 367 
     | 
    
         
            +
                    end
         
     | 
| 
      
 368 
     | 
    
         
            +
             
     | 
| 
      
 369 
     | 
    
         
            +
                    it 'should not raise' do
         
     | 
| 
      
 370 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 371 
     | 
    
         
            +
                        buffer
         
     | 
| 
      
 372 
     | 
    
         
            +
                      end.to_not raise_error
         
     | 
| 
      
 373 
     | 
    
         
            +
                    end
         
     | 
| 
      
 374 
     | 
    
         
            +
             
     | 
| 
      
 375 
     | 
    
         
            +
                    it 'has the correct class' do
         
     | 
| 
      
 376 
     | 
    
         
            +
                      expect(decoded['dbref']).to be_a described_class
         
     | 
| 
      
 377 
     | 
    
         
            +
                    end
         
     | 
| 
      
 378 
     | 
    
         
            +
                  end
         
     | 
| 
      
 379 
     | 
    
         
            +
             
     | 
| 
      
 380 
     | 
    
         
            +
                  context 'when it is an invalid dbref' do
         
     | 
| 
      
 381 
     | 
    
         
            +
             
     | 
| 
      
 382 
     | 
    
         
            +
                    shared_examples 'nested bson document' do
         
     | 
| 
      
 383 
     | 
    
         
            +
                      it 'should not raise' do
         
     | 
| 
      
 384 
     | 
    
         
            +
                        expect do
         
     | 
| 
      
 385 
     | 
    
         
            +
                          decoded
         
     | 
| 
      
 386 
     | 
    
         
            +
                        end.to_not raise_error
         
     | 
| 
      
 387 
     | 
    
         
            +
                      end
         
     | 
| 
      
 388 
     | 
    
         
            +
             
     | 
| 
      
 389 
     | 
    
         
            +
                      it 'has the correct class' do
         
     | 
| 
      
 390 
     | 
    
         
            +
                        expect(decoded['dbref']).to be_a BSON::Document
         
     | 
| 
      
 391 
     | 
    
         
            +
                        expect(decoded['dbref']).to_not be_a described_class
         
     | 
| 
      
 392 
     | 
    
         
            +
                      end
         
     | 
| 
      
 393 
     | 
    
         
            +
                    end
         
     | 
| 
      
 394 
     | 
    
         
            +
             
     | 
| 
      
 395 
     | 
    
         
            +
                    context 'when the hash has invalid collection type' do
         
     | 
| 
      
 396 
     | 
    
         
            +
                      let(:hash) do
         
     | 
| 
      
 397 
     | 
    
         
            +
                        { 'dbref' => { '$ref' => 1, '$id' => object_id } }
         
     | 
| 
      
 398 
     | 
    
         
            +
                      end
         
     | 
| 
      
 399 
     | 
    
         
            +
                      include_examples 'nested bson document'
         
     | 
| 
      
 400 
     | 
    
         
            +
                    end
         
     | 
| 
      
 401 
     | 
    
         
            +
             
     | 
| 
      
 402 
     | 
    
         
            +
                    context 'when the hash has an invalid database type' do
         
     | 
| 
      
 403 
     | 
    
         
            +
                      let(:hash) do
         
     | 
| 
      
 404 
     | 
    
         
            +
                        { 'dbref' => { '$ref' => 'users', '$id' => object_id, '$db' => 1 } }
         
     | 
| 
      
 405 
     | 
    
         
            +
                      end
         
     | 
| 
      
 406 
     | 
    
         
            +
                      include_examples 'nested bson document'
         
     | 
| 
      
 407 
     | 
    
         
            +
                    end
         
     | 
| 
      
 408 
     | 
    
         
            +
             
     | 
| 
      
 409 
     | 
    
         
            +
                    context 'when the hash is missing a collection' do
         
     | 
| 
      
 410 
     | 
    
         
            +
                      let(:hash) do
         
     | 
| 
      
 411 
     | 
    
         
            +
                        { 'dbref' => { '$id' => object_id } }
         
     | 
| 
      
 412 
     | 
    
         
            +
                      end
         
     | 
| 
      
 413 
     | 
    
         
            +
                      include_examples 'nested bson document'
         
     | 
| 
      
 414 
     | 
    
         
            +
                    end
         
     | 
| 
      
 415 
     | 
    
         
            +
             
     | 
| 
      
 416 
     | 
    
         
            +
                    context 'when the hash is missing an id' do
         
     | 
| 
      
 417 
     | 
    
         
            +
                      let(:hash) do
         
     | 
| 
      
 418 
     | 
    
         
            +
                        { 'dbref' => { '$ref' => 'users' } }
         
     | 
| 
      
 419 
     | 
    
         
            +
                      end
         
     | 
| 
      
 420 
     | 
    
         
            +
                      include_examples 'nested bson document'
         
     | 
| 
      
 421 
     | 
    
         
            +
                    end
         
     | 
| 
      
 422 
     | 
    
         
            +
                  end
         
     | 
| 
      
 423 
     | 
    
         
            +
                end
         
     | 
| 
      
 424 
     | 
    
         
            +
             
     | 
| 
      
 425 
     | 
    
         
            +
                context 'when nesting a dbref inside a dbref' do
         
     | 
| 
      
 426 
     | 
    
         
            +
                  context 'when it is a valid dbref' do
         
     | 
| 
      
 427 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 428 
     | 
    
         
            +
                      { 'dbref1' => { '$ref' => 'users', '$id' => object_id, 'dbref2' => { '$ref' => 'users', '$id' => object_id } } }
         
     | 
| 
      
 429 
     | 
    
         
            +
                    end
         
     | 
| 
      
 430 
     | 
    
         
            +
             
     | 
| 
      
 431 
     | 
    
         
            +
                    it 'should not raise' do
         
     | 
| 
      
 432 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 433 
     | 
    
         
            +
                        buffer
         
     | 
| 
      
 434 
     | 
    
         
            +
                      end.to_not raise_error
         
     | 
| 
      
 435 
     | 
    
         
            +
                    end
         
     | 
| 
      
 436 
     | 
    
         
            +
             
     | 
| 
      
 437 
     | 
    
         
            +
                    it 'has the correct class' do
         
     | 
| 
      
 438 
     | 
    
         
            +
                      expect(decoded['dbref1']).to be_a described_class
         
     | 
| 
      
 439 
     | 
    
         
            +
                      expect(decoded['dbref1']['dbref2']).to be_a described_class
         
     | 
| 
      
 440 
     | 
    
         
            +
                    end
         
     | 
| 
      
 441 
     | 
    
         
            +
                  end
         
     | 
| 
      
 442 
     | 
    
         
            +
             
     | 
| 
      
 443 
     | 
    
         
            +
                  context 'when it is an invalid dbref' do
         
     | 
| 
      
 444 
     | 
    
         
            +
                    let(:hash) do
         
     | 
| 
      
 445 
     | 
    
         
            +
                      { 'dbref' => { '$ref' => 'users', '$id' => object_id, 'dbref' => { '$ref' => 1, '$id' => object_id } } }
         
     | 
| 
      
 446 
     | 
    
         
            +
                    end
         
     | 
| 
      
 447 
     | 
    
         
            +
             
     | 
| 
      
 448 
     | 
    
         
            +
                    it 'should not raise' do
         
     | 
| 
      
 449 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 450 
     | 
    
         
            +
                        decoded
         
     | 
| 
      
 451 
     | 
    
         
            +
                      end.to_not raise_error
         
     | 
| 
      
 452 
     | 
    
         
            +
                    end
         
     | 
| 
      
 453 
     | 
    
         
            +
             
     | 
| 
      
 454 
     | 
    
         
            +
                    it 'has the correct class' do
         
     | 
| 
      
 455 
     | 
    
         
            +
                      expect(decoded['dbref']).to be_a described_class
         
     | 
| 
      
 456 
     | 
    
         
            +
                      expect(decoded['dbref']['dbref']).to be_a BSON::Document
         
     | 
| 
      
 457 
     | 
    
         
            +
                    end
         
     | 
| 
      
 458 
     | 
    
         
            +
                  end
         
     | 
| 
      
 459 
     | 
    
         
            +
                end
         
     | 
| 
      
 460 
     | 
    
         
            +
              end
         
     | 
| 
      
 461 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -309,6 +309,22 @@ describe BSON::Decimal128 do 
     | 
|
| 
       309 
309 
     | 
    
         
             
                    it_behaves_like 'an initialized BSON::Decimal128'
         
     | 
| 
       310 
310 
     | 
    
         
             
                  end
         
     | 
| 
       311 
311 
     | 
    
         
             
                end
         
     | 
| 
      
 312 
     | 
    
         
            +
             
     | 
| 
      
 313 
     | 
    
         
            +
                context 'when range is exceeded' do
         
     | 
| 
      
 314 
     | 
    
         
            +
                  it 'raises InvalidRange' do
         
     | 
| 
      
 315 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 316 
     | 
    
         
            +
                      described_class.new('1e10000')
         
     | 
| 
      
 317 
     | 
    
         
            +
                    end.should raise_error(BSON::Decimal128::InvalidRange, /Value out of range/)
         
     | 
| 
      
 318 
     | 
    
         
            +
                  end
         
     | 
| 
      
 319 
     | 
    
         
            +
                end
         
     | 
| 
      
 320 
     | 
    
         
            +
             
     | 
| 
      
 321 
     | 
    
         
            +
                context 'when precision is exceeded' do
         
     | 
| 
      
 322 
     | 
    
         
            +
                  it 'raises UnrepresentablePrecision' do
         
     | 
| 
      
 323 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 324 
     | 
    
         
            +
                      described_class.new('1.000000000000000000000000000000000000000000000000001')
         
     | 
| 
      
 325 
     | 
    
         
            +
                    end.should raise_error(BSON::Decimal128::UnrepresentablePrecision, /The value contains too much precision/)
         
     | 
| 
      
 326 
     | 
    
         
            +
                  end
         
     | 
| 
      
 327 
     | 
    
         
            +
                end
         
     | 
| 
       312 
328 
     | 
    
         
             
              end
         
     | 
| 
       313 
329 
     | 
    
         | 
| 
       314 
330 
     | 
    
         
             
              context 'when deserializing' do
         
     | 
| 
         @@ -0,0 +1,46 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Copyright (C) 2021 MongoDB 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 
     | 
    
         
            +
            # BSON::Document ActiveSupport extensions
         
     | 
| 
      
 18 
     | 
    
         
            +
            describe BSON::Document do
         
     | 
| 
      
 19 
     | 
    
         
            +
              require_active_support
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              describe '#symbolize_keys' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                context 'string keys' do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  let(:doc) do
         
     | 
| 
      
 24 
     | 
    
         
            +
                    described_class.new('foo' => 'bar')
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  it 'works correctly' do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    doc.symbolize_keys.should == {foo: 'bar'}
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
              end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
              describe '#symbolize_keys!' do
         
     | 
| 
      
 34 
     | 
    
         
            +
                context 'string keys' do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  let(:doc) do
         
     | 
| 
      
 36 
     | 
    
         
            +
                    described_class.new('foo' => 'bar')
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  it 'raises ArgumentError' do
         
     | 
| 
      
 40 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 41 
     | 
    
         
            +
                      doc.symbolize_keys!
         
     | 
| 
      
 42 
     | 
    
         
            +
                    end.should raise_error(ArgumentError, /symbolize_keys! is not supported on BSON::Document instances/)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
              end
         
     | 
| 
      
 46 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/bson/document_spec.rb
    CHANGED
    
    | 
         @@ -215,6 +215,10 @@ describe BSON::Document do 
     | 
|
| 
       215 
215 
     | 
    
         | 
| 
       216 
216 
     | 
    
         
             
                  context "when provided string keys" do
         
     | 
| 
       217 
217 
     | 
    
         | 
| 
      
 218 
     | 
    
         
            +
                    it "is a BSON Document" do
         
     | 
| 
      
 219 
     | 
    
         
            +
                      expect(document.slice("key1")).to be_a(BSON::Document)
         
     | 
| 
      
 220 
     | 
    
         
            +
                    end
         
     | 
| 
      
 221 
     | 
    
         
            +
             
     | 
| 
       218 
222 
     | 
    
         
             
                    it "returns the partial document" do
         
     | 
| 
       219 
223 
     | 
    
         
             
                      expect(document.slice("key1")).to contain_exactly(['key1', 'value1'])
         
     | 
| 
       220 
224 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -222,13 +226,45 @@ describe BSON::Document do 
     | 
|
| 
       222 
226 
     | 
    
         | 
| 
       223 
227 
     | 
    
         
             
                  context "when provided symbol keys" do
         
     | 
| 
       224 
228 
     | 
    
         | 
| 
      
 229 
     | 
    
         
            +
                    it "is a BSON Document" do
         
     | 
| 
      
 230 
     | 
    
         
            +
                      expect(document.slice(:key1)).to be_a(BSON::Document)
         
     | 
| 
      
 231 
     | 
    
         
            +
                    end
         
     | 
| 
      
 232 
     | 
    
         
            +
             
     | 
| 
       225 
233 
     | 
    
         
             
                    it "returns the partial document" do
         
     | 
| 
       226 
234 
     | 
    
         
             
                      expect(document.slice(:key1)).to contain_exactly(['key1', 'value1'])
         
     | 
| 
       227 
235 
     | 
    
         
             
                    end
         
     | 
| 
       228 
236 
     | 
    
         
             
                  end
         
     | 
| 
      
 237 
     | 
    
         
            +
             
     | 
| 
      
 238 
     | 
    
         
            +
                  context "when provided keys that do not exist in the document" do
         
     | 
| 
      
 239 
     | 
    
         
            +
             
     | 
| 
      
 240 
     | 
    
         
            +
                    it "returns only the keys that exist in the document" do
         
     | 
| 
      
 241 
     | 
    
         
            +
                      expect(document.slice(:key1, :key3)).to contain_exactly(['key1', 'value1'])
         
     | 
| 
      
 242 
     | 
    
         
            +
                    end
         
     | 
| 
      
 243 
     | 
    
         
            +
                  end
         
     | 
| 
      
 244 
     | 
    
         
            +
                end
         
     | 
| 
      
 245 
     | 
    
         
            +
              end
         
     | 
| 
      
 246 
     | 
    
         
            +
             
     | 
| 
      
 247 
     | 
    
         
            +
              describe "#except" do
         
     | 
| 
      
 248 
     | 
    
         
            +
                let(:document) do
         
     | 
| 
      
 249 
     | 
    
         
            +
                  described_class.new("key1" => "value1", key2: "value2")
         
     | 
| 
      
 250 
     | 
    
         
            +
                end
         
     | 
| 
      
 251 
     | 
    
         
            +
             
     | 
| 
      
 252 
     | 
    
         
            +
                context "when provided string keys" do
         
     | 
| 
      
 253 
     | 
    
         
            +
             
     | 
| 
      
 254 
     | 
    
         
            +
                  it "returns the partial document" do
         
     | 
| 
      
 255 
     | 
    
         
            +
                    expect(document.except("key1")).to contain_exactly(['key2', 'value2'])
         
     | 
| 
      
 256 
     | 
    
         
            +
                  end
         
     | 
| 
      
 257 
     | 
    
         
            +
                end
         
     | 
| 
      
 258 
     | 
    
         
            +
             
     | 
| 
      
 259 
     | 
    
         
            +
                context "when provided symbol keys" do
         
     | 
| 
      
 260 
     | 
    
         
            +
             
     | 
| 
      
 261 
     | 
    
         
            +
                  it "returns the partial document" do
         
     | 
| 
      
 262 
     | 
    
         
            +
                    expect(document.except(:key1)).to contain_exactly(['key2', 'value2'])
         
     | 
| 
      
 263 
     | 
    
         
            +
                  end
         
     | 
| 
       229 
264 
     | 
    
         
             
                end
         
     | 
| 
       230 
265 
     | 
    
         
             
              end
         
     | 
| 
       231 
266 
     | 
    
         | 
| 
      
 267 
     | 
    
         
            +
             
     | 
| 
       232 
268 
     | 
    
         
             
              describe "#delete" do
         
     | 
| 
       233 
269 
     | 
    
         | 
| 
       234 
270 
     | 
    
         
             
                shared_examples_for "a document with deletable pairs" do
         
     | 
| 
         @@ -484,7 +520,13 @@ describe BSON::Document do 
     | 
|
| 
       484 
520 
     | 
    
         
             
                  context "when the document has been serialized" do
         
     | 
| 
       485 
521 
     | 
    
         | 
| 
       486 
522 
     | 
    
         
             
                    let(:deserialized) do
         
     | 
| 
       487 
     | 
    
         
            -
                      YAML. 
     | 
| 
      
 523 
     | 
    
         
            +
                      if YAML.respond_to?(:unsafe_load)
         
     | 
| 
      
 524 
     | 
    
         
            +
                        # In psych >= 4.0.0 `load` is basically an alias to `safe_load`,
         
     | 
| 
      
 525 
     | 
    
         
            +
                        # which will fail here.
         
     | 
| 
      
 526 
     | 
    
         
            +
                        YAML.unsafe_load(YAML.dump(doc))
         
     | 
| 
      
 527 
     | 
    
         
            +
                      else
         
     | 
| 
      
 528 
     | 
    
         
            +
                        YAML.load(YAML.dump(doc))
         
     | 
| 
      
 529 
     | 
    
         
            +
                      end
         
     | 
| 
       488 
530 
     | 
    
         
             
                    end
         
     | 
| 
       489 
531 
     | 
    
         | 
| 
       490 
532 
     | 
    
         
             
                    let!(:enum) do
         
     | 
| 
         @@ -148,6 +148,7 @@ describe "BSON::ExtJSON.parse" do 
     | 
|
| 
       148 
148 
     | 
    
         
             
                end
         
     | 
| 
       149 
149 
     | 
    
         | 
| 
       150 
150 
     | 
    
         
             
                let(:parsed) { BSON::ExtJSON.parse_obj(input, mode: mode) }
         
     | 
| 
      
 151 
     | 
    
         
            +
                let(:mode) { :bson }
         
     | 
| 
       151 
152 
     | 
    
         | 
| 
       152 
153 
     | 
    
         
             
                context 'when mode is invalid' do
         
     | 
| 
       153 
154 
     | 
    
         
             
                  let(:mode) { :foo }
         
     | 
| 
         @@ -158,6 +159,42 @@ describe "BSON::ExtJSON.parse" do 
     | 
|
| 
       158 
159 
     | 
    
         
             
                    end.should raise_error(ArgumentError, /Invalid value for :mode option/)
         
     | 
| 
       159 
160 
     | 
    
         
             
                  end
         
     | 
| 
       160 
161 
     | 
    
         
             
                end
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                context 'when it contains a string key with a null byte' do
         
     | 
| 
      
 164 
     | 
    
         
            +
                  let(:input) do
         
     | 
| 
      
 165 
     | 
    
         
            +
                    { "key\x00" => 1 }
         
     | 
| 
      
 166 
     | 
    
         
            +
                  end
         
     | 
| 
      
 167 
     | 
    
         
            +
             
     | 
| 
      
 168 
     | 
    
         
            +
                  it 'raises an exception' do
         
     | 
| 
      
 169 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 170 
     | 
    
         
            +
                      parsed
         
     | 
| 
      
 171 
     | 
    
         
            +
                    end.should raise_error(BSON::Error::ExtJSONParseError, /Hash key cannot contain a null byte/)
         
     | 
| 
      
 172 
     | 
    
         
            +
                  end
         
     | 
| 
      
 173 
     | 
    
         
            +
                end
         
     | 
| 
      
 174 
     | 
    
         
            +
             
     | 
| 
      
 175 
     | 
    
         
            +
                context 'when it contains a symbol key with a null byte' do
         
     | 
| 
      
 176 
     | 
    
         
            +
                  let(:input) do
         
     | 
| 
      
 177 
     | 
    
         
            +
                    { "key\x00".to_sym => 1 }
         
     | 
| 
      
 178 
     | 
    
         
            +
                  end
         
     | 
| 
      
 179 
     | 
    
         
            +
             
     | 
| 
      
 180 
     | 
    
         
            +
                  it 'raises an exception' do
         
     | 
| 
      
 181 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 182 
     | 
    
         
            +
                      parsed
         
     | 
| 
      
 183 
     | 
    
         
            +
                    end.should raise_error(BSON::Error::ExtJSONParseError, /Hash key cannot contain a null byte/)
         
     | 
| 
      
 184 
     | 
    
         
            +
                  end
         
     | 
| 
      
 185 
     | 
    
         
            +
                end
         
     | 
| 
      
 186 
     | 
    
         
            +
             
     | 
| 
      
 187 
     | 
    
         
            +
                context 'when it contains an integer key' do
         
     | 
| 
      
 188 
     | 
    
         
            +
                  let(:input) do
         
     | 
| 
      
 189 
     | 
    
         
            +
                    { 0 => 1 }
         
     | 
| 
      
 190 
     | 
    
         
            +
                  end
         
     | 
| 
      
 191 
     | 
    
         
            +
             
     | 
| 
      
 192 
     | 
    
         
            +
                  it 'does not raises an exception' do
         
     | 
| 
      
 193 
     | 
    
         
            +
                    lambda do
         
     | 
| 
      
 194 
     | 
    
         
            +
                      parsed
         
     | 
| 
      
 195 
     | 
    
         
            +
                    end.should_not raise_error
         
     | 
| 
      
 196 
     | 
    
         
            +
                  end
         
     | 
| 
      
 197 
     | 
    
         
            +
                end
         
     | 
| 
       161 
198 
     | 
    
         
             
              end
         
     | 
| 
       162 
199 
     | 
    
         | 
| 
       163 
200 
     | 
    
         
             
              context 'when input is a binary' do
         
     | 
| 
         @@ -0,0 +1,57 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Copyright (C) 2021 MongoDB 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 'Hash ActiveSupport extensions' do
         
     | 
| 
      
 18 
     | 
    
         
            +
              require_active_support
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              describe '#symbolize_keys' do
         
     | 
| 
      
 21 
     | 
    
         
            +
                let(:symbolized) { hash.symbolize_keys }
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                shared_examples 'works correctly' do
         
     | 
| 
      
 24 
     | 
    
         
            +
                  it 'returns a hash' do
         
     | 
| 
      
 25 
     | 
    
         
            +
                    symbolized.class.should be Hash
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  it 'works correctly' do
         
     | 
| 
      
 29 
     | 
    
         
            +
                    hash.symbolize_keys.should == {foo: 'bar'}
         
     | 
| 
      
 30 
     | 
    
         
            +
                  end
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                context 'string keys' do
         
     | 
| 
      
 34 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 35 
     | 
    
         
            +
                    {'foo' => 'bar'}
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  include_examples 'works correctly'
         
     | 
| 
      
 39 
     | 
    
         
            +
                end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                context 'symbol keys' do
         
     | 
| 
      
 42 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 43 
     | 
    
         
            +
                    {foo: 'bar'}
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  include_examples 'works correctly'
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                context 'both string and symbol keys' do
         
     | 
| 
      
 50 
     | 
    
         
            +
                  let(:hash) do
         
     | 
| 
      
 51 
     | 
    
         
            +
                    {'foo' => 42, foo: 'bar'}
         
     | 
| 
      
 52 
     | 
    
         
            +
                  end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                  include_examples 'works correctly'
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
              end
         
     | 
| 
      
 57 
     | 
    
         
            +
            end
         
     |