Documentation ¶
Overview ¶
Package mockfs mocks Google Firestore for Golang testing. This code has been extracted from the unit tests of the official Go Firestore package (cloud.google.com/go/firestore) and edited to make it sutible for publication as a stand-alone package.
Example (Error) ¶
package main import ( "context" "testing" "time" assert "github.com/stretchr/testify/assert" "google.golang.org/protobuf/types/known/timestamppb" mockfs "github.com/weathersource/go-mockfs" pb "google.golang.org/genproto/googleapis/firestore/v1" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" ) func main() { var t *testing.T // Get a firestore client and mock firestore server client, server, err := mockfs.New() assert.NotNil(t, client) assert.NotNil(t, server) assert.Nil(t, err) // Populate a mock document "a" in collection "C" var ( aTime = time.Date(2017, 1, 26, 0, 0, 0, 0, time.UTC) aTimestamp = timestamppb.New(aTime) dbPath = "projects/projectID/databases/(default)" path = "projects/projectID/databases/(default)/documents/C/a" ) server.AddRPC( &pb.BatchGetDocumentsRequest{ Database: dbPath, Documents: []string{path}, }, []interface{}{ &pb.BatchGetDocumentsResponse{ Result: &pb.BatchGetDocumentsResponse_Missing{path}, ReadTime: aTimestamp, }, }, ) // Get document "a" in collection "C" _, err2 := client.Collection("C").Doc("a").Get(context.Background()) // Test the response assert.Equal(t, codes.NotFound, grpc.Code(err2)) }
Output:
Example (Success) ¶
package main import ( "context" "testing" "time" assert "github.com/stretchr/testify/assert" "google.golang.org/protobuf/types/known/timestamppb" mockfs "github.com/weathersource/go-mockfs" pb "google.golang.org/genproto/googleapis/firestore/v1" ) func main() { var t *testing.T // Get a firestore client and mock firestore server client, server, err := mockfs.New() assert.NotNil(t, client) assert.NotNil(t, server) assert.Nil(t, err) // Populate a mock document "b" in collection "C" var ( aTime = time.Date(2017, 1, 26, 0, 0, 0, 0, time.UTC) aTime2 = time.Date(2017, 2, 5, 0, 0, 0, 0, time.UTC) aTimestamp = timestamppb.New(aTime) aTimestamp2 = timestamppb.New(aTime2) dbPath = "projects/projectID/databases/(default)" path = "projects/projectID/databases/(default)/documents/C/b" pdoc = &pb.Document{ Name: path, CreateTime: aTimestamp, UpdateTime: aTimestamp, Fields: map[string]*pb.Value{"f": {ValueType: &pb.Value_IntegerValue{int64(1)}}}, } ) server.AddRPC( &pb.BatchGetDocumentsRequest{ Database: dbPath, Documents: []string{path}, }, []interface{}{ &pb.BatchGetDocumentsResponse{ Result: &pb.BatchGetDocumentsResponse_Found{pdoc}, ReadTime: aTimestamp2, }, }, ) // Get document "b" in collection "C" ref := client.Collection("C").Doc("b") gotDoc, err := ref.Get(context.Background()) // Test the response assert.Nil(t, err) if assert.NotNil(t, gotDoc) { assert.Equal(t, ref, gotDoc.Ref) assert.Equal(t, aTime, gotDoc.CreateTime) assert.Equal(t, aTime, gotDoc.UpdateTime) assert.Equal(t, aTime2, gotDoc.ReadTime) } }
Output:
Index ¶
- type MockServer
- func (s *MockServer) AddRPC(wantReq proto.Message, resp interface{})
- func (s *MockServer) AddRPCAdjust(wantReq proto.Message, resp interface{}, adjust func(gotReq proto.Message))
- func (s *MockServer) BatchGetDocuments(req *pb.BatchGetDocumentsRequest, bs pb.Firestore_BatchGetDocumentsServer) error
- func (s *MockServer) BeginTransaction(ctx context.Context, req *pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error)
- func (s *MockServer) Commit(ctx context.Context, req *pb.CommitRequest) (*pb.CommitResponse, error)
- func (s *MockServer) GetDocument(ctx context.Context, req *pb.GetDocumentRequest) (*pb.Document, error)
- func (s *MockServer) Listen(stream pb.Firestore_ListenServer) error
- func (s *MockServer) Reset()
- func (s *MockServer) Rollback(ctx context.Context, req *pb.RollbackRequest) (*empty.Empty, error)
- func (s *MockServer) RunQuery(req *pb.RunQueryRequest, qs pb.Firestore_RunQueryServer) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockServer ¶
type MockServer struct { pb.FirestoreServer Addr string // contains filtered or unexported fields }
MockServer mocks the pb.FirestoreServer interface (https://godoc.org/google.golang.org/genproto/googleapis/firestore/v1beta1#FirestoreServer)
func New ¶
func New() (*firestore.Client, *MockServer, error)
New creates a new Firestore Client and MockServer
func (*MockServer) AddRPC ¶
func (s *MockServer) AddRPC(wantReq proto.Message, resp interface{})
AddRPC adds a (request, response) pair to the server's list of expected interactions. The server will compare the incoming request with wantReq using proto.Equal. The response can be a message or an error.
For the Listen RPC, resp should be a []interface{}, where each element is either ListenResponse or an error.
Passing nil for wantReq disables the request check.
func (*MockServer) AddRPCAdjust ¶
func (s *MockServer) AddRPCAdjust(wantReq proto.Message, resp interface{}, adjust func(gotReq proto.Message))
AddRPCAdjust is like AddRPC, but accepts a function that can be used to tweak the requests before comparison, for example to adjust for randomness.
func (*MockServer) BatchGetDocuments ¶
func (s *MockServer) BatchGetDocuments(req *pb.BatchGetDocumentsRequest, bs pb.Firestore_BatchGetDocumentsServer) error
BatchGetDocuments overrides the FirestoreServer BatchGetDocuments method
func (*MockServer) BeginTransaction ¶
func (s *MockServer) BeginTransaction(ctx context.Context, req *pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error)
BeginTransaction overrides the FirestoreServer BeginTransaction method
func (*MockServer) Commit ¶
func (s *MockServer) Commit(ctx context.Context, req *pb.CommitRequest) (*pb.CommitResponse, error)
Commit overrides the FirestoreServer Commit method
func (*MockServer) GetDocument ¶
func (s *MockServer) GetDocument(ctx context.Context, req *pb.GetDocumentRequest) (*pb.Document, error)
GetDocument overrides the FirestoreServer GetDocument method
func (*MockServer) Listen ¶
func (s *MockServer) Listen(stream pb.Firestore_ListenServer) error
Listen overrides the FirestoreServer Listen method
func (*MockServer) Reset ¶
func (s *MockServer) Reset()
Reset returns the MockServer to an empty state.
func (*MockServer) Rollback ¶
func (s *MockServer) Rollback(ctx context.Context, req *pb.RollbackRequest) (*empty.Empty, error)
Rollback overrides the FirestoreServer Rollback method
func (*MockServer) RunQuery ¶
func (s *MockServer) RunQuery(req *pb.RunQueryRequest, qs pb.Firestore_RunQueryServer) error
RunQuery overrides the FirestoreServer RunQuery method