jet/tests/mysql/generator_test.go

394 lines
11 KiB
Go
Raw Normal View History

2019-08-08 17:51:20 +02:00
package mysql
import (
"io/ioutil"
"os"
"os/exec"
"strconv"
2019-08-08 17:51:20 +02:00
"testing"
2021-08-30 15:09:09 +03:00
"github.com/go-jet/jet/v2/generator/mysql"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/tests/dbconfig"
"github.com/stretchr/testify/require"
2019-08-08 17:51:20 +02:00
)
const genTestDirRoot = "./.gentestdata3"
const genTestDir3 = "./.gentestdata3/mysql"
2019-08-08 17:51:20 +02:00
func TestGenerator(t *testing.T) {
2019-09-20 19:24:54 +02:00
for i := 0; i < 3; i++ {
err := mysql.Generate(genTestDir3, dbConnection("dvds"))
2019-08-08 17:51:20 +02:00
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
2019-09-20 19:24:54 +02:00
assertGeneratedFiles(t)
}
2019-08-08 17:51:20 +02:00
2021-08-30 15:09:09 +03:00
for i := 0; i < 3; i++ {
dsn := dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds")
2021-08-30 15:09:09 +03:00
err := mysql.GenerateDSN(dsn, genTestDir3)
require.NoError(t, err)
assertGeneratedFiles(t)
}
2019-09-20 19:24:54 +02:00
err := os.RemoveAll(genTestDirRoot)
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
}
func TestCmdGenerator(t *testing.T) {
2021-01-24 16:47:06 +01:00
err := os.RemoveAll(genTestDir3)
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
var cmd *exec.Cmd
if sourceIsMariaDB() {
cmd = exec.Command("jet",
"-source=MariaDB",
"-dbname=dvds",
"-host="+dbconfig.MariaDBHost,
"-port="+strconv.Itoa(dbconfig.MariaDBPort),
"-user="+dbconfig.MariaDBUser,
"-password="+dbconfig.MariaDBPassword,
"-path="+genTestDir3)
} else {
cmd = exec.Command("jet",
"-source=MySQL",
"-dbname=dvds",
"-host="+dbconfig.MySqLHost,
"-port="+strconv.Itoa(dbconfig.MySQLPort),
"-user="+dbconfig.MySQLUser,
"-password="+dbconfig.MySQLPassword,
"-path="+genTestDir3)
}
2019-08-08 17:51:20 +02:00
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
assertGeneratedFiles(t)
err = os.RemoveAll(genTestDirRoot)
require.NoError(t, err)
2021-08-30 15:09:09 +03:00
// check that generation via DSN works
dsn := "mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds")
2021-08-30 15:09:09 +03:00
cmd = exec.Command("jet", "-dsn="+dsn, "-path="+genTestDir3)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
require.NoError(t, err)
}
2021-08-30 15:09:09 +03:00
func TestIgnoreTablesViewsEnums(t *testing.T) {
tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"),
"-ignore-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-ignore-views=actor_info,CUSTomER_LIST, film_list",
"-ignore-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,
"-ignore-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-ignore-views=actor_info,CUSTomER_LIST, film_list",
"-ignore-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)
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")
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")
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "nicer_but_slower_film_list_rating.go")
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, modelFiles,
"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")
})
}
2019-08-08 17:51:20 +02:00
}
func assertGeneratedFiles(t *testing.T) {
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
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",
2019-08-08 17:51:20 +02:00
"payment.go", "rental.go", "staff.go", "store.go")
2020-04-13 10:18:14 +02:00
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/actor.go", actorSQLBuilderFile)
// 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")
2020-04-13 10:18:14 +02:00
testutils.AssertFileContent(t, genTestDir3+"/dvds/view/actor_info.go", actorInfoSQLBuilderFile)
2019-08-08 17:51:20 +02:00
// Enums SQL Builder files
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
testutils.AssertFileNamesEqual(t, enumFiles, "film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_rating.go")
2020-04-13 10:18:14 +02:00
testutils.AssertFileContent(t, genTestDir3+"/dvds/enum/film_rating.go", mpaaRatingEnumFile)
2019-08-08 17:51:20 +02:00
// Model files
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)
2019-08-08 17:51:20 +02:00
testutils.AssertFileNamesEqual(t, modelFiles, "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",
"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")
2019-08-08 17:51:20 +02:00
2020-04-13 10:18:14 +02:00
testutils.AssertFileContent(t, genTestDir3+"/dvds/model/actor.go", actorModelFile)
2019-08-08 17:51:20 +02:00
}
var mpaaRatingEnumFile = `
2020-04-13 10:18:14 +02:00
//
// 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
//
2019-08-08 17:51:20 +02:00
package enum
2020-06-27 18:48:19 +02:00
import "github.com/go-jet/jet/v2/mysql"
2019-08-08 17:51:20 +02:00
var FilmRating = &struct {
G mysql.StringExpression
Pg mysql.StringExpression
Pg13 mysql.StringExpression
R mysql.StringExpression
Nc17 mysql.StringExpression
}{
G: mysql.NewEnumValue("G"),
Pg: mysql.NewEnumValue("PG"),
Pg13: mysql.NewEnumValue("PG-13"),
R: mysql.NewEnumValue("R"),
Nc17: mysql.NewEnumValue("NC-17"),
}
`
var actorSQLBuilderFile = `
2020-04-13 10:18:14 +02:00
//
// 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
//
2019-08-08 17:51:20 +02:00
package table
import (
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/mysql"
2019-08-08 17:51:20 +02:00
)
var Actor = newActorTable("dvds", "actor", "")
2019-08-08 17:51:20 +02:00
type ActorTable struct {
2019-08-08 17:51:20 +02:00
mysql.Table
//Columns
ActorID mysql.ColumnInteger
FirstName mysql.ColumnString
LastName mysql.ColumnString
LastUpdate mysql.ColumnTimestamp
AllColumns mysql.ColumnList
MutableColumns mysql.ColumnList
2019-08-08 17:51:20 +02:00
}
// AS creates new ActorTable with assigned alias
func (a ActorTable) AS(alias string) ActorTable {
return newActorTable(a.SchemaName(), a.TableName(), alias)
2019-08-08 17:51:20 +02:00
}
// Schema creates new ActorTable with assigned schema name
func (a ActorTable) FromSchema(schemaName string) ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
2022-03-18 11:18:22 +08:00
// 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 {
2019-08-08 17:51:20 +02:00
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")
FirstNameColumn = mysql.StringColumn("first_name")
LastNameColumn = mysql.StringColumn("last_name")
LastUpdateColumn = mysql.TimestampColumn("last_update")
allColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn}
mutableColumns = mysql.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn}
2019-08-08 17:51:20 +02:00
)
return ActorTable{
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
2019-08-08 17:51:20 +02:00
//Columns
ActorID: ActorIDColumn,
FirstName: FirstNameColumn,
LastName: LastNameColumn,
LastUpdate: LastUpdateColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
2019-08-08 17:51:20 +02:00
}
}
`
var actorModelFile = `
2020-04-13 10:18:14 +02:00
//
// 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
//
2019-08-08 17:51:20 +02:00
package model
import (
"time"
)
type Actor struct {
ActorID uint16 ` + "`sql:\"primary_key\"`" + `
FirstName string
LastName string
LastUpdate time.Time
}
`
var actorInfoSQLBuilderFile = `
2020-04-13 10:18:14 +02:00
//
// 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
import (
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/mysql"
)
var ActorInfo = newActorInfoTable("dvds", "actor_info", "")
type ActorInfoTable struct {
mysql.Table
//Columns
ActorID mysql.ColumnInteger
FirstName mysql.ColumnString
LastName mysql.ColumnString
FilmInfo mysql.ColumnString
AllColumns mysql.ColumnList
MutableColumns mysql.ColumnList
}
// 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())
}
2022-03-18 11:18:22 +08:00
// 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 {
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")
FirstNameColumn = mysql.StringColumn("first_name")
LastNameColumn = mysql.StringColumn("last_name")
FilmInfoColumn = mysql.StringColumn("film_info")
allColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
mutableColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
)
return ActorInfoTable{
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
ActorID: ActorIDColumn,
FirstName: FirstNameColumn,
LastName: LastNameColumn,
FilmInfo: FilmInfoColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
}
}
`