...

Source file src/github.com/go-openapi/strfmt/bson_test.go

Documentation: github.com/go-openapi/strfmt

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package strfmt
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"go.mongodb.org/mongo-driver/bson"
    23  )
    24  
    25  func TestBSONObjectId_fullCycle(t *testing.T) {
    26  	id := NewObjectId("507f1f77bcf86cd799439011")
    27  	bytes, err := id.MarshalText()
    28  	require.NoError(t, err)
    29  
    30  	var idCopy ObjectId
    31  
    32  	err = idCopy.Scan(bytes)
    33  	require.NoError(t, err)
    34  	assert.Equal(t, id, idCopy)
    35  
    36  	err = idCopy.UnmarshalText(bytes)
    37  	require.NoError(t, err)
    38  	assert.Equal(t, id, idCopy)
    39  
    40  	jsonBytes, err := id.MarshalJSON()
    41  	require.NoError(t, err)
    42  
    43  	err = idCopy.UnmarshalJSON(jsonBytes)
    44  	require.NoError(t, err)
    45  	assert.Equal(t, id, idCopy)
    46  
    47  	bsonBytes, err := bson.Marshal(&id)
    48  	require.NoError(t, err)
    49  
    50  	err = bson.Unmarshal(bsonBytes, &idCopy)
    51  	require.NoError(t, err)
    52  	assert.Equal(t, id, idCopy)
    53  }
    54  
    55  func TestDeepCopyObjectId(t *testing.T) {
    56  	id := NewObjectId("507f1f77bcf86cd799439011")
    57  	in := &id
    58  
    59  	out := new(ObjectId)
    60  	in.DeepCopyInto(out)
    61  	assert.Equal(t, in, out)
    62  
    63  	out2 := in.DeepCopy()
    64  	assert.Equal(t, in, out2)
    65  
    66  	var inNil *ObjectId
    67  	out3 := inNil.DeepCopy()
    68  	assert.Nil(t, out3)
    69  }
    70  

View as plain text