jet/internal/testutils/test_utils.go

306 lines
8.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"
2020-05-02 22:15:38 +02:00
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-07-29 18:08:53 +02:00
"io/ioutil"
2019-08-08 10:51:59 +02:00
"os"
"path/filepath"
2019-07-29 18:08:53 +02:00
"runtime"
"testing"
2020-05-02 22:15:38 +02:00
"time"
"github.com/google/go-cmp/cmp"
2019-07-29 18:08:53 +02:00
)
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)
require.NoError(t, err)
2019-08-02 11:08:24 +02:00
rows, err := res.RowsAffected()
require.NoError(t, err)
2019-08-02 11:08:24 +02:00
if len(rowsAffected) > 0 {
require.Equal(t, rows, rowsAffected[0])
2019-08-02 11:08:24 +02:00
}
}
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.NoError(t, err)
2019-07-29 18:08:53 +02:00
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)
assert.NoError(t, err)
2019-07-29 18:08:53 +02:00
if runtime.GOOS == "windows" {
fileJSONData = bytes.Replace(fileJSONData, []byte("\r\n"), []byte("\n"), -1)
}
jsonData, err := json.MarshalIndent(data, "", "\t")
assert.NoError(t, err)
2019-07-29 18:08:53 +02:00
assert.True(t, string(fileJSONData) == string(jsonData))
//AssertDeepEqual(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
}
AssertDeepEqual(t, args, expectedArgs)
2019-07-30 11:18:12 +02:00
}
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 {
AssertDeepEqual(t, args, expectedArgs, "arguments are not equal")
}
2019-07-29 18:08:53 +02:00
debuqSql := query.DebugSql()
2019-07-29 18:08:53 +02:00
assert.Equal(t, debuqSql, expectedQuery)
}
// AssertSerialize checks if clause serialize produces expected query and args
func AssertSerialize(t *testing.T, dialect jet.Dialect, serializer jet.Serializer, query string, args ...interface{}) {
2019-08-17 18:32:01 +02:00
out := jet.SQLBuilder{Dialect: dialect}
jet.Serialize(serializer, 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
AssertDeepEqual(t, out.Buff.String(), query)
2019-08-13 10:16:26 +02:00
if len(args) > 0 {
AssertDeepEqual(t, out.Args, args)
2019-08-13 10:16:26 +02:00
}
2019-07-31 18:43:54 +02:00
}
// AssertClauseSerialize checks if clause serialize produces expected query and args
func AssertClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Clause, query string, args ...interface{}) {
out := jet.SQLBuilder{Dialect: dialect}
clause.Serialize(jet.SelectStatementType, &out)
require.Equal(t, out.Buff.String(), query)
if len(args) > 0 {
AssertDeepEqual(t, out.Args, args)
}
}
// AssertDebugSerialize checks if clause serialize produces expected debug query and args
func AssertDebugSerialize(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)
AssertDeepEqual(t, out.Buff.String(), query)
if len(args) > 0 {
AssertDeepEqual(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()
}
// AssertSerializeErr check if clause serialize panics with errString
func AssertSerializeErr(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
AssertDeepEqual(t, out.Buff.String(), query)
AssertDeepEqual(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.
2020-04-13 10:18:14 +02:00
func AssertFileContent(t *testing.T, filePath string, expectedContent string) {
enumFileData, err := ioutil.ReadFile(filePath)
2020-04-13 10:18:14 +02:00
require.NoError(t, err)
2020-04-13 10:18:14 +02:00
require.Equal(t, "\n"+string(enumFileData), 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.True(t, fileNamesMap[fileName], fileName+" does not exist.")
}
}
// AssertDeepEqual checks if actual and expected objects are deeply equal.
func AssertDeepEqual(t *testing.T, actual, expected interface{}, msg ...string) {
require.True(t, cmp.Equal(actual, expected), msg)
}
2020-05-02 22:15:38 +02:00
// BoolPtr returns address of bool parameter
func BoolPtr(b bool) *bool {
return &b
}
// Int8Ptr returns address of int8 parameter
func Int8Ptr(i int8) *int8 {
return &i
}
// UInt8Ptr returns address of uint8 parameter
func UInt8Ptr(i uint8) *uint8 {
return &i
}
// Int16Ptr returns address of int16 parameter
func Int16Ptr(i int16) *int16 {
return &i
}
// UInt16Ptr returns address of uint16 parameter
func UInt16Ptr(i uint16) *uint16 {
return &i
}
// Int32Ptr returns address of int32 parameter
func Int32Ptr(i int32) *int32 {
return &i
}
// UInt32Ptr returns address of uint32 parameter
func UInt32Ptr(i uint32) *uint32 {
return &i
}
// Int64Ptr returns address of int64 parameter
func Int64Ptr(i int64) *int64 {
return &i
}
// UInt64Ptr returns address of uint64 parameter
func UInt64Ptr(i uint64) *uint64 {
return &i
}
// StringPtr returns address of string parameter
func StringPtr(s string) *string {
return &s
}
// TimePtr returns address of time.Time parameter
func TimePtr(t time.Time) *time.Time {
return &t
}
// ByteArrayPtr returns address of []byte parameter
func ByteArrayPtr(arr []byte) *[]byte {
return &arr
}
// Float32Ptr returns address of float32 parameter
func Float32Ptr(f float32) *float32 {
return &f
}
// Float64Ptr returns address of float64 parameter
func Float64Ptr(f float64) *float64 {
return &f
}
// UUIDPtr returns address of uuid.UUID
func UUIDPtr(u string) *uuid.UUID {
newUUID := uuid.MustParse(u)
return &newUUID
}