選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

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