Commit 5f4ed797 authored by Markus Koller's avatar Markus Koller

Tweak some factories to support stubbing

- Use `association()` instead of `create()` in association blocks,
  to avoid enforcing the builder strategy.
- Pass on `project` in association blocks instead of `after` callbacks.
- Add `has_internal_id` trait to generate IIDs on stubbed models.
- Add `ProjectWiki#id` accessor.
- Rename `discussion_note_on_snippet` to `*_project_snippet`, for
  consistency with the `note_*` factories.
- Add `#sha` on stubbed commits in `RepoHelpers`.
parent eb021719
......@@ -39,6 +39,7 @@ class ProjectWiki
def full_path
@project.full_path + '.wiki'
end
alias_method :id, :full_path
# @deprecated use full_path when you need it for an URL route or disk_path when you want to point to the filesystem
alias_method :path_with_namespace, :full_path
......
# frozen_string_literal: true
FactoryBot.define do
factory :epic do
factory :epic, traits: [:has_internal_id] do
title { generate(:title) }
group
author
......
......@@ -3,19 +3,19 @@
FactoryBot.modify do
factory :note do
trait :on_epic do
noteable { create(:epic) }
noteable { association(:epic) }
project { nil }
end
trait :on_vulnerability do
noteable { create(:vulnerability) }
noteable { association(:vulnerability, project: project) }
end
trait :on_design do
transient do
issue { create(:issue, project: project) }
issue { association(:issue, project: project) }
end
noteable { create(:design, :with_file, issue: issue) }
noteable { association(:design, :with_file, issue: issue) }
after(:build) do |note|
next if note.project == note.noteable.project
......
......@@ -27,9 +27,11 @@ describe 'Epic Issues', :js do
]
end
def visit_epic
before do
stub_licensed_features(epics: true, subepics: true)
end
def visit_epic
sign_in(user)
visit group_epic_path(group, epic)
......
......@@ -29,6 +29,7 @@ FactoryBot.define do
end
pipeline factory: :ci_pipeline
project { pipeline.project }
trait :degenerated do
options { nil }
......@@ -220,10 +221,6 @@ FactoryBot.define do
end
end
after(:build) do |build, evaluator|
build.project ||= build.pipeline.project
end
trait :with_deployment do
after(:build) do |build, evaluator|
##
......
......@@ -6,4 +6,10 @@ FactoryBot.define do
usage { :issues }
last_value { project.issues.maximum(:iid) || 0 }
end
trait :has_internal_id do
after(:stub) do |record|
record.iid ||= generate(:iid)
end
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :issue do
factory :issue, traits: [:has_internal_id] do
title { generate(:title) }
project
author { project.creator }
......
# frozen_string_literal: true
FactoryBot.define do
factory :merge_request do
factory :merge_request, traits: [:has_internal_id] do
title { generate(:title) }
association :source_project, :repository, factory: :project
target_project { source_project }
......
# frozen_string_literal: true
FactoryBot.define do
factory :milestone do
factory :milestone, traits: [:has_internal_id] do
title
transient do
......
......@@ -40,7 +40,7 @@ FactoryBot.define do
factory :discussion_note_on_personal_snippet, traits: [:on_personal_snippet], class: 'DiscussionNote'
factory :discussion_note_on_snippet, traits: [:on_snippet], class: 'DiscussionNote'
factory :discussion_note_on_project_snippet, traits: [:on_project_snippet], class: 'DiscussionNote'
factory :legacy_diff_note_on_commit, traits: [:on_commit, :legacy_diff_note], class: 'LegacyDiffNote'
......@@ -120,23 +120,19 @@ FactoryBot.define do
end
trait :on_issue do
noteable { create(:issue, project: project) }
end
trait :on_snippet do
noteable { create(:snippet, project: project) }
noteable { association(:issue, project: project) }
end
trait :on_merge_request do
noteable { create(:merge_request, source_project: project) }
noteable { association(:merge_request, source_project: project) }
end
trait :on_project_snippet do
noteable { create(:project_snippet, project: project) }
noteable { association(:project_snippet, project: project) }
end
trait :on_personal_snippet do
noteable { create(:personal_snippet) }
noteable { association(:personal_snippet) }
project { nil }
end
......
......@@ -11,4 +11,5 @@ FactoryBot.define do
sequence(:label_title) { |n| "label#{n}" }
sequence(:branch) { |n| "my-branch-#{n}" }
sequence(:past_time) { |n| 4.hours.ago + (2 * n).seconds }
sequence(:iid)
end
......@@ -27,7 +27,7 @@ describe SnippetsController, '(JavaScript fixtures)', type: :controller do
end
it 'snippets/show.html' do
create(:discussion_note_on_snippet, noteable: snippet, project: project, author: admin, note: '- [ ] Task List Item')
create(:discussion_note_on_project_snippet, noteable: snippet, project: project, author: admin, note: '- [ ] Task List Item')
get(:show, params: { id: snippet.to_param })
......
......@@ -181,7 +181,7 @@ describe EventsHelper do
end
it 'returns a project snippet note url' do
event.target = create(:note, :on_snippet, note: 'keep going')
event.target = create(:note_on_project_snippet, note: 'keep going')
expect(subject).to eq("#{project_base_url}/snippets/#{event.note_target.id}#note_#{event.target.id}")
end
......
......@@ -3,11 +3,11 @@
require 'spec_helper'
describe Gitlab::Gfm::ReferenceRewriter do
let(:group) { create(:group) }
let(:old_project) { create(:project, name: 'old-project', group: group) }
let(:new_project) { create(:project, name: 'new-project', group: group) }
let(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let(:new_project) { create(:project, name: 'new-project', group: group) }
let(:old_project) { create(:project, name: 'old-project', group: group) }
let(:old_project_ref) { old_project.to_reference_base(new_project) }
let(:text) { 'some text' }
......
......@@ -31,7 +31,7 @@ describe API::Discussions do
context 'when noteable is a Snippet' do
let!(:snippet) { create(:project_snippet, project: project, author: user) }
let!(:snippet_note) { create(:discussion_note_on_snippet, noteable: snippet, project: project, author: user) }
let!(:snippet_note) { create(:discussion_note_on_project_snippet, noteable: snippet, project: project, author: user) }
it_behaves_like 'discussions API', 'projects', 'snippets', 'id' do
let(:parent) { project }
......
......@@ -33,6 +33,7 @@ eos
def sample_commit
OpenStruct.new(
id: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
sha: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
parent_id: '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9',
author_full_name: "Dmitriy Zaporozhets",
author_email: "dmitriy.zaporozhets@gmail.com",
......@@ -50,6 +51,7 @@ eos
def another_sample_commit
OpenStruct.new(
id: "e56497bb5f03a90a51293fc6d516788730953899",
sha: "e56497bb5f03a90a51293fc6d516788730953899",
parent_id: '4cd80ccab63c82b4bad16faa5193fbd2aa06df40',
author_full_name: "Sytse Sijbrandij",
author_email: "sytse@gitlab.com",
......@@ -69,6 +71,7 @@ eos
def sample_big_commit
OpenStruct.new(
id: "913c66a37b4a45b9769037c55c2d238bd0942d2e",
sha: "913c66a37b4a45b9769037c55c2d238bd0942d2e",
author_full_name: "Dmitriy Zaporozhets",
author_email: "dmitriy.zaporozhets@gmail.com",
message: <<eos
......@@ -81,6 +84,7 @@ eos
def sample_image_commit
OpenStruct.new(
id: "2f63565e7aac07bcdadb654e253078b727143ec4",
sha: "2f63565e7aac07bcdadb654e253078b727143ec4",
author_full_name: "Dmitriy Zaporozhets",
author_email: "dmitriy.zaporozhets@gmail.com",
old_blob_id: '33f3729a45c02fc67d00adb1b8bca394b0e761d9',
......
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