Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

refactor: Remove boiler-plate code

Christophe Bliardc703673657f3
diff --git a/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb b/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb
index 4a365f70250..8e594790025 100644
--- a/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb
+++ b/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb
@@ -35,9 +35,9 @@ require Rails.root.join("spec/services/principals/replace_references_context")
RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
subject(:service_call) { instance.call(from: principal, to: to_principal) }
- shared_let(:other_user) { create(:user) }
- shared_let(:principal) { create(:user) }
- shared_let(:to_principal) { create(:user) }
+ shared_let(:other_user) { create(:user, firstname: "other user") }
+ shared_let(:principal) { create(:user, firstname: "old principal") }
+ shared_let(:to_principal) { create(:user, firstname: "new principal") }
let(:instance) do
described_class.new
@@ -46,26 +46,10 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
context "with MeetingAgendaItem" do
it_behaves_like "rewritten record",
:meeting_agenda_item,
- :author_id do
- let(:attributes) do
- {
- author_id: principal.id,
- created_at: "NOW()",
- updated_at: "NOW()"
- }
- end
- end
+ :author_id
it_behaves_like "rewritten record",
:meeting_agenda_item,
- :presenter_id do
- let(:attributes) do
- {
- presenter_id: principal.id,
- created_at: "NOW()",
- updated_at: "NOW()"
- }
- end
- end
+ :presenter_id
end
end
diff --git a/spec/services/principals/replace_references_context.rb b/spec/services/principals/replace_references_context.rb
index 8c8063b2675..2c9c0537e72 100644
--- a/spec/services/principals/replace_references_context.rb
+++ b/spec/services/principals/replace_references_context.rb
@@ -30,6 +30,8 @@ RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = In
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")
inserted = ActiveRecord::Base.connection.select_one <<~SQL.squish
INSERT INTO #{klass.table_name}
@@ -55,7 +57,7 @@ RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = In
end
context "for #{factory}" do
- context "with the replaced user" do
+ context "when #{attribute} is set to the replaced user" do
let(:principal_id) { principal.id }
before do
@@ -63,13 +65,13 @@ RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = In
model.reload
end
- it "replaces #{attribute}" do
+ it "replaces its value" do
expect(model.send(attribute))
.to eql expected(to_principal, format)
end
end
- context "with a different user" do
+ context "when #{attribute} is set to a different user" do
let(:principal_id) { other_user.id }
before do
@@ -77,7 +79,7 @@ RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = In
model.reload
end
- it "keeps #{attribute}" do
+ it "keeps its value" do
expect(model.send(attribute))
.to eql expected(other_user, format)
end
diff --git a/spec/services/principals/replace_references_service_call_integration_spec.rb b/spec/services/principals/replace_references_service_call_integration_spec.rb
index ecc39f569aa..5daffad06e5 100644
--- a/spec/services/principals/replace_references_service_call_integration_spec.rb
+++ b/spec/services/principals/replace_references_service_call_integration_spec.rb
@@ -200,9 +200,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
:meeting_agenda,
:author_id do
let(:attributes) do
- { type: "'MeetingAgenda'",
- created_at: "NOW()",
- updated_at: "NOW()" }
+ { type: "'MeetingAgenda'" }
end
end
@@ -210,32 +208,19 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
:meeting_minutes,
:author_id do
let(:attributes) do
- { type: "'MeetingMinutes'",
- created_at: "NOW()",
- updated_at: "NOW()" }
+ { type: "'MeetingMinutes'" }
end
end
it_behaves_like "rewritten record",
:journal_meeting_content_journal,
- :author_id do
- let(:attributes) do
- {}
- end
- end
+ :author_id
end
context "with MeetingParticipant" do
it_behaves_like "rewritten record",
:meeting_participant,
- :user_id do
- let(:attributes) do
- {
- created_at: "NOW()",
- updated_at: "NOW()"
- }
- end
- end
+ :user_id
end
context "with News" do
@@ -466,9 +451,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
{
recipient_id: user.id,
resource_id: 1234,
- resource_type: "'WorkPackage'",
- created_at: "NOW()",
- updated_at: "NOW()"
+ resource_type: "'WorkPackage'"
}
end
end
@@ -483,9 +466,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
name: "'foo'",
uid: "'bar'",
secret: "'bar'",
- redirect_uri: "'urn:whatever'",
- created_at: "NOW()",
- updated_at: "NOW()"
+ redirect_uri: "'urn:whatever'"
}
end
end