Newer
Older
var testProduct = Product{
Code: 234,
Name: "Aceite",
Price: 1700,
Stock: 10,
}
func TestProductAddList(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get products:", resp.Status)
}
if len(products) != 1 {
t.Fatal("Wrong number of products", len(products), products)
}
t.Error("Wrong price:", products[0].Price)
}
}
func TestProductGetDelete(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
resp := tapi.do("GET", "/product/234", nil, nil)
if resp.StatusCode != http.StatusNotFound {
t.Error("Expected not found:", resp.Status, resp.Body)
}
var gotProduct Product
resp = tapi.do("GET", "/product/234", nil, &gotProduct)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the product:", resp.Status)
}
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the product:", resp.Status)
}
resp = tapi.do("GET", "/product/234", nil, nil)
if resp.StatusCode != http.StatusNotFound {
t.Error("Expected not found after delete:", resp.Status, resp.Body)
}
}
func TestProductUpdate(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
product := testProduct
product.Stock = testProduct.Stock - 5
resp := tapi.doAdmin("PUT", "/product/234", product, nil)
if resp.StatusCode != http.StatusAccepted {
t.Fatal("Can't update product:", resp.Status)
}
var gotProduct Product
resp = tapi.do("GET", "/product/234", nil, &gotProduct)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the product:", resp.Status)
}
if gotProduct.Stock != 5 {
t.Error("Wrong sotck:", gotProduct)
}
}
resp := tapi.doAdmin("POST", "/product", testProduct, nil)