2016-06-12 20:19:23 +02:00
|
|
|
package main
|
2016-02-29 04:30:54 +01:00
|
|
|
|
2016-03-03 05:14:21 +01:00
|
|
|
import (
|
2016-04-16 07:17:25 +02:00
|
|
|
"bufio"
|
2016-03-03 20:30:48 +01:00
|
|
|
"bytes"
|
2016-04-16 07:17:25 +02:00
|
|
|
"fmt"
|
2016-03-03 20:30:48 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-04-16 07:17:25 +02:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2016-03-03 05:14:21 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb"
|
2016-08-18 08:18:10 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb/drivers"
|
2016-03-03 05:14:21 +01:00
|
|
|
)
|
2016-03-02 06:37:14 +01:00
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
var state *State
|
2016-04-16 07:17:25 +02:00
|
|
|
var rgxHasSpaces = regexp.MustCompile(`^\s+`)
|
2016-03-27 17:03:14 +02:00
|
|
|
|
2016-03-02 06:37:14 +01:00
|
|
|
func init() {
|
2016-06-12 03:25:00 +02:00
|
|
|
state = &State{
|
2016-08-18 08:18:10 +02:00
|
|
|
Driver: drivers.MockDriver(0),
|
2016-06-23 08:09:56 +02:00
|
|
|
Tables: []bdb.Table{
|
2016-03-23 07:18:41 +01:00
|
|
|
{
|
|
|
|
Name: "patrick_table",
|
2016-06-23 08:09:56 +02:00
|
|
|
Columns: []bdb.Column{
|
2016-07-09 19:13:35 +02:00
|
|
|
{Name: "patrick_column", Type: "string", Nullable: false},
|
|
|
|
{Name: "aaron_column", Type: "null.String", Nullable: true},
|
|
|
|
{Name: "id", Type: "null.Int", Nullable: true},
|
|
|
|
{Name: "fun_id", Type: "int64", Nullable: false},
|
|
|
|
{Name: "time", Type: "null.Time", Nullable: true},
|
|
|
|
{Name: "fun_time", Type: "time.Time", Nullable: false},
|
|
|
|
{Name: "cool_stuff_forever", Type: "[]byte", Nullable: false},
|
2016-03-23 07:18:41 +01:00
|
|
|
},
|
2016-06-23 08:09:56 +02:00
|
|
|
PKey: &bdb.PrimaryKey{
|
2016-04-25 03:43:09 +02:00
|
|
|
Name: "pkey_thing",
|
|
|
|
Columns: []string{"id", "fun_id"},
|
|
|
|
},
|
2016-03-03 20:30:48 +01:00
|
|
|
},
|
2016-03-23 07:18:41 +01:00
|
|
|
{
|
|
|
|
Name: "spiderman",
|
2016-06-23 08:09:56 +02:00
|
|
|
Columns: []bdb.Column{
|
2016-07-09 19:13:35 +02:00
|
|
|
{Name: "id", Type: "int64", Nullable: false},
|
2016-05-02 08:34:25 +02:00
|
|
|
},
|
2016-06-23 08:09:56 +02:00
|
|
|
PKey: &bdb.PrimaryKey{
|
2016-05-02 08:34:25 +02:00
|
|
|
Name: "pkey_id",
|
|
|
|
Columns: []string{"id"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "spiderman_table_two",
|
2016-06-23 08:09:56 +02:00
|
|
|
Columns: []bdb.Column{
|
2016-07-09 19:13:35 +02:00
|
|
|
{Name: "id", Type: "int64", Nullable: false},
|
|
|
|
{Name: "patrick", Type: "string", Nullable: false},
|
2016-03-23 07:18:41 +01:00
|
|
|
},
|
2016-06-23 08:09:56 +02:00
|
|
|
PKey: &bdb.PrimaryKey{
|
2016-05-02 08:34:25 +02:00
|
|
|
Name: "pkey_id",
|
|
|
|
Columns: []string{"id"},
|
|
|
|
},
|
2016-03-02 06:37:14 +01:00
|
|
|
},
|
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
Config: &Config{
|
|
|
|
PkgName: "patrick",
|
|
|
|
OutFolder: "",
|
|
|
|
DriverName: "postgres",
|
|
|
|
},
|
2016-04-06 22:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadTemplate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
template, err := loadTemplate("templates_test/main_test", "postgres_main.tpl")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to loadTemplate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if template == nil {
|
|
|
|
t.Fatal("Unable to load template.")
|
2016-03-02 06:37:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 05:14:21 +01:00
|
|
|
func TestTemplates(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2016-03-03 20:30:48 +01:00
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
if err := checkPKeys(state.Tables); err != nil {
|
2016-05-02 08:34:25 +02:00
|
|
|
t.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
|
2016-03-03 20:30:48 +01:00
|
|
|
// Initialize the templates
|
|
|
|
var err error
|
2016-06-12 03:25:00 +02:00
|
|
|
state.Templates, err = loadTemplates("templates")
|
2016-03-03 20:30:48 +01:00
|
|
|
if err != nil {
|
2016-03-04 02:57:22 +01:00
|
|
|
t.Fatalf("Unable to initialize templates: %s", err)
|
2016-03-03 20:30:48 +01:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
if len(state.Templates.Templates()) == 0 {
|
2016-04-03 08:01:26 +02:00
|
|
|
t.Errorf("Templates is empty.")
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
state.SingletonTemplates, err = loadTemplates("templates/singleton")
|
2016-04-19 04:02:32 +02:00
|
|
|
if err != nil {
|
2016-06-12 00:17:47 +02:00
|
|
|
t.Fatalf("Unable to initialize singleton templates: %s", err)
|
2016-04-19 04:02:32 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
if len(state.SingletonTemplates.Templates()) == 0 {
|
2016-06-12 03:25:00 +02:00
|
|
|
t.Errorf("SingletonTemplates is empty.")
|
2016-04-19 04:02:32 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
state.TestTemplates, err = loadTemplates("templates_test")
|
2016-04-03 08:01:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize templates: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
if len(state.Templates.Templates()) == 0 {
|
2016-04-03 08:01:26 +02:00
|
|
|
t.Errorf("Templates is empty.")
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
state.TestMainTemplate, err = loadTemplate("templates_test/main_test", "postgres_main.tpl")
|
2016-04-06 22:10:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize templates: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-12 08:50:38 +02:00
|
|
|
state.SingletonTestTemplates, err = loadTemplates("templates_test/singleton")
|
2016-06-12 08:41:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize single test templates: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
if len(state.SingletonTestTemplates.Templates()) == 0 {
|
2016-06-12 08:41:04 +02:00
|
|
|
t.Errorf("SingleTestTemplates is empty.")
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
state.Config.OutFolder, err = ioutil.TempDir("", "templates")
|
2016-03-03 20:30:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %s", err)
|
|
|
|
}
|
2016-07-13 05:00:13 +02:00
|
|
|
defer func() {
|
|
|
|
if t.Failed() {
|
|
|
|
t.Log("template test output:", state.Config.OutFolder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(state.Config.OutFolder)
|
|
|
|
}()
|
2016-03-03 20:30:48 +01:00
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
if err = state.Run(true); err != nil {
|
2016-04-03 08:01:26 +02:00
|
|
|
t.Errorf("Unable to run SQLBoilerRun: %s", err)
|
|
|
|
}
|
2016-03-03 20:30:48 +01:00
|
|
|
|
2016-04-16 07:17:25 +02:00
|
|
|
buf := &bytes.Buffer{}
|
2016-03-23 04:03:35 +01:00
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
cmd := exec.Command("go", "test", "-c")
|
2016-06-12 03:25:00 +02:00
|
|
|
cmd.Dir = state.Config.OutFolder
|
2016-04-16 07:17:25 +02:00
|
|
|
cmd.Stderr = buf
|
2016-03-03 20:30:48 +01:00
|
|
|
|
|
|
|
if err = cmd.Run(); err != nil {
|
2016-04-16 07:17:25 +02:00
|
|
|
t.Errorf("go test cmd execution failed: %s", err)
|
2016-06-12 03:25:00 +02:00
|
|
|
outputCompileErrors(buf, state.Config.OutFolder)
|
2016-04-16 07:17:25 +02:00
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputCompileErrors(buf *bytes.Buffer, outFolder string) {
|
|
|
|
type errObj struct {
|
|
|
|
errMsg string
|
|
|
|
fileName string
|
|
|
|
lineNumber int
|
|
|
|
}
|
|
|
|
|
|
|
|
var errObjects []errObj
|
|
|
|
lineBuf := &bytes.Buffer{}
|
|
|
|
|
|
|
|
bufLines := bytes.Split(buf.Bytes(), []byte{'\n'})
|
|
|
|
for i := 0; i < len(bufLines); i++ {
|
|
|
|
lineBuf.Reset()
|
|
|
|
if !bytes.HasPrefix(bufLines[i], []byte("./")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(lineBuf, "%s\n", bufLines[i])
|
|
|
|
|
|
|
|
splits := bytes.Split(bufLines[i], []byte{':'})
|
|
|
|
lineNum, err := strconv.Atoi(string(splits[1]))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Cant convert line number to int: %s", bufLines[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
eObj := errObj{
|
|
|
|
fileName: string(splits[0]),
|
|
|
|
lineNumber: lineNum,
|
|
|
|
}
|
|
|
|
|
|
|
|
for y := i; y < len(bufLines); y++ {
|
|
|
|
if !rgxHasSpaces.Match(bufLines[y]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Fprintf(lineBuf, "%s\n", bufLines[y])
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
eObj.errMsg = lineBuf.String()
|
|
|
|
|
|
|
|
errObjects = append(errObjects, eObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, eObj := range errObjects {
|
|
|
|
fmt.Printf("-----------------\n")
|
|
|
|
fmt.Println(eObj.errMsg)
|
|
|
|
|
|
|
|
filePath := filepath.Join(outFolder, eObj.fileName)
|
|
|
|
fh, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Cant open the file: %#v", eObj))
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(fh)
|
2016-04-23 13:54:24 +02:00
|
|
|
throwaway := eObj.lineNumber - 5
|
2016-04-16 07:17:25 +02:00
|
|
|
for throwaway > 0 && scanner.Scan() {
|
|
|
|
throwaway--
|
|
|
|
}
|
|
|
|
|
2016-04-23 13:54:24 +02:00
|
|
|
for i := 0; i < 6; i++ {
|
2016-04-16 07:17:25 +02:00
|
|
|
if scanner.Scan() {
|
|
|
|
b := scanner.Bytes()
|
|
|
|
if len(b) != 0 {
|
|
|
|
fmt.Printf("%s\n", b)
|
|
|
|
} else {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh.Close()
|
2016-03-03 20:30:48 +01:00
|
|
|
}
|
2016-03-03 05:14:21 +01:00
|
|
|
}
|