...

Source file src/go.mongodb.org/mongo-driver/bson/extjson_prose_test.go

Documentation: go.mongodb.org/mongo-driver/bson

     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 bson
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/bson/bsonrw"
    14  	"go.mongodb.org/mongo-driver/internal/assert"
    15  )
    16  
    17  func TestExtJSON(t *testing.T) {
    18  	timestampNegativeInt32Err := fmt.Errorf("$timestamp i number should be uint32: -1")
    19  	timestampNegativeInt64Err := fmt.Errorf("$timestamp i number should be uint32: -2147483649")
    20  	timestampLargeValueErr := fmt.Errorf("$timestamp i number should be uint32: 4294967296")
    21  
    22  	testCases := []struct {
    23  		name      string
    24  		input     string
    25  		canonical bool
    26  		err       error
    27  	}{
    28  		{"timestamp - negative int32 value", `{"":{"$timestamp":{"t":0,"i":-1}}}`, false, timestampNegativeInt32Err},
    29  		{"timestamp - negative int64 value", `{"":{"$timestamp":{"t":0,"i":-2147483649}}}`, false, timestampNegativeInt64Err},
    30  		{"timestamp - value overflows uint32", `{"":{"$timestamp":{"t":0,"i":4294967296}}}`, false, timestampLargeValueErr},
    31  		{"top level key is not treated as special", `{"$code": "foo"}`, false, nil},
    32  		{"escaped single quote errors", `{"f\'oo": "bar"}`, false, bsonrw.ErrInvalidJSON},
    33  	}
    34  	for _, tc := range testCases {
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			var res Raw
    37  			err := UnmarshalExtJSON([]byte(tc.input), tc.canonical, &res)
    38  			if tc.err == nil {
    39  				assert.Nil(t, err, "UnmarshalExtJSON error: %v", err)
    40  				return
    41  			}
    42  
    43  			assert.NotNil(t, err, "expected error %v, got nil", tc.err)
    44  			assert.Equal(t, tc.err.Error(), err.Error(), "expected error %v, got %v", tc.err, err)
    45  		})
    46  	}
    47  }
    48  

View as plain text