JSON tests

This commit is contained in:
2018-11-08 23:29:09 +01:00
parent ff91136fab
commit 8a22495a0c
6 changed files with 528 additions and 12 deletions
+8
View File
@@ -6,6 +6,14 @@ import (
"github.com/localhots/bocadillo/mysql"
)
func TestJSON(t *testing.T) {
tbl := suite.createTable(mysql.ColumnTypeJSON, "", attrNone)
defer tbl.drop(t)
exp := []byte(`{"hello": "world", "foo": [1, 2, 3.75]}`)
suite.insertAndCompare(t, tbl, exp)
}
func TestSet(t *testing.T) {
tbl := suite.createTable(mysql.ColumnTypeSet, "'a', 'b', 'c'", attrNone)
defer tbl.drop(t)
+25 -8
View File
@@ -3,6 +3,7 @@ package tests
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"log"
"reflect"
@@ -10,6 +11,7 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/localhots/bocadillo/mysql"
"github.com/localhots/bocadillo/reader"
)
@@ -155,6 +157,13 @@ func colTypeSyntax(ct mysql.ColumnType) (typName, attrs string) {
case mysql.ColumnTypeSet:
return "SET", ""
case mysql.ColumnTypeJSON:
return "JSON", ""
case mysql.ColumnTypeGeometry:
return "GEOMETRY", ""
case mysql.ColumnTypeBit:
return "BIT", ""
default:
panic(fmt.Errorf("Syntax not defined for %s", ct.String()))
}
@@ -223,8 +232,22 @@ func (s *testSuite) compare(t *testing.T, tbl *table, exp, res interface{}) {
// fmt.Printf("VALUE RECEIVED: %T(%+v), EXPECTED: %T(%+v)\n", res, res, exp, exp)
switch texp := exp.(type) {
case []byte:
if !bytes.Equal(texp, res.([]byte)) {
t.Errorf("Expected %T(%+v), got %T(%+v)", exp, exp, res, res)
switch tbl.colTyp {
case mysql.ColumnTypeJSON:
var jExp, jRes interface{}
if err := json.Unmarshal(texp, &jExp); err != nil {
panic(err)
}
if err := json.Unmarshal(res.([]byte), &jRes); err != nil {
panic(err)
}
if !cmp.Equal(jExp, jRes) {
t.Errorf("JSON values are different: %s", cmp.Diff(jExp, jRes))
}
default:
if !bytes.Equal(texp, res.([]byte)) {
t.Errorf("Expected %T(%+v), got %T(%+v)", exp, exp, res, res)
}
}
default:
if exp != res {
@@ -233,12 +256,6 @@ func (s *testSuite) compare(t *testing.T, tbl *table, exp, res interface{}) {
}
}
func (s *testSuite) insertAndCompare(t *testing.T, tbl *table, val interface{}) {
t.Helper()
tbl.insert(t, val)
suite.expectValue(t, tbl, val)
}
func signNumber(val interface{}, ct mysql.ColumnType) interface{} {
switch tval := val.(type) {
case uint8: