Fail fast when db name is invalid

Without this commit save operation on first document would fail instead of immediate failure upon connection attempt. Such later failure is much less obvious.
This commit is contained in:
Yurii Andrieiev
2019-04-07 02:02:26 +03:00
parent 827de76345
commit b5213097e8
4 changed files with 45 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
import datetime
from pymongo.errors import OperationFailure
from pymongo.errors import OperationFailure, InvalidName
try:
import unittest2 as unittest
@@ -49,6 +49,36 @@ class ConnectionTest(unittest.TestCase):
conn = get_connection('testdb')
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
def test_connect_with_invalid_db_name(self):
"""Ensure that connect() method fails fast if db name is invalid
"""
with self.assertRaises(InvalidName):
connect('mongomock://localhost')
def test_connect_with_db_name_external(self):
"""Ensure that connect() works if db name is $external
"""
"""Ensure that the connect() method works properly."""
connect('$external')
conn = get_connection()
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
db = get_db()
self.assertIsInstance(db, pymongo.database.Database)
self.assertEqual(db.name, '$external')
connect('$external', alias='testdb')
conn = get_connection('testdb')
self.assertIsInstance(conn, pymongo.mongo_client.MongoClient)
def test_connect_with_invalid_db_name_type(self):
"""Ensure that connect() method fails fast if db name has invalid type
"""
with self.assertRaises(TypeError):
non_string_db_name = ['e. g. list instead of a string']
connect(non_string_db_name)
def test_connect_in_mocking(self):
"""Ensure that the connect() method works properly in mocking.
"""