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.
 
 
 

97 lines
2.6 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. "github.com/golang/protobuf/proto"
  19. pb "google.golang.org/genproto/googleapis/firestore/v1"
  20. )
  21. func TestDoc(t *testing.T) {
  22. coll := testClient.Collection("C")
  23. got := coll.Doc("d")
  24. want := &DocumentRef{
  25. Parent: coll,
  26. ID: "d",
  27. Path: "projects/projectID/databases/(default)/documents/C/d",
  28. shortPath: "C/d",
  29. }
  30. if !testEqual(got, want) {
  31. t.Errorf("got %+v, want %+v", got, want)
  32. }
  33. }
  34. func TestNewDoc(t *testing.T) {
  35. c := &Client{}
  36. coll := c.Collection("C")
  37. got := coll.NewDoc()
  38. if got.Parent != coll {
  39. t.Errorf("got %v, want %v", got.Parent, coll)
  40. }
  41. if len(got.ID) != 20 {
  42. t.Errorf("got %d-char ID, wanted 20", len(got.ID))
  43. }
  44. got2 := coll.NewDoc()
  45. if got.ID == got2.ID {
  46. t.Error("got same ID")
  47. }
  48. }
  49. func TestAdd(t *testing.T) {
  50. ctx := context.Background()
  51. c, srv := newMock(t)
  52. wantReq := commitRequestForSet()
  53. w := wantReq.Writes[0]
  54. w.CurrentDocument = &pb.Precondition{
  55. ConditionType: &pb.Precondition_Exists{false},
  56. }
  57. srv.addRPCAdjust(wantReq, commitResponseForSet, func(gotReq proto.Message) {
  58. // We can't know the doc ID before Add is called, so we take it from
  59. // the request.
  60. w.Operation.(*pb.Write_Update).Update.Name = gotReq.(*pb.CommitRequest).Writes[0].Operation.(*pb.Write_Update).Update.Name
  61. })
  62. _, wr, err := c.Collection("C").Add(ctx, testData)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if !testEqual(wr, writeResultForSet) {
  67. t.Errorf("got %v, want %v", wr, writeResultForSet)
  68. }
  69. }
  70. func TestNilErrors(t *testing.T) {
  71. ctx := context.Background()
  72. c, _ := newMock(t)
  73. // Test that a nil CollectionRef results in a nil DocumentRef and errors
  74. // where possible.
  75. coll := c.Collection("a/b") // nil because "a/b" denotes a doc.
  76. if coll != nil {
  77. t.Fatal("collection not nil")
  78. }
  79. if got := coll.Doc("d"); got != nil {
  80. t.Fatalf("got %v, want nil", got)
  81. }
  82. if got := coll.NewDoc(); got != nil {
  83. t.Fatalf("got %v, want nil", got)
  84. }
  85. if _, _, err := coll.Add(ctx, testData); err != errNilDocRef {
  86. t.Fatalf("got <%v>, want <%v>", err, errNilDocRef)
  87. }
  88. }