123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // Copyright (C) MongoDB, Inc. 2017-present.
- //
- // Licensed under the Apache License, Version 2.0 (the "License"); you may
- // not use this file except in compliance with the License. You may obtain
- // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- package bson
- import (
- "bytes"
- "testing"
- "time"
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/require"
- "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
- )
- func noerr(t *testing.T, err error) {
- if err != nil {
- t.Helper()
- t.Errorf("Unexpected error: (%T)%v", err, err)
- t.FailNow()
- }
- }
- func requireErrEqual(t *testing.T, err1 error, err2 error) { require.True(t, compareErrors(err1, err2)) }
- func TestTimeRoundTrip(t *testing.T) {
- val := struct {
- Value time.Time
- ID string
- }{
- ID: "time-rt-test",
- }
- if !val.Value.IsZero() {
- t.Errorf("Did not get zero time as expected.")
- }
- bsonOut, err := Marshal(val)
- noerr(t, err)
- rtval := struct {
- Value time.Time
- ID string
- }{}
- err = Unmarshal(bsonOut, &rtval)
- noerr(t, err)
- if !cmp.Equal(val, rtval) {
- t.Errorf("Did not round trip properly. got %v; want %v", val, rtval)
- }
- if !rtval.Value.IsZero() {
- t.Errorf("Did not get zero time as expected.")
- }
- }
- func TestNonNullTimeRoundTrip(t *testing.T) {
- now := time.Now()
- now = time.Unix(now.Unix(), 0)
- val := struct {
- Value time.Time
- ID string
- }{
- ID: "time-rt-test",
- Value: now,
- }
- bsonOut, err := Marshal(val)
- noerr(t, err)
- rtval := struct {
- Value time.Time
- ID string
- }{}
- err = Unmarshal(bsonOut, &rtval)
- noerr(t, err)
- if !cmp.Equal(val, rtval) {
- t.Errorf("Did not round trip properly. got %v; want %v", val, rtval)
- }
- }
- func TestD(t *testing.T) {
- t.Run("can marshal", func(t *testing.T) {
- d := D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
- idx, want := bsoncore.AppendDocumentStart(nil)
- want = bsoncore.AppendStringElement(want, "foo", "bar")
- want = bsoncore.AppendStringElement(want, "hello", "world")
- want = bsoncore.AppendDoubleElement(want, "pi", 3.14159)
- want, err := bsoncore.AppendDocumentEnd(want, idx)
- noerr(t, err)
- got, err := Marshal(d)
- noerr(t, err)
- if !bytes.Equal(got, want) {
- t.Errorf("Marshaled documents do not match. got %v; want %v", Raw(got), Raw(want))
- }
- })
- t.Run("can unmarshal", func(t *testing.T) {
- want := D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
- idx, doc := bsoncore.AppendDocumentStart(nil)
- doc = bsoncore.AppendStringElement(doc, "foo", "bar")
- doc = bsoncore.AppendStringElement(doc, "hello", "world")
- doc = bsoncore.AppendDoubleElement(doc, "pi", 3.14159)
- doc, err := bsoncore.AppendDocumentEnd(doc, idx)
- noerr(t, err)
- var got D
- err = Unmarshal(doc, &got)
- noerr(t, err)
- if !cmp.Equal(got, want) {
- t.Errorf("Unmarshaled documents do not match. got %v; want %v", got, want)
- }
- })
- }
|