Allow to use rewritten record helper with FactoryBot

The fact that it used to be called with a factory name speaks for
a history of using FactoryBot originally and then being rewritten
to use a manually written SQL insert statement.

This has now been changed so when a symbol is passed, it's taken
as a factory name and a factory is used. Only when a class name
is passed, the manual SQL path is chosen.

This is done, so that I can rely on the default-value-filling of
FactoryBot to create my test record.
This commit is contained in:
Jan Sandbrink
2026-03-23 08:50:04 +01:00
parent 7b413c945b
commit 82413e5b26
4 changed files with 51 additions and 47 deletions
@@ -45,7 +45,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Saml::Provider" do
it_behaves_like "rewritten record",
:saml_provider,
Saml::Provider,
:creator_id do
let(:attributes) do
{
@@ -45,23 +45,23 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with MeetingAgendaItem" do
it_behaves_like "rewritten record",
:meeting_agenda_item,
MeetingAgendaItem,
:author_id
it_behaves_like "rewritten record",
:meeting_agenda_item,
MeetingAgendaItem,
:presenter_id
end
context "with MeetingOutcome" do
it_behaves_like "rewritten record",
:meeting_outcome,
MeetingOutcome,
:author_id
end
context "with Journal::MeetingAgendaItemJournal" do
it_behaves_like "rewritten record",
:journal_meeting_agenda_item_journal,
Journal::MeetingAgendaItemJournal,
:author_id
end
end
@@ -28,22 +28,26 @@
# See COPYRIGHT and LICENSE files for more details.
#++
RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = Integer|
RSpec.shared_examples_for "rewritten record" do |factory_or_class, attribute, format = Integer|
let!(:model) do
klass = FactoryBot.factories.find(factory).build_class
all_attributes = other_attributes.merge(attribute => principal_id)
all_attributes[:created_at] ||= "NOW()" if klass.column_names.include?("created_at")
all_attributes[:updated_at] ||= "NOW()" if klass.column_names.include?("updated_at")
if factory_or_class.is_a?(Symbol)
create(factory_or_class, **all_attributes)
else
klass = factory_or_class
all_attributes[:created_at] ||= "NOW()" if klass.column_names.include?("created_at")
all_attributes[:updated_at] ||= "NOW()" if klass.column_names.include?("updated_at")
inserted = ActiveRecord::Base.connection.select_one <<~SQL.squish
INSERT INTO #{klass.table_name}
(#{all_attributes.keys.join(', ')})
VALUES
(#{all_attributes.values.join(', ')})
RETURNING id
SQL
inserted = ActiveRecord::Base.connection.select_one <<~SQL.squish
INSERT INTO #{klass.table_name}
(#{all_attributes.keys.join(', ')})
VALUES
(#{all_attributes.values.join(', ')})
RETURNING id
SQL
klass.find(inserted["id"])
klass.find(inserted["id"])
end
end
let(:other_attributes) do
@@ -58,7 +62,7 @@ RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = In
end
end
context "for #{factory}" do
context "for #{factory_or_class}" do
context "when #{attribute} is set to the replaced user" do
let(:principal_id) { principal.id }
@@ -87,11 +87,11 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Attachment" do
it_behaves_like "rewritten record",
:attachment,
Attachment,
:author_id
it_behaves_like "rewritten record",
:journal_attachment_journal,
Journal::AttachmentJournal,
:author_id do
let(:attributes) do
{
@@ -109,7 +109,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
shared_let(:news) { create(:news) }
it_behaves_like "rewritten record",
:comment,
Comment,
:author_id do
let(:attributes) do
{
@@ -124,7 +124,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
shared_let(:version) { create(:version) }
it_behaves_like "rewritten record",
:custom_value,
CustomValue,
:value,
String do
let(:user_cf) { create(:user_wp_custom_field) }
@@ -138,7 +138,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_customizable_journal,
Journal::CustomizableJournal,
:value,
String do
let(:user_cf) { create(:user_wp_custom_field) }
@@ -153,7 +153,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Changeset" do
it_behaves_like "rewritten record",
:changeset,
Changeset,
:user_id do
let(:attributes) do
{ repository_id: 1,
@@ -163,7 +163,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_changeset_journal,
Journal::ChangesetJournal,
:user_id do
let(:attributes) do
{ repository_id: 1,
@@ -175,7 +175,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Message" do
it_behaves_like "rewritten record",
:message,
Message,
:author_id do
let(:attributes) do
{
@@ -186,7 +186,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_message_journal,
Journal::MessageJournal,
:author_id do
let(:attributes) do
{
@@ -199,17 +199,17 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with MeetingParticipant" do
it_behaves_like "rewritten record",
:meeting_participant,
MeetingParticipant,
:user_id
end
context "with News" do
it_behaves_like "rewritten record",
:news,
News,
:author_id
it_behaves_like "rewritten record",
:journal_news_journal,
Journal::NewsJournal,
:author_id do
let(:attributes) do
{
@@ -222,7 +222,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with WikiPage" do
it_behaves_like "rewritten record",
:wiki_page,
WikiPage,
:author_id do
let(:attributes) do
{
@@ -236,7 +236,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_wiki_page_journal,
Journal::WikiPageJournal,
:author_id
end
@@ -260,19 +260,19 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:work_package,
WorkPackage,
:author_id
it_behaves_like "rewritten record",
:work_package,
WorkPackage,
:assigned_to_id
it_behaves_like "rewritten record",
:work_package,
WorkPackage,
:responsible_id
it_behaves_like "rewritten record",
:journal_work_package_journal,
Journal::WorkPackageJournal,
:assigned_to_id do
let(:attributes) do
{
@@ -291,7 +291,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_work_package_journal,
Journal::WorkPackageJournal,
:responsible_id do
let(:attributes) do
{
@@ -312,7 +312,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with TimeEntry" do
it_behaves_like "rewritten record",
:time_entry,
TimeEntry,
:user_id do
let(:attributes) do
{ project_id: 1,
@@ -327,7 +327,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:time_entry,
TimeEntry,
:logged_by_id do
let(:attributes) do
{ project_id: 1,
@@ -342,7 +342,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_time_entry_journal,
Journal::TimeEntryJournal,
:user_id do
let(:attributes) do
{ project_id: 1,
@@ -357,7 +357,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_time_entry_journal,
Journal::TimeEntryJournal,
:logged_by_id do
let(:attributes) do
{ project_id: 1,
@@ -374,7 +374,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Budget" do
it_behaves_like "rewritten record",
:budget,
Budget,
:author_id do
let(:attributes) do
{ project_id: 1,
@@ -385,7 +385,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
end
it_behaves_like "rewritten record",
:journal_budget_journal,
Journal::BudgetJournal,
:author_id do
let(:attributes) do
{ project_id: 1,
@@ -397,7 +397,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with Query" do
it_behaves_like "rewritten record",
:query,
Query,
:user_id do
let(:attributes) do
{
@@ -412,7 +412,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
let(:query) { create(:cost_query, user: principal) }
it_behaves_like "rewritten record",
:cost_query,
CostQuery,
:user_id do
let(:attributes) do
{ name: "'abc'",
@@ -425,7 +425,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
let(:recipient) { create(:user) }
it_behaves_like "rewritten record",
:notification,
Notification,
:actor_id do
let(:attributes) do
{
@@ -439,7 +439,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with OAuth application" do
it_behaves_like "rewritten record",
:oauth_application,
Doorkeeper::Application,
:owner_id do
let(:attributes) do
{