2016-02-29 04:30:54 +01:00
|
|
|
package cmds
|
|
|
|
|
2016-03-03 05:14:21 +01:00
|
|
|
import (
|
2016-03-03 20:30:48 +01:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-03-03 05:14:21 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pobri19/sqlboiler/dbdrivers"
|
|
|
|
)
|
2016-03-02 06:37:14 +01:00
|
|
|
|
2016-03-27 17:03:14 +02:00
|
|
|
var cmdData *CmdData
|
|
|
|
|
2016-03-02 06:37:14 +01:00
|
|
|
func init() {
|
|
|
|
cmdData = &CmdData{
|
2016-03-23 07:18:41 +01:00
|
|
|
Tables: []dbdrivers.Table{
|
|
|
|
{
|
|
|
|
Name: "patrick_table",
|
|
|
|
Columns: []dbdrivers.Column{
|
|
|
|
{Name: "patrick_column", Type: "string", IsNullable: false},
|
|
|
|
{Name: "aaron_column", Type: "null.String", IsNullable: true},
|
|
|
|
{Name: "id", Type: "null.Int", IsNullable: true},
|
|
|
|
{Name: "fun_id", Type: "int64", IsNullable: false},
|
|
|
|
{Name: "time", Type: "null.Time", IsNullable: true},
|
|
|
|
{Name: "fun_time", Type: "time.Time", IsNullable: false},
|
|
|
|
{Name: "cool_stuff_forever", Type: "[]byte", IsNullable: false},
|
|
|
|
},
|
2016-03-03 20:30:48 +01:00
|
|
|
},
|
2016-03-23 07:18:41 +01:00
|
|
|
{
|
|
|
|
Name: "spiderman",
|
|
|
|
Columns: []dbdrivers.Column{
|
|
|
|
{Name: "patrick", Type: "string", IsNullable: false},
|
|
|
|
},
|
2016-03-02 06:37:14 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
PkgName: "patrick",
|
|
|
|
OutFolder: "",
|
2016-03-23 07:18:41 +01:00
|
|
|
Interface: nil,
|
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
|
|
|
|
|
|
|
// Initialize the templates
|
|
|
|
var err error
|
2016-03-27 17:03:14 +02:00
|
|
|
cmdData.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-04-03 08:01:26 +02:00
|
|
|
if len(cmdData.Templates) == 0 {
|
|
|
|
t.Errorf("Templates is empty.")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdData.TestTemplates, err = loadTemplates("templates_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize templates: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmdData.Templates) == 0 {
|
|
|
|
t.Errorf("Templates is empty.")
|
|
|
|
}
|
|
|
|
|
2016-03-03 20:30:48 +01:00
|
|
|
cmdData.OutFolder, err = ioutil.TempDir("", "templates")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(cmdData.OutFolder)
|
|
|
|
|
2016-04-03 08:01:26 +02:00
|
|
|
if err = cmdData.run(true); err != nil {
|
|
|
|
t.Errorf("Unable to run SQLBoilerRun: %s", err)
|
|
|
|
}
|
2016-03-03 20:30:48 +01:00
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
2016-03-23 04:03:35 +01:00
|
|
|
buf2 := bytes.Buffer{}
|
|
|
|
|
|
|
|
cmd := exec.Command("go", "test")
|
2016-03-03 20:30:48 +01:00
|
|
|
cmd.Dir = cmdData.OutFolder
|
|
|
|
cmd.Stderr = &buf
|
2016-03-23 04:03:35 +01:00
|
|
|
cmd.Stdout = &buf2
|
2016-03-03 20:30:48 +01:00
|
|
|
|
|
|
|
if err = cmd.Run(); err != nil {
|
2016-03-04 02:57:22 +01:00
|
|
|
t.Errorf("go test cmd execution failed: %s\n\n%s", err, buf.String())
|
2016-03-03 20:30:48 +01:00
|
|
|
}
|
2016-03-03 05:14:21 +01:00
|
|
|
}
|