-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,4 +283,55 @@ | |
expect(DfE::Analytics::SendEvents).not_to have_received(:perform_later) | ||
end | ||
end | ||
|
||
describe 'transaction behaviour' do | ||
let(:allowlist_fields) { %w[email_address first_name] } | ||
|
||
it 'sends transaction create event with the correct data' do | ||
ActiveRecord::Base.transaction do | ||
Candidate.create(email_address: '[email protected]', first_name: 'Jason') | ||
end | ||
expect(DfE::Analytics::SendEvents).to have_received(:perform_later) | ||
.with([a_hash_including({ | ||
'entity_table_name' => Candidate.table_name, | ||
'event_type' => 'create_entity', | ||
'data' => [ | ||
{ 'key' => 'email_address', 'value' => ['[email protected]'] }, | ||
{ 'key' => 'first_name', 'value' => ['Jason'] } | ||
] | ||
})]) | ||
end | ||
|
||
it 'sends transaction delete event with the correct data' do | ||
entity = Candidate.create(email_address: '[email protected]', first_name: 'Jason') | ||
ActiveRecord::Base.transaction do | ||
entity.destroy | ||
end | ||
expect(DfE::Analytics::SendEvents).to have_received(:perform_later) | ||
.with([a_hash_including({ | ||
'entity_table_name' => Candidate.table_name, | ||
'event_type' => 'delete_entity', | ||
'data' => [ | ||
{ 'key' => 'email_address', 'value' => ['[email protected]'] }, | ||
{ 'key' => 'first_name', 'value' => ['Jason'] } | ||
] | ||
})]) | ||
end | ||
|
||
it 'sends transaction update event with the correct data' do | ||
entity = Candidate.create(email_address: '[email protected]', first_name: 'Jason') | ||
ActiveRecord::Base.transaction do | ||
entity.update(email_address: '[email protected]') | ||
end | ||
expect(DfE::Analytics::SendEvents).to have_received(:perform_later) | ||
.with([a_hash_including({ | ||
'entity_table_name' => Candidate.table_name, | ||
'event_type' => 'update_entity', | ||
'data' => [ | ||
{ 'key' => 'email_address', 'value' => ['[email protected]'] }, | ||
{ 'key' => 'first_name', 'value' => ['Jason'] } | ||
] | ||
})]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe DfE::Analytics::TransactionChanges do | ||
with_model :Candidate do | ||
table do |t| | ||
t.string :email_address | ||
t.string :last_name | ||
t.string :first_name | ||
t.string :dob | ||
end | ||
|
||
model do | ||
include DfE::Analytics::TransactionChanges | ||
attr_accessor :stored_transaction_changes | ||
|
||
after_commit :store_transaction_changes_for_tests | ||
|
||
def store_transaction_changes_for_tests | ||
@stored_transaction_changes = transaction_changed_attributes.reduce({}) do |changes, (attr_name, value)| | ||
changes[attr_name] = [value, send(attr_name)] | ||
changes | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'after commit' do | ||
context 'without transaction' do | ||
it 'create tracks changes to attributes' do | ||
entity = Candidate.create(email_address: 'foo@bar') | ||
|
||
expect(entity.stored_transaction_changes).to eq({ 'email_address' => [nil, 'foo@bar'], 'id' => [nil, 1] }) | ||
end | ||
|
||
it 'update tracks changes to attributes' do | ||
entity = Candidate.create(email_address: 'foo@bar') | ||
entity.update(email_address: 'bar@baz') | ||
|
||
expect(entity.stored_transaction_changes).to eq({ 'email_address' => ['foo@bar', 'bar@baz'] }) | ||
end | ||
end | ||
|
||
context 'with transaction' do | ||
it 'create tracks changes to attributes' do | ||
ActiveRecord::Base.transaction do | ||
@entity = Candidate.create(email_address: 'foo@bar') | ||
end | ||
|
||
expect(@entity.stored_transaction_changes).to eq({ 'email_address' => [nil, 'foo@bar'], 'id' => [nil, 1] }) | ||
end | ||
|
||
it 'update tracks changes to attributes' do | ||
entity = Candidate.create(email_address: 'foo@bar') | ||
ActiveRecord::Base.transaction do | ||
entity.update(email_address: 'bar@baz') | ||
end | ||
|
||
expect(entity.stored_transaction_changes).to eq({ 'email_address' => ['foo@bar', 'bar@baz'] }) | ||
end | ||
end | ||
end | ||
end |