Tests clean up.

This commit is contained in:
go-jet 2019-06-11 12:47:35 +02:00
parent ffba8718ca
commit 367602757f
20 changed files with 46932 additions and 178 deletions

View file

@ -2,8 +2,8 @@ package tests
import (
"database/sql"
"fmt"
"github.com/go-jet/jet/tests/.test_files/dvd_rental/dvds/model"
"github.com/go-jet/jet/tests/dbconfig"
_ "github.com/lib/pq"
"github.com/pkg/profile"
"gotest.tools/assert"
@ -12,103 +12,23 @@ import (
"testing"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
dbname = "dvd_rental"
)
var connectString = fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
var db *sql.DB
//var tx *sql.Tx
//go:generate generator -host=localhost -port=5432 -user=postgres -password=postgres -dbname=dvd_rental -schema dvds -path .test_files
//go:generate generator -host=localhost -port=5432 -user=postgres -password=postgres -dbname=dvd_rental -sslmode=disable -schema test_sample -path .test_files
func TestMain(m *testing.M) {
fmt.Println("Begin")
defer profile.Start().Stop()
var err error
db, err = sql.Open("postgres", connectString)
db, err = sql.Open("postgres", dbconfig.ConnectString)
if err != nil {
panic("Failed to connect to test db")
}
//tx, _ = db.Begin()
defer cleanUp()
dbInit()
defer db.Close()
ret := m.Run()
cleanUp()
fmt.Println("END")
os.Exit(ret)
}
func cleanUp() {
fmt.Println("CLEAN UP")
//tx.Rollback()
db.Close()
}
func dbInit() {
linkTableCreate := `
DROP TABLE IF EXISTS test_sample.link;
CREATE TABLE IF NOT EXISTS test_sample.link (
ID serial PRIMARY KEY,
url VARCHAR (255) NOT NULL,
name VARCHAR (255) NOT NULL,
description VARCHAR (255),
rel VARCHAR (50)
);
DROP TABLE IF EXISTS test_sample.employee;
CREATE TABLE test_sample.employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
manager_id INT,
FOREIGN KEY (manager_id)
REFERENCES test_sample.employee (employee_id)
ON DELETE CASCADE
);
INSERT INTO test_sample.employee (
employee_id,
first_name,
last_name,
manager_id
)
VALUES
(1, 'Windy', 'Hays', NULL),
(2, 'Ava', 'Christensen', 1),
(3, 'Hassan', 'Conner', 1),
(4, 'Anna', 'Reeves', 2),
(5, 'Sau', 'Norman', 2),
(6, 'Kelsie', 'Hays', 3),
(7, 'Tory', 'Goff', 3),
(8, 'Salley', 'Lester', 3);
`
result, err := db.Exec(linkTableCreate)
if err != nil {
panic(err)
}
fmt.Println(result)
}
func TestGenerateModel(t *testing.T) {
actor := model.Actor{}