add tests for model-json-tag
This commit is contained in:
parent
ba65c2ab0c
commit
9cc22af2dd
1 changed files with 94 additions and 0 deletions
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/model"
|
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/model"
|
||||||
"github.com/go-jet/jet/v2/tests/dbconfig"
|
"github.com/go-jet/jet/v2/tests/dbconfig"
|
||||||
"github.com/go-jet/jet/v2/tests/internal/utils/file"
|
"github.com/go-jet/jet/v2/tests/internal/utils/file"
|
||||||
|
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
func dsn(host string, port int, dbName, user, password string) string {
|
func dsn(host string, port int, dbName, user, password string) string {
|
||||||
|
|
@ -1505,3 +1506,96 @@ func TestAllowAndIgnoreEnums(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJsonInvalidModelTags(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "with invalid json tag",
|
||||||
|
args: []string{
|
||||||
|
"-dsn=" + defaultDSN(),
|
||||||
|
"-schema=dvds",
|
||||||
|
"-tables=actor,ADDRESS,country, Film , cITY,",
|
||||||
|
"-views=Actor_info, FILM_LIST ,staff_list",
|
||||||
|
"-enums=mpaa_rating",
|
||||||
|
"-path=" + genTestDir2,
|
||||||
|
"-model-json-tag=invalid",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with invalid json tag",
|
||||||
|
args: []string{
|
||||||
|
"-dsn=" + defaultDSN(),
|
||||||
|
"-schema=dvds",
|
||||||
|
"-tables=actor,ADDRESS,country, Film , cITY,",
|
||||||
|
"-views=Actor_info, FILM_LIST ,staff_list",
|
||||||
|
"-enums=mpaa_rating",
|
||||||
|
"-path=" + genTestDir2,
|
||||||
|
"-model-json-tag= invalid",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
cmd := exec.Command("jet", tt.args...)
|
||||||
|
|
||||||
|
var stdOut bytes.Buffer
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = &stdOut
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Equal(t, "exit status 1", err.Error())
|
||||||
|
|
||||||
|
stdOutput := stdOut.String()
|
||||||
|
require.Contains(t, stdOutput, "ERROR: json tag does not contain correct value")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSnakeCaseModelJsonTag(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "with snake-case",
|
||||||
|
args: []string{
|
||||||
|
"-dsn=" + defaultDSN(),
|
||||||
|
"-schema=dvds",
|
||||||
|
"-views=Actor_info",
|
||||||
|
"-tables=actor",
|
||||||
|
"-path=" + genTestDir2,
|
||||||
|
"-model-json-tag=snake-case",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
cmd := exec.Command("jet", tt.args...)
|
||||||
|
|
||||||
|
var stdOut bytes.Buffer
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = &stdOut
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
actor := file2.Exists(t, genTestDir2+"/jetdb/dvds/model", "actor.go")
|
||||||
|
require.Contains(t, actor, `json:"actor_id"`)
|
||||||
|
require.Contains(t, actor, `json:"first_name"`)
|
||||||
|
require.Contains(t, actor, `json:"last_name"`)
|
||||||
|
require.Contains(t, actor, `json:"last_update"`)
|
||||||
|
|
||||||
|
actorInfo := file2.Exists(t, genTestDir2+"/jetdb/dvds/model", "actor_info.go")
|
||||||
|
require.Contains(t, actorInfo, `json:"actor_id"`)
|
||||||
|
require.Contains(t, actorInfo, `json:"first_name"`)
|
||||||
|
require.Contains(t, actorInfo, `json:"last_name"`)
|
||||||
|
require.Contains(t, actorInfo, `json:"file_info"`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue