Skip to content

Commit

Permalink
add tests and compact logic
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Nov 5, 2024
1 parent 12d6bc0 commit 7d9d523
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 37 deletions.
8 changes: 5 additions & 3 deletions app/models/queries/operators/date_limits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 13 additions & 17 deletions app/models/queries/operators/date_range_clauses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,26 @@ def date_range_clause(table, field, from, to)
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
Expand Down
28 changes: 11 additions & 17 deletions app/models/queries/operators/datetime_range_clauses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,22 @@ def datetime_range_clause(table, field, from, to)
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
186 changes: 186 additions & 0 deletions spec/models/queries/operators/date_range_clauses_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 7d9d523

Please sign in to comment.