-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.go
140 lines (117 loc) · 3.56 KB
/
app_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
)
var a App
func TestMain(m *testing.M) {
err := a.Initialise(DbUser, DbPassword, "test")
if err != nil {
log.Fatal("Error occured while initialising the database")
}
createTable()
m.Run()
}
func createTable() {
createTableQuery := `
CREATE TABLE IF NOT EXISTS products (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
quantity INT,
price FLOAT(10,7),
PRIMARY KEY (id)
);`
_, err := a.DB.Exec(createTableQuery)
if err != nil {
log.Fatal(err)
}
}
func clearTable() {
a.DB.Exec("DELETE FROM products")
a.DB.Exec("ALTER TABLE products AUTO_INCREMENT=1")
log.Println("clearTable")
}
func addProduct(name string, quantity int, price float64) {
query := fmt.Sprintf("INSERT INTO products(name, quantity, price) VALUES('%v', %v, %v)", name, quantity, price)
_, err := a.DB.Exec(query)
if err != nil {
log.Println(err)
}
}
func TestGetProduct(t *testing.T) {
clearTable()
addProduct("keyboard", 100, 500)
req, _ := http.NewRequest("GET", "/product/1", nil)
res := sendRequest(req)
checkStatusCode(t, http.StatusOK, res.Code)
}
func checkStatusCode(t *testing.T, expectedStatusCode, actualStatusCode int) {
if expectedStatusCode != actualStatusCode {
t.Errorf("Expected status: %v, Received: %v", expectedStatusCode, actualStatusCode)
}
}
func sendRequest(req *http.Request) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
a.Router.ServeHTTP(rec, req)
return rec
}
func TestCreateProduct(t *testing.T) {
clearTable()
var product = []byte(`{"name": "chair", "quantity": 1, "price": 100}`)
req, _ := http.NewRequest("POST", "/product", bytes.NewBuffer(product))
req.Header.Set("Content-type", "application/json")
res := sendRequest(req)
checkStatusCode(t, http.StatusCreated, res.Code)
var m map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &m)
if m["name"] != "chair" {
t.Errorf("Expected name: %v, Got: %v", "chair", m["name"])
}
if m["quantity"] != 1.0 {
t.Errorf("Expected name: %v, Got: %v", 1, m["quantity"])
}
}
func TestDeleteProduct(t *testing.T) {
clearTable()
addProduct("connector", 10, 10)
req, _ := http.NewRequest("GET", "/product/1", nil)
res := sendRequest(req)
checkStatusCode(t, http.StatusOK, res.Code)
req, _ = http.NewRequest("DELETE", "/product/1", nil)
res = sendRequest(req)
checkStatusCode(t, http.StatusOK, res.Code)
req, _ = http.NewRequest("GET", "/product/1", nil)
res = sendRequest(req)
checkStatusCode(t, http.StatusNotFound, res.Code)
}
func TestUpdateProduct(t *testing.T) {
clearTable()
addProduct("connector", 10, 10)
req, _ := http.NewRequest("GET", "/product/1", nil)
res := sendRequest(req)
var oldValue map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &oldValue)
var product = []byte(`{"name": "connector", "quantity": 1, "price": 10}`)
req, _ = http.NewRequest("PUT", "/product/1", bytes.NewBuffer(product))
req.Header.Set("Content-type", "application/json")
res = sendRequest(req)
var newValue map[string]interface{}
json.Unmarshal(res.Body.Bytes(), &newValue)
if oldValue["id"] != newValue["id"] {
t.Errorf("Expected id: %v, Got: %v", newValue["id"], oldValue["id"])
}
if oldValue["name"] != newValue["name"] {
t.Errorf("Expected id: %v, Got: %v", newValue["name"], oldValue["name"])
}
if oldValue["price"] != newValue["price"] {
t.Errorf("Expected id: %v, Got: %v", newValue["price"], oldValue["price"])
}
if oldValue["quantity"] == newValue["quantity"] {
t.Errorf("Expected id: %v, Got: %v", newValue["quantity"], oldValue["quantity"])
}
}