Optimize model naming (#910)
* add unit test * fix #907 * format code * format code * format code Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -50,7 +50,7 @@ func MysqlDDL(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return fromDDl(src, dir, cfg, cache, idea, database)
|
||||
return fromDDL(src, dir, cfg, cache, idea, database)
|
||||
}
|
||||
|
||||
// MySqlDataSource generates model code from datasource
|
||||
@@ -102,7 +102,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
|
||||
return fromPostgreSqlDataSource(url, pattern, dir, schema, cfg, cache, idea)
|
||||
}
|
||||
|
||||
func fromDDl(src, dir string, cfg *config.Config, cache, idea bool, database string) error {
|
||||
func fromDDL(src, dir string, cfg *config.Config, cache, idea bool, database string) error {
|
||||
log := console.NewConsole(idea)
|
||||
src = strings.TrimSpace(src)
|
||||
if len(src) == 0 {
|
||||
|
||||
@@ -24,12 +24,12 @@ func TestFromDDl(t *testing.T) {
|
||||
err := gen.Clean()
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = fromDDl("./user.sql", t.TempDir(), cfg, true, false, "go_zero")
|
||||
err = fromDDL("./user.sql", t.TempDir(), cfg, true, false, "go_zero")
|
||||
assert.Equal(t, errNotMatched, err)
|
||||
|
||||
// case dir is not exists
|
||||
unknownDir := filepath.Join(t.TempDir(), "test", "user.sql")
|
||||
err = fromDDl(unknownDir, t.TempDir(), cfg, true, false, "go_zero")
|
||||
err = fromDDL(unknownDir, t.TempDir(), cfg, true, false, "go_zero")
|
||||
assert.True(t, func() bool {
|
||||
switch err.(type) {
|
||||
case *os.PathError:
|
||||
@@ -40,7 +40,7 @@ func TestFromDDl(t *testing.T) {
|
||||
}())
|
||||
|
||||
// case empty src
|
||||
err = fromDDl("", t.TempDir(), cfg, true, false, "go_zero")
|
||||
err = fromDDL("", t.TempDir(), cfg, true, false, "go_zero")
|
||||
if err != nil {
|
||||
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
|
||||
}
|
||||
@@ -70,9 +70,18 @@ func TestFromDDl(t *testing.T) {
|
||||
_, err = os.Stat(user2Sql)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, cfg, true, false, "go_zero")
|
||||
assert.Nil(t, err)
|
||||
filename := filepath.Join(tempDir, "usermodel.go")
|
||||
fromDDL := func(db string) {
|
||||
err = fromDDL(filepath.Join(tempDir, "user*.sql"), tempDir, cfg, true, false, db)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = os.Stat(filepath.Join(tempDir, "usermodel.go"))
|
||||
assert.Nil(t, err)
|
||||
_, err = os.Stat(filename)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
fromDDL("go_zero")
|
||||
_ = os.Remove(filename)
|
||||
fromDDL("go-zero")
|
||||
_ = os.Remove(filename)
|
||||
fromDDL("1gozero")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user