...

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

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

     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 bsoncodec
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  	"time"
    13  
    14  	"go.mongodb.org/mongo-driver/bson/bsonoptions"
    15  	"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
    16  	"go.mongodb.org/mongo-driver/bson/bsontype"
    17  	"go.mongodb.org/mongo-driver/internal/assert"
    18  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    19  )
    20  
    21  func TestTimeCodec(t *testing.T) {
    22  	now := time.Now().Truncate(time.Millisecond)
    23  
    24  	t.Run("UseLocalTimeZone", func(t *testing.T) {
    25  		reader := &bsonrwtest.ValueReaderWriter{BSONType: bsontype.DateTime, Return: now.UnixNano() / int64(time.Millisecond)}
    26  		testCases := []struct {
    27  			name string
    28  			opts *bsonoptions.TimeCodecOptions
    29  			utc  bool
    30  		}{
    31  			{"default", bsonoptions.TimeCodec(), true},
    32  			{"false", bsonoptions.TimeCodec().SetUseLocalTimeZone(false), true},
    33  			{"true", bsonoptions.TimeCodec().SetUseLocalTimeZone(true), false},
    34  		}
    35  		for _, tc := range testCases {
    36  			t.Run(tc.name, func(t *testing.T) {
    37  				timeCodec := NewTimeCodec(tc.opts)
    38  
    39  				actual := reflect.New(reflect.TypeOf(now)).Elem()
    40  				err := timeCodec.DecodeValue(DecodeContext{}, reader, actual)
    41  				assert.Nil(t, err, "TimeCodec.DecodeValue error: %v", err)
    42  
    43  				actualTime := actual.Interface().(time.Time)
    44  				assert.Equal(t, actualTime.Location().String() == "UTC", tc.utc,
    45  					"Expected UTC: %v, got %v", tc.utc, actualTime.Location())
    46  
    47  				if tc.utc {
    48  					nowUTC := now.UTC()
    49  					assert.Equal(t, nowUTC, actualTime, "expected time %v, got %v", nowUTC, actualTime)
    50  				} else {
    51  					assert.Equal(t, now, actualTime, "expected time %v, got %v", now, actualTime)
    52  				}
    53  			})
    54  		}
    55  	})
    56  
    57  	t.Run("DecodeFromBsontype", func(t *testing.T) {
    58  		testCases := []struct {
    59  			name   string
    60  			reader *bsonrwtest.ValueReaderWriter
    61  		}{
    62  			{"string", &bsonrwtest.ValueReaderWriter{BSONType: bsontype.String, Return: now.Format(timeFormatString)}},
    63  			{"int64", &bsonrwtest.ValueReaderWriter{BSONType: bsontype.Int64, Return: now.Unix()*1000 + int64(now.Nanosecond()/1e6)}},
    64  			{"timestamp", &bsonrwtest.ValueReaderWriter{BSONType: bsontype.Timestamp,
    65  				Return: bsoncore.Value{
    66  					Type: bsontype.Timestamp,
    67  					Data: bsoncore.AppendTimestamp(nil, uint32(now.Unix()), 0),
    68  				}},
    69  			},
    70  		}
    71  		for _, tc := range testCases {
    72  			t.Run(tc.name, func(t *testing.T) {
    73  				actual := reflect.New(reflect.TypeOf(now)).Elem()
    74  				err := defaultTimeCodec.DecodeValue(DecodeContext{}, tc.reader, actual)
    75  				assert.Nil(t, err, "DecodeValue error: %v", err)
    76  
    77  				actualTime := actual.Interface().(time.Time)
    78  				if tc.name == "timestamp" {
    79  					now = time.Unix(now.Unix(), 0)
    80  				}
    81  				nowUTC := now.UTC()
    82  				assert.Equal(t, nowUTC, actualTime, "expected time %v, got %v", nowUTC, actualTime)
    83  			})
    84  		}
    85  	})
    86  }
    87  

View as plain text