Commit 4e1f2606 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Fix flaky tests on historical vulnerability statistic specs

There were two problems with the previous specs;

1) They tended to fail at runs around midnight
2) `Date.today` does not respect to time zone configuration of Rails
which makes it incompatible with the value of `Time.zone.now` which is
used by Rails internally

Mocking time and using `Date.current` which respects the time zone
fixes the above issues.
parent 0cd4feb5
...@@ -4,6 +4,6 @@ FactoryBot.define do ...@@ -4,6 +4,6 @@ FactoryBot.define do
factory :vulnerability_historical_statistic, class: 'Vulnerabilities::HistoricalStatistic' do factory :vulnerability_historical_statistic, class: 'Vulnerabilities::HistoricalStatistic' do
project project
letter_grade { 'a' } letter_grade { 'a' }
date { Date.today } date { Date.current }
end end
end end
...@@ -5,6 +5,10 @@ require 'spec_helper' ...@@ -5,6 +5,10 @@ require 'spec_helper'
RSpec.describe Vulnerabilities::HistoricalStatistics::AdjustmentService do RSpec.describe Vulnerabilities::HistoricalStatistics::AdjustmentService do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
around do |example|
travel_to(Date.current) { example.run }
end
describe '.execute' do describe '.execute' do
let(:project_ids) { [1, 2, 3] } let(:project_ids) { [1, 2, 3] }
let(:mock_service_object) { instance_double(described_class, execute: true) } let(:mock_service_object) { instance_double(described_class, execute: true) }
...@@ -37,7 +41,7 @@ RSpec.describe Vulnerabilities::HistoricalStatistics::AdjustmentService do ...@@ -37,7 +41,7 @@ RSpec.describe Vulnerabilities::HistoricalStatistics::AdjustmentService do
'info' => 0, 'info' => 0,
'unknown' => 0, 'unknown' => 0,
'letter_grade' => 'f', 'letter_grade' => 'f',
'date' => Date.today.to_s 'date' => Date.current.to_s
} }
end end
......
...@@ -30,6 +30,10 @@ RSpec.describe Vulnerabilities::HistoricalStatistics::UpdateService do ...@@ -30,6 +30,10 @@ RSpec.describe Vulnerabilities::HistoricalStatistics::UpdateService do
end end
context 'when there is already a record in the database' do context 'when there is already a record in the database' do
around do |example|
travel_to(Date.current) { example.run }
end
it 'changes the existing historical statistic entity' do it 'changes the existing historical statistic entity' do
historical_statistic = create(:vulnerability_historical_statistic, project: project, letter_grade: 'c') historical_statistic = create(:vulnerability_historical_statistic, project: project, letter_grade: 'c')
......
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