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.
 
 
 

152 lines
3.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/v1"
  18. )
  19. func TestProcessPreconditionsForVerify(t *testing.T) {
  20. for _, test := range []struct {
  21. in []Precondition
  22. want *pb.Precondition
  23. wantErr bool
  24. }{
  25. {
  26. in: nil,
  27. want: nil,
  28. },
  29. {
  30. in: []Precondition{},
  31. want: nil,
  32. },
  33. {
  34. in: []Precondition{Exists},
  35. want: &pb.Precondition{ConditionType: &pb.Precondition_Exists{true}},
  36. },
  37. {
  38. in: []Precondition{LastUpdateTime(aTime)},
  39. want: &pb.Precondition{ConditionType: &pb.Precondition_UpdateTime{aTimestamp}},
  40. },
  41. {
  42. in: []Precondition{Exists, LastUpdateTime(aTime)},
  43. wantErr: true,
  44. },
  45. {
  46. in: []Precondition{Exists, Exists},
  47. wantErr: true,
  48. },
  49. } {
  50. got, err := processPreconditionsForVerify(test.in)
  51. switch {
  52. case test.wantErr && err == nil:
  53. t.Errorf("%v: got nil, want error", test.in)
  54. case !test.wantErr && err != nil:
  55. t.Errorf("%v: got <%v>, want no error", test.in, err)
  56. case !test.wantErr && err == nil && !testEqual(got, test.want):
  57. t.Errorf("%v: got %+v, want %v", test.in, got, test.want)
  58. }
  59. }
  60. }
  61. func TestProcessPreconditionsForDelete(t *testing.T) {
  62. for _, test := range []struct {
  63. in []Precondition
  64. want *pb.Precondition
  65. wantErr bool
  66. }{
  67. {
  68. in: nil,
  69. want: nil,
  70. },
  71. {
  72. in: []Precondition{},
  73. want: nil,
  74. },
  75. {
  76. in: []Precondition{Exists},
  77. want: &pb.Precondition{ConditionType: &pb.Precondition_Exists{true}},
  78. },
  79. {
  80. in: []Precondition{LastUpdateTime(aTime)},
  81. want: &pb.Precondition{ConditionType: &pb.Precondition_UpdateTime{aTimestamp}},
  82. },
  83. {
  84. in: []Precondition{Exists, LastUpdateTime(aTime)},
  85. wantErr: true,
  86. },
  87. {
  88. in: []Precondition{Exists, Exists},
  89. wantErr: true,
  90. },
  91. } {
  92. got, err := processPreconditionsForDelete(test.in)
  93. switch {
  94. case test.wantErr && err == nil:
  95. t.Errorf("%v: got nil, want error", test.in)
  96. case !test.wantErr && err != nil:
  97. t.Errorf("%v: got <%v>, want no error", test.in, err)
  98. case !test.wantErr && err == nil && !testEqual(got, test.want):
  99. t.Errorf("%v: got %+v, want %v", test.in, got, test.want)
  100. }
  101. }
  102. }
  103. func TestProcessPreconditionsForUpdate(t *testing.T) {
  104. for _, test := range []struct {
  105. in []Precondition
  106. want *pb.Precondition
  107. wantErr bool
  108. }{
  109. {
  110. in: nil,
  111. want: &pb.Precondition{ConditionType: &pb.Precondition_Exists{true}},
  112. },
  113. {
  114. in: []Precondition{},
  115. want: &pb.Precondition{ConditionType: &pb.Precondition_Exists{true}},
  116. },
  117. {
  118. in: []Precondition{Exists},
  119. wantErr: true,
  120. },
  121. {
  122. in: []Precondition{LastUpdateTime(aTime)},
  123. want: &pb.Precondition{ConditionType: &pb.Precondition_UpdateTime{aTimestamp}},
  124. },
  125. {
  126. in: []Precondition{Exists, LastUpdateTime(aTime)},
  127. wantErr: true,
  128. },
  129. {
  130. in: []Precondition{Exists, Exists},
  131. wantErr: true,
  132. },
  133. } {
  134. got, err := processPreconditionsForUpdate(test.in)
  135. switch {
  136. case test.wantErr && err == nil:
  137. t.Errorf("%v: got nil, want error", test.in)
  138. case !test.wantErr && err != nil:
  139. t.Errorf("%v: got <%v>, want no error", test.in, err)
  140. case !test.wantErr && err == nil && !testEqual(got, test.want):
  141. t.Errorf("%v: got %+v, want %v", test.in, got, test.want)
  142. }
  143. }
  144. }