Sha256: 90de63bf8a2ce79b73f8e51c7ce3c552057ce8d186ba8f8058d9e5cc5627f825

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "spec_helper"
require_relative './has_many_models'

describe Mongoid::Association::Referenced::BelongsTo do
  context 'when projecting with #only' do
    before do
      school = HmmSchool.create!(district: 'foo', team: 'Bulldogs')
      HmmStudent.create!(school: school, name: 'Dave', grade: 10)
    end

    let(:student) do
      HmmStudent.where(name: 'Dave').only(:school_id, 'school._id', 'school.district').first
    end

    let(:school) { student.school }

    it 'populates specified fields only' do
      pending 'https://jira.mongodb.org/browse/MONGOID-4704'

      expect(school.district).to eq('foo')
      # has a default value specified in the model
      expect do
        school.team
      end.to raise_error(ActiveModel::MissingAttributeError)
      expect(student.attributes.keys).to eq(['_id', 'name'])
    end

    # Delete this test when https://jira.mongodb.org/browse/MONGOID-4704 is
    # implemented and above test is unpended
    it 'fetches all fields' do
      expect(school.district).to eq('foo')
      expect(school.team).to eq('Bulldogs')
    end
  end

  context 'when projecting with #only while having similar inverse_of candidates' do
    before do
      alice = HmmOwner.create!(name: 'Alice')
      bob = HmmOwner.create!(name: 'Bob')

      HmmPet.create!(name: 'Rex', current_owner: bob, previous_owner: alice)
    end

    let(:pet) { HmmPet.where(name: 'Rex').only(:name, :previous_owner_id, 'previous_owner.name').first }

    it 'populates specified fields' do
      expect(pet.name).to eq('Rex')
      expect(pet.previous_owner.name).to eq('Alice')
    end

    it 'does not try to load the inverse for an association that explicitly prevents it' do
      expect { pet.previous_owner.name }.not_to raise_error
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-7.3.5 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.3.4 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.6 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.3.3 spec/mongoid/association/referenced/belongs_to_query_spec.rb