This commit is contained in:
2023-06-07 16:58:17 +02:00
parent c7df9d2264
commit 45d4fd7101
12 changed files with 365 additions and 255 deletions

View File

@@ -3,6 +3,8 @@ package wmo
import (
"go.mongodb.org/mongo-driver/mongo"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"gogs.mikescher.com/BlackForestBytes/goext/tst"
"testing"
"time"
@@ -18,8 +20,9 @@ func TestReflectionGetFieldType(t *testing.T) {
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
MDate rfctime.RFC3339NanoTime `bson:"mdate"`
}
coll := W[TestData](&mongo.Collection{})
@@ -27,6 +30,7 @@ func TestReflectionGetFieldType(t *testing.T) {
coll.init()
t0 := time.Now()
t1 := rfctime.NewRFC3339Nano(t0)
d := TestData{
ID: "1",
@@ -36,8 +40,9 @@ func TestReflectionGetFieldType(t *testing.T) {
}{
A: "2",
},
Str: "3",
Ptr: langext.Ptr(4),
Str: "3",
Ptr: langext.Ptr(4),
MDate: t1,
}
tst.AssertEqual(t, coll.getFieldType("_id").Kind.String(), "string")
@@ -81,9 +86,10 @@ func TestReflectionGetTokenValueAsMongoType(t *testing.T) {
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
MDate rfctime.RFC3339NanoTime `bson:"mdate"`
}
coll := W[TestData](&mongo.Collection{})
@@ -94,15 +100,23 @@ func TestReflectionGetTokenValueAsMongoType(t *testing.T) {
v, err := coll.getTokenValueAsMongoType(value, fieldName)
if err != nil {
t.Errorf("%s", "failed to getTokenValueAsMongoType")
t.Errorf("%v+", err)
}
return v
}
tx, err := time.Parse(time.RFC3339Nano, "2009-11-10T23:00:00Z")
if err != nil {
t.Errorf("%v", err)
}
tst.AssertEqual(t, gtvasmt("hello", "str").(string), "hello")
tst.AssertEqual(t, gtvasmt("4", "num").(int), 4)
tst.AssertEqual(t, gtvasmt("asdf", "_id").(IDType), "asdf")
tst.AssertEqual(t, gtvasmt("", "ptr").(*int), nil)
tst.AssertEqual(t, *(gtvasmt("123", "ptr").(*int)), 123)
tst.AssertEqual(t, gtvasmt("2009-11-10T23:00:00Z", "cdate").(time.Time), tx)
tst.AssertEqual(t, gtvasmt("2009-11-10T23:00:00Z", "mdate").(rfctime.RFC3339NanoTime), rfctime.NewRFC3339Nano(tx))
}
func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
@@ -115,22 +129,25 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
Sub struct {
A string `bson:"a"`
} `bson:"sub"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
Ptr2 *int `bson:"ptr2"`
FFF float64 `bson:"fff"`
Str string `bson:"str"`
Ptr *int `bson:"ptr"`
Num int `bson:"num"`
Ptr2 *int `bson:"ptr2"`
FFF float64 `bson:"fff"`
MDate rfctime.RFC3339NanoTime `bson:"mdate"`
}
coll := W[TestData](&mongo.Collection{})
coll.init()
t0 := time.Now()
t0 := time.Date(2000, 1, 1, 12, 0, 0, 0, timeext.TimezoneBerlin)
t1 := rfctime.NewRFC3339Nano(t0)
d := TestData{
ID: "1",
CDate: t0,
MDate: t1,
Sub: struct {
A string `bson:"a"`
}{
@@ -146,7 +163,7 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
gfvats := func(value TestData, fieldName string) string {
v, err := coll.getFieldValueAsTokenString(value, fieldName)
if err != nil {
t.Errorf("%s", "failed to getTokenValueAsMongoType")
t.Errorf("%s: %v", "failed to getTokenValueAsMongoType", err)
}
return v
}
@@ -157,4 +174,6 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
tst.AssertEqual(t, gfvats(d, "ptr"), "4")
tst.AssertEqual(t, gfvats(d, "ptr2"), "")
tst.AssertEqual(t, gfvats(d, "fff"), "22.5")
tst.AssertEqual(t, gfvats(d, "cdate"), t0.Format(time.RFC3339Nano))
tst.AssertEqual(t, gfvats(d, "mdate"), t0.Format(time.RFC3339Nano))
}