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.
 
 
 

120 lines
2.8 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 firestore
  15. import (
  16. "testing"
  17. pb "google.golang.org/genproto/googleapis/firestore/v1beta1"
  18. "golang.org/x/net/context"
  19. )
  20. func TestWriteBatch(t *testing.T) {
  21. type update struct{ A int }
  22. c, srv := newMock(t)
  23. docPrefix := c.Collection("C").Path + "/"
  24. srv.addRPC(
  25. &pb.CommitRequest{
  26. Database: c.path(),
  27. Writes: []*pb.Write{
  28. { // Create
  29. Operation: &pb.Write_Update{
  30. Update: &pb.Document{
  31. Name: docPrefix + "a",
  32. Fields: testFields,
  33. },
  34. },
  35. CurrentDocument: &pb.Precondition{
  36. ConditionType: &pb.Precondition_Exists{false},
  37. },
  38. },
  39. { // Set
  40. Operation: &pb.Write_Update{
  41. Update: &pb.Document{
  42. Name: docPrefix + "b",
  43. Fields: testFields,
  44. },
  45. },
  46. },
  47. { // Delete
  48. Operation: &pb.Write_Delete{
  49. Delete: docPrefix + "c",
  50. },
  51. },
  52. { // Update
  53. Operation: &pb.Write_Update{
  54. Update: &pb.Document{
  55. Name: docPrefix + "f",
  56. Fields: map[string]*pb.Value{"*": intval(3)},
  57. },
  58. },
  59. UpdateMask: &pb.DocumentMask{FieldPaths: []string{"`*`"}},
  60. CurrentDocument: &pb.Precondition{
  61. ConditionType: &pb.Precondition_Exists{true},
  62. },
  63. },
  64. },
  65. },
  66. &pb.CommitResponse{
  67. WriteResults: []*pb.WriteResult{
  68. {UpdateTime: aTimestamp},
  69. {UpdateTime: aTimestamp2},
  70. {UpdateTime: aTimestamp3},
  71. },
  72. },
  73. )
  74. gotWRs, err := c.Batch().
  75. Create(c.Doc("C/a"), testData).
  76. Set(c.Doc("C/b"), testData).
  77. Delete(c.Doc("C/c")).
  78. Update(c.Doc("C/f"), []Update{{FieldPath: []string{"*"}, Value: 3}}).
  79. Commit(context.Background())
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. wantWRs := []*WriteResult{{aTime}, {aTime2}, {aTime3}}
  84. if !testEqual(gotWRs, wantWRs) {
  85. t.Errorf("got %+v\nwant %+v", gotWRs, wantWRs)
  86. }
  87. }
  88. func TestWriteBatchErrors(t *testing.T) {
  89. ctx := context.Background()
  90. c, _ := newMock(t)
  91. for _, test := range []struct {
  92. desc string
  93. batch *WriteBatch
  94. }{
  95. {
  96. "empty batch",
  97. c.Batch(),
  98. },
  99. {
  100. "bad doc reference",
  101. c.Batch().Create(c.Doc("a"), testData),
  102. },
  103. {
  104. "bad data",
  105. c.Batch().Create(c.Doc("a/b"), 3),
  106. },
  107. } {
  108. if _, err := test.batch.Commit(ctx); err == nil {
  109. t.Errorf("%s: got nil, want error", test.desc)
  110. }
  111. }
  112. }