From 8626ef11ecce945780563983fc132e2368b43076 Mon Sep 17 00:00:00 2001 From: renaldybrada Date: Thu, 26 Feb 2026 12:46:20 +0700 Subject: [PATCH] test parse menu model to response --- go.mod | 1 + go.sum | 2 ++ internal/domain/access/access_test.go | 50 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 internal/domain/access/access_test.go diff --git a/go.mod b/go.mod index 2cc3902..b5a4f9d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2c15f51..98e470e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/domain/access/access_test.go b/internal/domain/access/access_test.go new file mode 100644 index 0000000..98540a0 --- /dev/null +++ b/internal/domain/access/access_test.go @@ -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) + } + }) + } +}