-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
181 lines (137 loc) · 4.13 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
// main_test.go
package main_test
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
main "github.com/vlaguduva/relyf"
)
var a main.App
func TestMain(m *testing.M) {
a.Initialize(
os.Getenv("APP_DB_HOST"),
os.Getenv("APP_DB_USERNAME"),
os.Getenv("APP_DB_PASSWORD"),
os.Getenv("APP_DB_NAME"))
ensureTableExists()
code := m.Run()
clearTable()
os.Exit(code)
}
func TestEmptyTable(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/products", nil)
res := executeRequest(req)
checkReponseCode(t, http.StatusOK, res.Code)
if body := res.Body.String(); body != "[]" {
t.Errorf("Expected empty array. Got %s\n", body)
}
}
func TestNonExistentProduct(t *testing.T) {
clearTable()
req, _ := http.NewRequest("GET", "/product/999", nil)
res := executeRequest(req)
checkReponseCode(t, http.StatusNotFound, res.Code)
var m map[string]string
json.Unmarshal(res.Body.Bytes(), &m)
if m["error"] != "Product not found." {
t.Errorf("Expected the 'error' to be 'Product not found'. Got %s\n", m["error"])
}
}
func TestGetProduct(t *testing.T) {
TestNewProduct(t)
req, _ := http.NewRequest("GET", "/product/1", nil)
res := executeRequest(req)
checkReponseCode(t, http.StatusOK, res.Code)
var m map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &m)
if m["name"] != "test_product" {
t.Errorf("Expected the 'name' to be 'test_product'. Got %v\n", m["name"])
}
if m["price"] != 11.21 {
t.Errorf("Expected the 'price' to be '11.21'. Got %v\n", m["price"])
}
if m["id"] != 1.0 {
t.Errorf("Expected the 'id' to be '1.0'. Got %v\n", m["price"])
}
}
func TestNewProduct(t *testing.T) {
clearTable()
var reqBody = []byte(`{"name": "test_product", "price": 11.21}`)
req, _ := http.NewRequest("POST", "/product", bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json")
res := executeRequest(req)
checkReponseCode(t, http.StatusOK, res.Code)
var m map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &m)
if m["name"] != "test_product" {
t.Errorf("Expected the 'name' to be 'test_product'. Got %v\n", m["name"])
}
if m["price"] != 11.21 {
t.Errorf("Expected the 'price' to be '11.21'. Got %v\n", m["price"])
}
if m["id"] != 1.0 {
t.Errorf("Expected the 'id' to be '1.0'. Got %v\n", m["price"])
}
}
func TestUpdateProduct(t *testing.T) {
TestNewProduct(t)
var reqBody = []byte(`{"name": "test_product_updated", "price": 11.22}`)
req, _ := http.NewRequest("PUT", "/product/1", bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application//1json")
res := executeRequest(req)
checkReponseCode(t, http.StatusOK, res.Code)
var m map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &m)
if m["name"] != "test_product_updated" {
t.Errorf("Expected the 'name' to be 'test_product_updated'. Got %v\n", m["name"])
}
if m["price"] != 11.22 {
t.Errorf("Expected the 'price' to be '11.22'. Got %v\n", m["price"])
}
if m["id"] != 1.0 {
t.Errorf("Expected the 'id' to be '1.0'. Got %v\n", m["price"])
}
}
func TestDeleteProduct(t *testing.T) {
TestNewProduct(t)
TestGetProduct(t)
req, _ := http.NewRequest("DELETE", "/product/1", nil)
res := executeRequest(req)
checkReponseCode(t, http.StatusOK, res.Code)
var m map[string]string
json.Unmarshal(res.Body.Bytes(), &m)
if m["error"] != "" {
t.Errorf("Expected the 'error' to be 'nil'. Got %s\n", m["error"])
}
TestGetProduct(t)
}
func ensureTableExists() {
if _, error := a.DB.Exec(tableCreationQuery); error != nil {
log.Fatal(error)
}
}
func clearTable() {
a.DB.Exec("DELETE from products")
a.DB.Exec("ALTER SEQUENCE products_id_seq RESTART WITH 1")
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
responseRecorder := httptest.NewRecorder()
a.Router.ServeHTTP(responseRecorder, req)
return responseRecorder
}
func checkReponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
const tableCreationQuery = `CREATE TABLE IF NOT EXISTS products (
id SERIAL,
name TEXT NOT NULL,
price NUMERIC (10,2) NOT NULL DEFAULT 0.00,
CONSTRAINT products_pkey PRIMARY KEY (id)
)`