test parse menu model to response

This commit is contained in:
renaldybrada
2026-02-26 12:46:20 +07:00
parent cbd90a7850
commit 8626ef11ec
3 changed files with 53 additions and 0 deletions
+1
View File
@@ -24,6 +24,7 @@ require (
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
+2
View File
@@ -51,6 +51,8 @@ github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQA
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+50
View File
@@ -0,0 +1,50 @@
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)
}
})
}
}