diff --git a/cmd/jet/main.go b/cmd/jet/main.go index 384653c..49a347e 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -312,34 +312,31 @@ func genTemplate(dialect jet.Dialect, tablesFilter, viewsFilter, enumsFilter tem } func createTemplateFilter(ignoreList, allowList string) templateFilter { - ignoreListParsed := parseList(ignoreList) - allowListParsed := parseList(allowList) - - if len(allowListParsed) > 0 { + if allowList != "" { return templateFilter{ - names: allowListParsed, + names: parseList(allowList), allow: true, } } return templateFilter{ - names: ignoreListParsed, + names: parseList(ignoreList), allow: false, } } -func shouldSkipTable(table metadata.Table, config templateFilter) bool { - if config.allow { - return !strslice.Contains(config.names, strings.ToLower(table.Name)) +func shouldSkipTable(table metadata.Table, filter templateFilter) bool { + if filter.allow { + 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 { - if config.allow { - return !strslice.Contains(config.names, strings.ToLower(enum.Name)) +func shouldSkipEnum(enum metadata.Enum, filter templateFilter) bool { + if filter.allow { + 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)) } diff --git a/tests/mysql/generator_test.go b/tests/mysql/generator_test.go index 763a310..01293fa 100644 --- a/tests/mysql/generator_test.go +++ b/tests/mysql/generator_test.go @@ -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") + }) + } +} diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 06478a6..ada4580 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -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") + }) + } +} diff --git a/tests/sqlite/generator_test.go b/tests/sqlite/generator_test.go index f261d22..4140d5c 100644 --- a/tests/sqlite/generator_test.go +++ b/tests/sqlite/generator_test.go @@ -427,3 +427,53 @@ type Address struct { 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") +}