Skip to content

Commit

Permalink
test: financial transaction model (foodcoops#1008 , PR foodcoops#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
yksflip committed Apr 8, 2024
1 parent 48f579e commit 8b4c36e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/factories/financial_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/models/financial_transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8b4c36e

Please sign in to comment.