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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
at_timestamp: moving some arel-related specs to historic_active_record_relation_spec
Sebastian Fiedlschusterc6999f7a68ee
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rbindex a4802283f1d..5c735065733 100644--- a/spec/models/journable/historic_active_record_relation_spec.rb+++ b/spec/models/journable/historic_active_record_relation_spec.rb@@ -59,7 +59,8 @@ describe Journable::HistoricActiveRecordRelation doattributes: { description: "The work package as it is since Friday", estimated_hours: 10 })end- let(:relation) { WorkPackage.at_timestamp(wednesday) }+ let(:relation) { WorkPackage.all }+ let(:historic_relation) { relation.at_timestamp(wednesday) }def create_journal(journable:, timestamp:, attributes: {})work_package_attributes = work_package.attributes.except("id")@@ -79,9 +80,11 @@ describe Journable::HistoricActiveRecordRelation dowork_package.reloadend+ subject { historic_relation }+describe "#where" dodescribe "project_id in array (Arel::Nodes::HomogeneousIn)" do- subject { relation.where(project_id: [project.id, 1, 2, 3]) }+ let(:relation) { WorkPackage.where(project_id: [project.id, 1, 2, 3]) }describe "#to_sql" doit "transforms the expression to query the correct table" do@@ -97,7 +100,7 @@ describe Journable::HistoricActiveRecordRelation doenddescribe "project_id not in array (Arel::Nodes::HomogeneousIn)" do- subject { relation.where.not(project_id: [9999, 999]) }+ let(:relation) { WorkPackage.where.not(project_id: [9999, 999]) }describe "#to_sql" doit "transforms the expression to query the correct table" do@@ -111,5 +114,238 @@ describe Journable::HistoricActiveRecordRelation doendendend++ describe "sql string (as used by Query#statement)" do+ let(:relation) { WorkPackage.where("(work_packages.description ILIKE '%been on Wednesday%')") }++ it "transforms the table name" do+ expect(subject.to_sql).to include "work_package_journals.description ILIKE"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end++ describe "when the sql where statement includes work_package.id" do+ # This is used, for example, in 'follows' relations.+ let(:relation) { WorkPackage.where("(work_packages.id IN (#{work_package.id}))") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include "journals.journable_id IN (#{work_package.id})"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "when the sql where statement includes \"work_package\".\"id\"" do+ # This is used in the manual-sorting feature.+ let(:relation) { WorkPackage.where("(\"work_packages\".\"id\" IN (#{work_package.id}))") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" IN (#{work_package.id})"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end+ end++ describe "foo OR bar (Arel::Nodes::Grouping)" do+ # https://github.com/opf/openproject/pull/11678#issuecomment-1328011996+ let(:relation) do+ WorkPackage.where(subject: "Foo").or(+ WorkPackage.where(description: "The work package as it has been on Wednesday")+ )+ end++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"work_package_journals\".\"subject\" = 'Foo' OR " \+ "\"work_package_journals\".\"description\" = 'The work package as it has been on Wednesday'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "work_packages.updated_at > ?" do+ # as used by spec/features/work_packages/timeline/timeline_dates_spec.rb+ let(:relation) { WorkPackage.where("work_packages.updated_at > '2022-01-01'") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "journals.created_at > '2022-01-01'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end++ describe "when using quotation marks" do+ let(:relation) { WorkPackage.where("\"work_packages\".\"updated_at\" > '2022-01-01'") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"journals\".\"created_at\" > '2022-01-01'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "when using a hash" do+ let(:relation) { WorkPackage.where(work_packages: { updated_at: ("2022-01-01".to_datetime).. }) }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"journals\".\"created_at\" >= '2022-01-01"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end+ end++ describe "work_packages.created_at > ?" do+ # as used by spec/features/work_packages/table/queries/filter_spec.rb+ let(:relation) { WorkPackage.where("work_packages.created_at > '2022-01-01'") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "journables.created_at > '2022-01-01'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end++ describe "when using quotation marks" do+ let(:relation) { WorkPackage.where("\"work_packages\".\"created_at\" > '2022-01-01'") }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"journables\".\"created_at\" > '2022-01-01'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "when using a hash" do+ let(:relation) { WorkPackage.where(work_packages: { created_at: ("2022-01-01".to_datetime).. }) }++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"journables\".\"created_at\" >= '2022-01-01"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end+ end+ end++ describe "#order" do+ let(:relation) { WorkPackage.order(description: :desc) }++ it "transforms the table name" do+ expect(subject.to_sql).to include "\"work_package_journals\".\"description\" DESC"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end++ describe "manual order clause" do+ let(:relation) { WorkPackage.order("work_packages.description DESC") }++ it "transforms the table name" do+ expect(subject.to_sql).to include "work_package_journals.description DESC"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "manual order clause using work_packages.id" do+ # This is used in the manual-sorting feature.+ let(:relation) { WorkPackage.order("work_packages.id DESC") }++ it "transforms the table name" do+ expect(subject.to_sql).to include "journals.journable_id DESC"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "order clause with work_packages.id" do+ let(:relation) { WorkPackage.order(id: :desc) }++ it "transforms the table name" do+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "several order clauses" do+ let(:relation) { WorkPackage.order(subject: :asc, id: :desc) }++ it "transforms the table name" do+ expect(subject.to_sql).to include "\"work_package_journals\".\"subject\" ASC"+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end+ end++ describe "#joins" do+ describe "using active record" do+ let(:relation) { WorkPackage.joins(:time_entries) }++ before { work_package.time_entries << create(:time_entry) }++ it "transforms the table name" do+ expect(subject.to_sql).to include \+ "JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end++ describe "using a manual sql expression" do+ # This is used in the manual-sorting feature.+ let(:relation) do+ WorkPackage \+ .joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id")+ end++ it "transforms the table name" do+ expect(subject.to_sql).to include \+ "LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = journals.journable_id"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ endendenddiff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbindex 314e7110503..2540af51a8e 100644--- a/spec/models/journable/timestamps_spec.rb+++ b/spec/models/journable/timestamps_spec.rb@@ -511,252 +511,6 @@ describe Journable::Timestamps doendendend-- describe "when chaining a sql where clause (as used by Query#statement)" do- subject { WorkPackage.where("(work_packages.description ILIKE '%been on Wednesday%')").at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include "work_package_journals.description ILIKE"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end-- describe "when the sql where statement includes work_package.id" do- # This is used, for example, in 'follows' relations.- subject { WorkPackage.where("(work_packages.id IN (#{work_package.id}))").at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include "journals.journable_id IN (#{work_package.id})"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when the sql where statement includes \"work_package\".\"id\"" do- # This is used in the manual-sorting feature.- subject { WorkPackage.where("(\"work_packages\".\"id\" IN (#{work_package.id}))").at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include "\"journals\".\"journable_id\" IN (#{work_package.id})"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end- end-- describe "when chaining where clauses with OR (Arel::Nodes::Grouping)" do- # https://github.com/opf/openproject/pull/11678#issuecomment-1328011996-- subject do- relation = WorkPackage.where(subject: "Foo").or(- WorkPackage.where(description: "The work package as it has been on Wednesday")- )- relation.at_timestamp(wednesday)- end-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "\"work_package_journals\".\"subject\" = 'Foo' OR " \- "\"work_package_journals\".\"description\" = 'The work package as it has been on Wednesday'"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when chaining a where clause with work_packages.updated_at" do- # as used by spec/features/work_packages/timeline/timeline_dates_spec.rb-- let(:relation) { WorkPackage.where("work_packages.updated_at > '2022-01-01'") }-- subject { relation.at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "journals.created_at > '2022-01-01'"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end-- describe "when using quotation marks" do- let(:relation) { WorkPackage.where("\"work_packages\".\"updated_at\" > '2022-01-01'") }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "\"journals\".\"created_at\" > '2022-01-01'"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when using a hash" do- let(:relation) { WorkPackage.where(work_packages: { updated_at: ("2022-01-01".to_datetime).. }) }-- subject { relation.at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "\"journals\".\"created_at\" >= '2022-01-01"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end- end-- describe "when chaining a where clause with work_packages.created_at" do- # as used by spec/features/work_packages/table/queries/filter_spec.rb-- let(:relation) { WorkPackage.where("work_packages.created_at > '2022-01-01'") }-- subject { relation.at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "journables.created_at > '2022-01-01'"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end-- describe "when using quotation marks" do- let(:relation) { WorkPackage.where("\"work_packages\".\"created_at\" > '2022-01-01'") }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "\"journables\".\"created_at\" > '2022-01-01'"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when using a hash" do- let(:relation) { WorkPackage.where(work_packages: { created_at: ("2022-01-01".to_datetime).. }) }-- subject { relation.at_timestamp(wednesday) }-- it "transforms the expression to query the correct table" do- expect(subject.to_sql).to include \- "\"journables\".\"created_at\" >= '2022-01-01"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end- end-- describe "when chaining an order clause" do- subject { WorkPackage.at_timestamp(wednesday).order(description: :desc) }-- it "transforms the table name" do- expect(subject.to_sql).to include "\"work_package_journals\".\"description\" DESC"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end-- describe "when chaining a manual order clause" do- subject { WorkPackage.order("work_packages.description DESC").at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include "work_package_journals.description DESC"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when chaining a manual order clause using work_packages.id" do- # This is used in the manual-sorting feature.- subject { WorkPackage.order("work_packages.id DESC").at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include "journals.journable_id DESC"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when chaining an order clause with work_packages.id" do- subject { WorkPackage.order(id: :desc).at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when chaining several order clauses" do- subject { WorkPackage.order(subject: :asc, id: :desc).at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include "\"work_package_journals\".\"subject\" ASC"- expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end- end-- describe "when chaining a join" do- describe "when joining using active record" do- before { work_package.time_entries << create(:time_entry) }-- subject { WorkPackage.joins(:time_entries).at_timestamp(wednesday) }-- it "transforms the table name" do- expect(subject.to_sql).to include \- "JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end-- describe "when joining using a manual sql expression" do- # This is used in the manual-sorting feature.- subject do- WorkPackage \- .joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id") \- .at_timestamp(wednesday)- end-- it "transforms the table name" do- expect(subject.to_sql).to include \- "LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = journals.journable_id"- end-- it "returns the requested work package" do- expect(subject).to include work_package- end- end- endenddescribe "#at_timestamp" do