Commit 31f7cd05 authored by Alex Kalderimis's avatar Alex Kalderimis

Ensure note factories are consistent

Notes validate that note.noteable.project and note.project are the same
object. This change allows factories to produce such notes, regardless
of whether you pass `noteable` or `project`, without infinite loops.
parent 57a165b4
......@@ -8,7 +8,17 @@ FactoryBot.modify do
end
trait :on_design do
noteable { create(:design, :with_file, issue: create(:issue, project: project)) }
transient do
issue { create(:issue, project: project) }
end
noteable { create(:design, :with_file, issue: issue) }
after(:build) do |note|
next if note.project == note.noteable.project
# note validations require consistency between these two objects
note.project = note.noteable.project
end
end
trait :with_review do
......
......@@ -77,7 +77,6 @@ describe Notify do
let_it_be(:note) do
create(:diff_note_on_design,
noteable: design,
project: design.project,
note: "Hello #{recipient.to_reference}")
end
......
......@@ -398,20 +398,20 @@ describe DesignManagement::Design do
# Note: Cache invalidation tests are in `design_user_notes_count_service_spec.rb`
it 'returns a count of user-generated notes' do
create(:diff_note_on_design, noteable: design, project: design.project)
create(:diff_note_on_design, noteable: design)
is_expected.to eq(1)
end
it 'does not count notes on other designs' do
second_design = create(:design, :with_file)
create(:diff_note_on_design, noteable: second_design, project: second_design.project)
create(:diff_note_on_design, noteable: second_design)
is_expected.to eq(0)
end
it 'does not count system notes' do
create(:diff_note_on_design, system: true, noteable: design, project: design.project)
create(:diff_note_on_design, system: true, noteable: design)
is_expected.to eq(0)
end
......
......@@ -12,7 +12,7 @@ describe DiffNote do
context 'diff files' do
let(:design) { create(:design, :with_file, versions_count: 2) }
let(:diff_note) { create(:diff_note_on_design, noteable: design, project: design.project) }
let(:diff_note) { create(:diff_note_on_design, noteable: design) }
describe '#latest_diff_file' do
it 'does not return a diff file' do
......
......@@ -90,8 +90,7 @@ describe Event do
context 'the event refers to a design on a confidential issue' do
let(:project) { create(:project, :public) }
let(:issue) { create(:issue, :confidential, project: project) }
let(:design) { create(:design, issue: issue) }
let(:note) { create(:note, :on_design, noteable: design) }
let(:note) { create(:note, :on_design, issue: issue) }
let(:event) { create(:event, project: project, target: note) }
let(:assignees) do
......
......@@ -318,7 +318,7 @@ describe 'Getting designs related to an issue' do
end
describe 'a design with note annotations' do
let_it_be(:note) { create(:diff_note_on_design, noteable: design, project: design.project) }
let_it_be(:note) { create(:diff_note_on_design, noteable: design) }
let(:design_query) do
<<~NODE
......
......@@ -11,7 +11,7 @@ describe DesignManagement::DesignUserNotesCountService, :use_clean_rails_memory_
describe '#count' do
it 'returns the count of notes' do
create_list(:diff_note_on_design, 3, noteable: design, project: design.project)
create_list(:diff_note_on_design, 3, noteable: design)
expect(subject.count).to eq(3)
end
......@@ -33,7 +33,7 @@ describe DesignManagement::DesignUserNotesCountService, :use_clean_rails_memory_
end
it 'changes when a note is destroyed' do
note = create(:diff_note_on_design, noteable: design, project: design.project)
note = create(:diff_note_on_design, noteable: design)
expect do
Notes::DestroyService.new(note.project, note.author).execute(note)
......
......@@ -11,7 +11,7 @@ describe Notes::PostProcessService do
subject { described_class.new(note).execute }
def create_note(in_reply_to: nil)
create(:diff_note_on_design, noteable: noteable, project: noteable.project, in_reply_to: in_reply_to)
create(:diff_note_on_design, noteable: noteable, in_reply_to: in_reply_to)
end
context 'when the note is the start of a new discussion' do
......
......@@ -19,7 +19,6 @@ describe EE::NotificationService, :mailer do
let_it_be(:note) do
create(:diff_note_on_design,
noteable: design,
project: project,
note: "Hello #{dev.to_reference}, G'day #{stranger.to_reference}")
end
......
......@@ -132,7 +132,7 @@ describe EE::SystemNotes::DesignManagementService do
let(:design) { create(:design, :with_file, issue: issue) }
let(:author) { create(:user) }
let(:discussion_note) do
create(:diff_note_on_design, noteable: design, author: author, project: project)
create(:diff_note_on_design, noteable: design, author: author)
end
let(:action) { 'designs_discussion_added' }
......
......@@ -341,7 +341,6 @@ describe TodoService do
let(:note) do
build(:diff_note_on_design,
project: project,
noteable: design,
author: author,
note: "Hey #{john_doe.to_reference}")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment