...

Source file src/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/element_test.go

Documentation: go.mongodb.org/mongo-driver/x/bsonx/bsoncore

     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  
     7  package bsoncore
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"go.mongodb.org/mongo-driver/bson/bsontype"
    14  )
    15  
    16  func TestElement(t *testing.T) {
    17  	t.Run("KeyErr", func(t *testing.T) {
    18  		testCases := []struct {
    19  			name string
    20  			elem Element
    21  			str  string
    22  			err  error
    23  		}{
    24  			{"No Type", Element{}, "", ErrElementMissingType},
    25  			{"No Key", Element{0x01, 'f', 'o', 'o'}, "", ErrElementMissingKey},
    26  			{"Success", AppendHeader(nil, bsontype.Double, "foo"), "foo", nil},
    27  		}
    28  
    29  		for _, tc := range testCases {
    30  			t.Run(tc.name, func(t *testing.T) {
    31  				t.Run("Key", func(t *testing.T) {
    32  					str := tc.elem.Key()
    33  					if str != tc.str {
    34  						t.Errorf("returned strings do not match. got %s; want %s", str, tc.str)
    35  					}
    36  				})
    37  				t.Run("KeyErr", func(t *testing.T) {
    38  					str, err := tc.elem.KeyErr()
    39  					if !compareErrors(err, tc.err) {
    40  						t.Errorf("errors do not match. got %v; want %v", err, tc.err)
    41  					}
    42  					if str != tc.str {
    43  						t.Errorf("returned strings do not match. got %s; want %s", str, tc.str)
    44  					}
    45  				})
    46  			})
    47  		}
    48  	})
    49  	t.Run("Validate", func(t *testing.T) {
    50  		testCases := []struct {
    51  			name string
    52  			elem Element
    53  			err  error
    54  		}{
    55  			{"No Type", Element{}, ErrElementMissingType},
    56  			{"No Key", Element{0x01, 'f', 'o', 'o'}, ErrElementMissingKey},
    57  			{"Insufficient Bytes", AppendHeader(nil, bsontype.Double, "foo"), NewInsufficientBytesError(nil, nil)},
    58  			{"Success", AppendDoubleElement(nil, "foo", 3.14159), nil},
    59  		}
    60  
    61  		for _, tc := range testCases {
    62  			t.Run(tc.name, func(t *testing.T) {
    63  				err := tc.elem.Validate()
    64  				if !compareErrors(err, tc.err) {
    65  					t.Errorf("errors do not match. got %v; want %v", err, tc.err)
    66  				}
    67  			})
    68  		}
    69  	})
    70  	t.Run("CompareKey", func(t *testing.T) {
    71  		testCases := []struct {
    72  			name  string
    73  			elem  Element
    74  			key   []byte
    75  			equal bool
    76  		}{
    77  			{"Element Too Short", Element{0x02}, nil, false},
    78  			{"Element Invalid Key", Element{0x02, 'f', 'o', 'o'}, nil, false},
    79  			{"Key With Null Byte", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 0x00}, true},
    80  			{"Key Without Null Byte", AppendHeader(nil, bsontype.Double, "pi"), []byte{'p', 'i'}, true},
    81  			{"Key With Null Byte With Extra", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r'}, true},
    82  			{"Prefix Key No Match", AppendHeader(nil, bsontype.Double, "foo"), []byte{'f', 'o', 'o', 'b', 'a', 'r'}, false},
    83  		}
    84  
    85  		for _, tc := range testCases {
    86  			t.Run(tc.name, func(t *testing.T) {
    87  				equal := tc.elem.CompareKey(tc.key)
    88  				if equal != tc.equal {
    89  					t.Errorf("Did not get expected equality result. got %t; want %t", equal, tc.equal)
    90  				}
    91  			})
    92  		}
    93  	})
    94  	t.Run("Value & ValueErr", func(t *testing.T) {
    95  		testCases := []struct {
    96  			name string
    97  			elem Element
    98  			val  Value
    99  			err  error
   100  		}{
   101  			{"No Type", Element{}, Value{}, ErrElementMissingType},
   102  			{"No Key", Element{0x01, 'f', 'o', 'o'}, Value{}, ErrElementMissingKey},
   103  			{"Insufficient Bytes", AppendHeader(nil, bsontype.Double, "foo"), Value{}, NewInsufficientBytesError(nil, nil)},
   104  			{"Success", AppendDoubleElement(nil, "foo", 3.14159), Value{Type: bsontype.Double, Data: AppendDouble(nil, 3.14159)}, nil},
   105  		}
   106  
   107  		for _, tc := range testCases {
   108  			t.Run(tc.name, func(t *testing.T) {
   109  				t.Run("Value", func(t *testing.T) {
   110  					val := tc.elem.Value()
   111  					if !cmp.Equal(val, tc.val) {
   112  						t.Errorf("Values do not match. got %v; want %v", val, tc.val)
   113  					}
   114  				})
   115  				t.Run("ValueErr", func(t *testing.T) {
   116  					val, err := tc.elem.ValueErr()
   117  					if !compareErrors(err, tc.err) {
   118  						t.Errorf("errors do not match. got %v; want %v", err, tc.err)
   119  					}
   120  					if !cmp.Equal(val, tc.val) {
   121  						t.Errorf("Values do not match. got %v; want %v", val, tc.val)
   122  					}
   123  				})
   124  			})
   125  		}
   126  	})
   127  }
   128  

View as plain text