func Expense(con db.Con) *ExpenseFabricator {
	expense := model.NewExpense()
	expense.Amount = int(rand.Int31())
	expense.Description = "A description"

	return &ExpenseFabricator{con: con, expense: expense}
}
Beispiel #2
0
func CreateExpense(l logger.Logger, con db.Con, params CreateExpenseParams) (*model.Expense, error) {
	expense := model.NewExpense()

	expense.Description = params.Description
	expense.Amount = params.Amount

	err := validate.Expense(expense)
	if err != nil {
		return nil, err
	}

	err = db.InsertExpense(con, expense)
	if err != nil {
		return nil, err
	}

	return expense, nil
}
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)
}
Beispiel #4
0
func iterateExpenses(con Con, f func(*model.Expense) error, opts ...func(*db.Opt)) error {
	rows, err := db.QueryWithOpts(con, "SELECT id, description, amount FROM expenses", opts...)
	if err != nil {
		return wrap(err)
	}

	for rows.Next() {
		expense := model.NewExpense()
		err := rows.Scan(&expense.ID, &expense.Description, &expense.Amount)
		if err != nil {
			return wrap(err)
		}
		err = f(expense)
		if err != nil {
			return wrap(err)
		}
	}

	return wrap(rows.Err())
}