From 4e038d8b969b3632a9b00c7f48b5e8bdc1d586ae Mon Sep 17 00:00:00 2001 From: Preston Vasquez <24281431+prestonvasquez@users.noreply.github.com> Date: Tue, 20 Dec 2022 09:38:34 -0700 Subject: [PATCH] GODRIVER-2570 update bsoncore value string to preserve timestamp type (#1150) --- x/bsonx/bsoncore/value.go | 2 +- x/bsonx/bsoncore/value_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/x/bsonx/bsoncore/value.go b/x/bsonx/bsoncore/value.go index 54aa617cfd..789d2b9828 100644 --- a/x/bsonx/bsoncore/value.go +++ b/x/bsonx/bsoncore/value.go @@ -323,7 +323,7 @@ func (v Value) String() string { if !ok { return "" } - return fmt.Sprintf(`{"$timestamp":{"t":"%s","i":"%s"}}`, strconv.FormatUint(uint64(t), 10), strconv.FormatUint(uint64(i), 10)) + return fmt.Sprintf(`{"$timestamp":{"t":%v,"i":%v}}`, t, i) case bsontype.Int64: i64, ok := v.Int64OK() if !ok { diff --git a/x/bsonx/bsoncore/value_test.go b/x/bsonx/bsoncore/value_test.go index 1a25f120ed..a9fdfb6663 100644 --- a/x/bsonx/bsoncore/value_test.go +++ b/x/bsonx/bsoncore/value_test.go @@ -17,8 +17,14 @@ import ( ) func TestValue(t *testing.T) { + t.Parallel() + t.Run("Validate", func(t *testing.T) { + t.Parallel() + t.Run("invalid", func(t *testing.T) { + t.Parallel() + v := Value{Type: bsontype.Double, Data: []byte{0x01, 0x02, 0x03, 0x04}} want := NewInsufficientBytesError(v.Data, v.Data) got := v.Validate() @@ -27,6 +33,8 @@ func TestValue(t *testing.T) { } }) t.Run("value", func(t *testing.T) { + t.Parallel() + v := Value{Type: bsontype.Double, Data: AppendDouble(nil, 3.14159)} var want error got := v.Validate() @@ -35,7 +43,10 @@ func TestValue(t *testing.T) { } }) }) + t.Run("IsNumber", func(t *testing.T) { + t.Parallel() + testCases := []struct { name string val Value @@ -50,7 +61,11 @@ func TestValue(t *testing.T) { } for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + isnum := tc.val.IsNumber() if isnum != tc.isnum { t.Errorf("IsNumber did not return the expected boolean. got %t; want %t", isnum, tc.isnum) @@ -619,10 +634,19 @@ func TestValue(t *testing.T) { nil, []interface{}{primitive.NewDecimal128(12345, 67890), true}, }, + { + "Timestamp.String/Success", Value.String, Value{Type: bsontype.Timestamp, Data: AppendTimestamp(nil, 12345, 67890)}, + nil, + []interface{}{"{\"$timestamp\":{\"t\":12345,\"i\":67890}}"}, + }, } for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + defer func() { err := recover() if !cmp.Equal(err, tc.panicErr, cmp.Comparer(compareErrors)) {