File: raw_value_test.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.17.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 25,988 kB
  • sloc: perl: 533; ansic: 491; python: 432; sh: 327; makefile: 174
file content (209 lines) | stat: -rw-r--r-- 5,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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 (
	"errors"
	"fmt"
	"reflect"
	"testing"

	"go.mongodb.org/mongo-driver/bson/bsoncodec"
	"go.mongodb.org/mongo-driver/bson/bsontype"
	"go.mongodb.org/mongo-driver/internal/assert"
	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

func TestRawValue(t *testing.T) {
	t.Parallel()

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

		t.Run("Uses registry attached to value", func(t *testing.T) {
			t.Parallel()

			reg := bsoncodec.NewRegistryBuilder().Build()
			val := RawValue{Type: bsontype.String, Value: bsoncore.AppendString(nil, "foobar"), r: reg}
			var s string
			want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
			got := val.Unmarshal(&s)
			if !compareErrors(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Uses default registry if no registry attached", func(t *testing.T) {
			t.Parallel()

			want := "foobar"
			val := RawValue{Type: bsontype.String, Value: bsoncore.AppendString(nil, want)}
			var got string
			err := val.Unmarshal(&got)
			noerr(t, err)
			if got != want {
				t.Errorf("Expected strings to match. got %s; want %s", got, want)
			}
		})
	})
	t.Run("UnmarshalWithRegistry", func(t *testing.T) {
		t.Parallel()

		t.Run("Returns error when registry is nil", func(t *testing.T) {
			t.Parallel()

			want := ErrNilRegistry
			var val RawValue
			got := val.UnmarshalWithRegistry(nil, &D{})
			if !errors.Is(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Returns lookup error", func(t *testing.T) {
			t.Parallel()

			reg := bsoncodec.NewRegistryBuilder().Build()
			var val RawValue
			var s string
			want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
			got := val.UnmarshalWithRegistry(reg, &s)
			if !compareErrors(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Returns DecodeValue error", func(t *testing.T) {
			t.Parallel()

			reg := NewRegistryBuilder().Build()
			val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
			var s string
			want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
			got := val.UnmarshalWithRegistry(reg, &s)
			if !compareErrors(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Success", func(t *testing.T) {
			t.Parallel()

			reg := NewRegistryBuilder().Build()
			want := float64(3.14159)
			val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
			var got float64
			err := val.UnmarshalWithRegistry(reg, &got)
			noerr(t, err)
			if got != want {
				t.Errorf("Expected results to match. got %g; want %g", got, want)
			}
		})
	})
	t.Run("UnmarshalWithContext", func(t *testing.T) {
		t.Parallel()

		t.Run("Returns error when DecodeContext is nil", func(t *testing.T) {
			t.Parallel()

			want := ErrNilContext
			var val RawValue
			got := val.UnmarshalWithContext(nil, &D{})
			if !errors.Is(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Returns lookup error", func(t *testing.T) {
			t.Parallel()

			dc := bsoncodec.DecodeContext{Registry: bsoncodec.NewRegistryBuilder().Build()}
			var val RawValue
			var s string
			want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
			got := val.UnmarshalWithContext(&dc, &s)
			if !compareErrors(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Returns DecodeValue error", func(t *testing.T) {
			t.Parallel()

			dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
			val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
			var s string
			want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
			got := val.UnmarshalWithContext(&dc, &s)
			if !compareErrors(got, want) {
				t.Errorf("Expected errors to match. got %v; want %v", got, want)
			}
		})
		t.Run("Success", func(t *testing.T) {
			t.Parallel()

			dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
			want := float64(3.14159)
			val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
			var got float64
			err := val.UnmarshalWithContext(&dc, &got)
			noerr(t, err)
			if got != want {
				t.Errorf("Expected results to match. got %g; want %g", got, want)
			}
		})
	})

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

		tests := []struct {
			name string
			val  RawValue
			want bool
		}{
			{
				name: "empty",
				val:  RawValue{},
				want: true,
			},
			{
				name: "zero type but non-zero value",
				val: RawValue{
					Type:  0x00,
					Value: bsoncore.AppendInt32(nil, 0),
				},
				want: false,
			},
			{
				name: "zero type and zero value",
				val: RawValue{
					Type:  0x00,
					Value: bsoncore.AppendInt32(nil, 0),
				},
			},
			{
				name: "non-zero type and non-zero value",
				val: RawValue{
					Type:  bsontype.String,
					Value: bsoncore.AppendString(nil, "foobar"),
				},
				want: false,
			},
			{
				name: "non-zero type and zero value",
				val: RawValue{
					Type:  bsontype.String,
					Value: bsoncore.AppendString(nil, "foobar"),
				},
			},
		}

		for _, tt := range tests {
			tt := tt // Capture the range variable
			t.Run(tt.name, func(t *testing.T) {
				t.Parallel()

				assert.Equal(t, tt.want, tt.val.IsZero())
			})
		}
	})
}