Browse Source

完成sql.Result测试

yjp 9 months ago
parent
commit
7e89ec421f

+ 3 - 2
framework/core/infrastructure/database/sql/result.go

@@ -33,11 +33,12 @@ func (result Result) ColumnValueStringAsTime(columnName string) time.Time {
 
 	layout := resultTimeSecFormat
 
+	parseValue := value
 	timeZoneIndex := strings.LastIndex(value, "+08:")
 	if timeZoneIndex != -1 {
-		value = value[:timeZoneIndex]
+		parseValue = parseValue[:timeZoneIndex]
 
-		afterSecondIndex := strings.LastIndex(value, ".")
+		afterSecondIndex := strings.LastIndex(parseValue, ".")
 		if afterSecondIndex != -1 {
 			if len(value)-afterSecondIndex-1 == 6 {
 				layout = resultTimeMicroFormat

+ 34 - 0
test/assign_tag_test.go

@@ -13,10 +13,12 @@ import (
 type AssignTagFrom struct {
 	StringField                  string              `assign:"toField:StringField"`
 	BoolField                    bool                `assign:"toField:BoolField"`
+	IntField                     int                 `assign:"toField:IntField"`
 	Int8Field                    int8                `assign:"toField:Int8Field"`
 	Int16Field                   int16               `assign:"toField:Int16Field"`
 	Int32Field                   int32               `assign:"toField:Int32Field"`
 	Int64Field                   int64               `assign:"toField:Int64Field"`
+	UintField                    uint                `assign:"toField:UintField"`
 	Uint8Field                   uint8               `assign:"toField:Uint8Field"`
 	Uint16Field                  uint16              `assign:"toField:Uint16Field"`
 	Uint32Field                  uint32              `assign:"toField:Uint32Field"`
@@ -37,10 +39,12 @@ type AssignTagFrom struct {
 type AssignTagFromPointerField struct {
 	StringField                  *string              `assign:"toField:StringField"`
 	BoolField                    *bool                `assign:"toField:BoolField"`
+	IntField                     *int                 `assign:"toField:IntField"`
 	Int8Field                    *int8                `assign:"toField:Int8Field"`
 	Int16Field                   *int16               `assign:"toField:Int16Field"`
 	Int32Field                   *int32               `assign:"toField:Int32Field"`
 	Int64Field                   *int64               `assign:"toField:Int64Field"`
+	UintField                    *uint                `assign:"toField:UintField"`
 	Uint8Field                   *uint8               `assign:"toField:Uint8Field"`
 	Uint16Field                  *uint16              `assign:"toField:Uint16Field"`
 	Uint32Field                  *uint32              `assign:"toField:Uint32Field"`
@@ -61,10 +65,12 @@ type AssignTagFromPointerField struct {
 type AssignTagTo struct {
 	StringField                  string
 	BoolField                    bool
+	IntField                     int
 	Int8Field                    int8
 	Int16Field                   int16
 	Int32Field                   int32
 	Int64Field                   int64
+	UintField                    uint
 	Uint8Field                   uint8
 	Uint16Field                  uint16
 	Uint32Field                  uint32
@@ -93,6 +99,11 @@ func (to AssignTagTo) checkFields(t *testing.T, from AssignTagFrom) {
 			from.BoolField, to.BoolField))
 	}
 
+	if from.IntField != to.IntField {
+		t.Fatalf("%+v\n", errors.Errorf("IntField not equal: from %v, to %v",
+			from.IntField, to.IntField))
+	}
+
 	if from.Int8Field != to.Int8Field {
 		t.Fatalf("%+v\n", errors.Errorf("Int8Field not equal: from %v, to %v",
 			from.Int8Field, to.Int8Field))
@@ -113,6 +124,11 @@ func (to AssignTagTo) checkFields(t *testing.T, from AssignTagFrom) {
 			from.Int64Field, to.Int64Field))
 	}
 
+	if from.UintField != to.UintField {
+		t.Fatalf("%+v\n", errors.Errorf("UintField not equal: from %v, to %v",
+			from.UintField, to.UintField))
+	}
+
 	if from.Uint8Field != to.Uint8Field {
 		t.Fatalf("%+v\n", errors.Errorf("Uint8Field not equal: from %v, to %v",
 			from.Uint8Field, to.Uint8Field))
@@ -196,10 +212,12 @@ func (to AssignTagTo) checkFields(t *testing.T, from AssignTagFrom) {
 type AssignTagToPointerField struct {
 	StringField                  *string
 	BoolField                    *bool
+	IntField                     *int
 	Int8Field                    *int8
 	Int16Field                   *int16
 	Int32Field                   *int32
 	Int64Field                   *int64
+	UintField                    *uint
 	Uint8Field                   *uint8
 	Uint16Field                  *uint16
 	Uint32Field                  *uint32
@@ -228,6 +246,11 @@ func (to AssignTagToPointerField) checkFields(t *testing.T, from AssignTagFrom)
 			from.BoolField, *to.BoolField))
 	}
 
+	if from.IntField != *to.IntField {
+		t.Fatalf("%+v\n", errors.Errorf("IntField not equal: from %v, to %v",
+			from.IntField, *to.IntField))
+	}
+
 	if from.Int8Field != *to.Int8Field {
 		t.Fatalf("%+v\n", errors.Errorf("Int8Field not equal: from %v, to %v",
 			from.Int8Field, *to.Int8Field))
@@ -248,6 +271,11 @@ func (to AssignTagToPointerField) checkFields(t *testing.T, from AssignTagFrom)
 			from.Int64Field, *to.Int64Field))
 	}
 
+	if from.UintField != *to.UintField {
+		t.Fatalf("%+v\n", errors.Errorf("UintField not equal: from %v, to %v",
+			from.UintField, *to.UintField))
+	}
+
 	if from.Uint8Field != *to.Uint8Field {
 		t.Fatalf("%+v\n", errors.Errorf("Uint8Field not equal: from %v, to %v",
 			from.Uint8Field, *to.Uint8Field))
@@ -331,10 +359,12 @@ func (to AssignTagToPointerField) checkFields(t *testing.T, from AssignTagFrom)
 func TestAssignTagDefaultUsage(t *testing.T) {
 	stringField := strutils.SimpleUUID()
 	boolField := rand.Intn(2) == 0
+	intField := rand.Int()
 	int8Field := int8(rand.Int())
 	int16Field := int16(rand.Int())
 	int32Field := int32(rand.Int())
 	int64Field := int64(rand.Int())
+	uintField := uint(rand.Int())
 	uint8Field := uint8(rand.Int())
 	uint16Field := uint16(rand.Int())
 	uint32Field := uint32(rand.Int())
@@ -355,10 +385,12 @@ func TestAssignTagDefaultUsage(t *testing.T) {
 	from := AssignTagFrom{
 		StringField:                  stringField,
 		BoolField:                    boolField,
+		IntField:                     intField,
 		Int8Field:                    int8Field,
 		Int16Field:                   int16Field,
 		Int32Field:                   int32Field,
 		Int64Field:                   int64Field,
+		UintField:                    uintField,
 		Uint8Field:                   uint8Field,
 		Uint16Field:                  uint16Field,
 		Uint32Field:                  uint32Field,
@@ -379,10 +411,12 @@ func TestAssignTagDefaultUsage(t *testing.T) {
 	fromPointer := AssignTagFromPointerField{
 		StringField:                  &stringField,
 		BoolField:                    &boolField,
+		IntField:                     &intField,
 		Int8Field:                    &int8Field,
 		Int16Field:                   &int16Field,
 		Int32Field:                   &int32Field,
 		Int64Field:                   &int64Field,
+		UintField:                    &uintField,
 		Uint8Field:                   &uint8Field,
 		Uint16Field:                  &uint16Field,
 		Uint32Field:                  &uint32Field,

+ 144 - 0
test/database_sql_test.go

@@ -2,8 +2,11 @@ package test
 
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
+	"git.sxidc.com/go-tools/utils/strutils"
 	"github.com/pkg/errors"
+	"math/rand"
 	"testing"
+	"time"
 )
 
 func TestDatabaseSqlConditions(t *testing.T) {
@@ -83,3 +86,144 @@ func TestDatabaseSqlConditions(t *testing.T) {
 			exceptAnd, conditions.And()))
 	}
 }
+
+func TestDatabaseSqlTableRow(t *testing.T) {
+	tableRow := sql.NewTableRow().
+		Add("name", "test").
+		Add("age", 20)
+
+	exceptColumns := []string{
+		`"name"`,
+		`"age"`,
+	}
+
+	exceptValues := []any{
+		"test",
+		20,
+	}
+
+	for i, column := range tableRow.Columns() {
+		if exceptColumns[i] != column {
+			t.Fatalf("%+v\n", errors.Errorf("Column Error: except %v, actural %v",
+				exceptColumns[i], column))
+		}
+	}
+
+	for i, value := range tableRow.Values() {
+		if exceptValues[i] != value {
+			t.Fatalf("%+v\n", errors.Errorf("Value Error: except %v, actural %v",
+				exceptValues[i], value))
+		}
+	}
+}
+
+func TestDatabaseSqlResult(t *testing.T) {
+	timeResult := time.Now().Local().Format("2006-01-02T15:04:05") + "+08:00"
+	stringResult := strutils.SimpleUUID()
+	boolResult := rand.Intn(2) == 0
+	intResult := rand.Int()
+	int8Result := int8(rand.Int())
+	int16Result := int16(rand.Int())
+	int32Result := int32(rand.Int())
+	int64Result := int64(rand.Int())
+	uintResult := uint(rand.Int())
+	uint8Result := uint8(rand.Int())
+	uint16Result := uint16(rand.Int())
+	uint32Result := uint32(rand.Int())
+	uint64Result := uint64(rand.Int())
+	float32Result := rand.Float32()
+	float64Result := rand.Float64()
+
+	result := sql.Result{
+		"time":    timeResult,
+		"string":  stringResult,
+		"bool":    boolResult,
+		"int":     intResult,
+		"int8":    int8Result,
+		"int16":   int16Result,
+		"int32":   int32Result,
+		"int64":   int64Result,
+		"uint":    uintResult,
+		"uint8":   uint8Result,
+		"uint16":  uint16Result,
+		"uint32":  uint32Result,
+		"uint64":  uint64Result,
+		"float32": float32Result,
+		"float64": float64Result,
+	}
+
+	if result.ColumnValueStringAsTime("time").Format("2006-01-02T15:04:05")+"+08:00" != timeResult {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			timeResult, result.ColumnValueStringAsTime("time")))
+	}
+
+	if result.ColumnValueString("string") != stringResult {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			stringResult, result.ColumnValueString("string")))
+	}
+
+	if result.ColumnValueBool("bool") != boolResult {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			boolResult, result.ColumnValueBool("bool")))
+	}
+
+	if result.ColumnValueInt("int") != intResult {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			intResult, result.ColumnValueInt("int")))
+	}
+
+	if result.ColumnValueInt8("int8") != int8Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			int8Result, result.ColumnValueInt8("int8")))
+	}
+
+	if result.ColumnValueInt16("int16") != int16Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			int16Result, result.ColumnValueInt16("int16")))
+	}
+
+	if result.ColumnValueInt32("int32") != int32Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			int32Result, result.ColumnValueInt32("int32")))
+	}
+
+	if result.ColumnValueInt64("int64") != int64Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			int64Result, result.ColumnValueInt64("int64")))
+	}
+
+	if result.ColumnValueUint("uint") != uintResult {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			uintResult, result.ColumnValueUint("uint")))
+	}
+
+	if result.ColumnValueUint8("uint8") != uint8Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			uint8Result, result.ColumnValueUint8("uint8")))
+	}
+
+	if result.ColumnValueUint16("uint16") != uint16Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			uint16Result, result.ColumnValueUint16("uint16")))
+	}
+
+	if result.ColumnValueUint32("uint32") != uint32Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			uint32Result, result.ColumnValueUint32("uint32")))
+	}
+
+	if result.ColumnValueUint64("uint64") != uint64Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			uint64Result, result.ColumnValueUint64("uint64")))
+	}
+
+	if result.ColumnValueFloat32("float32") != float32Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			float32Result, result.ColumnValueFloat32("float32")))
+	}
+
+	if result.ColumnValueFloat64("float64") != float64Result {
+		t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
+			float64Result, result.ColumnValueFloat64("float64")))
+	}
+}