From 92aa51c664708d7403d279c493eac4f1951af4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Mon, 24 Jun 2024 21:44:20 +0100 Subject: [PATCH] Return category on /transactions Issue #26 --- internal/dal/impl.go | 19 +++++++++- internal/dal/impl_test.go | 80 ++++++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/internal/dal/impl.go b/internal/dal/impl.go index e3a796a..4d0879b 100644 --- a/internal/dal/impl.go +++ b/internal/dal/impl.go @@ -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 } diff --git a/internal/dal/impl_test.go b/internal/dal/impl_test.go index c947098..400c692 100644 --- a/internal/dal/impl_test.go +++ b/internal/dal/impl_test.go @@ -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()