func TestListExpenses(t *testing.T) { tx := dbtest.BeginTx() defer tx.Rollback() _, err := fabricate.Expense(tx).Description("Important Expense").Exec() assert.MustBeNil(t, err) expenses, err := service.ListExpenses(logger.Discard, tx) assert.MustBeNil(t, err) assert.MustBeEqual(t, 1, len(expenses)) assert.Equal(t, "Important Expense", expenses[0].Description) }
func TestCreateExpense_Success(t *testing.T) { tx := dbtest.BeginTx() defer tx.Rollback() expense, err := service.CreateExpense(logger.Discard, tx, service.CreateExpenseParams{ Description: "Peter Lustig", Amount: 42, }) assert.MustBeNil(t, err) assert.Equal(t, "Peter Lustig", expense.Description) assert.Equal(t, 42, expense.Amount) cnt, err := db.CountExpenses(tx) assert.MustBeNil(t, err) assert.Equal(t, 1, cnt) }
func TestInsertExpense(t *testing.T) { tx := BeginTx() defer tx.Rollback() expense := model.NewExpense() expense.Description = "some description" expense.Amount = 23 err := db.InsertExpense(tx, expense) assert.MustBeNil(t, err) var amount int var description string row := tx.QueryRow("SELECT description, amount FROM expenses") err = row.Scan(&description, &amount) assert.MustBeNil(t, err) assert.MustBeEqual(t, "some description", description) assert.MustBeEqual(t, 23, amount) }
func TestAPIExpenses(t *testing.T) { txManager := dbtest.NewTxManager() defer txManager.Rollback() router := web.NewRouter(logger.Discard, txManager) s := testclient.New(router) { status, body, err := s.PostForm("/api/v1/expenses", "description=asd&amount=12") assert.MustBeNil(t, err) defer body.Close() assert.Equal(t, 201, status) AssertJSONSchema(t, schema.Map{ "amount": 12, "description": "asd", }, body, ) } { status, body, err := s.Get("/api/v1/expenses") assert.MustBeNil(t, err) defer body.Close() assert.Equal(t, 200, status) AssertJSONSchema(t, schema.Array( schema.Map{ "amount": 12, "description": "asd", }), body, ) } }
func TestCreateExpense_Validation(t *testing.T) { tx := dbtest.BeginTx() defer tx.Rollback() _, err := service.CreateExpense(logger.Discard, tx, service.CreateExpenseParams{ Description: "", Amount: 0, }) assert.MustBeEqual(t, "ValidationError: Description: must be present, Amount: must be greater than 0", err.Error()) cnt, err := db.CountExpenses(tx) assert.MustBeNil(t, err) assert.Equal(t, 0, cnt) }