|
7 | 7 | package primitive
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "encoding/json" |
10 | 11 | "testing"
|
11 | 12 |
|
12 | 13 | "github.com/stretchr/testify/require"
|
| 14 | + "go.mongodb.org/mongo-driver/internal/testutil/assert" |
13 | 15 | )
|
14 | 16 |
|
15 | 17 | // The same interface as bsoncodec.Zeroer implemented for tests.
|
@@ -78,3 +80,26 @@ func TestRegexCompare(t *testing.T) {
|
78 | 80 | })
|
79 | 81 | }
|
80 | 82 | }
|
| 83 | + |
| 84 | +func TestDateTime(t *testing.T) { |
| 85 | + t.Run("json", func(t *testing.T) { |
| 86 | + t.Run("round trip", func(t *testing.T) { |
| 87 | + original := DateTime(1000) |
| 88 | + jsonBytes, err := json.Marshal(original) |
| 89 | + assert.Nil(t, err, "Marshal error: %v", err) |
| 90 | + |
| 91 | + var unmarshalled DateTime |
| 92 | + err = json.Unmarshal(jsonBytes, &unmarshalled) |
| 93 | + assert.Nil(t, err, "Unmarshal error: %v", err) |
| 94 | + |
| 95 | + assert.Equal(t, original, unmarshalled, "expected DateTime %v, got %v", original, unmarshalled) |
| 96 | + }) |
| 97 | + t.Run("decode null", func(t *testing.T) { |
| 98 | + jsonBytes := []byte("null") |
| 99 | + var dt DateTime |
| 100 | + err := json.Unmarshal(jsonBytes, &dt) |
| 101 | + assert.Nil(t, err, "Unmarshal error: %v", err) |
| 102 | + assert.Equal(t, DateTime(0), dt, "expected DateTime value to be 0, got %v", dt) |
| 103 | + }) |
| 104 | + }) |
| 105 | +} |
0 commit comments