49 lines
974 B
Go
49 lines
974 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestServerPostTransactions(t *testing.T) {
|
|
path := testLedgerFile(t)
|
|
ledger, err := NewLedger(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := Server{ledger: ledger}
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{
|
|
"Date": "2099-02-03",
|
|
"Description": "test",
|
|
"Payer": "payer",
|
|
"Payee": "payee",
|
|
"Amount": 1.02
|
|
}`))
|
|
w := httptest.NewRecorder()
|
|
server.postTransactions(w, r)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if s := strings.TrimSpace(string(w.Body.Bytes())); s != `{"ok":true}` {
|
|
t.Fatal(s)
|
|
}
|
|
|
|
transactions, err := ledger.Transactions()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if transactions[len(transactions)-1] != (Transaction{
|
|
Date: "2099-02-03",
|
|
Description: "test",
|
|
Payer: "payer",
|
|
Payee: "payee",
|
|
Amount: 1.02,
|
|
}) {
|
|
t.Fatal(transactions[len(transactions)-1])
|
|
}
|
|
}
|