Skip to content

Commit 52042fd

Browse files
test: mongodb utilities test refactor (#435)
1 parent d1d937a commit 52042fd

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

custom_builtins/mongoclient.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func (mongoClient *MongoClient) FindOne(ctx context.Context, collectionName stri
7070
result := collection.FindOne(ctx, query)
7171

7272
var bsonDocument bson.D
73-
err := result.Decode(&bsonDocument)
74-
if err != nil {
73+
if err := result.Decode(&bsonDocument); err != nil {
7574
if errors.Is(err, mongo.ErrNoDocuments) {
7675
log.WithField("error", map[string]any{"message": err.Error()}).Warn("no document found")
7776
return nil, nil

custom_builtins/mongoclient_test.go

+23-28
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ package custom_builtins
1616

1717
import (
1818
"context"
19-
"fmt"
20-
"os"
2119
"testing"
2220

23-
"github.com/rond-authz/rond/internal/mongoclient"
2421
"github.com/rond-authz/rond/internal/testutils"
2522
"github.com/rond-authz/rond/logging"
2623

@@ -30,13 +27,10 @@ import (
3027
)
3128

3229
func TestNewMongoClient(t *testing.T) {
33-
log := logging.NewNoOpLogger()
34-
35-
mongoDBURL, _ := getMongoDBURL(t)
36-
mongoClient, err := mongoclient.NewMongoClient(log, mongoDBURL, mongoclient.ConnectionOpts{})
37-
require.NoError(t, err)
30+
actualClient, dbName := testutils.GetAndDisposeMongoClient(t)
31+
mockClient := testutils.MockMongoClient{ActualClient: actualClient, DBName: dbName}
3832

39-
client, err := NewMongoClient(logging.NewNoOpLogger(), mongoClient)
33+
client, err := NewMongoClient(logging.NewNoOpLogger(), mockClient)
4034
require.NoError(t, err)
4135
require.NotNil(t, client)
4236
}
@@ -67,16 +61,15 @@ func TestGetMongoCollectionFromContext(t *testing.T) {
6761

6862
func TestMongoFindOne(t *testing.T) {
6963
log := logging.NewNoOpLogger()
70-
mongoDBURL := testutils.GetMongoDBURL(t)
71-
client, err := mongoclient.NewMongoClient(log, mongoDBURL, mongoclient.ConnectionOpts{})
72-
require.NoError(t, err)
73-
defer client.Disconnect()
7464

75-
mongoClient, err := NewMongoClient(log, client)
65+
client, dbName := testutils.GetAndDisposeMongoClient(t)
66+
mockClient := testutils.MockMongoClient{ActualClient: client, DBName: dbName}
67+
68+
mongoClient, err := NewMongoClient(log, mockClient)
7669
require.NoError(t, err)
7770

7871
collectionName := "my-collection"
79-
populateCollection(t, client.Collection(collectionName))
72+
populateCollection(t, mockClient.Collection(collectionName))
8073

8174
t.Run("finds a document", func(t *testing.T) {
8275
result, err := mongoClient.FindOne(context.Background(), collectionName, map[string]interface{}{
@@ -105,6 +98,21 @@ func TestMongoFindOne(t *testing.T) {
10598
require.NoError(t, err)
10699
require.True(t, result == nil)
107100
})
101+
102+
t.Run("fails to find one for closed connection", func(t *testing.T) {
103+
client, dbName := testutils.GetMongoClientCallerMUSTDispose(t)
104+
mockClient := testutils.MockMongoClient{ActualClient: client, DBName: dbName}
105+
mongoClient, err := NewMongoClient(log, mockClient)
106+
require.NoError(t, err)
107+
108+
client.Disconnect(context.Background())
109+
110+
result, err := mongoClient.FindOne(context.Background(), collectionName, map[string]interface{}{
111+
"key": 42,
112+
})
113+
require.Error(t, err)
114+
require.Nil(t, result)
115+
})
108116
}
109117

110118
func TestMongoFindMany(t *testing.T) {
@@ -195,16 +203,3 @@ func populateCollection(t *testing.T, collection *mongo.Collection) {
195203
collection.Drop(ctx)
196204
})
197205
}
198-
199-
func getMongoDBURL(t *testing.T) (connectionString string, dbName string) {
200-
t.Helper()
201-
mongoHost := os.Getenv("MONGO_HOST_CI")
202-
if mongoHost == "" {
203-
mongoHost = testutils.LocalhostMongoDB
204-
t.Logf("Connection to localhost MongoDB, on CI env this is a problem!")
205-
}
206-
207-
dbName = testutils.GetRandomName(10)
208-
connectionString = fmt.Sprintf("mongodb://%s/%s", mongoHost, dbName)
209-
return
210-
}

internal/testutils/mongoclient.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ type MockMongoClient struct {
2525
DBName string
2626
}
2727

28-
func (m *MockMongoClient) Collection(collectionName string) *mongo.Collection {
28+
func (m MockMongoClient) Collection(collectionName string) *mongo.Collection {
2929
if m.ActualClient == nil {
3030
return nil
3131
}
3232
return m.ActualClient.Database(m.DBName).Collection(collectionName)
3333
}
3434

35-
func (m *MockMongoClient) Disconnect() error {
35+
func (m MockMongoClient) Disconnect() error {
3636
if m.ActualClient == nil {
3737
return nil
3838
}

internal/testutils/mongodb.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ func GetAndDisposeMongoClient(t *testing.T) (*mongo.Client, string) {
7575
return client, dbName
7676
}
7777

78+
func GetMongoClientCallerMUSTDispose(t *testing.T) (*mongo.Client, string) {
79+
t.Helper()
80+
81+
mongoHost := GetMongoHost(t)
82+
dbName := GetRandomName(10)
83+
84+
clientOpts := options.Client().ApplyURI(formatMongoDBURL(mongoHost, dbName))
85+
86+
client, err := mongo.Connect(context.Background(), clientOpts)
87+
require.NoError(t, err, "failed mongo db connection")
88+
return client, dbName
89+
}
90+
7891
// GetAndDisposeTestCollection returns a collection from a random database.
7992
// The function performs test clean up by dropping the database and closing MongoDB client connection.
8093
// The returned collections are meant to be used for roles and bindings, respectively.
@@ -111,7 +124,7 @@ func disposeFactory(t *testing.T, client *mongo.Client, dbName string) func() {
111124
// This sleep has been added to avoid mongo race condition
112125
time.Sleep(100 * time.Millisecond)
113126
if err := client.Database(dbName).Drop(context.Background()); err != nil {
114-
t.Fatalf("drop collcetion failed %s", err.Error())
127+
t.Fatalf("drop collection failed %s", err.Error())
115128
}
116129
if err := client.Disconnect(context.Background()); err != nil {
117130
t.Fatalf("db disconnect failed %s", err.Error())

0 commit comments

Comments
 (0)