Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update callbacks to have _commit suffix #158

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/dfe/analytics/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ module Entities
included do
attr_accessor :event_tags

after_create do
after_create_commit do
extracted_attributes = DfE::Analytics.extract_model_attributes(self)
send_event('create_entity', extracted_attributes) if extracted_attributes.any?
end

after_destroy do
after_destroy_commit do
extracted_attributes = DfE::Analytics.extract_model_attributes(self)
send_event('delete_entity', extracted_attributes) if extracted_attributes.any?
end

after_update do
after_update_commit do
# in this after_update hook we don't have access to the new fields via
# #attributes — we need to dig them out of saved_changes which stores
# them in the format { attr: ['old', 'new'] }
Expand Down
61 changes: 46 additions & 15 deletions spec/dfe/analytics/entities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@
Candidate.create(id: 123, email_address: 'adrienne@example.com')

expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
.with([a_hash_including({
'data' => match([ # #match will cause a strict match within hash_including
.with([a_hash_including({ # #match will cause a strict match within hash_including
'data' => match([
{ 'key' => 'id', 'value' => [123] }
])
})])
Expand Down Expand Up @@ -176,9 +176,9 @@
entity.update(last_name: 'GB')

expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later)
.with([a_hash_including({
'event_type' => 'update_entity'
})])
.with([a_hash_including({
'event_type' => 'update_entity'
})])
end

it 'sends events that are valid according to the schema' do
Expand All @@ -202,9 +202,9 @@
entity.update(first_name: 'Persephone')

expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later)
.with([a_hash_including({
'event_type' => 'update_entity'
})])
.with([a_hash_including({
'event_type' => 'update_entity'
})])
end
end

Expand All @@ -218,20 +218,20 @@

expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
.with([a_hash_including({
'event_type' => 'update_entity',
'data' => array_including(a_hash_including('key' => 'email_address', 'value' => ['updated@example.com']))
})])
'event_type' => 'update_entity',
'data' => array_including(a_hash_including('key' => 'email_address', 'value' => ['updated@example.com']))
})])
end

it 'sends events with updated allowed field and with updated hidden data' do
candidate.update(email_address: 'updated@example.com', dob: '21062000')

expect(DfE::Analytics::SendEvents).to have_received(:perform_later)
.with([a_hash_including({
'event_type' => 'update_entity',
'data' => array_including(a_hash_including('key' => 'email_address', 'value' => ['updated@example.com'])),
'hidden_data' => array_including(a_hash_including('key' => 'dob', 'value' => ['21062000']))
})])
'event_type' => 'update_entity',
'data' => array_including(a_hash_including('key' => 'email_address', 'value' => ['updated@example.com'])),
'hidden_data' => array_including(a_hash_including('key' => 'dob', 'value' => ['21062000']))
})])
end
end
end
Expand Down Expand Up @@ -270,4 +270,35 @@
end
end
end

describe 'rollback behavior' do
it 'does not send create event if the transaction is rolled back' do
ActiveRecord::Base.transaction do
Candidate.create(id: 123)
raise ActiveRecord::Rollback
end

expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later)
end

it 'does not send update event if the transaction is rolled back' do
entity = Candidate.create(email_address: 'foo@bar.com', first_name: 'Jason')
ActiveRecord::Base.transaction do
entity.update(email_address: 'bar@baz.com')
raise ActiveRecord::Rollback
end

expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later)
end

it 'does not send delete event if the transaction is rolled back' do
entity = Candidate.create(email_address: 'boo@example.com')
ActiveRecord::Base.transaction do
entity.destroy
raise ActiveRecord::Rollback
end

expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later)
end
end
end
Loading