Sha256: 1ebb46e595577aee3834dbdef42acd64adb4706543ee73c6c55870165bda75f5

Contents?: true

Size: 1.27 KB

Versions: 62

Compression:

Stored size: 1.27 KB

Contents

require 'test_helper'

module Workarea
  class StringIdTest < TestCase
    class FooModel
      include Mongoid::Document
      field :_id, type: StringId, default: -> { BSON::ObjectId.new }
    end

    def test_querying_on_id
      model = FooModel.create!
      assert(model.id.is_a?(BSON::ObjectId))
      assert_equal(model, FooModel.find(model.id))

      found = FooModel.find(model.id.to_s)
      assert_equal(model, found)
      assert(found.id.is_a?(BSON::ObjectId))

      model = FooModel.create!(id: '5b900d884907b7417f9c33d5')
      assert(model.id.is_a?(BSON::ObjectId))

      found = FooModel.find('5b900d884907b7417f9c33d5')
      assert(found.id.is_a?(BSON::ObjectId))
      assert_equal(model, found)

      found = FooModel.find(BSON::ObjectId.from_string('5b900d884907b7417f9c33d5'))
      assert(found.id.is_a?(BSON::ObjectId))
      assert_equal(model, found)

      string_model = FooModel.create!(id: 'foobar')
      assert(string_model.id.is_a?(String))

      found = FooModel.find('foobar')
      assert(found.id.is_a?(String))
      assert_equal(string_model, found)

      mixed = FooModel.in(id: ['foobar', '5b900d884907b7417f9c33d5']).to_a
      assert_equal(2, mixed.count)
      assert(mixed.include?(model))
      assert(mixed.include?(string_model))
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 test/lib/workarea/string_id_test.rb
workarea-core-3.5.26 test/lib/workarea/string_id_test.rb
workarea-core-3.4.45 test/lib/workarea/string_id_test.rb
workarea-core-3.5.25 test/lib/workarea/string_id_test.rb
workarea-core-3.5.23 test/lib/workarea/string_id_test.rb
workarea-core-3.4.44 test/lib/workarea/string_id_test.rb
workarea-core-3.5.22 test/lib/workarea/string_id_test.rb
workarea-core-3.4.43 test/lib/workarea/string_id_test.rb
workarea-core-3.5.21 test/lib/workarea/string_id_test.rb
workarea-core-3.4.42 test/lib/workarea/string_id_test.rb
workarea-core-3.5.20 test/lib/workarea/string_id_test.rb
workarea-core-3.4.41 test/lib/workarea/string_id_test.rb
workarea-core-3.5.19 test/lib/workarea/string_id_test.rb
workarea-core-3.4.40 test/lib/workarea/string_id_test.rb
workarea-core-3.5.18 test/lib/workarea/string_id_test.rb
workarea-core-3.4.39 test/lib/workarea/string_id_test.rb
workarea-core-3.5.17 test/lib/workarea/string_id_test.rb
workarea-core-3.4.38 test/lib/workarea/string_id_test.rb
workarea-core-3.5.16 test/lib/workarea/string_id_test.rb
workarea-core-3.4.37 test/lib/workarea/string_id_test.rb