Skip to content

Commit

Permalink
chore(datastore): fix for Get & Multi tests (#6941)
Browse files Browse the repository at this point in the history
* chore(datastore): fix for Get & Multi tests

* unit test fix
  • Loading branch information
telpirion committed Oct 27, 2022
1 parent aa262ee commit 6115152
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
6 changes: 4 additions & 2 deletions datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ func (c *Client) Get(ctx context.Context, key *Key, dst interface{}) (err error)
if !c.readSettings.readTime.IsZero() {
opts = &pb.ReadOptions{
ConsistencyType: &pb.ReadOptions_ReadTime{
ReadTime: timestamppb.New(c.readSettings.readTime),
// Timestamp cannot be less than microseconds accuracy. See #6938
ReadTime: &timestamppb.Timestamp{Seconds: c.readSettings.readTime.Unix()},
},
}
}
Expand Down Expand Up @@ -389,7 +390,8 @@ func (c *Client) GetMulti(ctx context.Context, keys []*Key, dst interface{}) (er
if c.readSettings != nil && !c.readSettings.readTime.IsZero() {
opts = &pb.ReadOptions{
ConsistencyType: &pb.ReadOptions_ReadTime{
ReadTime: timestamppb.New(c.readSettings.readTime),
// Timestamp cannot be less than microseconds accuracy. See #6938
ReadTime: &timestamppb.Timestamp{Seconds: c.readSettings.readTime.Unix()},
},
}
}
Expand Down
92 changes: 46 additions & 46 deletions datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package datastore
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"testing"
Expand All @@ -27,6 +26,7 @@ import (
"github.com/google/go-cmp/cmp"
pb "google.golang.org/genproto/googleapis/datastore/v1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestQueryConstruction(t *testing.T) {
Expand Down Expand Up @@ -306,29 +306,29 @@ func TestGetWithReadTime(t *testing.T) {
"A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
},
}
fakeClient := &fakeDatastoreClient{
lookup: func(req *pb.LookupRequest) (*pb.LookupResponse, error) {
if !req.ReadOptions.GetReadTime().AsTime().Equal(tm) {
return nil, fmt.Errorf("read time mismatch: expected %v, got %v", tm,
req.ReadOptions.GetReadTime())
}

return &pb.LookupResponse{
Found: []*pb.EntityResult{
{
Entity: e,
Version: 1,
},
},
}, nil
},
}

client := &Client{
client: fakeClient,
readSettings: &readSettings{},
}
client, srv, cleanup := newMock(t)
defer cleanup()

srv.addRPC(&pb.LookupRequest{
ProjectId: "projectID",
DatabaseId: "",
Keys: []*pb.Key{
keyToProto(k),
},
ReadOptions: &pb.ReadOptions{
ConsistencyType: &pb.ReadOptions_ReadTime{
ReadTime: &timestamppb.Timestamp{Seconds: tm.Unix()},
},
},
}, &pb.LookupResponse{
Found: []*pb.EntityResult{
{
Entity: e,
Version: 1,
},
},
})
ctx := context.Background()
client.WithReadOptions(ReadTime(tm))
dst := &ent{}
Expand Down Expand Up @@ -362,32 +362,32 @@ func TestGetMultiWithReadTime(t *testing.T) {
},
}

fakeClient := &fakeDatastoreClient{
lookup: func(req *pb.LookupRequest) (*pb.LookupResponse, error) {

if !req.ReadOptions.GetReadTime().AsTime().Equal(tm) {
return nil, fmt.Errorf("read time mismatch: expected %v, got %v", tm,
req.ReadOptions.GetReadTime())
}
client, srv, cleanup := newMock(t)
defer cleanup()

return &pb.LookupResponse{
Found: []*pb.EntityResult{
{
Entity: e,
Version: 1,
}, {
Entity: e2,
Version: 1,
},
},
}, nil
srv.addRPC(&pb.LookupRequest{
ProjectId: "projectID",
DatabaseId: "",
Keys: []*pb.Key{
keyToProto(k[0]),
keyToProto(k[1]),
},
}

client := &Client{
client: fakeClient,
readSettings: &readSettings{},
}
ReadOptions: &pb.ReadOptions{
ConsistencyType: &pb.ReadOptions_ReadTime{
ReadTime: &timestamppb.Timestamp{Seconds: tm.Unix()},
},
},
}, &pb.LookupResponse{
Found: []*pb.EntityResult{
{
Entity: e,
Version: 1,
}, {
Entity: e2,
Version: 1,
},
},
})

ctx := context.Background()
client.WithReadOptions(ReadTime(tm))
Expand Down

0 comments on commit 6115152

Please sign in to comment.