-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
136 lines (106 loc) · 3.62 KB
/
Copy pathmain_test.go
File metadata and controls
136 lines (106 loc) · 3.62 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
func setupTestAccounts() {
fmt.Println("Setting up test accounts")
accounts = map[string]*Account{
"Mark": {Name: "Mark", Balance: 100, Mutex: sync.Mutex{}},
"Jane": {Name: "Jane", Balance: 50, Mutex: sync.Mutex{}},
"Adam": {Name: "Adam", Balance: 0, Mutex: sync.Mutex{}},
}
fmt.Println("Test accounts initialized")
}
func TestTransferMoney_Success(t *testing.T) {
setupTestAccounts()
fmt.Println("Starting TestTransferMoney_Success")
reqBody := TransferRequest{
From: "Mark",
To: "Jane",
Amount: 30,
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/transfer", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(transferMoney)
handler.ServeHTTP(rr, req)
fmt.Printf("Response code: %d, Body: %s", rr.Code, rr.Body.String())
if rr.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", rr.Code)
}
expectedSenderBalance := 70
expectedReceiverBalance := 80
if accounts["Mark"].Balance != expectedSenderBalance {
t.Errorf("Expected Mark's balance to be %d, got %d", expectedSenderBalance, accounts["Mark"].Balance)
}
if accounts["Jane"].Balance != expectedReceiverBalance {
t.Errorf("Expected Jane's balance to be %d, got %d", expectedReceiverBalance, accounts["Jane"].Balance)
}
fmt.Println("TestTransferMoney_Success completed successfully")
}
func TestTransferMoney_InsufficientFunds(t *testing.T) {
setupTestAccounts()
fmt.Println("Starting TestTransferMoney_InsufficientFunds")
reqBody := TransferRequest{
From: "Jane",
To: "Mark",
Amount: 100, // More than Jane has
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/transfer", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(transferMoney)
handler.ServeHTTP(rr, req)
fmt.Printf("Response code: %d, Body: %s", rr.Code, rr.Body.String())
if rr.Code != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", rr.Code)
}
fmt.Println("TestTransferMoney_InsufficientFunds completed successfully")
}
func TestTransferMoney_SameAccount(t *testing.T) {
setupTestAccounts()
fmt.Println("Starting TestTransferMoney_SameAccount")
reqBody := TransferRequest{
From: "Mark",
To: "Mark",
Amount: 10,
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/transfer", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(transferMoney)
handler.ServeHTTP(rr, req)
fmt.Printf("Response code: %d, Body: %s", rr.Code, rr.Body.String())
if rr.Code != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", rr.Code)
}
fmt.Println("TestTransferMoney_SameAccount completed successfully")
}
func TestGetBalance(t *testing.T) {
setupTestAccounts()
fmt.Println("Starting TestGetBalance")
req := httptest.NewRequest(http.MethodGet, "/balance?name=Mark", nil)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(getBalance)
handler.ServeHTTP(rr, req)
fmt.Printf("Response code: %d, Body: %s", rr.Code, rr.Body.String())
if rr.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", rr.Code)
}
var resp map[string]int
json.Unmarshal(rr.Body.Bytes(), &resp)
expectedBalance := 100
if resp["balance"] != expectedBalance {
t.Errorf("Expected balance to be %d, got %d", expectedBalance, resp["balance"])
}
fmt.Println("TestGetBalance completed successfully")
}