Skip to content

Commit be1e516

Browse files
authored
Fix for flakey tests (#134)
* Freeze current_timestamp, set instance variable * Update variable syntax
1 parent 7af1a0a commit be1e516

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

spec/dfe/analytics/services/entity_table_checks_spec.rb

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,22 @@
5959

6060
describe '#call' do
6161
let(:time_zone) { 'London' }
62-
let(:checksum_calculated_at) { ActiveRecord::Base.connection.select_all('SELECT CURRENT_TIMESTAMP AS current_timestamp').first['current_timestamp'].in_time_zone(time_zone).iso8601(6) }
6362
let(:order_column) { 'UPDATED_AT' }
6463
let(:course_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('course') } }
6564
let(:institution_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('institution') } }
6665
let(:application_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('application') } }
6766
let(:candidate_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('candidate') } }
6867
let(:department_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('department') } }
6968
let(:entity_type) { 'entity_table_check' }
69+
let(:checksum_calculated_at) { @checksum_calculated_at }
70+
let(:current_timestamp) do
71+
ActiveRecord::Base.connection.select_all('SELECT CURRENT_TIMESTAMP AS current_timestamp').first['current_timestamp'].in_time_zone(time_zone)
72+
end
73+
let(:checksum_calculated_at) { current_timestamp.iso8601(6) }
7074

71-
before { Timecop.freeze(checksum_calculated_at) }
75+
before do
76+
Timecop.freeze(current_timestamp)
77+
end
7278
after { Timecop.return }
7379

7480
it 'returns if the adapter or environment is unsupported' do
@@ -92,7 +98,6 @@
9298

9399
it 'orders by created_at if updated_at is missing' do
94100
order_column = 'CREATED_AT'
95-
96101
[123, 124, 125].map { |id| Application.create(id: id) }
97102
application_entities = DfE::Analytics.entities_for_analytics.select { |entity| entity.to_s.include?('application') }
98103
table_ids = Application.where('created_at < ?', checksum_calculated_at).order(created_at: :asc).pluck(:id)
@@ -120,42 +125,41 @@
120125

121126
candidate_entities.each do |candidate|
122127
described_class.call(entity_name: candidate, entity_type: entity_type, entity_tag: nil)
123-
expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
124-
.with([a_hash_including({
125-
'data' =>
126-
[
127-
{ 'key' => 'row_count', 'value' => [table_ids.size] },
128-
{ 'key' => 'checksum', 'value' => [checksum] },
129-
{ 'key' => 'checksum_calculated_at', 'value' => [checksum_calculated_at] },
130-
{ 'key' => 'order_column', 'value' => [order_column] }
131-
]
132-
})])
133-
end
134-
end
135-
136-
it 'does not send the event if updated_at is greater than checksum_calculated_at' do
137-
Timecop.freeze(DateTime.parse(checksum_calculated_at)) do
138-
Candidate.create(id: '123', updated_at: DateTime.parse(checksum_calculated_at) - 2.hours)
139-
Candidate.create(id: '124', updated_at: DateTime.parse(checksum_calculated_at) - 5.hours)
140-
Candidate.create(id: '125', updated_at: DateTime.parse(checksum_calculated_at) + 5.hours)
141-
142-
table_ids = Candidate.where('updated_at < ?', checksum_calculated_at).order(:updated_at).pluck(:id)
143-
checksum = Digest::MD5.hexdigest(table_ids.join)
144-
145-
described_class.call(entity_name: candidate_entity, entity_type: entity_type, entity_tag: nil)
146128

147129
expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
148-
.with([a_hash_including({
130+
.with([hash_including({
149131
'data' => [
150132
{ 'key' => 'row_count', 'value' => [table_ids.size] },
151133
{ 'key' => 'checksum', 'value' => [checksum] },
152134
{ 'key' => 'checksum_calculated_at', 'value' => [checksum_calculated_at] },
153135
{ 'key' => 'order_column', 'value' => [order_column] }
154136
]
155-
})])
137+
})])
156138
end
157139
end
158140

141+
it 'does not send the event if updated_at is greater than checksum_calculated_at' do
142+
Candidate.create(id: '123', updated_at: DateTime.parse(checksum_calculated_at) - 2.hours)
143+
Candidate.create(id: '124', updated_at: DateTime.parse(checksum_calculated_at) - 5.hours)
144+
Candidate.create(id: '125', updated_at: DateTime.parse(checksum_calculated_at) + 5.hours)
145+
146+
table_ids = Candidate.where('updated_at < ?', checksum_calculated_at).order(:updated_at).pluck(:id)
147+
checksum = Digest::MD5.hexdigest(table_ids.join)
148+
149+
described_class.call(entity_name: candidate_entity, entity_type: entity_type, entity_tag: nil)
150+
puts "@checksum_calculated_at set to #{checksum_calculated_at}" # Debugging output
151+
152+
expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
153+
.with([a_hash_including({
154+
'data' => [
155+
{ 'key' => 'row_count', 'value' => [table_ids.size] },
156+
{ 'key' => 'checksum', 'value' => [checksum] },
157+
{ 'key' => 'checksum_calculated_at', 'value' => [checksum_calculated_at] },
158+
{ 'key' => 'order_column', 'value' => [order_column] }
159+
]
160+
})])
161+
end
162+
159163
it 'returns zero rows and checksum if table is empty' do
160164
table_ids = Candidate.where('updated_at < ?', checksum_calculated_at).order(updated_at: :asc).pluck(:id)
161165
checksum = Digest::MD5.hexdigest(table_ids.join)

0 commit comments

Comments
 (0)