Commit bfb20200 authored by Nick Thomas's avatar Nick Thomas

Add a be_like_time matcher and use it in specs

The amount of precision times have in databases is variable, so we need
tolerances when comparing in specs. It's better to have the tolerance defined
in one place than several.
parent 77507df6
...@@ -13,7 +13,7 @@ describe Issue::Metrics, models: true do ...@@ -13,7 +13,7 @@ describe Issue::Metrics, models: true do
metrics = subject.metrics metrics = subject.metrics
expect(metrics).to be_present expect(metrics).to be_present
expect(metrics.first_associated_with_milestone_at).to be_within(1.second).of(time) expect(metrics.first_associated_with_milestone_at).to be_like_time(time)
end end
it "does not record the second time an issue is associated with a milestone" do it "does not record the second time an issue is associated with a milestone" do
...@@ -24,7 +24,7 @@ describe Issue::Metrics, models: true do ...@@ -24,7 +24,7 @@ describe Issue::Metrics, models: true do
metrics = subject.metrics metrics = subject.metrics
expect(metrics).to be_present expect(metrics).to be_present
expect(metrics.first_associated_with_milestone_at).to be_within(1.second).of(time) expect(metrics.first_associated_with_milestone_at).to be_like_time(time)
end end
end end
...@@ -36,7 +36,7 @@ describe Issue::Metrics, models: true do ...@@ -36,7 +36,7 @@ describe Issue::Metrics, models: true do
metrics = subject.metrics metrics = subject.metrics
expect(metrics).to be_present expect(metrics).to be_present
expect(metrics.first_added_to_board_at).to be_within(1.second).of(time) expect(metrics.first_added_to_board_at).to be_like_time(time)
end end
it "does not record the second time an issue is associated with a list label" do it "does not record the second time an issue is associated with a list label" do
...@@ -48,7 +48,7 @@ describe Issue::Metrics, models: true do ...@@ -48,7 +48,7 @@ describe Issue::Metrics, models: true do
metrics = subject.metrics metrics = subject.metrics
expect(metrics).to be_present expect(metrics).to be_present
expect(metrics.first_added_to_board_at).to be_within(1.second).of(time) expect(metrics.first_added_to_board_at).to be_like_time(time)
end end
end end
end end
......
...@@ -12,7 +12,7 @@ describe MergeRequest::Metrics, models: true do ...@@ -12,7 +12,7 @@ describe MergeRequest::Metrics, models: true do
metrics = subject.metrics metrics = subject.metrics
expect(metrics).to be_present expect(metrics).to be_present
expect(metrics.merged_at).to be_within(1.second).of(time) expect(metrics.merged_at).to be_like_time(time)
end end
end end
end end
...@@ -694,7 +694,7 @@ describe API::API, api: true do ...@@ -694,7 +694,7 @@ describe API::API, api: true do
title: 'new issue', labels: 'label, label2', created_at: creation_time title: 'new issue', labels: 'label, label2', created_at: creation_time
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(Time.parse(json_response['created_at'])).to be_within(1.second).of(creation_time) expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
end end
end end
end end
...@@ -895,7 +895,7 @@ describe API::API, api: true do ...@@ -895,7 +895,7 @@ describe API::API, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['labels']).to include 'label3' expect(json_response['labels']).to include 'label3'
expect(Time.parse(json_response['updated_at'])).to be_within(1.second).of(update_time) expect(Time.parse(json_response['updated_at'])).to be_like_time(update_time)
end end
end end
end end
......
...@@ -217,7 +217,7 @@ describe API::API, api: true do ...@@ -217,7 +217,7 @@ describe API::API, api: true do
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(json_response['body']).to eq('hi!') expect(json_response['body']).to eq('hi!')
expect(json_response['author']['username']).to eq(user.username) expect(json_response['author']['username']).to eq(user.username)
expect(Time.parse(json_response['created_at'])).to be_within(1.second).of(creation_time) expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
end end
end end
......
...@@ -201,7 +201,7 @@ describe CreateDeploymentService, services: true do ...@@ -201,7 +201,7 @@ describe CreateDeploymentService, services: true do
time = Time.now time = Time.now
Timecop.freeze(time) { service.execute } Timecop.freeze(time) { service.execute }
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_within(1.second).of(time) expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
end end
it "doesn't set the time if the deploy's environment is not 'production'" do it "doesn't set the time if the deploy's environment is not 'production'" do
...@@ -227,13 +227,13 @@ describe CreateDeploymentService, services: true do ...@@ -227,13 +227,13 @@ describe CreateDeploymentService, services: true do
time = Time.now time = Time.now
Timecop.freeze(time) { service.execute } Timecop.freeze(time) { service.execute }
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_within(1.second).of(time) expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
# Current deploy # Current deploy
service = described_class.new(project, user, params) service = described_class.new(project, user, params)
Timecop.freeze(time + 12.hours) { service.execute } Timecop.freeze(time + 12.hours) { service.execute }
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_within(1.second).of(time) expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
end end
end end
......
...@@ -364,7 +364,7 @@ describe GitPushService, services: true do ...@@ -364,7 +364,7 @@ describe GitPushService, services: true do
it 'sets the metric for referenced issues' do it 'sets the metric for referenced issues' do
execute_service(project, user, @oldrev, @newrev, @ref) execute_service(project, user, @oldrev, @newrev, @ref)
expect(issue.reload.metrics.first_mentioned_in_commit_at).to be_within(1.second).of(commit_time) expect(issue.reload.metrics.first_mentioned_in_commit_at).to be_like_time(commit_time)
end end
it 'does not set the metric for non-referenced issues' do it 'does not set the metric for non-referenced issues' do
......
RSpec::Matchers.define :be_like_time do |expected|
match do |actual|
expect(actual).to be_within(1.second).of(expected)
end
description do
"within one second of #{expected}"
end
failure_message do |actual|
"expected #{actual} to be within one second of #{expected}"
end
end
...@@ -23,7 +23,7 @@ describe PipelineMetricsWorker do ...@@ -23,7 +23,7 @@ describe PipelineMetricsWorker do
it 'records the build start time' do it 'records the build start time' do
subject subject
expect(merge_request.reload.metrics.latest_build_started_at).to be_within(1.second).of(pipeline.started_at) expect(merge_request.reload.metrics.latest_build_started_at).to be_like_time(pipeline.started_at)
end end
it 'clears the build end time' do it 'clears the build end time' do
...@@ -39,7 +39,7 @@ describe PipelineMetricsWorker do ...@@ -39,7 +39,7 @@ describe PipelineMetricsWorker do
it 'records the build end time' do it 'records the build end time' do
subject subject
expect(merge_request.reload.metrics.latest_build_finished_at).to be_within(1.second).of(pipeline.finished_at) expect(merge_request.reload.metrics.latest_build_finished_at).to be_like_time(pipeline.finished_at)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment