-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
181 lines (149 loc) · 4.83 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
)
var a App
//EndPoint Test
func TestEmptyTable(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/customer", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
if body := response.Body.String(); body != "[]" {
t.Errorf("Expected empty array. Received %s\n", body)
}
}
func TestCreateCustomer(t *testing.T) {
clearTable()
payload := []byte(`{"name":"test cust", "email":"test@test.com","password":"pass"}`)
req, _ := http.NewRequest("POST", "/customer", bytes.NewBuffer(payload))
response := executeRequest(req)
checkResponseCode(t, http.StatusCreated, response.Code)
var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m)
if m["name"] != "test cust" {
t.Errorf("Expected name to be %s, Got %s", "test_cust", m["name"])
}
if m["email"] != "test@test.com" {
t.Errorf("Expected email to be %s, Got %s", "test@test.com", m["email"])
}
if m["password"] != "pass" {
t.Errorf("Expected password to be %s, Got %s", "pass", m["password"])
}
if m["id"] != 1.0 {
t.Errorf("Expected id to be %f, Got %f", 1.0, m["id"])
}
}
func TestGetCustomer(t *testing.T) {
clearTable()
addCustomers(1)
req, _ := http.NewRequest("GET", "/customer/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func TestGetCustomers(t *testing.T) {
clearTable()
addCustomers(5)
req, _ := http.NewRequest("GET", "/customer", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func addCustomers(count int) {
if count < 1 {
count = 1
}
for i := 1; i <= count; i++ {
statement := "INSERT INTO customers(name,email,password) VALUES(?,?,?)"
a.DB.Exec(statement, "cust "+strconv.Itoa(i), "test"+strconv.Itoa(i)+"@test.com", "pass"+strconv.Itoa(i))
}
}
func TestUpdateCustomer(t *testing.T) {
clearTable()
addCustomers(1)
req, _ := http.NewRequest("GET", "/customer/1", nil)
response := executeRequest(req)
var originalCustomer map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &originalCustomer)
payload := []byte(`{"name":"test cust-updated name", "email":"testupdated@test.com", "password":"passupdated"}`)
req, _ = http.NewRequest("PUT", "/customer/1", bytes.NewBuffer(payload))
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m)
if m["id"] != originalCustomer["id"] {
t.Errorf("Expected the id to remain the same(%f), but Got %f\n", m["id"], originalCustomer["id"])
}
if m["name"] == originalCustomer["name"] {
t.Errorf("Expected the name to be updated to %s, but Got %s\n", m["name"], originalCustomer["name"])
}
if m["email"] == originalCustomer["email"] {
t.Errorf("Expected the email to be updated to %s, but Got %s\n", m["email"], originalCustomer["email"])
}
if m["password"] == originalCustomer["password"] {
t.Errorf("Expected the password to be updated to %s, but Got %s\n", m["password"], originalCustomer["password"])
}
}
func TestDeleteCustomer(t *testing.T) {
clearTable()
addCustomers(1)
req, _ := http.NewRequest("GET", "/customer/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("DELETE", "/customer/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", "/customer/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
}
func TestGetNonExistentCustomer(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/customer/45", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
var m map[string]string
json.Unmarshal(response.Body.Bytes(), &m)
if m["error"] != "Customer not found" {
t.Errorf("Expected the 'error' key of the response to be set to 'Customer not found'. Got '%s'", m["error"])
}
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
a.Router.ServeHTTP(rr, req)
return rr
}
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
func TestMain(m *testing.M) {
a = App{}
a.Initialize(DbUser, DbPassword, DbHost, DbName)
ensureTableExists()
code := m.Run()
clearTable()
os.Exit(code)
}
func ensureTableExists() {
if !a.DB.HasTable(&Customer{}) {
log.Println("Table Does Not Exist")
}
}
func clearTable() {
a.DB.Exec("DELETE FROM customers")
a.DB.Exec("ALTER TABLE customers AUTO_INCREMENT=1")
}
const tableCreationQuery = `CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50),
password VARCHAR(50)
)`