debian-mirror-gitlab/spec/models/concerns/manual_inverse_association_spec.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ManualInverseAssociation do
2018-03-17 18:26:18 +05:30
let(:model) do
Class.new(MergeRequest) do
belongs_to :manual_association, class_name: 'MergeRequestDiff', foreign_key: :latest_merge_request_diff_id
manual_inverse_association :manual_association, :merge_request
end
end
before do
stub_const("#{described_class}::Model", model)
end
2020-10-24 23:57:45 +05:30
let(:instance) { create(:merge_request).becomes(model) } # rubocop: disable Cop/AvoidBecomes
2018-03-17 18:26:18 +05:30
describe '.manual_inverse_association' do
context 'when the relation exists' do
before do
instance.create_merge_request_diff
instance.reload
end
it 'loads the relation' do
expect(instance.manual_association).to be_an_instance_of(MergeRequestDiff)
end
it 'does not perform extra queries after loading' do
instance.manual_association
expect { instance.manual_association.merge_request }
.not_to exceed_query_limit(0)
end
2019-03-02 22:35:43 +05:30
it 'allows reloading the relation' do
2018-03-17 18:26:18 +05:30
query_count = ActiveRecord::QueryRecorder.new do
instance.manual_association
2019-03-02 22:35:43 +05:30
instance.reload_manual_association
2018-03-17 18:26:18 +05:30
end.count
expect(query_count).to eq(2)
end
end
context 'when the relation does not return a value' do
it 'does not try to set an inverse' do
expect(instance.manual_association).to be_nil
end
end
end
end