github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/docstore/gcpfirestore/codec_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     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  //     https://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 gcpfirestore
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	"gocloud.dev/docstore"
    24  	"gocloud.dev/docstore/drivertest"
    25  	"google.golang.org/genproto/googleapis/type/latlng"
    26  	tspb "google.golang.org/protobuf/types/known/timestamppb"
    27  )
    28  
    29  // Test that special types round-trip.
    30  // These aren't tested in the docstore-wide conformance tests.
    31  func TestCodecSpecial(t *testing.T) {
    32  	const nameField = "Name"
    33  
    34  	type S struct {
    35  		Name    string
    36  		T       time.Time
    37  		TS, TSn *tspb.Timestamp
    38  		LL, LLn *latlng.LatLng
    39  	}
    40  	tm := time.Date(2019, 3, 14, 0, 0, 0, 0, time.UTC)
    41  	ts := tspb.New(tm)
    42  	in := &S{
    43  		Name: "name",
    44  		T:    tm,
    45  		TS:   ts,
    46  		TSn:  nil,
    47  		LL:   &latlng.LatLng{Latitude: 3, Longitude: 4},
    48  		LLn:  nil,
    49  	}
    50  	var got S
    51  
    52  	enc, err := encodeDoc(drivertest.MustDocument(in), nameField)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	enc.Name = "collPath/" + in.Name
    57  	gotdoc := drivertest.MustDocument(&got)
    58  	// Test type-driven decoding (where the types of the struct fields are available).
    59  	if err := decodeDoc(enc, gotdoc, nameField, docstore.DefaultRevisionField); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if diff := cmp.Diff(&got, in, cmpopts.IgnoreUnexported(tspb.Timestamp{}, latlng.LatLng{})); diff != "" {
    63  		t.Error(diff)
    64  	}
    65  
    66  	// Test type-blind decoding.
    67  	gotmap := map[string]interface{}{}
    68  	gotmapdoc := drivertest.MustDocument(gotmap)
    69  	if err := decodeDoc(enc, gotmapdoc, nameField, docstore.DefaultRevisionField); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	wantmap := map[string]interface{}{
    73  		"Name": "name",
    74  		"T":    in.T,
    75  		"TS":   in.T, // timestamps always decode as time.Time
    76  		"TSn":  nil,
    77  		"LL":   in.LL,
    78  		"LLn":  nil,
    79  	}
    80  	if diff := cmp.Diff(gotmap, wantmap, cmpopts.IgnoreUnexported(latlng.LatLng{})); diff != "" {
    81  		t.Error(diff)
    82  	}
    83  }