/
main_test.go
136 lines (100 loc) · 3.46 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
package main
import (
"api_product/domain"
"api_product/inventory"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestProductsRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/products", nil)
router.ServeHTTP(w, req)
expected := 2
var productList []domain.Product
json.Unmarshal(w.Body.Bytes(), &productList)
received := len(productList)
testLog := "Testing /products route"
testError := fmt.Sprintf("Listing products failed verification expected:%d but recieved: %d", expected, received)
if expected != received {
t.Logf(testLog)
t.Errorf(testError)
}
}
func TestGetProductByIDRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/product/21", nil)
router.ServeHTTP(w, req)
expectedProduct := domain.Product{ID: "21", Name: "Nintendo Switch Lite", Price: 399.90, Quantity: 2000}
var receivedProduct domain.Product
json.Unmarshal(w.Body.Bytes(), &receivedProduct)
testLog := "Testing /product/:id route"
testError := fmt.Sprintf("Listing product via id failed verification expected ID: %s but received ID: %s",
expectedProduct.ID, receivedProduct.ID)
if !reflect.DeepEqual(expectedProduct, receivedProduct) {
t.Logf(testLog)
t.Errorf(testError)
}
}
func TestAddProductRoute(t *testing.T) {
expected := true
router := setupRouter()
snes := domain.Product{ID: "756", Name: "Super Nintendo", Price: 399.90, Quantity: 4000}
payload, _ := json.Marshal(snes)
product := []byte(payload)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/product", bytes.NewBuffer(product))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
router.ServeHTTP(w, req)
testLog := "Testing POST on /product route"
testError := fmt.Sprintf("Adding product failed expected inventory ID: %s but inventory check failed with %t",
snes.ID, inventory.Exists(snes))
if inventory.Exists(snes) != expected {
t.Logf(testLog)
t.Errorf(testError)
}
}
func TestPatchProductRoute(t *testing.T) {
expected := true
router := setupRouter()
xbox := domain.Product{ID: "929", Name: "Xbox", Price: 200.00, Quantity: 7000}
_ = inventory.Add(xbox)
xbox.Price = 300.00
payload, _ := json.Marshal(xbox)
patchedProduct := []byte(payload)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PATCH", "/product", bytes.NewBuffer(patchedProduct))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
router.ServeHTTP(w, req)
testLog := "Testing PATCH on /product route"
testError := fmt.Sprintf("Adding product failed expected inventory price: %f but inventory check failed with %t",
xbox.Price, inventory.Exists(xbox))
if inventory.Exists(xbox) != expected {
t.Logf(testLog)
t.Errorf(testError)
}
}
func TestDeleteProductRoute(t *testing.T) {
expected := false
PS5 := domain.Product{ID: "800", Name: "PS5", Price: 400.00, Quantity: 17000}
deletePath := fmt.Sprintf("/product/%s", PS5.ID)
router := setupRouter()
_ = inventory.Add(PS5)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", deletePath, nil)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
router.ServeHTTP(w, req)
testLog := "Testing Delete on /product route with id"
testError := fmt.Sprintf("Deleting product failed for %s inventory check returned %t",
PS5.Name, inventory.Exists(PS5))
if inventory.Exists(PS5) != expected {
t.Logf(testLog)
t.Errorf(testError)
}
}