-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
147 lines (120 loc) · 3.34 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
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
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestHealthCheck(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
if assert.NoError(t, HealthCheckHandler(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "\"Hello, Go Bootcamp!\"\n", rec.Body.String())
}
}
func TestTaxCalculationsHandler(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/tax/calculations", strings.NewReader(`{"totalIncome": 1000000, "wht": 0, "allowances": []}`))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
taxState := TaxLevelToggle
TaxLevelToggle = false
personalState := PersonalDeduction
PersonalDeduction = 60000.0
if assert.NoError(t, TaxCalculationsHandler(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "{\"tax\":101000}\n", rec.Body.String())
}
TaxLevelToggle = taxState
PersonalDeduction = personalState
}
func TestCalculateTax(t *testing.T) {
type testCase struct {
totalIncome float64
wht float64
allowances []Allowance
expectedTax float64
}
testCases := []testCase{
{150000.0, 0.0, []Allowance{}, 0.0},
{210000.0, 0.0, []Allowance{}, 0.0},
{330000.0, 0.0, []Allowance{}, 12000.00},
{560000.0, 0.0, []Allowance{}, 35000.0},
{1000000.0, 0.0, []Allowance{}, 101000.00},
{1060000.0, 0.0, []Allowance{}, 110000.0},
{2000000.0, 0.0, []Allowance{}, 298000.00},
{2060000.0, 0.0, []Allowance{}, 310000.0},
{10000000.0, 0.0, []Allowance{}, 3089000.00},
{500000.0, 25000.0, []Allowance{}, 4000.0},
}
personalState := PersonalDeduction
PersonalDeduction = 60000.0
for _, tc := range testCases {
tax, err := CalculateTotalTax(tc.totalIncome, tc.wht, tc.allowances)
if err != nil {
t.Fatalf("CalculateTax failed with error: %v", err)
}
if tax != tc.expectedTax {
t.Errorf("Expected tax to be %.2f but got %.2f", tc.expectedTax, tax)
}
}
PersonalDeduction = personalState
}
func TestCSVTaxCalculationsHandler(t *testing.T) {
e := echo.New()
fileContent := "totalIncome,wht,donation\n500000,0,0\n600000,40000,20000\n750000,50000,15000\n"
temp, err := os.CreateTemp("./", "test-*.csv")
if err != nil {
t.Fatal(err)
}
if _, err := temp.Write([]byte(fileContent)); err != nil {
t.Fatal(err)
}
if err := temp.Close(); err != nil {
t.Fatal(err)
}
file, err := os.Open(temp.Name())
if err != nil {
t.Fatal(err)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(temp.Name()))
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(part, file)
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodPost, "/tax/calculations/upload-csv", body)
req.Header.Set(echo.HeaderContentType, writer.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
if assert.NoError(t, CSVTaxCalculationsHandler(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
}
err = file.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(temp.Name())
if err != nil {
t.Fatal(err)
}
}