@@ -16,11 +16,8 @@ package custom_builtins
16
16
17
17
import (
18
18
"context"
19
- "fmt"
20
- "os"
21
19
"testing"
22
20
23
- "github.com/rond-authz/rond/internal/mongoclient"
24
21
"github.com/rond-authz/rond/internal/testutils"
25
22
"github.com/rond-authz/rond/logging"
26
23
@@ -30,13 +27,10 @@ import (
30
27
)
31
28
32
29
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 }
38
32
39
- client , err := NewMongoClient (logging .NewNoOpLogger (), mongoClient )
33
+ client , err := NewMongoClient (logging .NewNoOpLogger (), mockClient )
40
34
require .NoError (t , err )
41
35
require .NotNil (t , client )
42
36
}
@@ -67,16 +61,15 @@ func TestGetMongoCollectionFromContext(t *testing.T) {
67
61
68
62
func TestMongoFindOne (t * testing.T ) {
69
63
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 ()
74
64
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 )
76
69
require .NoError (t , err )
77
70
78
71
collectionName := "my-collection"
79
- populateCollection (t , client .Collection (collectionName ))
72
+ populateCollection (t , mockClient .Collection (collectionName ))
80
73
81
74
t .Run ("finds a document" , func (t * testing.T ) {
82
75
result , err := mongoClient .FindOne (context .Background (), collectionName , map [string ]interface {}{
@@ -105,6 +98,21 @@ func TestMongoFindOne(t *testing.T) {
105
98
require .NoError (t , err )
106
99
require .True (t , result == nil )
107
100
})
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
+ })
108
116
}
109
117
110
118
func TestMongoFindMany (t * testing.T ) {
@@ -195,16 +203,3 @@ func populateCollection(t *testing.T, collection *mongo.Collection) {
195
203
collection .Drop (ctx )
196
204
})
197
205
}
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
- }
0 commit comments