Newer
Older
package api
import (
"net/http"
"testing"
)
func TestPurchaseAddListMine(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestProducts()
{
ProductCode: testProduct.Code,
Ammount: 5,
},
}
resp := tapi.do("POST", "/purchase", products, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create purchase:", resp.Status)
}
var transactions []Transaction
resp = tapi.do("GET", "/transaction/mine", nil, &transactions)
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(transactions), transactions)
if transactions[0].Total != -testProduct.Price*products[0].Ammount {
if len(transactions[0].Purchase) != 1 {
t.Fatal("Wrong number of products", len(transactions[0].Purchase), transactions[0].Purchase)
if transactions[0].Purchase[0].ProductCode != testProduct.Code {
t.Error("Wrong product code:", transactions[0].Purchase[0].ProductCode)
if transactions[0].Purchase[0].Price != testProduct.Price {
t.Error("Wrong product price:", transactions[0].Purchase[0].Price)
}
var product Product
resp = tapi.do("GET", "/product/234", nil, &product)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the product:", resp.Status)
}
if product.Stock != testProduct.Stock-products[0].Ammount {
t.Error("Wrong product stock:", product)
}
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the member:", resp.Status)
}
if member.Balance != testMember.Balance-(testProduct.Price*products[0].Ammount) {
t.Error("Wrong product balance:", member.Balance)
}