package api

import (
	"net/http"
	"testing"
)

func TestTopupAddListMine(t *testing.T) {
	tapi := newTestAPI(t)
	defer tapi.close()
	tapi.addTestMember()
	tapi.addTestProducts()

	topup := map[string]interface{}{
		"member":  testMember.Num,
		"comment": "my topup",
		"ammount": 20,
	}
	resp := tapi.do("POST", "/topup", topup, nil)
	if resp.StatusCode != http.StatusCreated {
		t.Fatal("Can't create topup:", 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 != 20 {
		t.Error("Wrong total:", transactions[0].Total)
	}
	if transactions[0].Topup.MemberNum != testMember.Num {
		t.Error("Wrong topup member:", transactions[0].Topup.MemberNum)
	}
	if transactions[0].Topup.Comment != "my topup" {
		t.Error("Wrong topup comment:", transactions[0].Topup.Comment)
	}

	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+20 {
		t.Error("Wrong product balance:", member.Balance)
	}
}