Compare commits
1 Commits
ca692e9aaf
...
467afae68f
| Author | SHA1 | Date | |
|---|---|---|---|
| 467afae68f |
@ -20,7 +20,15 @@ func (dal *DalImpl) Transaction(transactionId int64) (*entity.Transaction, error
|
||||
log.Panic("database not available")
|
||||
}
|
||||
|
||||
rows, err := dal.Db.Query("SELECT t.id, t.date, t.description, t.amount FROM pfbudget.transactions t WHERE t.id = $1", transactionId)
|
||||
stmt := `
|
||||
SELECT t.id, t.date, t.description, t.amount, tc.name
|
||||
FROM pfbudget.transactions t
|
||||
LEFT JOIN pfbudget.transactions_categorized tc
|
||||
ON t.id = tc.id
|
||||
WHERE t.id = $1
|
||||
`
|
||||
|
||||
rows, err := dal.Db.Query(stmt, transactionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -40,7 +48,14 @@ func (dal *DalImpl) Transactions() (entity.Transactions, error) {
|
||||
log.Panic("database not available")
|
||||
}
|
||||
|
||||
rows, err := dal.Db.Query("SELECT t.id, t.date, t.description, t.amount FROM pfbudget.transactions t")
|
||||
stmt := `
|
||||
SELECT t.id, t.date, t.description, t.amount, tc.name
|
||||
FROM pfbudget.transactions t
|
||||
LEFT JOIN pfbudget.transactions_categorized tc
|
||||
ON t.id = tc.id
|
||||
`
|
||||
|
||||
rows, err := dal.Db.Query(stmt)
|
||||
if err != nil {
|
||||
return entity.Transactions{}, err
|
||||
}
|
||||
|
||||
@ -26,27 +26,38 @@ func TestDalImpl_Transaction(t *testing.T) {
|
||||
}
|
||||
type args struct {
|
||||
transactionId int64
|
||||
rows [][]driver.Value
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
mocks [][]driver.Value
|
||||
want *entity.Transaction
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
"200",
|
||||
"SelectTransaction",
|
||||
fields{db},
|
||||
args{
|
||||
1,
|
||||
[][]driver.Value{
|
||||
{1, date, "income", 1000},
|
||||
}},
|
||||
args{1},
|
||||
[][]driver.Value{
|
||||
{1, date, "income", 1000, nil},
|
||||
},
|
||||
&entity.Transaction{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000)},
|
||||
false,
|
||||
},
|
||||
{"404", fields{db}, args{2, nil}, nil, false},
|
||||
{
|
||||
"SelectTransactionWithCategory",
|
||||
fields{db},
|
||||
args{1},
|
||||
[][]driver.Value{
|
||||
{1, date, "income", 1000, "C1"},
|
||||
},
|
||||
&entity.Transaction{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000), Category: golang.Ptr("C1")},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"SelectNoTransaction", fields{db}, args{2}, nil, nil, false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@ -55,12 +66,15 @@ func TestDalImpl_Transaction(t *testing.T) {
|
||||
}
|
||||
|
||||
mock.
|
||||
ExpectQuery("^SELECT .* FROM .*transactions t WHERE t.id = \\$1$").
|
||||
ExpectQuery(`
|
||||
^SELECT \w+\.id, \w+\.date, \w+\.description, \w+\.amount, \w+\.name
|
||||
FROM \w+\.transactions \w+
|
||||
LEFT JOIN \w+\.transactions_categorized \w+
|
||||
ON \w+\.id = \w+\.id
|
||||
WHERE \w+\.id = \$1$`).
|
||||
WithArgs(tt.args.transactionId).
|
||||
WillReturnRows(
|
||||
mock.
|
||||
NewRows([]string{"id", "date", "description", "amount"}).
|
||||
AddRows(tt.args.rows...),
|
||||
mock.NewRows([]string{"id", "date", "description", "amount", "category"}).AddRows(tt.mocks...),
|
||||
)
|
||||
|
||||
got, err := d.Transaction(tt.args.transactionId)
|
||||
@ -85,30 +99,42 @@ func TestDalImpl_Transactions(t *testing.T) {
|
||||
type fields struct {
|
||||
Db *sql.DB
|
||||
}
|
||||
type args struct {
|
||||
rows [][]driver.Value
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
mocks [][]driver.Value
|
||||
want entity.Transactions
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
"200",
|
||||
"SelectTransactions",
|
||||
fields{db},
|
||||
args{[][]driver.Value{
|
||||
{1, date, "income", 1000},
|
||||
{2, date, "expense", -10.50},
|
||||
}},
|
||||
[][]driver.Value{
|
||||
{1, date, "income", 1000, nil},
|
||||
{2, date, "expense", -10.50, nil},
|
||||
},
|
||||
entity.Transactions{
|
||||
{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000)},
|
||||
{Id: 2, Date: date, Description: "expense", Value: decimal.NewFromFloat(-10.50)},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{"204", fields{db}, args{}, nil, false},
|
||||
{
|
||||
"SelectTransactionsWithCategory",
|
||||
fields{db},
|
||||
[][]driver.Value{
|
||||
{1, date, "income", 1000, "C1"},
|
||||
{2, date, "expense", -10.50, nil},
|
||||
},
|
||||
entity.Transactions{
|
||||
{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000), Category: golang.Ptr("C1")},
|
||||
{Id: 2, Date: date, Description: "expense", Value: decimal.NewFromFloat(-10.50)},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"SelectNoTransactions", fields{db}, nil, nil, false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@ -117,12 +143,14 @@ func TestDalImpl_Transactions(t *testing.T) {
|
||||
}
|
||||
|
||||
mock.
|
||||
ExpectQuery("^SELECT .* FROM .*transactions t$").
|
||||
ExpectQuery(`
|
||||
^SELECT \w+\.id, \w+\.date, \w+\.description, \w+\.amount, \w+\.name
|
||||
FROM \w+\.transactions \w+
|
||||
LEFT JOIN \w+\.transactions_categorized \w+
|
||||
ON \w+\.id = \w+\.id`).
|
||||
WithoutArgs().
|
||||
WillReturnRows(
|
||||
mock.
|
||||
NewRows([]string{"id", "date", "description", "amount"}).
|
||||
AddRows(tt.args.rows...),
|
||||
mock.NewRows([]string{"id", "date", "description", "amount", "category"}).AddRows(tt.mocks...),
|
||||
)
|
||||
|
||||
got, err := dal.Transactions()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user