jet/internal/testutils/test_utils.go

211 lines
6.3 KiB
Go
Raw Normal View History

2019-07-29 18:08:53 +02:00
package testutils
import (
"bytes"
"encoding/json"
"fmt"
2019-08-03 14:10:47 +02:00
"github.com/go-jet/jet/internal/jet"
2019-09-20 13:55:07 +02:00
"github.com/go-jet/jet/internal/utils"
"github.com/go-jet/jet/qrm"
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-17 18:32:01 +02:00
// AssertExec assert statement execution for successful execution and number of rows affected
func AssertExec(t *testing.T, stmt jet.Statement, db qrm.DB, rowsAffected ...int64) {
2019-08-02 11:08:24 +02:00
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])
}
}
2019-08-17 18:32:01 +02:00
// AssertExecErr assert statement execution for failed execution with error string errorStr
func AssertExecErr(t *testing.T, stmt jet.Statement, db qrm.DB, errorStr string) {
2019-08-02 11:08:24 +02:00
_, 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)
}
2019-08-17 18:32:01 +02:00
// PrintJson print v as json
2019-08-08 10:51:59 +02:00
func PrintJson(v interface{}) {
jsonText, _ := json.MarshalIndent(v, "", "\t")
fmt.Println(string(jsonText))
}
2019-08-17 18:32:01 +02:00
// AssertJSON check if data json output is the same as expectedJSON
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-17 18:32:01 +02:00
// SaveJSONFile saves v as json at testRelativePath
func SaveJSONFile(v interface{}, testRelativePath string) {
2019-08-08 10:51:59 +02:00
jsonText, _ := json.MarshalIndent(v, "", "\t")
filePath := getFullPath(testRelativePath)
err := ioutil.WriteFile(filePath, jsonText, 0644)
2019-09-20 13:55:07 +02:00
utils.PanicOnError(err)
2019-08-08 10:51:59 +02:00
}
2019-08-17 18:32:01 +02:00
// AssertJSONFile check if data json representation is the same as json at testRelativePath
2019-08-08 10:51:59 +02:00
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
}
2019-08-17 18:32:01 +02:00
// AssertStatementSql check if statement Sql() is the same as expectedQuery and expectedArgs
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-17 18:32:01 +02:00
// AssertStatementSqlErr checks if statement Sql() panics with errorStr
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-08-17 18:32:01 +02:00
// AssertDebugStatementSql check if statement Sql() is the same as expectedQuery
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-17 18:32:01 +02:00
// AssertClauseSerialize checks if clause serialize produces expected query and args
2019-08-12 12:11:16 +02:00
func AssertClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Serializer, query string, args ...interface{}) {
2019-08-17 18:32:01 +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
//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-12-08 11:07:49 +01:00
// AssertDebugClauseSerialize checks if clause serialize produces expected debug query and args
func AssertDebugClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Serializer, query string, args ...interface{}) {
out := jet.SQLBuilder{Dialect: dialect, Debug: true}
jet.Serialize(clause, jet.SelectStatementType, &out)
assert.DeepEqual(t, out.Buff.String(), query)
if len(args) > 0 {
assert.DeepEqual(t, out.Args, args)
}
}
// AssertPanicErr checks if running a function fun produces a panic with errorStr string
func AssertPanicErr(t *testing.T, fun func(), errorStr string) {
defer func() {
r := recover()
assert.Equal(t, r, errorStr)
}()
fun()
}
2019-08-17 18:32:01 +02:00
// AssertClauseSerializeErr check if clause serialize panics with errString
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
2019-08-17 18:32:01 +02:00
out := jet.SQLBuilder{Dialect: dialect}
jet.Serialize(clause, jet.SelectStatementType, &out)
2019-07-31 18:43:54 +02:00
}
2019-08-17 18:32:01 +02:00
// AssertProjectionSerialize check if projection serialize produces expected query and args
2019-08-12 12:11:16 +02:00
func AssertProjectionSerialize(t *testing.T, dialect jet.Dialect, projection jet.Projection, query string, args ...interface{}) {
2019-08-17 18:32:01 +02:00
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
}
2019-08-17 18:32:01 +02:00
// AssertQueryPanicErr check if statement Query execution panics with error errString
func AssertQueryPanicErr(t *testing.T, stmt jet.Statement, db qrm.DB, dest interface{}, errString string) {
defer func() {
r := recover()
assert.Equal(t, r, errString)
}()
stmt.Query(db, dest)
}
2019-09-20 13:55:07 +02:00
// AssertFileContent check if file content at filePath contains expectedContent text.
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)
}
2019-09-20 13:55:07 +02:00
// AssertFileNamesEqual check if all filesInfos are contained in fileNames
func AssertFileNamesEqual(t *testing.T, fileInfos []os.FileInfo, fileNames ...string) {
assert.Equal(t, len(fileInfos), len(fileNames))
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.")
}
}