Skip to content

Commit 929064d

Browse files
Fix MongoDB connection URI extras and typo (#73)
1 parent ee2c492 commit 929064d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

peepdb/db/mongodb.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ def connect(self) -> None:
1717

1818
if self.extra_params:
1919
params = '&'.join(
20-
[f"{k}={v}" for k, v in self.extra_params.items]
20+
[f"{k}={v}" for k, v in self.extra_params.items()]
2121
)
2222

2323
mongo_uri = f"{mongo_uri}?{params}"
2424

25-
self.conection = pymongo.MongoClient(mongo_uri)
26-
self.db = self.conection[self.database]
25+
self.connection = pymongo.MongoClient(mongo_uri)
26+
self.db = self.connection[self.database]
2727
self.logger.info(f"Connected to MongoDB database: {self.database}")
2828
except pymongo.errors.PyMongoError as e:
2929
self.logger.error(f"Error connecting to MongoDB database: {e}")
3030
raise
3131

3232
def disconnect(self) -> None:
33-
if self.conection:
34-
self.conection.close()
33+
if self.connection:
34+
self.connection.close()
3535
self.logger.info(
3636
f"Disconnected from MongoDB database: {self.database}"
3737
)

peepdb/tests/test_mongodb_uri.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
from unittest.mock import patch, MagicMock
3+
from peepdb.db.mongodb import MongoDBDatabase
4+
5+
6+
def test_mongodb_connection_uri_generation():
7+
db = MongoDBDatabase(
8+
host='localhost',
9+
user='user name',
10+
password='p@ssword',
11+
database='mydb',
12+
port=1234,
13+
replicaSet='rs0'
14+
)
15+
mock_client = MagicMock()
16+
with patch('pymongo.MongoClient', return_value=mock_client) as mock_mc:
17+
db.connect()
18+
mock_mc.assert_called_once()
19+
uri = mock_mc.call_args.args[0]
20+
21+
assert uri == 'mongodb://user+name:p%40ssword@localhost:1234/mydb?replicaSet=rs0'
22+
assert db.connection is mock_client
23+
assert db.db == mock_client.__getitem__.return_value

0 commit comments

Comments
 (0)