MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / TestGetBalanceByID

Function TestGetBalanceByID

balance_test.go:178–227  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

176}
177
178func TestGetBalanceByID(t *testing.T) {
179 datasource, mock, err := newTestDataSource()
180 if err != nil {
181 t.Fatalf("Error creating test data source: %s", err)
182 }
183
184 d, err := NewLedgerForge(datasource)
185 if err != nil {
186 t.Fatalf("Error creating LedgerForge instance: %s", err)
187 }
188 balanceID := gofakeit.UUID()
189
190 mock.ExpectBegin()
191
192 // Adjust the expected SQL to match the actual SQL output.
193 expectedSQL := `SELECT b\.balance_id, b\.balance, b\.credit_balance, b\.debit_balance, b\.currency, b\.currency_multiplier, b\.ledger_id, COALESCE\(b\.identity_id, ''\) as identity_id, b\.created_at, b\.meta_data, b\.inflight_balance, b\.inflight_credit_balance, b\.inflight_debit_balance, b\.version, b\.indicator, b\.track_fund_lineage, COALESCE\(b\.allocation_strategy, 'FIFO'\) as allocation_strategy FROM \( SELECT \* FROM ledgerforge\.balances WHERE balance_id = \$1 \) AS b`
194 rows := sqlmock.NewRows([]string{"balance_id", "balance", "credit_balance", "debit_balance", "currency", "currency_multiplier", "ledger_id", "identity_id", "created_at", "meta_data", "inflight_balance", "inflight_credit_balance", "inflight_debit_balance", "version", "indicator", "track_fund_lineage", "allocation_strategy"}).
195 AddRow(balanceID,
196 BigIntString{big.NewInt(100)},
197 BigIntString{big.NewInt(50)},
198 BigIntString{big.NewInt(50)},
199 "USD", 100, "test-ledger", "test-identity", time.Now(),
200 `{"key":"value"}`,
201 BigIntString{big.NewInt(0)},
202 BigIntString{big.NewInt(0)},
203 BigIntString{big.NewInt(0)},
204 0,
205 "test-indicator",
206 false,
207 "FIFO")
208
209 mock.ExpectQuery(expectedSQL).
210 WithArgs(balanceID).
211 WillReturnRows(rows)
212
213 mock.ExpectCommit()
214
215 result, err := d.GetBalanceByID(context.Background(), balanceID, nil, false)
216 assert.NoError(t, err)
217 assert.NotNil(t, result)
218 assert.Equal(t, balanceID, result.BalanceID)
219 assert.Equal(t, big.NewInt(100), result.Balance)
220 assert.Equal(t, big.NewInt(50), result.CreditBalance)
221 assert.Equal(t, big.NewInt(50), result.DebitBalance)
222 assert.Equal(t, "test-indicator", result.Indicator)
223
224 if err := mock.ExpectationsWereMet(); err != nil {
225 t.Errorf("there were unfulfilled expectations: %s", err)
226 }
227}
228
229func TestGetAllBalances(t *testing.T) {
230 datasource, mock, err := newTestDataSource()

Callers

nothing calls this directly

Calls 3

GetBalanceByIDMethod · 0.95
newTestDataSourceFunction · 0.85
NewLedgerForgeFunction · 0.85

Tested by

no test coverage detected