Sha256: 6d2637e4df3a73eee3ad7d07c85a81b5724f4eb1f097aadf71f3405c44706e08

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

# Copyright (C) 2009-2014 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "spec_helper"

describe Hash do

  describe "#to_bson/#from_bson" do

    let(:type) { 3.chr }

    it_behaves_like "a bson element"

    context "when the hash is a single level" do

      let(:obj) do
        { "key" => "value" }
      end

      let(:bson) do
        "#{20.to_bson.to_s}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
        "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
      end

      it_behaves_like "a serializable bson element"
      it_behaves_like "a deserializable bson element"
    end

    context "when the hash has non-string keys" do

      let(:obj) do
        { 1 => "value" }
      end

      let(:expected) do
        { "1" => "value" }
      end

      it "properly converts to bson" do
        expect(BSON::Document.from_bson(BSON::ByteBuffer.new(obj.to_bson.to_s))).to eq(expected)
      end
    end

    context "when the hash is embedded" do

      let(:obj) do
        { "field" => { "key" => "value" }}
      end

      let(:bson) do
        "#{32.to_bson.to_s}#{Hash::BSON_TYPE}field#{BSON::NULL_BYTE}" +
        "#{20.to_bson.to_s}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
        "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
      end

      it_behaves_like "a serializable bson element"
      it_behaves_like "a deserializable bson element"
    end
  end
end

Version data entries

12 entries across 11 versions & 2 rubygems

Version Path
bson-4.0.4-java spec/bson/hash_spec.rb
bson-4.0.4 spec/bson/hash_spec.rb
bson-4.0.3-java spec/bson/hash_spec.rb
bson-4.0.3 spec/bson/hash_spec.rb
bson-4.0.2-java spec/bson/hash_spec.rb
bson-4.0.2 spec/bson/hash_spec.rb
bson-4.0.1-java spec/bson/hash_spec.rb
bson-4.0.1 spec/bson/hash_spec.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/bson-4.0.0/spec/bson/hash_spec.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/bson-4.0.0/spec/bson/hash_spec.rb
bson-4.0.0 spec/bson/hash_spec.rb
bson-4.0.0-java spec/bson/hash_spec.rb