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

Remove the Timestamp#iso8601 methods and add specs for valid?

Dombi Attila581c92a12d3f
diff --git a/app/models/query/timestamps.rb b/app/models/query/timestamps.rb
index 1f4360719cd..294b057630e 100644
--- a/app/models/query/timestamps.rb
+++ b/app/models/query/timestamps.rb
@@ -34,7 +34,7 @@ module Query::Timestamps
# Returns the timestamps the query should be evaluated at.
#
- # In the database, the timestamps are stored as ISO8601 strings.
+ # In the database, the timestamps are stored as strings.
# This method returns the timestamps as array of `Timestamp` objects.
#
# Timestamps can be absolute (e.g. a certain date and time) or relative
@@ -49,7 +49,7 @@ module Query::Timestamps
end
def timestamps=(params)
- super(Array(params).collect { |element| element.respond_to?(:iso8601) ? element.iso8601 : element })
+ super(Array(params).collect(&:to_s))
end
# Does this query perform a historic search?
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index acbfadae739..dfae4f92e9e 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -27,7 +27,7 @@
#++
class Timestamp
- delegate :hash, to: :iso8601
+ delegate :hash, to: :to_s
class Exception < StandardError; end
@@ -135,23 +135,15 @@ class Timestamp
end
def to_s
- iso8601
+ @timestamp_string.to_s
end
def to_str
to_s
end
- def iso8601
- @timestamp_string.to_s
- end
-
- def to_iso8601
- iso8601
- end
-
def inspect
- "#<Timestamp \"#{iso8601}\">"
+ "#<Timestamp \"#{self}\">"
end
def absolute
@@ -204,9 +196,9 @@ class Timestamp
def ==(other)
case other
when String
- iso8601 == other or to_s == other
+ to_s == other
when Timestamp
- iso8601 == other.iso8601
+ to_s == other.to_s
when NilClass
to_s.blank?
else
@@ -223,7 +215,7 @@ class Timestamp
end
def valid?
- self.class.parse(iso8601)
+ TimestampParser.new(to_s).parse!
rescue StandardError
false
end
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index ff72e0dbb94..b40501d1192 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -551,7 +551,7 @@ module API
end
def self.timestamps_to_param_value(timestamps)
- Array(timestamps).map { |timestamp| Timestamp.parse(timestamp).absolute.iso8601 }.join(",")
+ Array(timestamps).map { |timestamp| Timestamp.parse(timestamp).absolute }.join(",")
end
def self.path_for(path, filters: nil, sort_by: nil, group_by: nil, page_size: nil, offset: nil,
diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
index 08ad21e8ddc..4959341cbad 100644
--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
@@ -80,7 +80,7 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
describe 'self' do
describe 'when providing timestamps' do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:absolute_timestamp_strings) { timestamps.collect { |timestamp| timestamp.absolute.iso8601 } }
+ let(:absolute_timestamp_strings) { timestamps.collect(&:absolute) }
let(:absolute_timestamps_query_param) { { timestamps: absolute_timestamp_strings.join(",") }.to_query }
it 'has the absolute timestamps within the self link' do
@@ -563,7 +563,7 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
end
shared_examples_for 'has the absolute timestamps within the self link' do
- let(:absolute_timestamp_strings) { timestamps.collect { |timestamp| timestamp.absolute.iso8601 } }
+ let(:absolute_timestamp_strings) { timestamps.collect(&:absolute) }
let(:absolute_timestamps_query_param) { { timestamps: absolute_timestamp_strings.join(",") }.to_query }
it 'has the absolute timestamps within the self link' do
diff --git a/spec/models/query/timestamps_spec.rb b/spec/models/query/timestamps_spec.rb
index 29767ea9f91..90d119569cf 100644
--- a/spec/models/query/timestamps_spec.rb
+++ b/spec/models/query/timestamps_spec.rb
@@ -93,8 +93,8 @@ describe Query::Timestamps do
end
it "remembers the timestamp values" do
- expect(subject.first.iso8601).to eq "P-50Y"
- expect(subject.last.iso8601).to eq "2022-10-29T23:01:23Z"
+ expect(subject.first).to eq "P-50Y"
+ expect(subject.last).to eq "2022-10-29T23:01:23Z"
end
end
end
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index 8d20aad3f9b..32a82539263 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -120,6 +120,7 @@ describe Timestamp do
it "returns a described_class representing that absolute time" do
expect(subject).to be_a described_class
+ expect(subject).to be_valid
expect(subject.to_s).to eq "2022-10-29T21:55:58Z"
expect(subject.to_time).to eq Time.zone.parse("2022-10-29T21:55:58Z")
expect(subject).not_to be_relative
@@ -132,7 +133,8 @@ describe Timestamp do
it "returns a Timestamp representing the current time" do
expect(subject).to be_a described_class
- expect(subject.to_iso8601).to eq "PT0S"
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "PT0S"
expect(subject).to be_relative
end
end
@@ -142,7 +144,8 @@ describe Timestamp do
it "returns a Timestamp representing a time ago that duration" do
expect(subject).to be_a described_class
- expect(subject.to_iso8601).to eq "P-1Y"
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "P-1Y"
expect(subject.to_duration).to eq ActiveSupport::Duration.build(-1.year)
expect(subject).to be_relative
end
@@ -153,7 +156,8 @@ describe Timestamp do
it "returns a Timestamp representing a time ago that duration" do
expect(subject).to be_a described_class
- expect(subject.to_iso8601).to eq "P-1Y-2M"
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "P-1Y-2M"
expect(subject.to_duration).to eq ActiveSupport::Duration.build(-1.year - 2.months)
expect(subject).to be_relative
end
@@ -164,7 +168,8 @@ describe Timestamp do
it "returns a Timestamp representing that absolute time" do
expect(subject).to be_a described_class
- expect(subject.to_iso8601).to eq "2022-01-01T00:00:00Z"
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "2022-01-01T00:00:00Z"
expect(subject.to_time).to eq Time.zone.parse("2022-01-01T00:00:00Z")
expect(subject).not_to be_relative
end
@@ -177,6 +182,7 @@ describe Timestamp do
it "returns a Timestamp representing the yesterday at 12:00 pm" do
expect(subject).to be_a described_class
+ expect(subject).to be_valid
expect(subject).to be_relative
expect(subject).to be_relative_date_keyword
expect(subject.to_time).to eq 1.day.ago.change(hour: 12)
@@ -193,6 +199,7 @@ describe Timestamp do
it "returns a Timestamp representing the last working day at 12:00 pm" do
expect(subject).to be_a described_class
+ expect(subject).to be_valid
expect(subject).to be_relative
expect(subject).to be_relative_date_keyword
expect(subject.to_time).to eq 2.days.ago.change(hour: 12)
@@ -204,6 +211,7 @@ describe Timestamp do
it "returns a Timestamp representing the last week at 12:00 pm" do
expect(subject).to be_a described_class
+ expect(subject).to be_valid
expect(subject).to be_relative
expect(subject).to be_relative_date_keyword
expect(subject.to_time).to eq 1.week.ago.change(hour: 12)
@@ -215,6 +223,7 @@ describe Timestamp do
it "returns a Timestamp representing the last month at 12:00 pm" do
expect(subject).to be_a described_class
+ expect(subject).to be_valid
expect(subject).to be_relative
expect(subject).to be_relative_date_keyword
expect(subject.to_time).to eq 1.month.ago.change(hour: 12)
@@ -344,8 +353,8 @@ describe Timestamp do
end
end
- describe "#iso8601" do
- subject { timestamp.iso8601 }
+ describe "#to_s" do
+ subject { timestamp.to_s }
describe "for a timestamp representing an absolute time" do
let(:timestamp) { described_class.new(1.year.ago) }
@@ -365,31 +374,10 @@ describe Timestamp do
end
describe "for a timestamp as a date keyword representing a point in time relative to now" do
- let(:timestamp) { described_class.new("yesterday@12:00") }
-
- it "returns an ISO8601 String representing the duration between that time and now" do
- expect(subject).to eq "yesterday@12:00"
- end
- end
- end
-
- describe "#to_s" do
- subject { timestamp.to_s }
-
- describe "for a timestamp representing an absolute time" do
- let(:timestamp) { described_class.new(1.year.ago) }
-
- it "returns an ISO8601 String representing that time" do
- expect(subject).to eq timestamp.to_time.iso8601
- end
- end
+ let(:timestamp) { described_class.new('yesterday@12:00') }
- describe "for a timestamp representing a point in time relative to now" do
- let(:timestamp) { described_class.new("PT10S") }
-
- it "returns an ISO8601 String representing the duration between that time and now" do
- expect(subject).to eq "PT10S"
- expect(subject).to eq ActiveSupport::Duration.build(10).iso8601
+ it "returns an relative date keyword" do
+ expect(subject).to eq 'yesterday@12:00'
end
end
end
diff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rb
index 2964be80544..f12c237c08a 100644
--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb
@@ -879,6 +879,57 @@ describe 'API v3 Work package resource',
context "with caching" do
context "with relative timestamps" do
+ let(:timestamps) { [Timestamp.parse("P-2D"), Timestamp.now] }
+ let(:created_at) { '2015-01-01' }
+
+ describe "when the filter becomes outdated" do
+ # The work package has been updated 1 day ago, which is after the baseline
+ # date (2 days ago). When time progresses, the date of the update will be
+ # date (last week). When time progresses, the date of the update will be
+ # before the baseline date, because the baseline date is relative to the
+ # current date. This means that the filter will become outdated and we cannot
+ # use a cached result in this case.
+ let(:path) { "#{api_v3_paths.path_for(:work_packages, filters:)}&timestamps=#{timestamps.join(',')}" }
+ let(:filters) do
+ [
+ {
+ subject: {
+ operator: '~',
+ values: [search_term]
+ }
+ }
+ ]
+ end
+ let(:search_term) { 'original' }
+
+ it 'has the relative timestamps within the _meta timestamps' do
+ expect(timestamps.first.to_s).to eq('P-2D')
+ expect(timestamps.first).to be_relative
+ expect(subject.body)
+ .to be_json_eql('P-2D'.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')
+ expect(subject.body)
+ .to be_json_eql('PT0S'.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/timestamp')
+ expect(subject.body)
+ .to be_json_eql('PT0S'.to_json)
+ .at_path('_embedded/elements/0/_meta/timestamp')
+ end
+
+ it "does not use an outdated cache" do
+ get path
+ expect do
+ Timecop.travel 5.days do
+ get path
+ end
+ end.to change {
+ JSON.parse(last_response.body).dig('_embedded', 'elements').count
+ }.from(1).to(0)
+ end
+ end
+ end
+
+ context "with relative date keyword timestamps" do
let(:timestamps) { [Timestamp.parse('lastWeek@12:00'), Timestamp.now] }
let(:created_at) { '2015-01-01' }
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index 5cb859beb43..fb42cff78f9 100644
--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb
@@ -455,13 +455,17 @@ describe 'API v3 Work package resource',
before { login_as current_user }
context "with relative timestamps" do
- let(:timestamps) { [Timestamp.parse("lastWeek@12:00"), Timestamp.now] }
+ let(:timestamps) { [Timestamp.parse("yesterday@00:00"), Timestamp.now] }
let(:created_at) { '2015-01-01' }
describe "attributesByTimestamp" do
it "does not cache the self link" do
get get_path
+
expect do
+ # Travel 1 day to test the href not being cached, because the
+ # relative date keyword has a fixed hour part, which means the timestamp
+ # will change its value only in 1 day units
Timecop.travel 1.day do
get get_path
end
@@ -474,7 +478,7 @@ describe 'API v3 Work package resource',
it "does not cache the attributes" do
get get_path
expect do
- Timecop.travel 1.week do
+ Timecop.travel 2.days do
get get_path
end
end.to change {
@@ -486,7 +490,7 @@ describe 'API v3 Work package resource',
describe "_meta" do
describe "exists" do
- let(:timestamps) { [Timestamp.parse("lastWorkingDay@12:00")] }
+ let(:timestamps) { [Timestamp.parse("yesterday@00:00")] }
let(:created_at) { 25.hours.ago }
it "is not cached" do