2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe 'factories' do
|
2021-03-08 18:12:59 +05:30
|
|
|
include Database::DatabaseHelpers
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
# https://gitlab.com/groups/gitlab-org/-/epics/5464 tracks the remaining
|
|
|
|
# skipped traits.
|
|
|
|
#
|
|
|
|
# Consider adding a code comment if a trait cannot produce a valid object.
|
|
|
|
def skipped_traits
|
|
|
|
[
|
|
|
|
[:audit_event, :unauthenticated],
|
|
|
|
[:ci_build_trace_chunk, :fog_with_data],
|
|
|
|
[:ci_job_artifact, :remote_store],
|
|
|
|
[:ci_job_artifact, :raw],
|
|
|
|
[:ci_job_artifact, :gzip],
|
|
|
|
[:ci_job_artifact, :correct_checksum],
|
|
|
|
[:environment, :non_playable],
|
|
|
|
[:composer_cache_file, :object_storage],
|
|
|
|
[:debian_project_component_file, :object_storage],
|
|
|
|
[:debian_project_distribution, :object_storage],
|
|
|
|
[:debian_file_metadatum, :unknown],
|
|
|
|
[:package_file, :object_storage],
|
|
|
|
[:pages_domain, :without_certificate],
|
|
|
|
[:pages_domain, :without_key],
|
|
|
|
[:pages_domain, :with_missing_chain],
|
|
|
|
[:pages_domain, :with_trusted_chain],
|
|
|
|
[:pages_domain, :with_trusted_expired_chain],
|
|
|
|
[:pages_domain, :explicit_ecdsa],
|
|
|
|
[:project_member, :blocked],
|
|
|
|
[:remote_mirror, :ssh],
|
2021-04-29 21:17:54 +05:30
|
|
|
[:user_preference, :only_comments],
|
|
|
|
[:ci_pipeline_artifact, :remote_store]
|
2021-04-17 20:07:23 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
shared_examples 'factory' do |factory|
|
2016-06-02 11:05:42 +05:30
|
|
|
describe "#{factory.name} factory" do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not raise error when built' do
|
|
|
|
expect { build(factory.name) }.not_to raise_error
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
it 'does not raise error when created' do
|
2021-04-29 21:17:54 +05:30
|
|
|
expect { create(factory.name) }.not_to raise_error # rubocop:disable Rails/SaveBang
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe "linting :#{trait_name} trait" do
|
|
|
|
it 'does not raise error when created' do
|
|
|
|
pending("Trait skipped linting due to legacy error") if skipped_traits.include?([factory.name, trait_name.to_sym])
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect { create(factory.name, trait_name) }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
# FactoryDefault speed up specs by creating associations only once
|
|
|
|
# and reuse them in other factories.
|
|
|
|
#
|
|
|
|
# However, for some factories we cannot use FactoryDefault because the
|
2021-04-17 20:07:23 +05:30
|
|
|
# associations must be unique and cannot be reused, or the factory default
|
|
|
|
# is being mutated.
|
2021-01-03 14:25:43 +05:30
|
|
|
skip_factory_defaults = %i[
|
2021-09-04 01:27:46 +05:30
|
|
|
ci_job_token_project_scope_link
|
2021-04-29 21:17:54 +05:30
|
|
|
evidence
|
|
|
|
exported_protected_branch
|
2021-01-03 14:25:43 +05:30
|
|
|
fork_network_member
|
2021-04-17 20:07:23 +05:30
|
|
|
group_member
|
|
|
|
import_state
|
2021-04-29 21:17:54 +05:30
|
|
|
milestone_release
|
2021-04-17 20:07:23 +05:30
|
|
|
namespace
|
|
|
|
project_broken_repo
|
2021-09-04 01:27:46 +05:30
|
|
|
project_repository
|
2021-04-17 20:07:23 +05:30
|
|
|
prometheus_alert
|
|
|
|
prometheus_alert_event
|
|
|
|
prometheus_metric
|
2021-04-29 21:17:54 +05:30
|
|
|
protected_branch
|
|
|
|
protected_branch_merge_access_level
|
|
|
|
protected_branch_push_access_level
|
|
|
|
protected_tag
|
|
|
|
release
|
|
|
|
release_link
|
2021-04-17 20:07:23 +05:30
|
|
|
self_managed_prometheus_alert_event
|
2021-09-04 01:27:46 +05:30
|
|
|
shard
|
2021-04-17 20:07:23 +05:30
|
|
|
users_star_project
|
|
|
|
wiki_page
|
|
|
|
wiki_page_meta
|
2021-01-03 14:25:43 +05:30
|
|
|
].to_set.freeze
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
# Some factories and their corresponding models are based on
|
|
|
|
# database views. In order to use those, we have to swap the
|
|
|
|
# view out with a table of the same structure.
|
|
|
|
factories_based_on_view = %i[
|
|
|
|
postgres_index
|
|
|
|
postgres_index_bloat_estimate
|
|
|
|
].to_set.freeze
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
without_fd, with_fd = FactoryBot.factories
|
|
|
|
.partition { |factory| skip_factory_defaults.include?(factory.name) }
|
|
|
|
|
|
|
|
context 'with factory defaults', factory_default: :keep do
|
2021-04-17 20:07:23 +05:30
|
|
|
let_it_be(:namespace) { create_default(:namespace).freeze }
|
|
|
|
let_it_be(:project) { create_default(:project, :repository).freeze }
|
|
|
|
let_it_be(:user) { create_default(:user).freeze }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
before do
|
|
|
|
factories_based_on_view.each do |factory|
|
|
|
|
view = build(factory).class.table_name
|
|
|
|
swapout_view_for_table(view)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
with_fd.each do |factory|
|
|
|
|
it_behaves_like 'factory', factory
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without factory defaults' do
|
|
|
|
without_fd.each do |factory|
|
|
|
|
it_behaves_like 'factory', factory
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|