jet/tests/mysql/sample_test.go

225 lines
5.9 KiB
Go
Raw Normal View History

2019-07-29 18:08:53 +02:00
package mysql
import (
"github.com/go-jet/jet/internal/testutils"
"github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/model"
. "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table"
2019-07-30 11:18:12 +02:00
. "github.com/go-jet/jet/mysql"
2019-07-29 18:08:53 +02:00
"gotest.tools/assert"
"testing"
)
func TestAllTypes(t *testing.T) {
dest := []model.AllTypes{}
err := AllTypes.
SELECT(AllTypes.AllColumns).
LIMIT(2).
Query(db, &dest)
assert.NilError(t, err)
//testutils.JsonPrint(dest)
testutils.AssertJSON(t, dest, allTypesJson)
}
2019-07-30 11:18:12 +02:00
func TestBoolOperators(t *testing.T) {
query := AllTypes.SELECT(
AllTypes.Boolean.EQ(AllTypes.BooleanPtr).AS("EQ1"),
AllTypes.Boolean.EQ(Bool(true)).AS("EQ2"),
AllTypes.Boolean.NOT_EQ(AllTypes.BooleanPtr).AS("NEq1"),
AllTypes.Boolean.NOT_EQ(Bool(false)).AS("NEq2"),
AllTypes.Boolean.IS_DISTINCT_FROM(AllTypes.BooleanPtr).AS("distinct1"),
AllTypes.Boolean.IS_DISTINCT_FROM(Bool(true)).AS("distinct2"),
AllTypes.Boolean.IS_NOT_DISTINCT_FROM(AllTypes.BooleanPtr).AS("not_distinct_1"),
AllTypes.Boolean.IS_NOT_DISTINCT_FROM(Bool(true)).AS("NOTDISTINCT2"),
AllTypes.Boolean.IS_TRUE().AS("ISTRUE"),
AllTypes.Boolean.IS_NOT_TRUE().AS("isnottrue"),
AllTypes.Boolean.IS_FALSE().AS("is_False"),
AllTypes.Boolean.IS_NOT_FALSE().AS("is not false"),
AllTypes.Boolean.IS_UNKNOWN().AS("is unknown"),
AllTypes.Boolean.IS_NOT_UNKNOWN().AS("is_not_unknown"),
AllTypes.Boolean.AND(AllTypes.Boolean).EQ(AllTypes.Boolean.AND(AllTypes.Boolean)).AS("complex1"),
AllTypes.Boolean.OR(AllTypes.Boolean).EQ(AllTypes.Boolean.AND(AllTypes.Boolean)).AS("complex2"),
)
//fmt.Println(query.Sql())
testutils.AssertStatementSql(t, query, `
SELECT (all_types.boolean = all_types.boolean_ptr) AS "EQ1",
(all_types.boolean = ?) AS "EQ2",
(all_types.boolean != all_types.boolean_ptr) AS "NEq1",
(all_types.boolean != ?) AS "NEq2",
(NOT all_types.boolean <=> all_types.boolean_ptr) AS "distinct1",
(NOT all_types.boolean <=> ?) AS "distinct2",
(all_types.boolean <=> all_types.boolean_ptr) AS "not_distinct_1",
(all_types.boolean <=> ?) AS "NOTDISTINCT2",
all_types.boolean IS TRUE AS "ISTRUE",
all_types.boolean IS NOT TRUE AS "isnottrue",
all_types.boolean IS FALSE AS "is_False",
all_types.boolean IS NOT FALSE AS "is not false",
all_types.boolean IS UNKNOWN AS "is unknown",
all_types.boolean IS NOT UNKNOWN AS "is_not_unknown",
((all_types.boolean AND all_types.boolean) = (all_types.boolean AND all_types.boolean)) AS "complex1",
((all_types.boolean OR all_types.boolean) = (all_types.boolean AND all_types.boolean)) AS "complex2"
FROM test_sample.all_types;
`, true, false, true, true)
var dest []struct {
Eq1 *bool
Eq2 *bool
NEq1 *bool
NEq2 *bool
Distinct1 *bool
Distinct2 *bool
NotDistinct1 *bool
NotDistinct2 *bool
IsTrue *bool
IsNotTrue *bool
IsFalse *bool
IsNotFalse *bool
IsUnknown *bool
IsNotUnknown *bool
Complex1 *bool
Complex2 *bool
}
err := query.Query(db, &dest)
assert.NilError(t, err)
testutils.JsonPrint(dest)
2019-07-30 11:45:10 +02:00
testutils.AssertJSONFile(t, "./testdata/common/bool_operators.json", dest)
2019-07-30 11:18:12 +02:00
}
2019-07-29 18:08:53 +02:00
var allTypesJson = `
[
{
2019-07-30 11:18:12 +02:00
"Boolean": false,
"BooleanPtr": true,
2019-07-29 18:08:53 +02:00
"TinyInt": -3,
"UtinyInt": 3,
"SmallInt": -14,
"UsmallInt": 14,
"MediumInt": -150,
"UmediumInt": 150,
"Int": -1600,
"Uint": 1600,
"BigInt": -17000,
"UbigInt": 17000,
"TinyIntPtr": -3,
"UtinyIntPtr": 3,
"SmallIntPtr": -14,
"UsmallIntPtr": 14,
"MediumIntPtr": -150,
"UmediumIntPtr": 150,
"IntPtr": -1600,
"UintPtr": 1600,
"BigIntPtr": -17000,
"UbigIntPtr": 17000,
"Decimal": 1.11,
"DecimalPtr": 1.11,
"Numeric": 2.22,
"NumericPtr": 2.22,
"Float": 3.33,
"FloatPtr": 3.33,
"Double": 4.44,
"DoublePtr": 4.44,
"Bit": "\u0000\u0003",
"BitPtr": "\u0000\u0003",
"Date": "2008-07-04T00:00:00Z",
"DatePtr": "2008-07-04T00:00:00Z",
"DateTime": "2011-12-18T13:17:17Z",
"DateTimePtr": "2011-12-18T13:17:17Z",
"Timestamp": "2007-12-31T23:00:01Z",
"TimestampPtr": "2007-12-31T23:00:01Z",
"Year": 2004,
"YearPtr": 2004,
"Char": "char",
"CharPtr": "char",
"Varchar": "varchar",
"VarcharPtr": "varchar",
"Binary": "YmluYXJ5AAAAAAAAAAAAAAAAAAA=",
"BinaryPtr": "YmluYXJ5AAAAAAAAAAAAAAAAAAA=",
"VarBinary": "dmFyYmluYXJ5",
"VarBinaryPtr": "dmFyYmluYXJ5",
"Blob": "YmxvYg==",
"BlobPtr": "YmxvYg==",
"Text": "text",
"TextPtr": "text",
"Enum": "value1",
"EnumPtr": "value1",
"Set": "s1",
"SetPtr": "s2",
"JSON": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
"JSONPtr": "{\"key1\": \"value1\", \"key2\": \"value2\"}"
},
{
2019-07-30 11:18:12 +02:00
"Boolean": false,
"BooleanPtr": null,
2019-07-29 18:08:53 +02:00
"TinyInt": -3,
"UtinyInt": 3,
"SmallInt": -14,
"UsmallInt": 14,
"MediumInt": -150,
"UmediumInt": 150,
"Int": -1600,
"Uint": 1600,
"BigInt": -17000,
"UbigInt": 17000,
"TinyIntPtr": null,
"UtinyIntPtr": null,
"SmallIntPtr": null,
"UsmallIntPtr": null,
"MediumIntPtr": null,
"UmediumIntPtr": null,
"IntPtr": null,
"UintPtr": null,
"BigIntPtr": null,
"UbigIntPtr": null,
"Decimal": 1.11,
"DecimalPtr": null,
"Numeric": 2.22,
"NumericPtr": null,
"Float": 3.33,
"FloatPtr": null,
"Double": 4.44,
"DoublePtr": null,
"Bit": "\u0000\u0003",
"BitPtr": null,
"Date": "2008-07-04T00:00:00Z",
"DatePtr": null,
"DateTime": "2011-12-18T13:17:17Z",
"DateTimePtr": null,
"Timestamp": "2007-12-31T23:00:01Z",
"TimestampPtr": null,
"Year": 2004,
"YearPtr": null,
"Char": "char",
"CharPtr": null,
"Varchar": "varchar",
"VarcharPtr": null,
"Binary": "YmluYXJ5AAAAAAAAAAAAAAAAAAA=",
"BinaryPtr": null,
"VarBinary": "dmFyYmluYXJ5",
"VarBinaryPtr": null,
"Blob": "YmxvYg==",
"BlobPtr": null,
"Text": "text",
"TextPtr": null,
"Enum": "value1",
"EnumPtr": null,
"Set": "s1",
"SetPtr": null,
"JSON": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
"JSONPtr": null
}
]
`