Test for SplitTransactionPrecise
(t *testing.T)
| 222 | |
| 223 | // Test for SplitTransactionPrecise |
| 224 | func TestSplitTransactionPrecise(t *testing.T) { |
| 225 | // Sample transaction with sources |
| 226 | transaction := &Transaction{ |
| 227 | TransactionID: "txn_test_123", |
| 228 | Reference: "REF123", |
| 229 | Amount: 1000.00, |
| 230 | Precision: 100, |
| 231 | Sources: []Distribution{ |
| 232 | {Identifier: "A", Distribution: "200"}, |
| 233 | {Identifier: "B", Distribution: "50%"}, |
| 234 | {Identifier: "C", Distribution: "left"}, |
| 235 | }, |
| 236 | } |
| 237 | |
| 238 | // Apply precision to ensure PreciseAmount is set |
| 239 | ApplyPrecision(transaction) |
| 240 | |
| 241 | // Call the precise split function |
| 242 | splitTxns, err := transaction.SplitTransactionPrecise(context.Background()) |
| 243 | if err != nil { |
| 244 | t.Errorf("SplitTransactionPrecise() error = %v", err) |
| 245 | return |
| 246 | } |
| 247 | |
| 248 | // Check if we have the expected number of transactions |
| 249 | expectedCount := 3 |
| 250 | if len(splitTxns) != expectedCount { |
| 251 | t.Errorf("SplitTransactionPrecise() returned %d transactions, expected %d", len(splitTxns), expectedCount) |
| 252 | } |
| 253 | |
| 254 | // Check each transaction's amount |
| 255 | expectedAmounts := map[string]float64{ |
| 256 | "A": 200.00, |
| 257 | "B": 500.00, |
| 258 | "C": 300.00, |
| 259 | } |
| 260 | |
| 261 | for _, txn := range splitTxns { |
| 262 | // Find which source this transaction corresponds to |
| 263 | for identifier, expectedAmount := range expectedAmounts { |
| 264 | if txn.Source == identifier { |
| 265 | if !floatEquals(txn.Amount, expectedAmount) { |
| 266 | t.Errorf("Transaction for source %s has amount %v, expected %v", |
| 267 | identifier, txn.Amount, expectedAmount) |
| 268 | } |
| 269 | |
| 270 | // Verify PreciseAmount matches Amount |
| 271 | decPreciseAmount := decimal.NewFromBigInt(txn.PreciseAmount, 0) |
| 272 | decPrecision := decimal.NewFromFloat(txn.Precision) |
| 273 | floatAmount := decPreciseAmount.Div(decPrecision).InexactFloat64() |
| 274 | |
| 275 | if !floatEquals(floatAmount, txn.Amount) { |
| 276 | t.Errorf("Transaction for source %s has PreciseAmount equivalent to %v, expected %v", |
| 277 | identifier, floatAmount, txn.Amount) |
| 278 | } |
| 279 | |
| 280 | break |
| 281 | } |
nothing calls this directly
no test coverage detected