-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
146 lines (120 loc) · 3.41 KB
/
setup_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
package tomeit
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
var (
testClient *http.Client
testUrl string
testDB *DB
taskResponseCmpOpts = cmpopts.IgnoreFields(taskResponse{}, "CompletedOn", "CreatedAt", "UpdatedAt")
pomodoroResponseCmpOpts = cmpopts.IgnoreFields(pomodoroResponse{}, "Task.CompletedOn", "Task.CreatedAt", "Task.UpdatedAt", "CreatedAt")
)
func TestMain(m *testing.M) {
firebaseApp := &firebaseAppMock{}
testDB = OpenDB("test:password@tcp(localhost:13306)/db_test?charset=utf8mb4&parseTime=true")
defer CloseDB(testDB)
r := chi.NewRouter()
r.Use(render.SetContentType(render.ContentTypeJSON))
r.Use(UserCtx(testDB, firebaseApp))
Route(r, testDB)
ts := httptest.NewServer(r)
defer ts.Close()
testUrl = ts.URL
testClient = &http.Client{}
m.Run()
}
func setupTestDB(tb testing.TB) {
file, err := os.ReadFile(filepath.Join(".", "build", "create_tables.sql"))
if err != nil {
tb.Fatal("os.ReadFile failed:", err)
}
queries := strings.Split(string(file), ";")
for _, query := range queries {
if query == "" {
break
}
testDB.Exec(query)
}
const createTestUser = `INSERT INTO users (digest_uid) VALUES ('a2c4ba85c41f186283948b1a54efacea04cb2d3f54a88d5826a7e6a917b28c5a')`
testDB.Exec(createTestUser)
}
func teardownTestDB() {
const dropPomodorosTable = `DROP TABLE IF EXISTS pomodoros`
const dropTasksTable = `DROP TABLE IF EXISTS tasks`
const dropUsersTable = `DROP TABLE IF EXISTS users`
testDB.Exec(dropPomodorosTable)
testDB.Exec(dropTasksTable)
testDB.Exec(dropUsersTable)
}
func doTestRequest(tb testing.TB, method, path string, params *map[string]string, body io.Reader, respBodyType string) (*http.Response, interface{}) {
req, err := http.NewRequest(method, testUrl+path, body)
if err != nil {
tb.Fatal("Create request failed:", err)
}
if params != nil {
ps := req.URL.Query()
for k, v := range *params {
ps.Add(k, v)
}
req.URL.RawQuery = ps.Encode()
}
resp, err := testClient.Do(req)
if err != nil {
tb.Fatal("Do request failed:", err)
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
tb.Fatal("Read respBody failed:", err)
}
if err := resp.Body.Close(); err != nil {
tb.Fatal("Close respBody failed:", err)
}
switch respBodyType {
case "taskResponse":
var respBody taskResponse
if err := json.Unmarshal(bytes, &respBody); err != nil {
return resp, nil
}
return resp, respBody
case "tasksResponse":
var respBody tasksResponse
if err := json.Unmarshal(bytes, &respBody); err != nil {
return resp, nil
}
return resp, respBody
case "pomodoroResponse":
var respBody pomodoroResponse
if err := json.Unmarshal(bytes, &respBody); err != nil {
return resp, nil
}
return resp, respBody
case "pomodorosResponse":
var respBody pomodorosResponse
if err := json.Unmarshal(bytes, &respBody); err != nil {
return resp, nil
}
return resp, respBody
case "restCountResponse":
var respBody restCountResponse
if err := json.Unmarshal(bytes, &respBody); err != nil {
return resp, nil
}
return resp, respBody
}
return resp, nil
}
func checkStatusCode(tb testing.TB, response *http.Response, want int) {
if response.StatusCode != want {
tb.Fatalf("Status code should be %v, but %v", want, response.StatusCode)
}
}