Made 'SetSchema' to be generated in a dedicated file for views and tables.

This commit is contained in:
Jay 2022-12-02 23:02:44 +05:30
parent 199bb2a20a
commit 7db99b10bc
5 changed files with 164 additions and 72 deletions

View file

@ -133,12 +133,12 @@ func TestIgnoreTablesViewsEnums(t *testing.T) {
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")
"film_category.go", "film_text.go", "inventory.go", "language.go", "payment.go", "table.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")
"sales_by_film_category.go", "sales_by_store.go", "staff_list.go", "view.go")
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
@ -162,18 +162,20 @@ func assertGeneratedFiles(t *testing.T) {
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "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")
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/table.go", tableSetSchemaFile)
// 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")
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/actor_info.go", actorInfoSQLBuilderFile)
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/view.go", viewSetSchemaFile)
// Enums SQL Builder files
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
@ -308,6 +310,17 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
MutableColumns: mutableColumns,
}
}
`
var tableSetSchemaFile = `
//
// 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
func SetDvdsSchema(schema string) {
Actor = Actor.FromSchema(schema)
@ -437,3 +450,23 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
}
}
`
var viewSetSchemaFile = `
//
// 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
func SetDvdsSchema(schema string) {
ActorInfo = ActorInfo.FromSchema(schema)
CustomerList = CustomerList.FromSchema(schema)
FilmList = FilmList.FromSchema(schema)
NicerButSlowerFilmList = NicerButSlowerFilmList.FromSchema(schema)
SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema)
SalesByStore = SalesByStore.FromSchema(schema)
StaffList = StaffList.FromSchema(schema)
}
`

View file

@ -153,14 +153,14 @@ func TestGeneratorIgnoreTables(t *testing.T) {
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")
"payment.go", "rental.go", "staff.go", "store.go", "table.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")
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "view.go")
// Enums SQL Builder files
_, err = ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
@ -241,18 +241,20 @@ func assertGeneratedFiles(t *testing.T) {
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "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")
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/table.go", actorSQLBuilderTableFile)
// 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")
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/actor_info.go", actorInfoSQLBuilderFile)
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/view.go", actorInfoSQLBuilderViewFile)
// Enums SQL Builder files
enumFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
@ -388,6 +390,35 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
}
`
var actorSQLBuilderTableFile = `
//
// 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
func SetDvdsSchema(schema string) {
Film = Film.FromSchema(schema)
Address = Address.FromSchema(schema)
Actor = Actor.FromSchema(schema)
Category = Category.FromSchema(schema)
City = City.FromSchema(schema)
Country = Country.FromSchema(schema)
Customer = Customer.FromSchema(schema)
FilmActor = FilmActor.FromSchema(schema)
FilmCategory = FilmCategory.FromSchema(schema)
Inventory = Inventory.FromSchema(schema)
Language = Language.FromSchema(schema)
Rental = Rental.FromSchema(schema)
Staff = Staff.FromSchema(schema)
Payment = Payment.FromSchema(schema)
Store = Store.FromSchema(schema)
}
`
var actorModelFile = `
//
// Code generated by go-jet DO NOT EDIT.
@ -497,6 +528,27 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
}
`
var actorInfoSQLBuilderViewFile = `
//
// 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
func SetDvdsSchema(schema string) {
ActorInfo = ActorInfo.FromSchema(schema)
CustomerList = CustomerList.FromSchema(schema)
FilmList = FilmList.FromSchema(schema)
NicerButSlowerFilmList = NicerButSlowerFilmList.FromSchema(schema)
SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema)
SalesByStore = SalesByStore.FromSchema(schema)
StaffList = StaffList.FromSchema(schema)
}
`
func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
skipForCockroachDB(t)
@ -523,7 +575,7 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
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")
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "table.go")
testutils.AssertFileContent(t, tableDir+"/all_types.go", allTypesTableContent)
}
@ -914,15 +966,4 @@ func newAllTypesTableImpl(schemaName, tableName, alias string) allTypesTable {
MutableColumns: mutableColumns,
}
}
func SetTestSampleSchema(schema string) {
AllTypes = AllTypes.FromSchema(schema)
Link = Link.FromSchema(schema)
Employee = Employee.FromSchema(schema)
Person = Person.FromSchema(schema)
PersonPhone = PersonPhone.FromSchema(schema)
WeirdNamesTable = WeirdNamesTable.FromSchema(schema)
User = User.FromSchema(schema)
Floats = Floats.FromSchema(schema)
}
`

View file

@ -92,12 +92,12 @@ func TestCmdGeneratorIgnoreTablesViewsEnums(t *testing.T) {
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "country.go",
"customer.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "staff.go")
"payment.go", "staff.go", "table.go")
viewSQLBuilderFiles, err := ioutil.ReadDir(genDestDir + "/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "sales_by_film_category.go",
"sales_by_store.go")
"sales_by_store.go", "view.go")
modelFiles, err := ioutil.ReadDir(genDestDir + "/model")
require.NoError(t, err)
@ -114,18 +114,20 @@ func assertGeneratedFiles(t *testing.T) {
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "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")
"payment.go", "rental.go", "staff.go", "store.go", "table.go")
testutils.AssertFileContent(t, genDestDir+"/table/actor.go", actorSQLBuilderFile)
testutils.AssertFileContent(t, genDestDir+"/table/table.go", actorSQLBuilderTableFile)
// 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")
"customer_list.go", "sales_by_store.go", "staff_list.go", "view.go")
testutils.AssertFileContent(t, genDestDir+"/view/film_list.go", filmListSQLBuilderFile)
testutils.AssertFileContent(t, genDestDir+"/view/view.go", filmListSQLBuilderViewFile)
// Model files
modelFiles, err := ioutil.ReadDir(genDestDir + "/model")
@ -225,6 +227,16 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable {
MutableColumns: mutableColumns,
}
}
`
const actorSQLBuilderTableFile = `
//
// 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
func SetSchema(schema string) {
Actor = Actor.FromSchema(schema)
@ -345,6 +357,25 @@ func newFilmListTableImpl(schemaName, tableName, alias string) filmListTable {
}
`
const filmListSQLBuilderViewFile = `
//
// 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
func SetSchema(schema string) {
CustomerList = CustomerList.FromSchema(schema)
FilmList = FilmList.FromSchema(schema)
SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema)
SalesByStore = SalesByStore.FromSchema(schema)
StaffList = StaffList.FromSchema(schema)
}
`
const addressModelFile = `
//
// Code generated by go-jet DO NOT EDIT.