-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
65 lines (54 loc) · 1.93 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"testing"
"time"
)
func TestCalculateTotalBalance(t *testing.T) {
transactions := []Transaction{
{ID: 0, Date: time.Now(), Transaction: 100},
{ID: 1, Date: time.Now(), Transaction: -50},
{ID: 2, Date: time.Now(), Transaction: 75},
}
expectedBalance := 125.0
totalBalance := calculateTotalBalance(transactions)
if totalBalance != expectedBalance {
t.Errorf("Expected total balance %.2f, got %.2f", expectedBalance, totalBalance)
}
}
func TestGroupTransactionsByMonth(t *testing.T) {
transactions := []Transaction{
{ID: 0, Date: time.Date(2023, time.January, 10, 0, 0, 0, 0, time.UTC), Transaction: 100},
{ID: 1, Date: time.Date(2023, time.February, 15, 0, 0, 0, 0, time.UTC), Transaction: -50},
{ID: 2, Date: time.Date(2023, time.January, 20, 0, 0, 0, 0, time.UTC), Transaction: 75},
}
expectedCounts := map[string]int{
"January": 2,
"February": 1,
}
transactionCounts := groupTransactionsByMonth(transactions)
if len(transactionCounts) != len(expectedCounts) {
t.Errorf("Expected %d months, got %d", len(expectedCounts), len(transactionCounts))
}
for month, expectedCount := range expectedCounts {
if count, ok := transactionCounts[month]; !ok || count != expectedCount {
t.Errorf("Expected transaction count for %s: %d, got %d", month, expectedCount, count)
}
}
}
func TestCalculateAverageAmounts(t *testing.T) {
transactions := []Transaction{
{ID: 0, Date: time.Now(), Transaction: 100},
{ID: 1, Date: time.Now(), Transaction: -50},
{ID: 2, Date: time.Now(), Transaction: -25},
{ID: 3, Date: time.Now(), Transaction: 75},
}
expectedDebit := -37.5
expectedCredit := 87.5
averageDebit, averageCredit := calculateAverageAmounts(transactions)
if averageDebit != expectedDebit {
t.Errorf("Expected average debit %.2f, got %.2f", expectedDebit, averageDebit)
}
if averageCredit != expectedCredit {
t.Errorf("Expected average credit %.2f, got %.2f", expectedCredit, averageCredit)
}
}