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.
 
 
 

151 lines
3.5 KiB

  1. // Copyright 2018 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. "testing"
  17. "cloud.google.com/go/internal/testutil"
  18. pb "google.golang.org/genproto/googleapis/datastore/v1"
  19. )
  20. func TestMutationProtos(t *testing.T) {
  21. var keys []*Key
  22. for i := 1; i <= 4; i++ {
  23. k := IDKey("kind", int64(i), nil)
  24. keys = append(keys, k)
  25. }
  26. entity := &PropertyList{{Name: "n", Value: "v"}}
  27. entityForKey := func(k *Key) *pb.Entity {
  28. return &pb.Entity{
  29. Key: keyToProto(k),
  30. Properties: map[string]*pb.Value{
  31. "n": {ValueType: &pb.Value_StringValue{StringValue: "v"}},
  32. },
  33. }
  34. }
  35. for _, test := range []struct {
  36. desc string
  37. in []*Mutation
  38. want []*pb.Mutation
  39. }{
  40. {
  41. desc: "nil",
  42. in: nil,
  43. want: nil,
  44. },
  45. {
  46. desc: "empty",
  47. in: []*Mutation{},
  48. want: nil,
  49. },
  50. {
  51. desc: "various",
  52. in: []*Mutation{
  53. NewInsert(keys[0], entity),
  54. NewUpsert(keys[1], entity),
  55. NewUpdate(keys[2], entity),
  56. NewDelete(keys[3]),
  57. },
  58. want: []*pb.Mutation{
  59. {Operation: &pb.Mutation_Insert{Insert: entityForKey(keys[0])}},
  60. {Operation: &pb.Mutation_Upsert{Upsert: entityForKey(keys[1])}},
  61. {Operation: &pb.Mutation_Update{Update: entityForKey(keys[2])}},
  62. {Operation: &pb.Mutation_Delete{Delete: keyToProto(keys[3])}},
  63. },
  64. },
  65. {
  66. desc: "duplicate deletes",
  67. in: []*Mutation{
  68. NewDelete(keys[0]),
  69. NewInsert(keys[1], entity),
  70. NewDelete(keys[0]),
  71. NewDelete(keys[2]),
  72. NewDelete(keys[0]),
  73. },
  74. want: []*pb.Mutation{
  75. {Operation: &pb.Mutation_Delete{Delete: keyToProto(keys[0])}},
  76. {Operation: &pb.Mutation_Insert{Insert: entityForKey(keys[1])}},
  77. {Operation: &pb.Mutation_Delete{Delete: keyToProto(keys[2])}},
  78. },
  79. },
  80. } {
  81. got, err := mutationProtos(test.in)
  82. if err != nil {
  83. t.Errorf("%s: %v", test.desc, err)
  84. continue
  85. }
  86. if diff := testutil.Diff(got, test.want); diff != "" {
  87. t.Errorf("%s: %s", test.desc, diff)
  88. }
  89. }
  90. }
  91. func TestMutationProtosErrors(t *testing.T) {
  92. entity := &PropertyList{{Name: "n", Value: "v"}}
  93. k := IDKey("kind", 1, nil)
  94. ik := IncompleteKey("kind", nil)
  95. for _, test := range []struct {
  96. desc string
  97. in []*Mutation
  98. want []int // non-nil indexes of MultiError
  99. }{
  100. {
  101. desc: "invalid key",
  102. in: []*Mutation{
  103. NewInsert(nil, entity),
  104. NewUpdate(nil, entity),
  105. NewUpsert(nil, entity),
  106. NewDelete(nil),
  107. },
  108. want: []int{0, 1, 2, 3},
  109. },
  110. {
  111. desc: "incomplete key",
  112. in: []*Mutation{
  113. NewInsert(ik, entity),
  114. NewUpdate(ik, entity),
  115. NewUpsert(ik, entity),
  116. NewDelete(ik),
  117. },
  118. want: []int{1, 3},
  119. },
  120. {
  121. desc: "bad entity",
  122. in: []*Mutation{
  123. NewInsert(k, 1),
  124. NewUpdate(k, 2),
  125. NewUpsert(k, 3),
  126. },
  127. want: []int{0, 1, 2},
  128. },
  129. } {
  130. _, err := mutationProtos(test.in)
  131. if err == nil {
  132. t.Errorf("%s: got nil, want error", test.desc)
  133. continue
  134. }
  135. var got []int
  136. for i, err := range err.(MultiError) {
  137. if err != nil {
  138. got = append(got, i)
  139. }
  140. }
  141. if !testutil.Equal(got, test.want) {
  142. t.Errorf("%s: got errors at %v, want at %v", test.desc, got, test.want)
  143. }
  144. }
  145. }