You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
2.1 KiB

  1. // Copyright 2017 Google LLC
  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. package datastore
  15. import (
  16. "context"
  17. "testing"
  18. "github.com/golang/protobuf/proto"
  19. pb "google.golang.org/genproto/googleapis/datastore/v1"
  20. )
  21. func TestNewTransaction(t *testing.T) {
  22. var got *pb.BeginTransactionRequest
  23. client := &Client{
  24. dataset: "project",
  25. client: &fakeDatastoreClient{
  26. beginTransaction: func(req *pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error) {
  27. got = req
  28. return &pb.BeginTransactionResponse{
  29. Transaction: []byte("tid"),
  30. }, nil
  31. },
  32. },
  33. }
  34. ctx := context.Background()
  35. for _, test := range []struct {
  36. settings *transactionSettings
  37. want *pb.BeginTransactionRequest
  38. }{
  39. {
  40. &transactionSettings{},
  41. &pb.BeginTransactionRequest{ProjectId: "project"},
  42. },
  43. {
  44. &transactionSettings{readOnly: true},
  45. &pb.BeginTransactionRequest{
  46. ProjectId: "project",
  47. TransactionOptions: &pb.TransactionOptions{
  48. Mode: &pb.TransactionOptions_ReadOnly_{ReadOnly: &pb.TransactionOptions_ReadOnly{}},
  49. },
  50. },
  51. },
  52. {
  53. &transactionSettings{prevID: []byte("tid")},
  54. &pb.BeginTransactionRequest{
  55. ProjectId: "project",
  56. TransactionOptions: &pb.TransactionOptions{
  57. Mode: &pb.TransactionOptions_ReadWrite_{ReadWrite: &pb.TransactionOptions_ReadWrite{
  58. PreviousTransaction: []byte("tid"),
  59. },
  60. },
  61. },
  62. },
  63. },
  64. } {
  65. _, err := client.newTransaction(ctx, test.settings)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if !proto.Equal(got, test.want) {
  70. t.Errorf("%+v:\ngot %+v\nwant %+v", test.settings, got, test.want)
  71. }
  72. }
  73. }