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

add tests and compact logic

Ivan Kuchin7d9d523b61b2
diff --git a/app/models/queries/operators/date_limits.rb b/app/models/queries/operators/date_limits.rb
index 51f6ad440b9..9a0864ec37b 100644
--- a/app/models/queries/operators/date_limits.rb
+++ b/app/models/queries/operators/date_limits.rb
@@ -29,11 +29,13 @@
module Queries::Operators
module DateLimits
# Technically dates in PostgreSQL can be up to 5874897 AD, but limit to
- # timestamp range, as dates are used to query for timestamps too
+ # timestamp range, as dates are used to query for timestamps too.
+ # Date.new BCE years are counted astronomically, so 0 is 1 BC.
+ # Minimal allowed date is Date.new(-4713, 11, 24), but specification says 4713 BC (-4712).
#
# https://www.postgresql.org/docs/current/datatype-datetime.html
- PG_DATE_FROM = ::Date.new(-4713, 1, 1)
- PG_DATE_TO_EXCLUSIVE = ::Date.new(294276 + 1, 1, 1)
+ PG_DATE_FROM = Date.new(-4712, 1, 1)
+ PG_DATE_TO_EXCLUSIVE = Date.new(294276 + 1, 1, 1)
def date_too_small?(date)
date < PG_DATE_FROM
diff --git a/app/models/queries/operators/date_range_clauses.rb b/app/models/queries/operators/date_range_clauses.rb
index 81e9adaf523..0bc9d30d640 100644
--- a/app/models/queries/operators/date_range_clauses.rb
+++ b/app/models/queries/operators/date_range_clauses.rb
@@ -47,30 +47,26 @@ module Queries::Operators
s = []
if from
- s << case
- when date_too_small?(from)
- "1 = 1"
- when date_too_big?(from)
- "1 <> 1"
- else
- "#{table}.#{field} > '#{quoted_date_from_utc(from.yesterday)}'"
- end
+ return "1 <> 1" if date_too_big?(from)
+
+ unless date_too_small?(from)
+ s << "#{table}.#{field} > '#{quoted_date_from_utc(from.yesterday)}'"
+ end
end
if to
- s << case
- when date_too_small?(to)
- "1 <> 1"
- when date_too_big?(to)
- "1 = 1"
- else
- "#{table}.#{field} <= '#{quoted_date_from_utc(to)}'"
- end
+ return "1 <> 1" if date_too_small?(to)
+
+ unless date_too_big?(to)
+ s << "#{table}.#{field} <= '#{quoted_date_from_utc(to)}'"
+ end
end
- s.join(" AND ")
+ s.join(" AND ").presence || "1 = 1"
end
+ private
+
def quoted_date_from_utc(value)
connection.quoted_date(value.to_time(:utc).end_of_day)
end
diff --git a/app/models/queries/operators/datetime_range_clauses.rb b/app/models/queries/operators/datetime_range_clauses.rb
index c754716fd2a..9c278138a5b 100644
--- a/app/models/queries/operators/datetime_range_clauses.rb
+++ b/app/models/queries/operators/datetime_range_clauses.rb
@@ -34,28 +34,22 @@ module Queries::Operators
s = []
if from
- s << case
- when date_too_small?(from)
- "1 = 1"
- when date_too_big?(from)
- "1 <> 1"
- else
- "#{table}.#{field} >= '#{connection.quoted_date(from)}'"
- end
+ return "1 <> 1" if date_too_big?(from)
+
+ unless date_too_small?(from)
+ s << "#{table}.#{field} >= '#{connection.quoted_date(from)}'"
+ end
end
if to
- s << case
- when date_too_small?(to)
- "1 <> 1"
- when date_too_big?(to)
- "1 = 1"
- else
- "#{table}.#{field} <= '#{connection.quoted_date(to)}'"
- end
+ return "1 <> 1" if date_too_small?(to)
+
+ unless date_too_big?(to)
+ s << "#{table}.#{field} <= '#{connection.quoted_date(to)}'"
+ end
end
- s.join(" AND ")
+ s.join(" AND ").presence || "1 = 1"
end
end
end
diff --git a/spec/models/queries/operators/date_range_clauses_spec.rb b/spec/models/queries/operators/date_range_clauses_spec.rb
new file mode 100644
index 00000000000..917b11df70c
--- /dev/null
+++ b/spec/models/queries/operators/date_range_clauses_spec.rb
@@ -0,0 +1,186 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe Queries::Operators::DateRangeClauses do
+ let(:instance) do
+ Class.new { def connection; end }.include(described_class).new
+ end
+
+ let(:connection) { WorkPackage.connection }
+
+ let(:today) { Date.new(2024, 11, 5) }
+ let(:smallest_date) { Date.new(-4712, 1, 1) }
+ let(:biggest_date) { Date.new(294276, 12, 31) }
+
+ before do
+ allow(instance).to receive(:connection).and_return(connection)
+ allow(Time).to receive(:zone).and_return(instance_double(ActiveSupport::TimeZone, today:))
+ end
+
+ describe "#relative_date_range_clause and #date_range_clause" do
+ shared_examples "returns sql" do |expected|
+ it "returns expected sql from relative_date_range_clause" do
+ expect(instance.relative_date_range_clause(:some_table, :some_field, from, to))
+ .to eq(expected)
+ end
+
+ it "returns expected sql from date_range_clause" do
+ from_date = from && (today + from)
+ to_date = to && (today + to)
+
+ expect(instance.date_range_clause(:some_table, :some_field, from_date, to_date))
+ .to eq(expected)
+ end
+ end
+
+ shared_examples "doesn't error in database" do
+ it "returns sql from relative_date_range_clause that doesn't error in database" do
+ sql = instance.relative_date_range_clause(WorkPackage.table_name, :created_at, from, to)
+
+ expect { WorkPackage.where(sql).to_a }.not_to raise_error
+ end
+
+ it "returns sql from date_range_clause that doesn't error in database" do
+ from_date = from && (today + from)
+ to_date = to && (today + to)
+ sql = instance.date_range_clause(WorkPackage.table_name, :created_at, from_date, to_date)
+
+ expect { WorkPackage.where(sql).to_a }.not_to raise_error
+ end
+ end
+
+ context "when both values are provided" do
+ let(:from) { -1 }
+ let(:to) { +1 }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field > '2024-11-03 23:59:59.999999'
+ AND some_table.some_field <= '2024-11-06 23:59:59.999999'
+ SQL
+ end
+
+ context "when only from is provided" do
+ let(:from) { -1 }
+ let(:to) { nil }
+
+ include_examples "returns sql", "some_table.some_field > '2024-11-03 23:59:59.999999'"
+ end
+
+ context "when only to is provided" do
+ let(:from) { nil }
+ let(:to) { +1 }
+
+ include_examples "returns sql", "some_table.some_field <= '2024-11-06 23:59:59.999999'"
+ end
+
+ context "when none is provided" do
+ let(:from) { nil }
+ let(:to) { nil }
+
+ include_examples "returns sql", "1 = 1"
+ end
+
+ context "when from is around extrema" do
+ let(:to) { 0 }
+
+ context "when from is minimum allowed date" do
+ let(:from) { smallest_date - today }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field > '4714-12-31 23:59:59.999999 BC'
+ AND some_table.some_field <= '2024-11-05 23:59:59.999999'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when from is less than minimum allowed date" do
+ let(:from) { smallest_date - today - 1 }
+
+ include_examples "returns sql", "some_table.some_field <= '2024-11-05 23:59:59.999999'"
+ end
+
+ context "when from is maximum allowed date" do
+ let(:from) { biggest_date - today }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field > '294276-12-30 23:59:59.999999'
+ AND some_table.some_field <= '2024-11-05 23:59:59.999999'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when from is more than maximum allowed date" do
+ let(:from) { biggest_date - today + 1 }
+
+ include_examples "returns sql", "1 <> 1"
+ end
+ end
+
+ context "when to is around extrema" do
+ let(:from) { 0 }
+
+ context "when to is minimum allowed date" do
+ let(:to) { smallest_date - today }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field > '2024-11-04 23:59:59.999999'
+ AND some_table.some_field <= '4713-01-01 23:59:59.999999 BC'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when to is less than minimum allowed date" do
+ let(:to) { smallest_date - today - 1 }
+
+ include_examples "returns sql", "1 <> 1"
+ end
+
+ context "when to is maximum allowed date" do
+ let(:to) { biggest_date - today }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field > '2024-11-04 23:59:59.999999'
+ AND some_table.some_field <= '294276-12-31 23:59:59.999999'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when to is more than maximum allowed date" do
+ let(:to) { biggest_date - today + 1 }
+
+ include_examples "returns sql", "some_table.some_field > '2024-11-04 23:59:59.999999'"
+ end
+ end
+ end
+end
diff --git a/spec/models/queries/operators/datetime_range_clauses_spec.rb b/spec/models/queries/operators/datetime_range_clauses_spec.rb
new file mode 100644
index 00000000000..5432600e905
--- /dev/null
+++ b/spec/models/queries/operators/datetime_range_clauses_spec.rb
@@ -0,0 +1,170 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe Queries::Operators::DatetimeRangeClauses do
+ let(:instance) do
+ Class.new { def connection; end }.include(described_class).new
+ end
+
+ let(:connection) { WorkPackage.connection }
+
+ let(:today) { DateTime.new(2024, 11, 5, 12, 34, 56789/1000r) }
+ let(:smallest_date) { DateTime.new(-4712, 1, 1, 12, 34, 56789/1000r) }
+ let(:biggest_date) { DateTime.new(294276, 12, 31, 12, 34, 56789/1000r) }
+
+ before do
+ allow(instance).to receive(:connection).and_return(connection)
+ allow(Time).to receive(:zone).and_return(instance_double(ActiveSupport::TimeZone, today:))
+ end
+
+ describe "#datetime_range_clause" do
+ shared_examples "returns sql" do |expected|
+ it "returns expected sql" do
+ expect(instance.datetime_range_clause(:some_table, :some_field, from, to))
+ .to eq(expected)
+ end
+ end
+
+ shared_examples "doesn't error in database" do
+ it "returns sql that doesn't error in database" do
+ sql = instance.datetime_range_clause(WorkPackage.table_name, :created_at, from, to)
+
+ expect { WorkPackage.where(sql).to_a }.not_to raise_error
+ end
+ end
+
+ context "when both values are provided" do
+ let(:from) { today - 1 }
+ let(:to) { today + 1 }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field >= '2024-11-04 12:34:56.789000'
+ AND some_table.some_field <= '2024-11-06 12:34:56.789000'
+ SQL
+ end
+
+ context "when only from is provided" do
+ let(:from) { today - 1 }
+ let(:to) { nil }
+
+ include_examples "returns sql", "some_table.some_field >= '2024-11-04 12:34:56.789000'"
+ end
+
+ context "when only to is provided" do
+ let(:from) { nil }
+ let(:to) { today + 1 }
+
+ include_examples "returns sql", "some_table.some_field <= '2024-11-06 12:34:56.789000'"
+ end
+
+ context "when none is provided" do
+ let(:from) { nil }
+ let(:to) { nil }
+
+ include_examples "returns sql", "1 = 1"
+ end
+
+ context "when from is around extrema" do
+ let(:to) { today }
+
+ context "when from is minimum allowed date" do
+ let(:from) { smallest_date }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field >= '4713-01-01 12:34:56.789000 BC'
+ AND some_table.some_field <= '2024-11-05 12:34:56.789000'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when from is less than minimum allowed date" do
+ let(:from) { smallest_date - 1 }
+
+ include_examples "returns sql", "some_table.some_field <= '2024-11-05 12:34:56.789000'"
+ end
+
+ context "when from is maximum allowed date" do
+ let(:from) { biggest_date }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field >= '294276-12-31 12:34:56.789000'
+ AND some_table.some_field <= '2024-11-05 12:34:56.789000'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when from is more than maximum allowed date" do
+ let(:from) { biggest_date + 1 }
+
+ include_examples "returns sql", "1 <> 1"
+ end
+ end
+
+ context "when to is around extrema" do
+ let(:from) { today }
+
+ context "when to is minimum allowed date" do
+ let(:to) { smallest_date }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field >= '2024-11-05 12:34:56.789000'
+ AND some_table.some_field <= '4713-01-01 12:34:56.789000 BC'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when to is less than minimum allowed date" do
+ let(:to) { smallest_date - 1 }
+
+ include_examples "returns sql", "1 <> 1"
+ end
+
+ context "when to is maximum allowed date" do
+ let(:to) { biggest_date }
+
+ include_examples "returns sql", <<-SQL.squish
+ some_table.some_field >= '2024-11-05 12:34:56.789000'
+ AND some_table.some_field <= '294276-12-31 12:34:56.789000'
+ SQL
+
+ include_examples "doesn't error in database"
+ end
+
+ context "when to is more than maximum allowed date" do
+ let(:to) { biggest_date + 1 }
+
+ include_examples "returns sql", "some_table.some_field >= '2024-11-05 12:34:56.789000'"
+ end
+ end
+ end
+end