From 84998c32866b72546290d2f9a5bdfe93b5390a9b Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Fri, 5 Apr 2024 10:09:37 +0200 Subject: [PATCH] test: financial transaction model (#1008 , PR #1031) --- spec/factories/financial_transaction.rb | 4 +++ spec/models/financial_transaction_spec.rb | 30 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 spec/models/financial_transaction_spec.rb diff --git a/spec/factories/financial_transaction.rb b/spec/factories/financial_transaction.rb index 6a3aa369e..bc285bb23 100644 --- a/spec/factories/financial_transaction.rb +++ b/spec/factories/financial_transaction.rb @@ -11,5 +11,9 @@ # transactions we'd use the default. This, however, is the easiest way to # get the factory going. If you want equal types, specify it explicitly. financial_transaction_type + + trait :pending do + amount { nil } + end end end diff --git a/spec/models/financial_transaction_spec.rb b/spec/models/financial_transaction_spec.rb new file mode 100644 index 000000000..9faf72762 --- /dev/null +++ b/spec/models/financial_transaction_spec.rb @@ -0,0 +1,30 @@ +require_relative '../spec_helper' + +describe FinancialTransaction do + let!(:ordergroup) { create(:ordergroup) } + let!(:ft) { create(:financial_transaction, ordergroup: ordergroup, amount: 20) } + + it 'updates the amount of the ordergroup balance' do + expect(ordergroup.account_balance).to eq(20) + create(:financial_transaction, ordergroup: ordergroup, amount: 10) + expect(ordergroup.account_balance).to eq(30) + end + + it 'can be reverted' do + ft.revert!(ft.user) + expect(ft).to be_hidden + expect(ordergroup.financial_transactions.count).to eq 2 + expect(ordergroup.financial_transactions.last.amount).to eq(-20) + expect(ordergroup.financial_transactions.last.reverts).to eq ft + expect(ordergroup.account_balance).to eq 0 + end + + context 'with a pending transaction' do + let!(:ft) { create(:financial_transaction, :pending, ordergroup: ordergroup) } + + it 'fails on revert' do + puts ft.inspect + expect { ft.revert!(ft.user) }.to raise_error(RuntimeError, 'Pending Transaction cannot be reverted') + end + end +end