Skip to content

Commit

Permalink
Merge pull request #15684 from opf/add-time-matcher
Browse files Browse the repository at this point in the history
Add a dedicated time matcher and use it instead of `be_within.of`
  • Loading branch information
dombesz authored May 28, 2024
2 parents e8a5f78 + ddd1f44 commit 1a14641
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
10 changes: 5 additions & 5 deletions modules/bim/spec/requests/api/bcf/v2_1/shared_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#++

RSpec.shared_examples_for "bcf api successful response" do
def expect_identical_without_time(subject, expected_body)
def expect_identical_without_time(subject, expected_body) # rubocop:disable Metrics/PerceivedComplexity
body = Array.wrap(JSON.parse(subject.body))
expected = Array.wrap(expected_body)
expect(body.size).to eql(expected.size)
Expand All @@ -41,18 +41,18 @@ def expect_identical_without_time(subject, expected_body)
expected_modified_date = expected_item.delete("modified_date")&.to_time

if expected_modified_date
expect(subject_modified_date).to be_within(10.seconds).of(expected_modified_date)
expect(subject_modified_date).to equal_time_without_usec(expected_modified_date)
else
expect(subject_modified_date).to eql(expected_modified_date)
expect(subject_modified_date).to be_nil
end

subject_created_date = subject_body.delete("date")&.to_time
expected_created_date = expected_item.delete("date")&.to_time

if expected_created_date
expect(subject_created_date).to be_within(10.seconds).of(expected_created_date)
expect(subject_created_date).to equal_time_without_usec(expected_created_date)
else
expect(subject_created_date).to eql(expected_created_date)
expect(subject_created_date).to be_nil
end

expect(subject_body.to_json).to be_json_eql(expected_item.to_json)
Expand Down
6 changes: 3 additions & 3 deletions modules/costs/spec/models/project/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def latest_activity
budget2.update(updated_at: initial_time - 20.seconds)

# there is a loss of precision for timestamps stored in database
expect(latest_activity).to be_within(0.00001).of(budget.updated_at)
expect(latest_activity).to equal_time_without_usec(budget.updated_at)
end

it "takes the time stamp of the latest activity across models" do
Expand All @@ -71,15 +71,15 @@ def latest_activity
# work_package
# budget

expect(latest_activity).to be_within(0.00001).of(work_package.updated_at)
expect(latest_activity).to equal_time_without_usec(work_package.updated_at)

work_package.update(updated_at: budget.updated_at - 10.seconds)

# Order:
# budget
# work_package

expect(latest_activity).to be_within(0.00001).of(budget.updated_at)
expect(latest_activity).to equal_time_without_usec(budget.updated_at)
end
end
end
6 changes: 3 additions & 3 deletions modules/meeting/spec/models/project/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def latest_activity
meeting2.update(updated_at: initial_time - 20.seconds)

# there is a loss of precision for timestamps stored in database
expect(latest_activity).to be_within(0.00001).of(meeting.updated_at)
expect(latest_activity).to equal_time_without_usec(meeting.updated_at)
end

it "takes the time stamp of the latest activity across models" do
Expand All @@ -71,15 +71,15 @@ def latest_activity
# work_package
# meeting

expect(latest_activity).to be_within(0.00001).of(work_package.updated_at)
expect(latest_activity).to equal_time_without_usec(work_package.updated_at)

work_package.update(updated_at: meeting.updated_at - 10.seconds)

# Order:
# meeting
# work_package

expect(latest_activity).to be_within(0.00001).of(meeting.updated_at)
expect(latest_activity).to equal_time_without_usec(meeting.updated_at)
end
end
end
4 changes: 2 additions & 2 deletions spec/controllers/concerns/auth_source_sso_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
shared_examples "should log in the user" do
it "logs in given user" do
expect(response).to redirect_to my_account_path
expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.current)
expect(user.reload.last_login_on).to equal_time_without_usec(Time.current)
expect(session[:user_id]).to eq user.id
end
end
Expand Down Expand Up @@ -194,7 +194,7 @@
expect(session[:updated_at]).to be > session_update_time

# User not is not relogged
expect(user.reload.last_login_on).to be_within(1.second).of(last_login)
expect(user.reload.last_login_on).to equal_time_without_usec(last_login)

# Session values are kept
expect(session[:should_be_kept]).to be true
Expand Down
26 changes: 13 additions & 13 deletions spec/models/projects/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def latest_activity
work_package2.reload

# there is a loss of precision for timestamps stored in database
expect(latest_activity).to be_within(0.00001).of(work_package.updated_at)
expect(latest_activity).to equal_time_without_usec(work_package.updated_at)
end

it "is the latest wiki_pages update" do
Expand All @@ -132,7 +132,7 @@ def latest_activity
wiki_page.reload
wiki_page2.reload

expect(latest_activity).to be_within(0.00001).of(wiki_page.updated_at)
expect(latest_activity).to equal_time_without_usec(wiki_page.updated_at)
end

it "is the latest news update" do
Expand All @@ -141,7 +141,7 @@ def latest_activity
news.reload
news2.reload

expect(latest_activity).to be_within(0.00001).of(news.updated_at)
expect(latest_activity).to equal_time_without_usec(news.updated_at)
end

it "is the latest changeset update" do
Expand All @@ -150,7 +150,7 @@ def latest_activity
changeset.reload
changeset2.reload

expect(latest_activity).to be_within(0.00001).of(changeset.committed_on)
expect(latest_activity).to equal_time_without_usec(changeset.committed_on)
end

it "is the latest message update" do
Expand All @@ -159,7 +159,7 @@ def latest_activity
message.reload
message2.reload

expect(latest_activity).to be_within(0.00001).of(message.updated_at)
expect(latest_activity).to equal_time_without_usec(message.updated_at)
end

it "is the latest time_entry update" do
Expand All @@ -169,14 +169,14 @@ def latest_activity
time_entry.reload
time_entry2.reload

expect(latest_activity).to be_within(0.00001).of(time_entry.updated_at)
expect(latest_activity).to equal_time_without_usec(time_entry.updated_at)
end

it "is the latest project update" do
work_package.update(updated_at: initial_time - 60.seconds)
project.update(updated_at: initial_time - 10.seconds)

expect(latest_activity).to be_within(0.00001).of(project.updated_at)
expect(latest_activity).to equal_time_without_usec(project.updated_at)
end

it "takes the time stamp of the latest activity across models" do
Expand All @@ -201,7 +201,7 @@ def latest_activity
# message
# project

expect(latest_activity).to be_within(0.00001).of(work_package.updated_at)
expect(latest_activity).to equal_time_without_usec(work_package.updated_at)

work_package.update(updated_at: project.updated_at - 10.seconds)

Expand All @@ -213,7 +213,7 @@ def latest_activity
# project
# work_package

expect(latest_activity).to be_within(0.00001).of(wiki_page.updated_at)
expect(latest_activity).to equal_time_without_usec(wiki_page.updated_at)

wiki_page.update(updated_at: work_package.updated_at - 10.seconds)

Expand All @@ -225,7 +225,7 @@ def latest_activity
# work_package
# wiki_page

expect(latest_activity).to be_within(0.00001).of(news.updated_at)
expect(latest_activity).to equal_time_without_usec(news.updated_at)

news.update(updated_at: wiki_page.updated_at - 10.seconds)

Expand All @@ -237,7 +237,7 @@ def latest_activity
# wiki_page
# news

expect(latest_activity).to be_within(0.00001).of(changeset.committed_on)
expect(latest_activity).to equal_time_without_usec(changeset.committed_on)

changeset.update(committed_on: news.updated_at - 10.seconds)

Expand All @@ -249,7 +249,7 @@ def latest_activity
# news
# changeset

expect(latest_activity).to be_within(0.00001).of(message.updated_at)
expect(latest_activity).to equal_time_without_usec(message.updated_at)

message.update(updated_at: changeset.committed_on - 10.seconds)

Expand All @@ -261,7 +261,7 @@ def latest_activity
# changeset
# message

expect(latest_activity).to be_within(0.00001).of(project.updated_at)
expect(latest_activity).to equal_time_without_usec(project.updated_at)
end
end
end
4 changes: 2 additions & 2 deletions spec/services/projects/create_service_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
let(:attributes) do
{
name: "test",
created_at:,
created_at:
}
end

Expand All @@ -55,7 +55,7 @@
.to be_success

new_project.reload
expect(new_project.created_at).to be_within(1.second).of created_at
expect(new_project.created_at).to equal_time_without_usec(created_at)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
expect(service_result)
.to be_success

expect(new_work_package.created_at).to be_within(1.second).of(created_at)
expect(new_work_package.created_at).to equal_time_without_usec(created_at)
end
end

Expand Down
34 changes: 34 additions & 0 deletions spec/support/time_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 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.
#++
RSpec::Matchers.define :equal_time_without_usec do |expected|
failure_message { "expected: #{expected.iso8601}, actual: #{actual.iso8601}, difference: #{actual - expected}s" }

match do |actual|
actual.change(usec: 0) == expected.change(usec: 0)
end
end

0 comments on commit 1a14641

Please sign in to comment.