51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package access
|
|
|
|
import (
|
|
"database/sql"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
type testMenuMapping struct {
|
|
testContext string
|
|
model []RolePageModel
|
|
expected []AvailableMenuResponse
|
|
}
|
|
|
|
func TestMappingMenuModelToResponse(t *testing.T) {
|
|
dataset := []testMenuMapping{
|
|
{
|
|
testContext: "all menu",
|
|
model: []RolePageModel{
|
|
{ID: "1", Name: "MenuParent1", Level: 1, Sort: 1},
|
|
{ID: "2", Name: "MenuChild11", Level: 2, Sort: 1, ParentId: sql.NullString{String: "1"}},
|
|
{ID: "3", Name: "MenuParent2", Level: 1, Sort: 2},
|
|
{ID: "4", Name: "MenuChild21", Level: 2, Sort: 1, ParentId: sql.NullString{String: "3"}},
|
|
{ID: "5", Name: "MenuChild22", Level: 2, Sort: 2, ParentId: sql.NullString{String: "3"}},
|
|
{ID: "6", Name: "MenuChild23", Level: 2, Sort: 3, ParentId: sql.NullString{String: "3"}},
|
|
},
|
|
expected: []AvailableMenuResponse{
|
|
{ID: "1", Header: "MenuParent1", Children: []*AvailableMenuChildResponse{
|
|
{Title: "MenuChild11", Children: nil},
|
|
}},
|
|
{ID: "3", Header: "MenuParent2", Children: []*AvailableMenuChildResponse{
|
|
{Title: "MenuChild21", Children: nil},
|
|
{Title: "MenuChild22", Children: nil},
|
|
{Title: "MenuChild23", Children: nil},
|
|
}},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tCase := range dataset {
|
|
t.Run(tCase.testContext, func(t *testing.T) {
|
|
result := MapMenuModelToResponse(tCase.model)
|
|
|
|
if diff := cmp.Diff(tCase.expected, result); diff != "" {
|
|
t.Errorf("mismatch (-expected +result):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|