Skip to content

Commit

Permalink
GODRIVER-2570 update bsoncore value string to preserve timestamp type (
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored and matthewdale committed Feb 11, 2023
1 parent 667bfcf commit 4d68f59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/bsonx/bsoncore/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions x/bsonx/bsoncore/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -35,7 +43,10 @@ func TestValue(t *testing.T) {
}
})
})

t.Run("IsNumber", func(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
val Value
Expand All @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 4d68f59

Please sign in to comment.