Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

menu crud operations #29

Merged
merged 32 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
362e13f
fix: docker mysql image
shouta0715 Jan 2, 2024
f73ecd9
add: domain test and NewDishesFromJson
shouta0715 Jan 3, 2024
f106759
Merge pull request #28 from ogurilab/feature/domain
shouta0715 Jan 3, 2024
8cde998
fix TestFetchMenusByOfferedAt
shouta0715 Jan 3, 2024
00c0572
fix newMenu -> ReNewMenu
shouta0715 Jan 3, 2024
3441c65
add: mewnu repository
shouta0715 Jan 3, 2024
5d31870
refactor: split menu domain and menu with dishes domain
shouta0715 Jan 8, 2024
1220666
fix: menu domain
shouta0715 Jan 8, 2024
af22ed0
add: menu usecase
shouta0715 Jan 8, 2024
2ab3698
fix: menu sqlc tests
shouta0715 Jan 8, 2024
053bc01
fix: with dishes domain name
shouta0715 Jan 8, 2024
6696779
fix: city_controller.go
shouta0715 Jan 8, 2024
e2f3a8e
fix: validator
shouta0715 Jan 9, 2024
17b71d8
fix: re new make menu sqlc
shouta0715 Jan 9, 2024
3044745
fix: re new make menu domain
shouta0715 Jan 9, 2024
f5730da
fix: typo
shouta0715 Jan 9, 2024
5ebdb07
fix: re menu repository
shouta0715 Jan 9, 2024
256ec9c
fix: re menu usecase
shouta0715 Jan 9, 2024
bed1e30
update: submodule and utils
shouta0715 Jan 9, 2024
60aee69
fix: github actions
shouta0715 Jan 9, 2024
5c70342
Merge pull request #30 from ogurilab/feature/fix-menu-domain
shouta0715 Jan 9, 2024
69997df
update swagger
shouta0715 Jan 9, 2024
a768903
add: YYYY-MM-DD validator and validator tests
shouta0715 Jan 9, 2024
7edd467
update submodule
shouta0715 Jan 9, 2024
7b08cee
add: menu controller
shouta0715 Jan 9, 2024
93199be
add: utils date and update validator
shouta0715 Jan 9, 2024
32a7084
add: menu json convertor
shouta0715 Jan 10, 2024
97a3ca2
add: dishes custom marshal
shouta0715 Jan 10, 2024
1de1857
add: menu routes
shouta0715 Jan 10, 2024
7cf956f
fix: req offered is required
shouta0715 Jan 10, 2024
c64382c
update submodule
shouta0715 Jan 10, 2024
c78d526
update submodule merge develop
shouta0715 Jan 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
name: Run unit Tests

on:
push:
branches: ['main', 'develop']
pull_request:
branches: ['main', 'develop']
paths:
- 'app/**'

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion app/doc/api
Submodule api updated 1 files
+238 −75 spec/open-api.yaml
2 changes: 1 addition & 1 deletion app/doc/statik/statik.go

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions app/domain/city_domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package domain

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewCity(t *testing.T) {

type input struct {
cityCode int32
cityName string
prefectureCode int32
prefectureName string
}
testCases := []struct {
name string
createStub func() input
check func(*City, error)
}{
{
name: "OK",
createStub: func() input {
return input{
cityCode: 1,
cityName: "cityName",
prefectureCode: 1,
prefectureName: "prefectureName",
}
},
check: func(c *City, err error) {
require.NoError(t, err)
require.NotNil(t, c)
require.Equal(t, int32(1), c.CityCode)
require.Equal(t, "cityName", c.CityName)
require.Equal(t, int32(1), c.PrefectureCode)
require.Equal(t, "prefectureName", c.PrefectureName)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
input := tc.createStub()
c := NewCity(input.cityCode, input.cityName, input.prefectureCode, input.prefectureName)
tc.check(c, nil)
})
}

}
14 changes: 14 additions & 0 deletions app/domain/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package domain

import "time"

const (
DEFAULT_LIMIT = int32(10)
DEFAULT_OFFSET = int32(0)
Expand All @@ -9,8 +11,20 @@ type Response struct {
Data interface{} `json:"data"`
}

type FetchResponse struct {
Data interface{} `json:"data"`
Next interface{} `json:"next"`
}

func NewResponse(data interface{}) *Response {
return &Response{
Data: data,
}
}

func NewFetchResponse[T string | int32 | time.Time](data interface{}, next T) *FetchResponse {
return &FetchResponse{
Data: data,
Next: next,
}
}
36 changes: 36 additions & 0 deletions app/domain/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewResponse(t *testing.T) {

testCases := []struct {
name string
createStub func() interface{}
check func(*Response)
}{
{
name: "OK",
createStub: func() interface{} {
return "data"
},
check: func(r *Response) {
require.NotNil(t, r)
require.Equal(t, "data", r.Data)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
data := tc.createStub()
r := NewResponse(data)
tc.check(r)
})
}

}
13 changes: 13 additions & 0 deletions app/domain/dish_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain

import (
"context"
"encoding/json"

"github.com/ogurilab/school-lunch-api/util"
)
Expand Down Expand Up @@ -48,3 +49,15 @@ func NewDish(menuID string, name string) (*Dish, error) {

return newDish(id, menuID, name)
}

func NewDishesFromJson(
input json.RawMessage,
) ([]*Dish, error) {
var dishes []*Dish

if err := json.Unmarshal(input, &dishes); err != nil {
return nil, err
}

return dishes, nil
}
146 changes: 146 additions & 0 deletions app/domain/dish_domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package domain

import (
"encoding/json"
"fmt"
"testing"

"github.com/ogurilab/school-lunch-api/util"
"github.com/stretchr/testify/require"
)

func TestNewDish(t *testing.T) {

testCases := []struct {
name string
menuID string
dishName string
check func(*Dish, error)
}{
{
name: "OK",
menuID: "dish1",
dishName: "dish1",
check: func(dish *Dish, err error) {
require.NoError(t, err)
require.NotNil(t, dish)
require.Equal(t, "dish1", dish.MenuID)
require.Equal(t, "dish1", dish.Name)
require.NotEmpty(t, dish.ID)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dish, err := NewDish(tc.menuID, tc.dishName)
tc.check(dish, err)
})
}

}

func TestReNewDish(t *testing.T) {
id := util.NewUlid()
testCases := []struct {
name string
id string
menuID string
dishName string
check func(*Dish, error)
}{
{
name: "OK",
id: id,
menuID: "dish1",
dishName: "dish1",
check: func(dish *Dish, err error) {
require.NoError(t, err)
require.NotNil(t, dish)
require.Equal(t, "dish1", dish.MenuID)
require.Equal(t, "dish1", dish.Name)
require.Equal(t, id, dish.ID)
},
},

{
name: "Invalid Ulid",
id: "invalid",
menuID: "dish1",
dishName: "dish1",
check: func(dish *Dish, err error) {
require.Error(t, err)
require.Nil(t, dish)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dish, err := ReNewDish(tc.id, tc.menuID, tc.dishName)
tc.check(dish, err)
})
}
}

func TestNewDishesFromJson(t *testing.T) {
id := util.NewUlid()
testCases := []struct {
name string
createStubs func() json.RawMessage
check func([]*Dish, error)
}{
{
name: "OK",
createStubs: func() json.RawMessage {
data := fmt.Sprintf(`[{"id": "%s", "menu_id": "menu1", "name": "dish1"}]`, id)

return json.RawMessage(data)
},
check: func(dishes []*Dish, err error) {
require.NoError(t, err)
require.NotNil(t, dishes)
require.Len(t, dishes, 1)
require.Equal(t, "menu1", dishes[0].MenuID)
require.Equal(t, "dish1", dishes[0].Name)
require.Equal(t, id, dishes[0].ID)
},
},

{
name: "Multiple Dishes",
createStubs: func() json.RawMessage {
data := fmt.Sprintf(`[{"id": "%s", "menu_id": "menu1", "name": "dish1"},{"id": "%s", "menu_id": "menu1", "name": "dish2"}]`, id, id)

return json.RawMessage(data)

},
check: func(dishes []*Dish, err error) {
require.NoError(t, err)
require.NotNil(t, dishes)
require.Len(t, dishes, 2)
},
},
{
name: "Invalid JSON",
createStubs: func() json.RawMessage {
data := fmt.Sprintf(`[{"id": "%s", "menu_id": "menu1", "name": "dish1"}`, id)

return json.RawMessage(data)
},
check: func(dishes []*Dish, err error) {
require.Error(t, err)
require.Nil(t, dishes)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
stub := tc.createStubs()
dishes, err := NewDishesFromJson(stub)
tc.check(dishes, err)
})

}
}
Loading