2019-04-07 09:58:12 +02:00
|
|
|
package tests
|
|
|
|
|
|
|
|
|
|
import (
|
2019-07-19 12:39:10 +02:00
|
|
|
"bytes"
|
2019-04-07 09:58:12 +02:00
|
|
|
"database/sql"
|
2019-07-19 12:39:10 +02:00
|
|
|
"github.com/go-jet/jet/generator/postgres"
|
2019-06-24 10:01:34 +02:00
|
|
|
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model"
|
2019-06-11 12:47:35 +02:00
|
|
|
"github.com/go-jet/jet/tests/dbconfig"
|
2019-04-14 17:55:10 +02:00
|
|
|
_ "github.com/lib/pq"
|
2019-05-20 17:37:55 +02:00
|
|
|
"github.com/pkg/profile"
|
2019-05-01 18:23:19 +02:00
|
|
|
"gotest.tools/assert"
|
2019-07-19 12:39:10 +02:00
|
|
|
"io/ioutil"
|
2019-04-07 09:58:12 +02:00
|
|
|
"os"
|
2019-07-19 12:39:10 +02:00
|
|
|
"os/exec"
|
2019-05-24 13:13:13 +02:00
|
|
|
"reflect"
|
2019-04-07 09:58:12 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var db *sql.DB
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2019-05-20 17:37:55 +02:00
|
|
|
defer profile.Start().Stop()
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
var err error
|
2019-07-29 18:08:53 +02:00
|
|
|
db, err = sql.Open("postgres", dbconfig.PostgresConnectString)
|
2019-04-07 09:58:12 +02:00
|
|
|
if err != nil {
|
|
|
|
|
panic("Failed to connect to test db")
|
|
|
|
|
}
|
2019-06-11 12:47:35 +02:00
|
|
|
defer db.Close()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
ret := m.Run()
|
|
|
|
|
|
|
|
|
|
os.Exit(ret)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-19 12:39:10 +02:00
|
|
|
func TestGeneratedModel(t *testing.T) {
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
actor := model.Actor{}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(actor.ActorID).String(), "int32")
|
|
|
|
|
actorIDField, ok := reflect.TypeOf(actor).FieldByName("ActorID")
|
|
|
|
|
assert.Assert(t, ok)
|
2019-06-12 12:47:30 +02:00
|
|
|
assert.Equal(t, actorIDField.Tag.Get("sql"), "primary_key")
|
2019-05-24 13:13:13 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(actor.FirstName).String(), "string")
|
|
|
|
|
assert.Equal(t, reflect.TypeOf(actor.LastName).String(), "string")
|
|
|
|
|
assert.Equal(t, reflect.TypeOf(actor.LastUpdate).String(), "time.Time")
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
filmActor := model.FilmActor{}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(filmActor.FilmID).String(), "int16")
|
|
|
|
|
filmIDField, ok := reflect.TypeOf(filmActor).FieldByName("FilmID")
|
|
|
|
|
assert.Assert(t, ok)
|
2019-06-12 12:47:30 +02:00
|
|
|
assert.Equal(t, filmIDField.Tag.Get("sql"), "primary_key")
|
2019-05-01 18:23:19 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(filmActor.ActorID).String(), "int16")
|
|
|
|
|
actorIDField, ok = reflect.TypeOf(filmActor).FieldByName("ActorID")
|
|
|
|
|
assert.Assert(t, ok)
|
2019-06-12 12:47:30 +02:00
|
|
|
assert.Equal(t, filmIDField.Tag.Get("sql"), "primary_key")
|
2019-05-01 18:23:19 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
staff := model.Staff{}
|
2019-05-01 18:23:19 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(staff.Email).String(), "*string")
|
2019-06-12 16:21:50 +02:00
|
|
|
assert.Equal(t, reflect.TypeOf(staff.Picture).String(), "*[]uint8")
|
2019-05-01 18:23:19 +02:00
|
|
|
}
|
2019-07-19 12:39:10 +02:00
|
|
|
|
|
|
|
|
const genTestDir2 = "./.gentestdata2"
|
|
|
|
|
|
|
|
|
|
func TestCmdGenerator(t *testing.T) {
|
|
|
|
|
err := os.RemoveAll(genTestDir2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command("jet", "-dbname=jetdb", "-host=localhost", "-port=5432",
|
|
|
|
|
"-user=jet", "-password=jet", "-schema=dvds", "-path="+genTestDir2)
|
|
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assertGeneratedFiles(t)
|
|
|
|
|
|
|
|
|
|
err = os.RemoveAll(genTestDir2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGenerator(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
err := os.RemoveAll(genTestDir2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
err = postgres.Generate(genTestDir2, postgres.DBConnection{
|
|
|
|
|
Host: dbconfig.Host,
|
|
|
|
|
Port: dbconfig.Port,
|
|
|
|
|
User: dbconfig.User,
|
|
|
|
|
Password: dbconfig.Password,
|
|
|
|
|
SslMode: "disable",
|
|
|
|
|
Params: "",
|
|
|
|
|
|
|
|
|
|
DBName: dbconfig.DBName,
|
|
|
|
|
SchemaName: "dvds",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assertGeneratedFiles(t)
|
|
|
|
|
|
|
|
|
|
err = os.RemoveAll(genTestDir2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertGeneratedFiles(t *testing.T) {
|
|
|
|
|
// Table SQL Builder files
|
|
|
|
|
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assertFileNameEqual(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")
|
|
|
|
|
|
|
|
|
|
assertFileContent(t, "./.gentestdata2/jetdb/dvds/table/actor.go", "\npackage table", actorSQLBuilderFile)
|
|
|
|
|
|
|
|
|
|
// Enums SQL Builder files
|
|
|
|
|
enumFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
|
2019-07-20 14:07:59 +02:00
|
|
|
assert.NilError(t, err)
|
2019-07-19 12:39:10 +02:00
|
|
|
|
|
|
|
|
assertFileNameEqual(t, enumFiles, "mpaa_rating.go")
|
|
|
|
|
assertFileContent(t, "./.gentestdata2/jetdb/dvds/enum/mpaa_rating.go", "\npackage enum", mpaaRatingEnumFile)
|
|
|
|
|
|
|
|
|
|
// Model files
|
|
|
|
|
modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assertFileNameEqual(t, modelFiles, "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")
|
|
|
|
|
|
|
|
|
|
assertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", "\npackage model", actorModelFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertFileContent(t *testing.T, filePath string, contentBegin string, expectedContent string) {
|
|
|
|
|
enumFileData, err := ioutil.ReadFile(filePath)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
beginIndex := bytes.Index(enumFileData, []byte(contentBegin))
|
|
|
|
|
|
|
|
|
|
//fmt.Println("-"+string(enumFileData[beginIndex:])+"-")
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, string(enumFileData[beginIndex:]), expectedContent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertFileNameEqual(t *testing.T, fileInfos []os.FileInfo, fileNames ...string) {
|
|
|
|
|
|
|
|
|
|
fileNamesMap := map[string]bool{}
|
|
|
|
|
|
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
|
fileNamesMap[fileInfo.Name()] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, fileName := range fileNames {
|
|
|
|
|
assert.Assert(t, fileNamesMap[fileName], fileName+" does not exist.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mpaaRatingEnumFile = `
|
|
|
|
|
package enum
|
|
|
|
|
|
|
|
|
|
import "github.com/go-jet/jet"
|
|
|
|
|
|
|
|
|
|
var MpaaRating = &struct {
|
|
|
|
|
G jet.StringExpression
|
|
|
|
|
Pg jet.StringExpression
|
|
|
|
|
Pg13 jet.StringExpression
|
|
|
|
|
R jet.StringExpression
|
|
|
|
|
Nc17 jet.StringExpression
|
|
|
|
|
}{
|
|
|
|
|
G: jet.NewEnumValue("G"),
|
|
|
|
|
Pg: jet.NewEnumValue("PG"),
|
|
|
|
|
Pg13: jet.NewEnumValue("PG-13"),
|
|
|
|
|
R: jet.NewEnumValue("R"),
|
|
|
|
|
Nc17: jet.NewEnumValue("NC-17"),
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
var actorSQLBuilderFile = `
|
|
|
|
|
package table
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/go-jet/jet"
|
2019-07-29 18:08:53 +02:00
|
|
|
"github.com/go-jet/jet/postgres"
|
2019-07-19 12:39:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var Actor = newActorTable()
|
|
|
|
|
|
|
|
|
|
type ActorTable struct {
|
|
|
|
|
jet.Table
|
|
|
|
|
|
|
|
|
|
//Columns
|
2019-07-29 18:08:53 +02:00
|
|
|
ActorID postgres.ColumnInteger
|
|
|
|
|
FirstName postgres.ColumnString
|
|
|
|
|
LastName postgres.ColumnString
|
|
|
|
|
LastUpdate postgres.ColumnTimestamp
|
2019-07-19 12:39:10 +02:00
|
|
|
|
|
|
|
|
AllColumns jet.ColumnList
|
|
|
|
|
MutableColumns jet.ColumnList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// creates new ActorTable with assigned alias
|
|
|
|
|
func (a *ActorTable) AS(alias string) *ActorTable {
|
|
|
|
|
aliasTable := newActorTable()
|
|
|
|
|
|
|
|
|
|
aliasTable.Table.AS(alias)
|
|
|
|
|
|
|
|
|
|
return aliasTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newActorTable() *ActorTable {
|
|
|
|
|
var (
|
2019-07-29 18:08:53 +02:00
|
|
|
ActorIDColumn = postgres.IntegerColumn("actor_id")
|
|
|
|
|
FirstNameColumn = postgres.StringColumn("first_name")
|
|
|
|
|
LastNameColumn = postgres.StringColumn("last_name")
|
|
|
|
|
LastUpdateColumn = postgres.TimestampColumn("last_update")
|
2019-07-19 12:39:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return &ActorTable{
|
2019-07-29 18:08:53 +02:00
|
|
|
Table: jet.NewTable(jet.PostgreSQL, "dvds", "actor", ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn),
|
2019-07-19 12:39:10 +02:00
|
|
|
|
|
|
|
|
//Columns
|
|
|
|
|
ActorID: ActorIDColumn,
|
|
|
|
|
FirstName: FirstNameColumn,
|
|
|
|
|
LastName: LastNameColumn,
|
|
|
|
|
LastUpdate: LastUpdateColumn,
|
|
|
|
|
|
|
|
|
|
AllColumns: jet.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn},
|
|
|
|
|
MutableColumns: jet.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
var actorModelFile = `
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Actor struct {
|
|
|
|
|
ActorID int32 ` + "`sql:\"primary_key\"`" + `
|
|
|
|
|
FirstName string
|
|
|
|
|
LastName string
|
|
|
|
|
LastUpdate time.Time
|
|
|
|
|
}
|
|
|
|
|
`
|