diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 87a7eeec..a03864bc 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -108,6 +108,34 @@ func TestCmdGenerator(t *testing.T) { require.NoError(t, err) } +func TestCmdGeneratorWithPkgNames(t *testing.T) { + err := os.RemoveAll(genTestDir2) + require.NoError(t, err) + + cmd := exec.Command("jet", "-source=PostgreSQL", "-dbname=jetdb", "-host=localhost", + "-port="+strconv.Itoa(dbconfig.PgPort), + "-user=jet", + "-password=jet", + "-schema=dvds", + "-path="+genTestDir2, + "-model-pkg=newmodel", + "-table-pkg=newtable", + "-view-pkg=newview", + "-enum-pkg=newenum", + ) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err = cmd.Run() + require.NoError(t, err) + + assertGeneratedFilesWithPkgNames(t) + + err = os.RemoveAll(genTestDir2) + require.NoError(t, err) +} + func TestGeneratorIgnoreTables(t *testing.T) { tests := []struct { name string @@ -311,6 +339,38 @@ func assertGeneratedFiles(t *testing.T) { testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", actorModelFile) } +func assertGeneratedFilesWithPkgNames(t *testing.T) { + // Table SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newtable", + "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_use_schema.go") + + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/actor.go", actorSQLBuilderFileWithPkgName) + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/table_use_schema.go", tableUseSchemaFileWithPkgName) + + // View SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newview", + "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/newview/actor_info.go", actorInfoSQLBuilderFileWithPkgName) + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newview/view_use_schema.go", viewUseSchemaFileWithPkgName) + + // Enums SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newenum", "mpaa_rating.go") + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newenum/mpaa_rating.go", mpaaRatingEnumFileWithPkgName) + + // Model files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newmodel", "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", + "customer_list.go", "sales_by_store.go", "staff_list.go") + + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newmodel/actor.go", actorModelFileWithPkgName) +} + var mpaaRatingEnumFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -338,6 +398,33 @@ var MpaaRating = &struct { } ` +var mpaaRatingEnumFileWithPkgName = ` +// +// 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 newenum + +import "github.com/go-jet/jet/v2/postgres" + +var MpaaRating = &struct { + G postgres.StringExpression + Pg postgres.StringExpression + Pg13 postgres.StringExpression + R postgres.StringExpression + Nc17 postgres.StringExpression +}{ + G: postgres.NewEnumValue("G"), + Pg: postgres.NewEnumValue("PG"), + Pg13: postgres.NewEnumValue("PG-13"), + R: postgres.NewEnumValue("R"), + Nc17: postgres.NewEnumValue("NC-17"), +} +` + var actorSQLBuilderFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -425,6 +512,93 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable { } ` +var actorSQLBuilderFileWithPkgName = ` +// +// 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 newtable + +import ( + "github.com/go-jet/jet/v2/postgres" +) + +var Actor = newActorTable("dvds", "actor", "") + +type actorTable struct { + postgres.Table + + // Columns + ActorID postgres.ColumnInteger + FirstName postgres.ColumnString + LastName postgres.ColumnString + LastUpdate postgres.ColumnTimestamp + + AllColumns postgres.ColumnList + MutableColumns postgres.ColumnList +} + +type ActorTable struct { + actorTable + + EXCLUDED actorTable +} + +// AS creates new ActorTable with assigned alias +func (a ActorTable) AS(alias string) *ActorTable { + return newActorTable(a.SchemaName(), a.TableName(), alias) +} + +// Schema creates new ActorTable with assigned schema name +func (a ActorTable) FromSchema(schemaName string) *ActorTable { + return newActorTable(schemaName, a.TableName(), a.Alias()) +} + +// WithPrefix creates new ActorTable with assigned table prefix +func (a ActorTable) WithPrefix(prefix string) *ActorTable { + return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) +} + +// WithSuffix creates new ActorTable with assigned table suffix +func (a ActorTable) WithSuffix(suffix string) *ActorTable { + return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) +} + +func newActorTable(schemaName, tableName, alias string) *ActorTable { + return &ActorTable{ + actorTable: newActorTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorTableImpl("", "excluded", ""), + } +} + +func newActorTableImpl(schemaName, tableName, alias string) actorTable { + var ( + ActorIDColumn = postgres.IntegerColumn("actor_id") + FirstNameColumn = postgres.StringColumn("first_name") + LastNameColumn = postgres.StringColumn("last_name") + LastUpdateColumn = postgres.TimestampColumn("last_update") + allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn} + mutableColumns = postgres.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn} + ) + + return actorTable{ + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), + + //Columns + ActorID: ActorIDColumn, + FirstName: FirstNameColumn, + LastName: LastNameColumn, + LastUpdate: LastUpdateColumn, + + AllColumns: allColumns, + MutableColumns: mutableColumns, + } +} +` + var tableUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -456,6 +630,37 @@ func UseSchema(schema string) { } ` +var tableUseSchemaFileWithPkgName = ` +// +// 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 newtable + +// 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) + Category = Category.FromSchema(schema) + City = City.FromSchema(schema) + Country = Country.FromSchema(schema) + Customer = Customer.FromSchema(schema) + Film = Film.FromSchema(schema) + FilmActor = FilmActor.FromSchema(schema) + FilmCategory = FilmCategory.FromSchema(schema) + Inventory = Inventory.FromSchema(schema) + Language = Language.FromSchema(schema) + Payment = Payment.FromSchema(schema) + Rental = Rental.FromSchema(schema) + Staff = Staff.FromSchema(schema) + Store = Store.FromSchema(schema) +} +` + var actorModelFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -478,6 +683,28 @@ type Actor struct { } ` +var actorModelFileWithPkgName = ` +// +// 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 newmodel + +import ( + "time" +) + +type Actor struct { + ActorID int32 ` + "`sql:\"primary_key\"`" + ` + FirstName string + LastName string + LastUpdate time.Time +} +` + var actorInfoSQLBuilderFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -565,6 +792,93 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { } ` +var actorInfoSQLBuilderFileWithPkgName = ` +// +// 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 newview + +import ( + "github.com/go-jet/jet/v2/postgres" +) + +var ActorInfo = newActorInfoTable("dvds", "actor_info", "") + +type actorInfoTable struct { + postgres.Table + + // Columns + ActorID postgres.ColumnInteger + FirstName postgres.ColumnString + LastName postgres.ColumnString + FilmInfo postgres.ColumnString + + AllColumns postgres.ColumnList + MutableColumns postgres.ColumnList +} + +type ActorInfoTable struct { + actorInfoTable + + EXCLUDED actorInfoTable +} + +// AS creates new ActorInfoTable with assigned alias +func (a ActorInfoTable) AS(alias string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName(), alias) +} + +// Schema creates new ActorInfoTable with assigned schema name +func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable { + return newActorInfoTable(schemaName, a.TableName(), a.Alias()) +} + +// WithPrefix creates new ActorInfoTable with assigned table prefix +func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) +} + +// WithSuffix creates new ActorInfoTable with assigned table suffix +func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) +} + +func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable { + return &ActorInfoTable{ + actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorInfoTableImpl("", "excluded", ""), + } +} + +func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { + var ( + ActorIDColumn = postgres.IntegerColumn("actor_id") + FirstNameColumn = postgres.StringColumn("first_name") + LastNameColumn = postgres.StringColumn("last_name") + FilmInfoColumn = postgres.StringColumn("film_info") + allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} + mutableColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} + ) + + return actorInfoTable{ + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), + + //Columns + ActorID: ActorIDColumn, + FirstName: FirstNameColumn, + LastName: LastNameColumn, + FilmInfo: FilmInfoColumn, + + AllColumns: allColumns, + MutableColumns: mutableColumns, + } +} +` + var viewUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -588,6 +902,29 @@ func UseSchema(schema string) { } ` +var viewUseSchemaFileWithPkgName = ` +// +// 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 newview + +// 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) + 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) // because of rowid column