Add more UseSchema tests.

Rename newly generated file to avoid potentional conflict with tables named table or views named view.
This commit is contained in:
go-jet 2023-04-02 13:58:44 +02:00
parent a9aa25416a
commit 5e34bef288
10 changed files with 250 additions and 160 deletions

View file

@ -99,9 +99,8 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st
var tableSqlBuilderSetSchemaTemplate = `package {{package}}
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated {{type}} SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
{{- range .}}
{{ .InstanceName }} = {{ .InstanceName }}.FromSchema(schema)

View file

@ -183,19 +183,20 @@ func processTableSQLBuilder(fileTypes, dirPath string,
func generateUseSchemaFunc(dirPath, fileTypes string, builders []TableSQLBuilder) {
basePath := path.Join(dirPath, builders[0].Path)
fmt.Printf("Generating global `UseSchema` method for %s\n", fileTypes)
text, err := generateTemplate(
autoGenWarningTemplate+tableSqlBuilderSetSchemaTemplate,
builders,
template.FuncMap{
"package": func() string { return builders[0].PackageName() },
"type": func() string { return fileTypes },
},
)
throw.OnError(err)
err = utils.SaveGoFile(basePath, fileTypes, text)
basePath := path.Join(dirPath, builders[0].Path)
fileName := fileTypes + "_use_schema"
err = utils.SaveGoFile(basePath, fileName, text)
throw.OnError(err)
}

View file

@ -253,12 +253,15 @@ func AssertFileContent(t *testing.T, filePath string, expectedContent string) {
}
// AssertFileNamesEqual check if all filesInfos are contained in fileNames
func AssertFileNamesEqual(t *testing.T, fileInfos []os.FileInfo, fileNames ...string) {
require.Equal(t, len(fileInfos), len(fileNames))
func AssertFileNamesEqual(t *testing.T, dirPath string, fileNames ...string) {
files, err := ioutil.ReadDir(dirPath)
require.NoError(t, err)
require.Equal(t, len(files), len(fileNames))
fileNamesMap := map[string]bool{}
for _, fileInfo := range fileInfos {
for _, fileInfo := range files {
fileNamesMap[fileInfo.Name()] = true
}

View file

@ -1,7 +1,6 @@
package mysql
import (
"io/ioutil"
"os"
"os/exec"
"strconv"
@ -130,24 +129,15 @@ func TestIgnoreTablesViewsEnums(t *testing.T) {
err := cmd.Run()
require.NoError(t, err)
tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "customer.go", "film.go", "film_actor.go",
"film_category.go", "film_text.go", "inventory.go", "language.go", "payment.go", "table.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "customer.go", "film.go", "film_actor.go",
"film_category.go", "film_text.go", "inventory.go", "language.go", "payment.go", "table_use_schema.go")
viewSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go")
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "nicer_but_slower_film_list_rating.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "nicer_but_slower_film_list_rating.go")
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles,
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model",
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "nicer_but_slower_film_list_rating.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go",
"sales_by_store.go", "staff_list.go")
@ -157,38 +147,26 @@ func TestIgnoreTablesViewsEnums(t *testing.T) {
func assertGeneratedFiles(t *testing.T) {
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
"payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go")
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/table.go", tableSetSchemaFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/table_use_schema.go", tableUseSchemaFile)
// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go")
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/actor_info.go", actorInfoSQLBuilderFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/view.go", viewSetSchemaFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/view_use_schema.go", viewUseSchemaFile)
// Enums SQL Builder files
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_rating.go")
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_rating.go")
testutils.AssertFileContent(t, genTestDir3+"/dvds/enum/film_rating.go", mpaaRatingEnumFile)
// Model files
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go",
"film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_rating.go",
@ -332,7 +310,7 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
}
`
var tableSetSchemaFile = `
var tableUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -342,9 +320,8 @@ var tableSetSchemaFile = `
package table
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
Actor = Actor.FromSchema(schema)
Address = Address.FromSchema(schema)
@ -473,7 +450,7 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
}
}
`
var viewSetSchemaFile = `
var viewUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -483,9 +460,8 @@ var viewSetSchemaFile = `
package view
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
ActorInfo = ActorInfo.FromSchema(schema)
CustomerList = CustomerList.FromSchema(schema)

View file

@ -928,6 +928,36 @@ LIMIT 5;
require.Equal(t, dest.Films[4].Title, "AFRICAN EGG")
}
func TestUseSchema(t *testing.T) {
UseSchema("dvds2")
defer UseSchema("dvds")
stmt := SELECT(
Film.FilmID,
Film.Title,
Film.LanguageID,
).FROM(
Film,
).WHERE(
Film.Title.EQ(String("AFRICAN EGG")),
)
testutils.AssertDebugStatementSql(t, stmt, `
SELECT film.film_id AS "film.film_id",
film.title AS "film.title",
film.language_id AS "film.language_id"
FROM dvds2.film
WHERE film.title = 'AFRICAN EGG';
`)
var dest model.Film
err := stmt.Query(db, &dest)
require.NoError(t, err)
require.Equal(t, dest.FilmID, uint16(5))
require.Equal(t, dest.Title, "AFRICAN EGG")
require.Equal(t, dest.LanguageID, uint8(1))
}
func TestLateral(t *testing.T) {
skipForMariaDB(t) // MariaDB does not implement LATERAL

View file

@ -883,6 +883,36 @@ ORDER BY "first10Artist"."Artist.ArtistId";
require.Equal(t, dest[0].Album[0].Title, "Plays Metallica By Four Cellos")
}
func TestUseSchema(t *testing.T) {
UseSchema("chinook2")
defer UseSchema("chinook")
stmt := SELECT(
Artist.AllColumns,
).FROM(
Artist,
).WHERE(Artist.ArtistId.EQ(Int(11)))
testutils.AssertDebugStatementSql(t, stmt, `
SELECT "Artist"."ArtistId" AS "Artist.ArtistId",
"Artist"."Name" AS "Artist.Name"
FROM chinook2."Artist"
WHERE "Artist"."ArtistId" = 11;
`)
var artist model.Artist
err := stmt.Query(db, &artist)
require.NoError(t, err)
testutils.AssertJSON(t, artist, `
{
"ArtistId": 11,
"Name": "Black Label Society"
}
`)
}
func TestMultiTenantSameSchemaDifferentTablePrefix(t *testing.T) {
var selectAlbumsFrom = func(tenant string) SelectStatement {

View file

@ -7,6 +7,7 @@ import (
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/internal/3rdparty/snaker"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/internal/utils"
postgres2 "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/tests/dbconfig"
@ -207,28 +208,65 @@ func TestGeneratorTemplate_SQLBuilder_SkipTableAndEnum(t *testing.T) {
return template.DefaultSchema(schemaMetaData).
UseSQLBuilder(template.DefaultSQLBuilder().
UseTable(func(table metadata.Table) template.TableSQLBuilder {
return template.TableSQLBuilder{
Skip: true,
if table.Name != "city" {
return template.TableSQLBuilder{Skip: true}
}
return template.DefaultTableSQLBuilder(table)
}).
UseView(func(table metadata.Table) template.TableSQLBuilder {
return template.TableSQLBuilder{
Skip: true,
UseView(func(view metadata.Table) template.ViewSQLBuilder {
if view.Name != "film_list" {
return template.TableSQLBuilder{Skip: true}
}
return template.DefaultViewSQLBuilder(view)
}).
UseEnum(func(enumMetaData metadata.Enum) template.EnumSQLBuilder {
return template.EnumSQLBuilder{
Skip: true,
}
return template.EnumSQLBuilder{Skip: true}
}),
)
}),
)
require.Nil(t, err)
file2.NotExists(t, defaultTableSQLBuilderFilePath, "actor.go")
file2.NotExists(t, defaultViewSQLBuilderFilePath, "actor_info.go")
testutils.AssertFileNamesEqual(t, defaultTableSQLBuilderFilePath, "city.go", "table_use_schema.go")
testutils.AssertFileNamesEqual(t, defaultViewSQLBuilderFilePath, "film_list.go", "view_use_schema.go")
file2.NotExists(t, defaultEnumSQLBuilderFilePath, "mpaa_rating.go")
testutils.AssertFileContent(t, defaultTableSQLBuilderFilePath+"/table_use_schema.go", `
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
City = City.FromSchema(schema)
}
`)
testutils.AssertFileContent(t, defaultViewSQLBuilderFilePath+"/view_use_schema.go", `
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package view
// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
FilmList = FilmList.FromSchema(schema)
}
`)
}
func TestGeneratorTemplate_SQLBuilder_ChangeTypeAndFileName(t *testing.T) {
@ -269,6 +307,37 @@ func TestGeneratorTemplate_SQLBuilder_ChangeTypeAndFileName(t *testing.T) {
require.Contains(t, actorInfo, "var V_ActorInfo = newActorInfoViewSQLBuilder(\"dvds\", \"actor_info\", \"\")")
mpaaRating := file2.Exists(t, defaultEnumSQLBuilderFilePath, "dvds_mpaa_rating_enum.go")
require.Contains(t, mpaaRating, "var MpaaRatingEnumSQLBuilder = &struct {")
testutils.AssertFileContent(t, defaultTableSQLBuilderFilePath+"/table_use_schema.go", `
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
T_Actor = T_Actor.FromSchema(schema)
T_Address = T_Address.FromSchema(schema)
T_Category = T_Category.FromSchema(schema)
T_City = T_City.FromSchema(schema)
T_Country = T_Country.FromSchema(schema)
T_Customer = T_Customer.FromSchema(schema)
T_Film = T_Film.FromSchema(schema)
T_FilmActor = T_FilmActor.FromSchema(schema)
T_FilmCategory = T_FilmCategory.FromSchema(schema)
T_Inventory = T_Inventory.FromSchema(schema)
T_Language = T_Language.FromSchema(schema)
T_Payment = T_Payment.FromSchema(schema)
T_Rental = T_Rental.FromSchema(schema)
T_Staff = T_Staff.FromSchema(schema)
T_Store = T_Store.FromSchema(schema)
}
`)
}
func TestGeneratorTemplate_Model_AddTags(t *testing.T) {

View file

@ -2,7 +2,7 @@ package postgres
import (
"fmt"
"io/ioutil"
"github.com/go-jet/jet/v2/tests/internal/utils/file"
"os"
"os/exec"
"path/filepath"
@ -148,28 +148,20 @@ func TestGeneratorIgnoreTables(t *testing.T) {
require.NoError(t, err)
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table",
"category.go", "customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go")
// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "view.go")
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view",
"nicer_but_slower_film_list.go", "sales_by_film_category.go", "customer_list.go",
"sales_by_store.go", "view_use_schema.go")
// Enums SQL Builder files
_, err = ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
require.Error(t, err, "open ./.gentestdata2/jetdb/dvds/enum: no such file or directory")
file.NotExists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go")
modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "category.go",
// Model files
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go",
"nicer_but_slower_film_list.go", "sales_by_film_category.go",
@ -236,38 +228,28 @@ func TestGeneratorSpecialCharacters(t *testing.T) {
func assertGeneratedFiles(t *testing.T) {
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table",
"actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
"payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go")
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/table.go", actorSQLBuilderTableFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/table_use_schema.go", tableUseSchemaFile)
// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view",
"actor_info.go", "film_list.go", "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go")
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/actor_info.go", actorInfoSQLBuilderFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/view.go", actorInfoSQLBuilderViewFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/view_use_schema.go", viewUseSchemaFile)
// Enums SQL Builder files
enumFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "mpaa_rating.go")
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go")
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/enum/mpaa_rating.go", mpaaRatingEnumFile)
// Model files
modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "mpaa_rating.go",
"actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go",
@ -390,7 +372,7 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
}
`
var actorSQLBuilderTableFile = `
var tableUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -400,9 +382,8 @@ var actorSQLBuilderTableFile = `
package table
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
Actor = Actor.FromSchema(schema)
Address = Address.FromSchema(schema)
@ -531,7 +512,7 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
}
`
var actorInfoSQLBuilderViewFile = `
var viewUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -541,9 +522,8 @@ var actorInfoSQLBuilderViewFile = `
package view
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
ActorInfo = ActorInfo.FromSchema(schema)
CustomerList = CustomerList.FromSchema(schema)
@ -562,26 +542,17 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
modelDir := filepath.Join(testRoot, "/.gentestdata/jetdb/test_sample/model/")
tableDir := filepath.Join(testRoot, "/.gentestdata/jetdb/test_sample/table/")
enumFiles, err := ioutil.ReadDir(enumDir)
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "mood.go", "level.go")
testutils.AssertFileNamesEqual(t, enumDir, "mood.go", "level.go")
testutils.AssertFileContent(t, enumDir+"/mood.go", moodEnumContent)
testutils.AssertFileContent(t, enumDir+"/level.go", levelEnumContent)
modelFiles, err := ioutil.ReadDir(modelDir)
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "all_types.go", "all_types_view.go", "employee.go", "link.go",
testutils.AssertFileNamesEqual(t, modelDir, "all_types.go", "all_types_view.go", "employee.go", "link.go",
"mood.go", "person.go", "person_phone.go", "weird_names_table.go", "level.go", "user.go", "floats.go", "people.go")
testutils.AssertFileContent(t, modelDir+"/all_types.go", allTypesModelContent)
tableFiles, err := ioutil.ReadDir(tableDir)
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableFiles, "all_types.go", "employee.go", "link.go",
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "table.go", "people.go")
testutils.AssertFileNamesEqual(t, tableDir, "all_types.go", "employee.go", "link.go",
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "people.go", "table_use_schema.go")
testutils.AssertFileContent(t, tableDir+"/all_types.go", allTypesTableContent)
}

View file

@ -1,7 +1,6 @@
package sqlite
import (
"io/ioutil"
"os"
"os/exec"
"reflect"
@ -88,52 +87,36 @@ func TestCmdGeneratorIgnoreTablesViewsEnums(t *testing.T) {
err := cmd.Run()
require.NoError(t, err)
tableSQLBuilderFiles, err := ioutil.ReadDir(genDestDir + "/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "country.go",
testutils.AssertFileNamesEqual(t, genDestDir+"/table", "country.go",
"customer.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "staff.go", "table.go")
"payment.go", "staff.go", "table_use_schema.go")
viewSQLBuilderFiles, err := ioutil.ReadDir(genDestDir + "/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "sales_by_film_category.go",
"sales_by_store.go", "view.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/view", "sales_by_film_category.go",
"sales_by_store.go", "view_use_schema.go")
modelFiles, err := ioutil.ReadDir(genDestDir + "/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "country.go",
testutils.AssertFileNamesEqual(t, genDestDir+"/model", "country.go",
"customer.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "staff.go", "sales_by_film_category.go", "sales_by_store.go")
}
func assertGeneratedFiles(t *testing.T) {
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir(genDestDir + "/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
"payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go")
testutils.AssertFileContent(t, genDestDir+"/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, genDestDir+"/table/table.go", actorSQLBuilderTableFile)
testutils.AssertFileContent(t, genDestDir+"/table/table_use_schema.go", tableUseSchemaFile)
// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir(genDestDir + "/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "film_list.go", "sales_by_film_category.go",
"customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileNamesEqual(t, genDestDir+"/view", "film_list.go", "sales_by_film_category.go",
"customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go")
testutils.AssertFileContent(t, genDestDir+"/view/film_list.go", filmListSQLBuilderFile)
testutils.AssertFileContent(t, genDestDir+"/view/view.go", filmListSQLBuilderViewFile)
testutils.AssertFileContent(t, genDestDir+"/view/view_use_schema.go", viewUseSchemaFile)
// Model files
modelFiles, err := ioutil.ReadDir(genDestDir + "/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go", "country.go",
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go",
"film_list.go", "sales_by_film_category.go",
@ -228,7 +211,7 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
}
}
`
const actorSQLBuilderTableFile = `
const tableUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -238,9 +221,8 @@ const actorSQLBuilderTableFile = `
package table
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
Actor = Actor.FromSchema(schema)
Address = Address.FromSchema(schema)
@ -360,7 +342,7 @@ func newFilmListTableImpl(schemaName, tableName, alias string) filmListTable {
}
`
const filmListSQLBuilderViewFile = `
const viewUseSchemaFile = `
//
// Code generated by go-jet DO NOT EDIT.
//
@ -370,9 +352,8 @@ const filmListSQLBuilderViewFile = `
package view
// UseSchema changes all global tables/views with the value returned
// returned by calling FromSchema on them. Passing an empty string to this function
// will cause queries to be generated without any table/view alias.
// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
CustomerList = CustomerList.FromSchema(schema)
FilmList = FilmList.FromSchema(schema)

View file

@ -728,6 +728,36 @@ LIMIT 10;
require.Len(t, dest, 7)
}
func TestUseSchema(t *testing.T) {
table.UseSchema("chinook")
defer table.UseSchema("")
stmt := SELECT(
table.Artists.AllColumns,
).FROM(
table.Artists,
).WHERE(table.Artists.ArtistId.EQ(Int(11)))
testutils.AssertDebugStatementSql(t, stmt, strings.Replace(`
SELECT artists.''ArtistId'' AS "artists.ArtistId",
artists.''Name'' AS "artists.Name"
FROM chinook.artists
WHERE artists.''ArtistId'' = 11;
`, "''", "`", -1))
var artist model2.Artists
err := stmt.Query(db, &artist)
require.NoError(t, err)
testutils.AssertJSON(t, artist, `
{
"ArtistId": 11,
"Name": "Black Label Society"
}
`)
}
func TestRowsScan(t *testing.T) {
stmt :=
SELECT(