|
| 1 | +// Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package bson |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "go.mongodb.org/mongo-driver/internal/testutil/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestExtJSON(t *testing.T) { |
| 17 | + timestampNegativeInt32Err := fmt.Errorf("$timestamp i number should be uint32: -1") |
| 18 | + timestampNegativeInt64Err := fmt.Errorf("$timestamp i number should be uint32: -2147483649") |
| 19 | + timestampLargeValueErr := fmt.Errorf("$timestamp i number should be uint32: 4294967296") |
| 20 | + |
| 21 | + testCases := []struct { |
| 22 | + name string |
| 23 | + input string |
| 24 | + canonical bool |
| 25 | + err error |
| 26 | + }{ |
| 27 | + {"timestamp - negative int32 value", `{"":{"$timestamp":{"t":0,"i":-1}}}`, false, timestampNegativeInt32Err}, |
| 28 | + {"timestamp - negative int64 value", `{"":{"$timestamp":{"t":0,"i":-2147483649}}}`, false, timestampNegativeInt64Err}, |
| 29 | + {"timestamp - value overflows uint32", `{"":{"$timestamp":{"t":0,"i":4294967296}}}`, false, timestampLargeValueErr}, |
| 30 | + } |
| 31 | + for _, tc := range testCases { |
| 32 | + t.Run(tc.name, func(t *testing.T) { |
| 33 | + var res Raw |
| 34 | + err := UnmarshalExtJSON([]byte(tc.input), tc.canonical, &res) |
| 35 | + if tc.err == nil { |
| 36 | + assert.Nil(t, err, "UnmarshalExtJSON error: %v", err) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + assert.NotNil(t, err, "expected error %v, got nil", tc.err) |
| 41 | + assert.Equal(t, tc.err.Error(), err.Error(), "expected error %v, got %v", tc.err, err) |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
0 commit comments