File: element_test.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.8.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,500 kB
  • sloc: perl: 533; ansic: 491; python: 432; makefile: 187; sh: 72
file content (127 lines) | stat: -rw-r--r-- 4,016 bytes parent folder | download | duplicates (3)
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
// 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 bsoncore

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"go.mongodb.org/mongo-driver/bson/bsontype"
)

func TestElement(t *testing.T) {
	t.Run("KeyErr", func(t *testing.T) {
		testCases := []struct {
			name string
			elem Element
			str  string
			err  error
		}{
			{"No Type", Element{}, "", ErrElementMissingType},
			{"No Key", Element{0x01, 'f', 'o', 'o'}, "", ErrElementMissingKey},
			{"Success", AppendHeader(nil, bsontype.Double, "foo"), "foo", nil},
		}

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Run("Key", func(t *testing.T) {
					str := tc.elem.Key()
					if str != tc.str {
						t.Errorf("returned strings do not match. got %s; want %s", str, tc.str)
					}
				})
				t.Run("KeyErr", func(t *testing.T) {
					str, err := tc.elem.KeyErr()
					if !compareErrors(err, tc.err) {
						t.Errorf("errors do not match. got %v; want %v", err, tc.err)
					}
					if str != tc.str {
						t.Errorf("returned strings do not match. got %s; want %s", str, tc.str)
					}
				})
			})
		}
	})
	t.Run("Validate", func(t *testing.T) {
		testCases := []struct {
			name string
			elem Element
			err  error
		}{
			{"No Type", Element{}, ErrElementMissingType},
			{"No Key", Element{0x01, 'f', 'o', 'o'}, ErrElementMissingKey},
			{"Insufficient Bytes", AppendHeader(nil, bsontype.Double, "foo"), NewInsufficientBytesError(nil, nil)},
			{"Success", AppendDoubleElement(nil, "foo", 3.14159), nil},
		}

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				err := tc.elem.Validate()
				if !compareErrors(err, tc.err) {
					t.Errorf("errors do not match. got %v; want %v", err, tc.err)
				}
			})
		}
	})
	t.Run("CompareKey", func(t *testing.T) {
		testCases := []struct {
			name  string
			elem  Element
			key   []byte
			equal bool
		}{
			{"Element Too Short", Element{0x02}, nil, false},
			{"Element Invalid Key", Element{0x02, 'f', 'o', 'o'}, nil, false},
			{"Key With Null Byte", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 0x00}, true},
			{"Key Without Null Byte", AppendHeader(nil, bsontype.Double, "pi"), []byte{'p', 'i'}, true},
			{"Key With Null Byte With Extra", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r'}, true},
			{"Prefix Key No Match", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 'b', 'a', 'r'}, false},
		}

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				equal := tc.elem.CompareKey(tc.key)
				if equal != tc.equal {
					t.Errorf("Did not get expected equality result. got %t; want %t", equal, tc.equal)
				}
			})
		}
	})
	t.Run("Value & ValueErr", func(t *testing.T) {
		testCases := []struct {
			name string
			elem Element
			val  Value
			err  error
		}{
			{"No Type", Element{}, Value{}, ErrElementMissingType},
			{"No Key", Element{0x01, 'f', 'o', 'o'}, Value{}, ErrElementMissingKey},
			{"Insufficient Bytes", AppendHeader(nil, bsontype.Double, "foo"), Value{}, NewInsufficientBytesError(nil, nil)},
			{"Success", AppendDoubleElement(nil, "foo", 3.14159), Value{Type: bsontype.Double, Data: AppendDouble(nil, 3.14159)}, nil},
		}

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Run("Value", func(t *testing.T) {
					val := tc.elem.Value()
					if !cmp.Equal(val, tc.val) {
						t.Errorf("Values do not match. got %v; want %v", val, tc.val)
					}
				})
				t.Run("ValueErr", func(t *testing.T) {
					val, err := tc.elem.ValueErr()
					if !compareErrors(err, tc.err) {
						t.Errorf("errors do not match. got %v; want %v", err, tc.err)
					}
					if !cmp.Equal(val, tc.val) {
						t.Errorf("Values do not match. got %v; want %v", val, tc.val)
					}
				})
			})
		}
	})
}