|
59 | 59 |
|
60 | 60 | describe '#call' do |
61 | 61 | 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) } |
63 | 62 | let(:order_column) { 'UPDATED_AT' } |
64 | 63 | let(:course_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('course') } } |
65 | 64 | let(:institution_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('institution') } } |
66 | 65 | let(:application_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('application') } } |
67 | 66 | let(:candidate_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('candidate') } } |
68 | 67 | let(:department_entity) { DfE::Analytics.entities_for_analytics.find { |entity| entity.to_s.include?('department') } } |
69 | 68 | 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) } |
70 | 74 |
|
71 | | - before { Timecop.freeze(checksum_calculated_at) } |
| 75 | + before do |
| 76 | + Timecop.freeze(current_timestamp) |
| 77 | + end |
72 | 78 | after { Timecop.return } |
73 | 79 |
|
74 | 80 | it 'returns if the adapter or environment is unsupported' do |
|
92 | 98 |
|
93 | 99 | it 'orders by created_at if updated_at is missing' do |
94 | 100 | order_column = 'CREATED_AT' |
95 | | - |
96 | 101 | [123, 124, 125].map { |id| Application.create(id: id) } |
97 | 102 | application_entities = DfE::Analytics.entities_for_analytics.select { |entity| entity.to_s.include?('application') } |
98 | 103 | table_ids = Application.where('created_at < ?', checksum_calculated_at).order(created_at: :asc).pluck(:id) |
|
120 | 125 |
|
121 | 126 | candidate_entities.each do |candidate| |
122 | 127 | 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) |
146 | 128 |
|
147 | 129 | expect(DfE::Analytics::SendEvents).to have_received(:perform_later) |
148 | | - .with([a_hash_including({ |
| 130 | + .with([hash_including({ |
149 | 131 | 'data' => [ |
150 | 132 | { 'key' => 'row_count', 'value' => [table_ids.size] }, |
151 | 133 | { 'key' => 'checksum', 'value' => [checksum] }, |
152 | 134 | { 'key' => 'checksum_calculated_at', 'value' => [checksum_calculated_at] }, |
153 | 135 | { 'key' => 'order_column', 'value' => [order_column] } |
154 | 136 | ] |
155 | | - })]) |
| 137 | + })]) |
156 | 138 | end |
157 | 139 | end |
158 | 140 |
|
| 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 | + |
159 | 163 | it 'returns zero rows and checksum if table is empty' do |
160 | 164 | table_ids = Candidate.where('updated_at < ?', checksum_calculated_at).order(updated_at: :asc).pluck(:id) |
161 | 165 | checksum = Digest::MD5.hexdigest(table_ids.join) |
|
0 commit comments