bson_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package bson
  7. import (
  8. "bytes"
  9. "testing"
  10. "time"
  11. "github.com/google/go-cmp/cmp"
  12. "github.com/stretchr/testify/require"
  13. "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
  14. )
  15. func noerr(t *testing.T, err error) {
  16. if err != nil {
  17. t.Helper()
  18. t.Errorf("Unexpected error: (%T)%v", err, err)
  19. t.FailNow()
  20. }
  21. }
  22. func requireErrEqual(t *testing.T, err1 error, err2 error) { require.True(t, compareErrors(err1, err2)) }
  23. func TestTimeRoundTrip(t *testing.T) {
  24. val := struct {
  25. Value time.Time
  26. ID string
  27. }{
  28. ID: "time-rt-test",
  29. }
  30. if !val.Value.IsZero() {
  31. t.Errorf("Did not get zero time as expected.")
  32. }
  33. bsonOut, err := Marshal(val)
  34. noerr(t, err)
  35. rtval := struct {
  36. Value time.Time
  37. ID string
  38. }{}
  39. err = Unmarshal(bsonOut, &rtval)
  40. noerr(t, err)
  41. if !cmp.Equal(val, rtval) {
  42. t.Errorf("Did not round trip properly. got %v; want %v", val, rtval)
  43. }
  44. if !rtval.Value.IsZero() {
  45. t.Errorf("Did not get zero time as expected.")
  46. }
  47. }
  48. func TestNonNullTimeRoundTrip(t *testing.T) {
  49. now := time.Now()
  50. now = time.Unix(now.Unix(), 0)
  51. val := struct {
  52. Value time.Time
  53. ID string
  54. }{
  55. ID: "time-rt-test",
  56. Value: now,
  57. }
  58. bsonOut, err := Marshal(val)
  59. noerr(t, err)
  60. rtval := struct {
  61. Value time.Time
  62. ID string
  63. }{}
  64. err = Unmarshal(bsonOut, &rtval)
  65. noerr(t, err)
  66. if !cmp.Equal(val, rtval) {
  67. t.Errorf("Did not round trip properly. got %v; want %v", val, rtval)
  68. }
  69. }
  70. func TestD(t *testing.T) {
  71. t.Run("can marshal", func(t *testing.T) {
  72. d := D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  73. idx, want := bsoncore.AppendDocumentStart(nil)
  74. want = bsoncore.AppendStringElement(want, "foo", "bar")
  75. want = bsoncore.AppendStringElement(want, "hello", "world")
  76. want = bsoncore.AppendDoubleElement(want, "pi", 3.14159)
  77. want, err := bsoncore.AppendDocumentEnd(want, idx)
  78. noerr(t, err)
  79. got, err := Marshal(d)
  80. noerr(t, err)
  81. if !bytes.Equal(got, want) {
  82. t.Errorf("Marshaled documents do not match. got %v; want %v", Raw(got), Raw(want))
  83. }
  84. })
  85. t.Run("can unmarshal", func(t *testing.T) {
  86. want := D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  87. idx, doc := bsoncore.AppendDocumentStart(nil)
  88. doc = bsoncore.AppendStringElement(doc, "foo", "bar")
  89. doc = bsoncore.AppendStringElement(doc, "hello", "world")
  90. doc = bsoncore.AppendDoubleElement(doc, "pi", 3.14159)
  91. doc, err := bsoncore.AppendDocumentEnd(doc, idx)
  92. noerr(t, err)
  93. var got D
  94. err = Unmarshal(doc, &got)
  95. noerr(t, err)
  96. if !cmp.Equal(got, want) {
  97. t.Errorf("Unmarshaled documents do not match. got %v; want %v", got, want)
  98. }
  99. })
  100. }