jet/internal/testutils/test_utils.go

149 lines
3.7 KiB
Go
Raw Normal View History

2019-07-29 18:08:53 +02:00
package testutils
import (
"bytes"
"encoding/json"
"fmt"
2019-08-02 11:08:24 +02:00
"github.com/go-jet/jet/execution"
2019-08-03 14:10:47 +02:00
"github.com/go-jet/jet/internal/jet"
2019-07-29 18:08:53 +02:00
"gotest.tools/assert"
"io/ioutil"
2019-08-08 10:51:59 +02:00
"os"
"path/filepath"
2019-07-29 18:08:53 +02:00
"runtime"
"testing"
)
2019-08-02 11:08:24 +02:00
func AssertExec(t *testing.T, stmt jet.Statement, db execution.DB, rowsAffected ...int64) {
res, err := stmt.Exec(db)
assert.NilError(t, err)
rows, err := res.RowsAffected()
assert.NilError(t, err)
if len(rowsAffected) > 0 {
assert.Equal(t, rows, rowsAffected[0])
}
}
func AssertExecErr(t *testing.T, stmt jet.Statement, db execution.DB, errorStr string) {
_, err := stmt.Exec(db)
assert.Error(t, err, errorStr)
}
2019-08-08 10:51:59 +02:00
func getFullPath(relativePath string) string {
goPath := os.Getenv("GOPATH")
return filepath.Join(goPath, "src/github.com/go-jet/jet/tests", relativePath)
}
func PrintJson(v interface{}) {
jsonText, _ := json.MarshalIndent(v, "", "\t")
fmt.Println(string(jsonText))
}
func AssertJSON(t *testing.T, data interface{}, expectedJSON string) {
2019-07-29 18:08:53 +02:00
jsonData, err := json.MarshalIndent(data, "", "\t")
assert.NilError(t, err)
assert.Equal(t, "\n"+string(jsonData)+"\n", expectedJSON)
}
2019-08-08 10:51:59 +02:00
func SaveJsonFile(v interface{}, testRelativePath string) {
jsonText, _ := json.MarshalIndent(v, "", "\t")
filePath := getFullPath(testRelativePath)
err := ioutil.WriteFile(filePath, jsonText, 0644)
if err != nil {
panic(err)
}
}
func AssertJSONFile(t *testing.T, data interface{}, testRelativePath string) {
filePath := getFullPath(testRelativePath)
fileJSONData, err := ioutil.ReadFile(filePath)
2019-07-29 18:08:53 +02:00
assert.NilError(t, err)
if runtime.GOOS == "windows" {
fileJSONData = bytes.Replace(fileJSONData, []byte("\r\n"), []byte("\n"), -1)
}
jsonData, err := json.MarshalIndent(data, "", "\t")
assert.NilError(t, err)
2019-08-02 11:08:24 +02:00
assert.Assert(t, string(fileJSONData) == string(jsonData))
//assert.DeepEqual(t, string(fileJSONData), string(jsonData))
2019-07-29 18:08:53 +02:00
}
func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
queryStr, args := query.Sql()
2019-07-30 11:18:12 +02:00
assert.Equal(t, queryStr, expectedQuery)
2019-07-30 15:04:36 +02:00
2019-07-31 13:02:30 +02:00
if len(expectedArgs) == 0 {
return
}
2019-07-30 11:18:12 +02:00
assert.DeepEqual(t, args, expectedArgs)
}
2019-08-12 12:11:16 +02:00
func AssertStatementSqlErr(t *testing.T, stmt jet.Statement, errorStr string) {
defer func() {
r := recover()
assert.Equal(t, r, errorStr)
}()
2019-08-12 12:11:16 +02:00
stmt.Sql()
2019-08-12 12:11:16 +02:00
}
2019-07-30 11:18:12 +02:00
func AssertDebugStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
_, args := query.Sql()
if len(expectedArgs) > 0 {
assert.DeepEqual(t, args, expectedArgs)
}
2019-07-29 18:08:53 +02:00
debuqSql := query.DebugSql()
2019-07-29 18:08:53 +02:00
assert.Equal(t, debuqSql, expectedQuery)
}
2019-08-12 12:11:16 +02:00
func AssertClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Serializer, query string, args ...interface{}) {
out := jet.SqlBuilder{Dialect: dialect}
jet.Serialize(clause, jet.SelectStatementType, &out)
2019-07-31 18:43:54 +02:00
2019-08-12 12:11:16 +02:00
//fmt.Println(out.Buff.String())
2019-07-31 18:43:54 +02:00
2019-08-12 12:11:16 +02:00
assert.DeepEqual(t, out.Buff.String(), query)
2019-08-13 10:16:26 +02:00
if len(args) > 0 {
assert.DeepEqual(t, out.Args, args)
}
2019-07-31 18:43:54 +02:00
}
2019-08-12 12:11:16 +02:00
func AssertClauseSerializeErr(t *testing.T, dialect jet.Dialect, clause jet.Serializer, errString string) {
defer func() {
r := recover()
assert.Equal(t, r, errString)
}()
2019-07-31 18:43:54 +02:00
out := jet.SqlBuilder{Dialect: dialect}
jet.Serialize(clause, jet.SelectStatementType, &out)
2019-07-31 18:43:54 +02:00
}
2019-08-12 12:11:16 +02:00
func AssertProjectionSerialize(t *testing.T, dialect jet.Dialect, projection jet.Projection, query string, args ...interface{}) {
out := jet.SqlBuilder{Dialect: dialect}
jet.SerializeForProjection(projection, jet.SelectStatementType, &out)
2019-07-31 18:43:54 +02:00
2019-08-12 12:11:16 +02:00
assert.DeepEqual(t, out.Buff.String(), query)
assert.DeepEqual(t, out.Args, args)
2019-07-31 18:43:54 +02:00
}
func AssertQueryPanicErr(t *testing.T, stmt jet.Statement, db execution.DB, dest interface{}, errString string) {
defer func() {
r := recover()
assert.Equal(t, r, errString)
}()
stmt.Query(db, dest)
}