Added unit test cases for the new flags '-allow-tables', '-allow-views', and '-allow-enums'.

This commit is contained in:
SanjaiyKumar 2025-06-18 16:02:36 +05:30
parent bfb02acb43
commit c35892320d
4 changed files with 319 additions and 14 deletions

View file

@ -312,34 +312,31 @@ func genTemplate(dialect jet.Dialect, tablesFilter, viewsFilter, enumsFilter tem
} }
func createTemplateFilter(ignoreList, allowList string) templateFilter { func createTemplateFilter(ignoreList, allowList string) templateFilter {
ignoreListParsed := parseList(ignoreList) if allowList != "" {
allowListParsed := parseList(allowList)
if len(allowListParsed) > 0 {
return templateFilter{ return templateFilter{
names: allowListParsed, names: parseList(allowList),
allow: true, allow: true,
} }
} }
return templateFilter{ return templateFilter{
names: ignoreListParsed, names: parseList(ignoreList),
allow: false, allow: false,
} }
} }
func shouldSkipTable(table metadata.Table, config templateFilter) bool { func shouldSkipTable(table metadata.Table, filter templateFilter) bool {
if config.allow { if filter.allow {
return !strslice.Contains(config.names, strings.ToLower(table.Name)) return !strslice.Contains(filter.names, strings.ToLower(table.Name))
} }
return strslice.Contains(config.names, strings.ToLower(table.Name)) return strslice.Contains(filter.names, strings.ToLower(table.Name))
} }
func shouldSkipEnum(enum metadata.Enum, config templateFilter) bool { func shouldSkipEnum(enum metadata.Enum, filter templateFilter) bool {
if config.allow { if filter.allow {
return !strslice.Contains(config.names, strings.ToLower(enum.Name)) return !strslice.Contains(filter.names, strings.ToLower(enum.Name))
} }
return strslice.Contains(config.names, strings.ToLower(enum.Name)) return strslice.Contains(filter.names, strings.ToLower(enum.Name))
} }

View file

@ -1002,3 +1002,123 @@ func newAllTypesTableImpl(schemaName, tableName, alias string) allTypesTable {
} }
} }
` `
func TestAllowTablesViewsEnums(t *testing.T) {
tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"),
"-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-allow-views=actor_info,CUSTomER_LIST, film_list",
"-allow-enums=film_list_rating,film_rating",
"-path=" + genTestDir3,
},
},
{
name: "without dsn",
args: []string{
"-source=MySQL",
"-dbname=dvds",
"-host=" + dbconfig.MySqLHost,
"-port=" + strconv.Itoa(dbconfig.MySQLPort),
"-user=" + dbconfig.MySQLUser,
"-password=" + dbconfig.MySQLPassword,
"-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-allow-views=actor_info,CUSTomER_LIST, film_list",
"-allow-enums=film_list_rating,film_rating",
"-path=" + genTestDir3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command("jet", tt.args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "category.go", "actor.go", "address.go",
"city.go", "country.go", "staff.go", "store.go", "rental.go", "table_use_schema.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "customer_list.go",
"film_list.go", "view_use_schema.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_list_rating.go", "film_rating.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "category.go", "actor.go", "address.go",
"city.go", "country.go", "staff.go", "store.go", "rental.go", "actor_info.go",
"customer_list.go", "film_list.go", "film_list_rating.go", "film_rating.go")
})
}
}
func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) {
tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"),
"-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-allow-views=actor_info,CUSTomER_LIST, film_list",
"-allow-enums=film_list_rating,film_rating",
"-ignore-tables=actor,ADDRESS,store,rental",
"-ignore-views=film_list",
"-ignore-enums=film_rating",
"-path=" + genTestDir3,
},
},
{
name: "without dsn",
args: []string{
"-source=MySQL",
"-dbname=dvds",
"-host=" + dbconfig.MySqLHost,
"-port=" + strconv.Itoa(dbconfig.MySQLPort),
"-user=" + dbconfig.MySQLUser,
"-password=" + dbconfig.MySQLPassword,
"-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-allow-views=actor_info,CUSTomER_LIST, film_list",
"-allow-enums=film_list_rating,film_rating",
"-ignore-tables=actor,ADDRESS,store,rental",
"-ignore-views=film_list",
"-ignore-enums=film_rating",
"-path=" + genTestDir3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command("jet", tt.args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "category.go", "actor.go", "address.go",
"city.go", "country.go", "staff.go", "store.go", "rental.go", "table_use_schema.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "customer_list.go",
"film_list.go", "view_use_schema.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_list_rating.go", "film_rating.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "category.go", "actor.go", "address.go",
"city.go", "country.go", "staff.go", "store.go", "rental.go", "actor_info.go",
"customer_list.go", "film_list.go", "film_list_rating.go", "film_rating.go")
})
}
}

View file

@ -1384,3 +1384,141 @@ func newLinkTableImpl(schemaName, tableName, alias string) linkTable {
} }
} }
` `
func TestAllowTablesViewsEnums(t *testing.T) {
tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=" + defaultDSN(),
"-schema=dvds",
"-allow-tables=actor,ADDRESS,country, Film , cITY,",
"-allow-views=Actor_info, FILM_LIST ,staff_list",
"-allow-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
{
name: "without dsn",
args: []string{
"-source=PostgreSQL",
"-host=localhost",
"-port=" + strconv.Itoa(dbconfig.PgPort),
"-user=jet",
"-password=jet",
"-dbname=jetdb",
"-schema=dvds",
"-allow-tables=actor,ADDRESS,country, Film , cITY,",
"-allow-views=Actor_info, FILM_LIST ,staff_list",
"-allow-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.RemoveAll(genTestDir2)
require.NoError(t, err)
cmd := exec.Command("jet", tt.args...)
fmt.Println(cmd.Args)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
require.NoError(t, err)
// Table SQL Builder files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table", "actor.go", "address.go",
"country.go", "film.go", "city.go", "table_use_schema.go")
// View SQL Builder files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view", "actor_info.go", "film_list.go",
"staff_list.go", "view_use_schema.go")
// Enums SQL Builder files
file.Exists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go")
// Model files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go",
"country.go", "film.go", "city.go", "actor_info.go", "film_list.go", "staff_list.go", "mpaa_rating.go")
})
}
}
func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) {
tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=" + defaultDSN(),
"-schema=dvds",
"-allow-tables=actor,ADDRESS,country, Film , cITY,",
"-allow-views=Actor_info, FILM_LIST ,staff_list",
"-allow-enums=mpaa_rating",
"-ignore-tables=ADDRESS,country, Film",
"-ignore-views=FILM_LIST",
"-ignore-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
{
name: "without dsn",
args: []string{
"-source=PostgreSQL",
"-host=localhost",
"-port=" + strconv.Itoa(dbconfig.PgPort),
"-user=jet",
"-password=jet",
"-dbname=jetdb",
"-schema=dvds",
"-allow-tables=actor,ADDRESS,country, Film , cITY,",
"-allow-views=Actor_info, FILM_LIST ,staff_list",
"-allow-enums=mpaa_rating",
"-ignore-tables=ADDRESS,country, Film",
"-ignore-views=FILM_LIST",
"-ignore-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.RemoveAll(genTestDir2)
require.NoError(t, err)
cmd := exec.Command("jet", tt.args...)
fmt.Println(cmd.Args)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
require.NoError(t, err)
// Table SQL Builder files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table", "actor.go", "address.go",
"country.go", "film.go", "city.go", "table_use_schema.go")
// View SQL Builder files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view", "actor_info.go", "film_list.go",
"staff_list.go", "view_use_schema.go")
// Enums SQL Builder files
file.Exists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go")
// Model files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go",
"country.go", "film.go", "city.go", "actor_info.go", "film_list.go", "staff_list.go", "mpaa_rating.go")
})
}
}

View file

@ -427,3 +427,53 @@ type Address struct {
LastUpdate time.Time LastUpdate time.Time
} }
` `
func TestAllowTablesEnums(t *testing.T) {
cmd := exec.Command("jet",
"-source=SQLite",
"-dsn=file://"+testDatabaseFilePath,
"-allow-tables=actor,Address,CATEGORY , city ,film,rental,store",
"-allow-views=customer_list, film_list,STAFF_LIst",
"-path="+genDestDir)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go",
"film.go", "rental.go", "store.go", "table_use_schema.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/view", "customer_list.go", "film_list.go", "staff_list.go",
"view_use_schema.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go",
"film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go")
}
func TestAllowAndIgnoreTablesEnums(t *testing.T) {
cmd := exec.Command("jet",
"-source=SQLite",
"-dsn=file://"+testDatabaseFilePath,
"-allow-tables=actor,Address,CATEGORY , city ,film,rental,store",
"-allow-views=customer_list, film_list,STAFF_LIst",
"-ignore-tables=actor,CATEGORY ,store",
"-ignore-views=customer_list",
"-path="+genDestDir)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go",
"film.go", "rental.go", "store.go", "table_use_schema.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/view", "customer_list.go", "film_list.go", "staff_list.go",
"view_use_schema.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go",
"film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go")
}