Skip to content
Snippets Groups Projects
Select Git revision
  • 4618bd72858a7efe34a1467d981bd7a1be411c0d
  • main default protected
  • order_empty
  • order_purchase_bug
  • test
  • collected_arrived
  • notifications
  • 2.2
  • 2.1
  • 2.0
  • 1.6.1
  • 1.6
  • 1.5
  • 1.4
  • 1.3
  • 1.2
  • 1.1
  • 0.1
18 results

purchase_test.go

Blame
  • purchase_test.go 1.79 KiB
    package api
    
    import (
    	"net/http"
    	"testing"
    )
    
    func TestPurchaseAddListMine(t *testing.T) {
    	tapi := newTestAPI(t)
    	defer tapi.close()
    	tapi.addTestMember()
    	tapi.addTestProducts()
    
    	products := []Purchase{
    		{
    			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 resp.StatusCode != http.StatusOK {
    		t.Fatal("Can't get transactions:", resp.Status)
    	}
    
    	if len(transactions) != 1 {
    		t.Fatal("Wrong number of transactions", len(transactions), transactions)
    	}
    	if transactions[0].Total != -testProduct.Price*products[0].Ammount {
    		t.Error("Wrong total:", transactions[0].Total)
    	}
    	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)
    	}
    
    	var member Member
    	resp = tapi.do("GET", "/member/10", nil, &member)
    	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)
    	}
    }