Skip to content

Commit

Permalink
test: add test for firestore client adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianneto committed Jun 23, 2023
1 parent e0e7a25 commit fb46695
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/database/firestore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,38 @@ import (
"github.com/dorianneto/url-shortener/src/database/output/document"
)

type documentAdapterInterface interface {
Get(ctx context.Context) (_ *firestore.DocumentSnapshot, err error)
Set(ctx context.Context, data interface{}, opts ...firestore.SetOption) (_ *firestore.WriteResult, err error)
}

type FirestoreClientAdapterInterface interface {
Doc(path string) documentAdapterInterface
Close() error
}

type firestoreClientAdapter struct {
client *firestore.Client
}

func (fca *firestoreClientAdapter) Close() error {
return fca.client.Close()
}

func (fca *firestoreClientAdapter) Doc(path string) documentAdapterInterface {
return fca.client.Doc(path)
}

type firestoreAdapter struct {
client *firestore.Client
client FirestoreClientAdapterInterface
contextBackground context.Context
}

func NewFirestoreAdapter() *firestoreAdapter {
return &firestoreAdapter{}
}

func (fa *firestoreAdapter) getClient() *firestore.Client {
func (fa *firestoreAdapter) getClient() FirestoreClientAdapterInterface {
if fa.client != nil {
return fa.client
}
Expand All @@ -29,7 +51,9 @@ func (fa *firestoreAdapter) getClient() *firestore.Client {
log.Fatalln(err)
}

fa.client = client
fa.client = &firestoreClientAdapter{
client: client,
}

return fa.client
}
Expand Down
133 changes: 133 additions & 0 deletions src/database/firestore/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package firestore

import (
"context"
"errors"
"reflect"
"testing"

"cloud.google.com/go/firestore"
)

var (
haveCloseBeenCalled = false
getFn, setFn func() error
)

type documentAdapterMock struct{}

func (a *documentAdapterMock) Get(ctx context.Context) (_ *firestore.DocumentSnapshot, err error) {
return &firestore.DocumentSnapshot{}, getFn()
}

func (a *documentAdapterMock) Set(ctx context.Context, data interface{}, opts ...firestore.SetOption) (_ *firestore.WriteResult, err error) {
return nil, setFn()
}

type clientAdapterMock struct{}

func (c *clientAdapterMock) Doc(path string) documentAdapterInterface {
return &documentAdapterMock{}
}

func (c *clientAdapterMock) Close() error {
haveCloseBeenCalled = true
return nil
}

func resetMocks() {
haveCloseBeenCalled = false
getFn = func() error {
return nil
}
setFn = func() error {
return nil
}
}

func TestClientClose(t *testing.T) {
defer t.Cleanup(resetMocks)

c := NewFirestoreAdapter()
c.client = &clientAdapterMock{}

c.Close()

if !haveCloseBeenCalled {
t.Error("Method Close() is expected to be called")
}
}

func TestClientRead(t *testing.T) {
defer t.Cleanup(resetMocks)

c := NewFirestoreAdapter()
c.client = &clientAdapterMock{}

got, err := c.Read("foo")

if err != nil {
t.Errorf("An error is not expected")
}

if reflect.TypeOf(got).String() != "*document.ReadOutput" {
t.Error("The return expected to be instance of *document.ReadOutput")
}
}

func TestClientCannotReadBecauseBecauseSomethingGoesWrong(t *testing.T) {
defer t.Cleanup(resetMocks)

getFn = func() error {
return errors.New("something goes wrong")
}

c := NewFirestoreAdapter()
c.client = &clientAdapterMock{}

got, err := c.Read("foo")

if err == nil {
t.Errorf("An error is expected")
}

if got != nil {
t.Error("The return expected to be nil")
}
}

func TestClientWrite(t *testing.T) {
defer t.Cleanup(resetMocks)

c := NewFirestoreAdapter()
c.client = &clientAdapterMock{}

got, err := c.Write("foo", map[string]int{"foo": 1, "bar": 2})

if err != nil {
t.Errorf("An error is not expected")
}

_, ok := got.(map[string]int)

if !ok {
t.Error("The return expected to be map[string]int")
}
}

func TestClientCannotWriteBecauseSomethingGoesWrong(t *testing.T) {
defer t.Cleanup(resetMocks)

setFn = func() error {
return errors.New("something goes wrong")
}

c := NewFirestoreAdapter()
c.client = &clientAdapterMock{}

_, err := c.Write("foo", nil)

if err == nil {
t.Errorf("An error is expected")
}
}

0 comments on commit fb46695

Please sign in to comment.