mockfs

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

go-mockfs

CircleCI GoDoc

Package mockfs mocks Google Firestore for Golang testing. This code has been extracted from the unit tests of the official Go Firestore package and edited to make it suitable for publication as a stand-alone package.

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

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

BatchGetDocuments overrides the FirestoreServer BatchGetDocuments method

func (*MockServer) BeginTransaction

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

RunQuery overrides the FirestoreServer RunQuery method

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL