Implements GET /categories
Adds type to OpenAPI. Feature done and tested E2E. Adds common utilities package named "golang" to overcome language deficiencies. Issue #27
This commit is contained in:
parent
18c93b6c0f
commit
9ac249199b
@ -160,6 +160,22 @@ paths:
|
||||
"404":
|
||||
description: Bank not found
|
||||
|
||||
/categories:
|
||||
get:
|
||||
summary: Retrive existing categories
|
||||
operationId: getCategories
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Category"
|
||||
"204":
|
||||
description: No categories
|
||||
|
||||
components:
|
||||
schemas:
|
||||
Transaction:
|
||||
@ -204,3 +220,19 @@ components:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Bank"
|
||||
Category:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
group:
|
||||
$ref: "#/components/schemas/CategoryGroup"
|
||||
required:
|
||||
- name
|
||||
CategoryGroup:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
required:
|
||||
- name
|
||||
|
||||
@ -53,3 +53,18 @@ func convertBanks(bs entity.Banks) Banks {
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func entity2category(cat entity.Category) Category {
|
||||
if cat.Group != nil {
|
||||
return Category{Name: cat.Name, Group: &CategoryGroup{*cat.Group}}
|
||||
}
|
||||
return Category{Name: cat.Name}
|
||||
}
|
||||
|
||||
func entities2categories(cats entity.Categories) []Category {
|
||||
var ans []Category
|
||||
for _, cat := range cats {
|
||||
ans = append(ans, entity2category(cat))
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
@ -117,3 +117,16 @@ func (server *ServerImpl) UpdateTransaction(ctx echo.Context, transactionId int6
|
||||
|
||||
return ctx.JSON(http.StatusOK, entity2transaction(transaction))
|
||||
}
|
||||
|
||||
func (server *ServerImpl) GetCategories(ctx echo.Context) error {
|
||||
categories, err := server.Dal.Categories()
|
||||
if err != nil {
|
||||
return ctx.NoContent(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if len(categories) == 0 {
|
||||
return ctx.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, entities2categories(categories))
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/dal"
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/entity"
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/golang"
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/mock"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
@ -363,3 +364,86 @@ func TestServerImpl_UpdateTransaction(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerImpl_GetCategories(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
m := mock.NewMockDAL(ctrl)
|
||||
e := m.EXPECT()
|
||||
|
||||
type fields struct {
|
||||
fn any // func(...any) *gomock.Call
|
||||
args []any
|
||||
returns any
|
||||
err error
|
||||
}
|
||||
type args struct {
|
||||
method string
|
||||
request string
|
||||
}
|
||||
type want struct {
|
||||
status int
|
||||
body string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields []fields
|
||||
args args
|
||||
want want
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
"GetCategorySuccessful",
|
||||
[]fields{
|
||||
{e.Categories, []any{}, entity.Categories{{Name: "C1", Group: golang.StrPtr("G1")}}, nil},
|
||||
},
|
||||
args{"GET", ""},
|
||||
want{200, `[{"group":{"name":"G1"},"name":"C1"}]`},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"GetMultipleCategoriesSuccessful",
|
||||
[]fields{
|
||||
{e.Categories, []any{}, entity.Categories{{Name: "C1", Group: golang.StrPtr("G1")}, {Name: "C2"}}, nil},
|
||||
},
|
||||
args{"GET", ""},
|
||||
want{200, `[{"group":{"name":"G1"},"name":"C1"},{"name":"C2"}]`},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"GetNoCategories",
|
||||
[]fields{
|
||||
{e.Categories, []any{}, entity.Categories{}, nil},
|
||||
},
|
||||
args{"GET", ""},
|
||||
want{204, ""},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := &ServerImpl{
|
||||
Dal: m,
|
||||
}
|
||||
for _, expect := range tt.fields {
|
||||
golang.Call(expect.fn, expect.args...).Return(expect.returns, expect.err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(tt.args.method, "/", strings.NewReader(tt.args.request))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
ctx := echo.New().NewContext(req, rec)
|
||||
|
||||
if err := server.GetCategories(ctx); (err != nil) != tt.wantErr {
|
||||
t.Errorf("ServerImpl.GetCategories() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if got := rec.Code; !reflect.DeepEqual(got, tt.want.status) {
|
||||
t.Errorf("ServerImpl.GetCategories() = %v, want %v", got, tt.name)
|
||||
}
|
||||
if got := strings.TrimSpace(rec.Body.String()); !reflect.DeepEqual(got, tt.want.body) {
|
||||
t.Errorf("ServerImpl.GetCategories() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,6 +29,17 @@ type Bank struct {
|
||||
// Banks defines model for Banks.
|
||||
type Banks = []Bank
|
||||
|
||||
// Category defines model for Category.
|
||||
type Category struct {
|
||||
Group *CategoryGroup `json:"group,omitempty"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// CategoryGroup defines model for CategoryGroup.
|
||||
type CategoryGroup struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Transaction defines model for Transaction.
|
||||
type Transaction struct {
|
||||
Category *string `json:"category,omitempty"`
|
||||
@ -73,6 +84,9 @@ type ServerInterface interface {
|
||||
// Find bank by ID
|
||||
// (GET /banks/{bankId})
|
||||
GetBankById(ctx echo.Context, bankId string) error
|
||||
// Retrive existing categories
|
||||
// (GET /categories)
|
||||
GetCategories(ctx echo.Context) error
|
||||
// Retrieve existing transactions
|
||||
// (GET /transactions)
|
||||
GetTransactions(ctx echo.Context, params GetTransactionsParams) error
|
||||
@ -117,6 +131,15 @@ func (w *ServerInterfaceWrapper) GetBankById(ctx echo.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCategories converts echo context to params.
|
||||
func (w *ServerInterfaceWrapper) GetCategories(ctx echo.Context) error {
|
||||
var err error
|
||||
|
||||
// Invoke the callback with all the unmarshaled arguments
|
||||
err = w.Handler.GetCategories(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetTransactions converts echo context to params.
|
||||
func (w *ServerInterfaceWrapper) GetTransactions(ctx echo.Context) error {
|
||||
var err error
|
||||
@ -234,6 +257,7 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL
|
||||
|
||||
router.GET(baseURL+"/banks", wrapper.GetBanks)
|
||||
router.GET(baseURL+"/banks/:bankId", wrapper.GetBankById)
|
||||
router.GET(baseURL+"/categories", wrapper.GetCategories)
|
||||
router.GET(baseURL+"/transactions", wrapper.GetTransactions)
|
||||
router.POST(baseURL+"/transactions", wrapper.CreateTransaction)
|
||||
router.GET(baseURL+"/transactions/:transactionId", wrapper.GetTransactionById)
|
||||
@ -244,23 +268,24 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL
|
||||
// Base64 encoded, gzipped, json marshaled Swagger object
|
||||
var swaggerSpec = []string{
|
||||
|
||||
"H4sIAAAAAAAC/8RWTW/jNhD9KwTboyIpcdCDTm0abGCg2Abd3dN2D7Q4krmlSC05tGsE/u8FSdnRl9cJ",
|
||||
"ukkutmQO5+O9mTd+oKVuWq1AoaXFA7XlGho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MGuWwiq1B7du8+ltXDKRCudsvKQ6dZCwja0OLzOIvl",
|
||||
"DVG1D0FQEQ1otesb7s4c5nt5LWgISuPxQW0hiUAba86XFybhRzi4DLFH1csNE7wiyxtirEsFqmA7w5cL",
|
||||
"SKRCUisrqxFr77isAoqrHVneBLZ6udwviyNMXT9aPROuJ4nRYZvNKtHz+jkqeKap454eWWY40tVjYA30",
|
||||
"90Rr11wgaEdI5J0cdljf5N8s+Ie+y6PjR2ClFYKtnJqFjp/s1nHosGzcZMWFTSdsFFzwluMgcgU1swJp",
|
||||
"cZ7nyWAnLi5O7MRpVqquDSCptWrJdg0a+oSC3uAQ2rn8goP5BM/OZ/I7nVPQH1yDn54jcfuj40KTTLkH",
|
||||
"URF33fG/XfNy7Yo1SuORGP3R24jZoK1/wiDi0N/398vQOKGdMjPDd62BIcSvMmETgMErVe1eAgqHxMME",
|
||||
"9fOXCjUG/WOsGr766ugiiW3djjjYD7APGBJGJGxj2KcamN1HTydWfRT76Rs/lsQTi3+QyXf3/+SdfTL/",
|
||||
"rzREr/piMOb+6PtBjHn/mpDQzs6w+qmrJsP246Tarv/v5jVIfQMluHwLJQigVsQc2knsntUagWvC5Kwi",
|
||||
"+8r/DwAA//8OAsybgxEAAA==",
|
||||
}
|
||||
|
||||
// GetSwagger returns the content of the embedded swagger specification file
|
||||
|
||||
@ -10,6 +10,7 @@ type DAL interface {
|
||||
TransactionExists(uint64) (bool, error)
|
||||
Bank(bankId string) (*entity.Bank, error)
|
||||
Banks() (entity.Banks, error)
|
||||
Categories() (entity.Categories, error)
|
||||
}
|
||||
|
||||
//go:generate go run go.uber.org/mock/mockgen@latest -destination=../mock/mock_dal.gen.go -package=mock . DAL
|
||||
|
||||
@ -157,3 +157,18 @@ func (dal *DalImpl) Banks() (entity.Banks, error) {
|
||||
|
||||
return convert[entity.Bank](rows), nil
|
||||
}
|
||||
|
||||
func (dal *DalImpl) Categories() (entity.Categories, error) {
|
||||
log.Print("DAL::Categories")
|
||||
|
||||
if dal.Db == nil {
|
||||
log.Panic("database not available")
|
||||
}
|
||||
|
||||
rows, err := dal.Db.Query("SELECT c.name, c.group FROM pfbudget.categories c")
|
||||
if err != nil {
|
||||
return []entity.Category{}, err
|
||||
}
|
||||
|
||||
return convert[entity.Category](rows), nil
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/entity"
|
||||
"git.rosemyrtle.work/personal-finance/server/internal/golang"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/google/uuid"
|
||||
"github.com/shopspring/decimal"
|
||||
@ -424,3 +425,64 @@ func TestDalImpl_TransactionExists(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDalImpl_Categories(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type fields struct {
|
||||
Db *sql.DB
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
mocks [][]driver.Value
|
||||
want entity.Categories
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
"SelectCategories",
|
||||
fields{db},
|
||||
[][]driver.Value{{"C1", "G1"}},
|
||||
entity.Categories{{Name: "C1", Group: golang.StrPtr("G1")}},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"SelectMutipleCategories",
|
||||
fields{db},
|
||||
[][]driver.Value{{"C1", "G1"}, {"C2", nil}},
|
||||
entity.Categories{{Name: "C1", Group: golang.StrPtr("G1")}, {Name: "C2"}},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"SelectNoCategories",
|
||||
fields{db},
|
||||
[][]driver.Value{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dal := &DalImpl{
|
||||
Db: tt.fields.Db,
|
||||
}
|
||||
|
||||
mock.
|
||||
ExpectQuery(`SELECT \w+\.name, \w+\.group FROM \w+.categories \w+`).
|
||||
WithoutArgs().
|
||||
WillReturnRows(mock.NewRows([]string{"name", "group"}).AddRows(tt.mocks...))
|
||||
|
||||
got, err := dal.Categories()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("DalImpl.Categories() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("DalImpl.Categories() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,3 +25,16 @@ type Bank struct {
|
||||
}
|
||||
|
||||
type Banks = []Bank
|
||||
|
||||
type Category struct {
|
||||
Name string
|
||||
Group *string
|
||||
}
|
||||
|
||||
type Categories = []Category
|
||||
|
||||
type CategoryGroup struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type CategoryGroups = []CategoryGroup
|
||||
|
||||
17
internal/golang/type.go
Normal file
17
internal/golang/type.go
Normal file
@ -0,0 +1,17 @@
|
||||
package golang
|
||||
|
||||
import "go.uber.org/mock/gomock"
|
||||
|
||||
func StrPtr(s string) *string { return &s }
|
||||
|
||||
func Call(fn any, args ...any) *gomock.Call {
|
||||
switch fn := fn.(type) {
|
||||
case func() *gomock.Call:
|
||||
return fn()
|
||||
case func(any) *gomock.Call:
|
||||
return fn(args[0])
|
||||
case func(any, any) *gomock.Call:
|
||||
return fn(args[0], args[1])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -69,6 +69,21 @@ func (mr *MockDALMockRecorder) Banks() *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Banks", reflect.TypeOf((*MockDAL)(nil).Banks))
|
||||
}
|
||||
|
||||
// Categories mocks base method.
|
||||
func (m *MockDAL) Categories() ([]entity.Category, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Categories")
|
||||
ret0, _ := ret[0].([]entity.Category)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Categories indicates an expected call of Categories.
|
||||
func (mr *MockDALMockRecorder) Categories() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Categories", reflect.TypeOf((*MockDAL)(nil).Categories))
|
||||
}
|
||||
|
||||
// InsertTransaction mocks base method.
|
||||
func (m *MockDAL) InsertTransaction(arg0 entity.Transaction) (entity.Transaction, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user