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.
 
 
 

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