2019-07-29 18:08:53 +02:00
|
|
|
package testutils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/go-jet/jet"
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func JsonPrint(v interface{}) {
|
|
|
|
|
jsonText, _ := json.MarshalIndent(v, "", "\t")
|
|
|
|
|
fmt.Println(string(jsonText))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
func JsonSave(v interface{}, path string) {
|
2019-07-29 18:08:53 +02:00
|
|
|
jsonText, _ := json.MarshalIndent(v, "", "\t")
|
|
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(path, jsonText, 0644)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
func AssertJSON(t *testing.T, expectedJSON string, data interface{}) {
|
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-01 16:56:54 +02:00
|
|
|
func AssertJSONFile(t *testing.T, data interface{}, jsonFilePath string) {
|
2019-07-29 18:08:53 +02:00
|
|
|
fileJSONData, err := ioutil.ReadFile(jsonFilePath)
|
|
|
|
|
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-07-31 13:02:30 +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{}) {
|
2019-07-30 11:18:12 +02:00
|
|
|
queryStr, args, err := query.Sql()
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AssertDebugStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
|
2019-07-29 18:08:53 +02:00
|
|
|
_, args, err := query.Sql()
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
//assert.Equal(t, queryStr, expectedQuery)
|
|
|
|
|
assert.DeepEqual(t, args, expectedArgs)
|
|
|
|
|
|
|
|
|
|
debuqSql, err := query.DebugSql()
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, debuqSql, expectedQuery)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
func Date(t string) *time.Time {
|
|
|
|
|
newTime, err := time.Parse("2006-01-02", t)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newTime
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 18:08:53 +02:00
|
|
|
func TimestampWithoutTimeZone(t string, precision int) *time.Time {
|
|
|
|
|
|
|
|
|
|
precisionStr := ""
|
|
|
|
|
|
|
|
|
|
if precision > 0 {
|
|
|
|
|
precisionStr = "." + strings.Repeat("9", precision)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" +0000", t+" +0000")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newTime
|
|
|
|
|
}
|
2019-07-31 18:43:54 +02:00
|
|
|
|
|
|
|
|
func TimeWithoutTimeZone(t string) *time.Time {
|
|
|
|
|
newTime, err := time.Parse("15:04:05", t)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TimeWithTimeZone(t string) *time.Time {
|
|
|
|
|
newTimez, err := time.Parse("15:04:05 -0700", t)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newTimez
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TimestampWithTimeZone(t string, precision int) *time.Time {
|
|
|
|
|
|
|
|
|
|
precisionStr := ""
|
|
|
|
|
|
|
|
|
|
if precision > 0 {
|
|
|
|
|
precisionStr = "." + strings.Repeat("9", precision)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" -0700 MST", t)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newTime
|
|
|
|
|
}
|