Code Cleanup and adding unit tests
Cleaning code to allow only URI type MongoClient connection,
adding unit tests
Change-Id: Iacca44c00006a1072ff70989b0220e2b27bc5fd8
Signed-off-by: aticig <gulsum.atici@canonical.com>
diff --git a/osm_common/tests/test_fsmongo.py b/osm_common/tests/test_fsmongo.py
index 01a8ef2..7e1e47c 100644
--- a/osm_common/tests/test_fsmongo.py
+++ b/osm_common/tests/test_fsmongo.py
@@ -44,7 +44,7 @@
@pytest.fixture(scope="function", params=[True, False])
def fs_mongo(request, monkeypatch):
- def mock_mongoclient_constructor(a, b, c):
+ def mock_mongoclient_constructor(a, b):
pass
def mock_mongoclient_getitem(a, b):
@@ -57,9 +57,7 @@
monkeypatch.setattr(MongoClient, "__getitem__", mock_mongoclient_getitem)
monkeypatch.setattr(GridFSBucket, "__init__", mock_gridfs_constructor)
fs = FsMongo(lock=request.param)
- fs.fs_connect(
- {"path": valid_path(), "host": "mongo", "port": 27017, "collection": "files"}
- )
+ fs.fs_connect({"path": valid_path(), "uri": "mongo:27017", "collection": "files"})
return fs
@@ -143,17 +141,6 @@
(
{
"logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "fs_mongo",
- valid_path(),
- ),
- (
- {
- "logger_name": "fs_mongo",
"path": valid_path()[:-1],
"uri": "mongo:27017",
"collection": "files",
@@ -162,46 +149,15 @@
valid_path(),
),
(
- {
- "logger_name": "fs_mongo",
- "path": valid_path()[:-1],
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "fs_mongo",
- valid_path(),
- ),
- (
{"path": valid_path(), "uri": "mongo:27017", "collection": "files"},
"fs",
valid_path(),
),
(
- {
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "fs",
- valid_path(),
- ),
- (
{"path": valid_path()[:-1], "uri": "mongo:27017", "collection": "files"},
"fs",
valid_path(),
),
- (
- {
- "path": valid_path()[:-1],
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "fs",
- valid_path(),
- ),
],
)
def test_fs_connect_with_valid_config(config, exp_logger, exp_path):
@@ -228,16 +184,6 @@
(
{
"logger_name": "fs_mongo",
- "path": invalid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- fs_connect_exception_message(invalid_path()),
- ),
- (
- {
- "logger_name": "fs_mongo",
"path": invalid_path()[:-1],
"uri": "mongo:27017",
"collection": "files",
@@ -245,43 +191,15 @@
fs_connect_exception_message(invalid_path()[:-1]),
),
(
- {
- "logger_name": "fs_mongo",
- "path": invalid_path()[:-1],
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- fs_connect_exception_message(invalid_path()[:-1]),
- ),
- (
{"path": invalid_path(), "uri": "mongo:27017", "collection": "files"},
fs_connect_exception_message(invalid_path()),
),
(
- {
- "path": invalid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- fs_connect_exception_message(invalid_path()),
- ),
- (
{"path": invalid_path()[:-1], "uri": "mongo:27017", "collection": "files"},
fs_connect_exception_message(invalid_path()[:-1]),
),
(
- {
- "path": invalid_path()[:-1],
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- fs_connect_exception_message(invalid_path()[:-1]),
- ),
- (
- {"path": "/", "host": "mongo", "port": 27017, "collection": "files"},
+ {"path": "/", "uri": "mongo:27017", "collection": "files"},
generic_fs_exception_message(
"Invalid configuration param at '[storage]': path '/' is not writable"
),
@@ -303,49 +221,13 @@
'Missing parameter "path"',
),
(
- {
- "logger_name": "fs_mongo",
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- 'Missing parameter "path"',
- ),
- (
{"logger_name": "fs_mongo", "path": valid_path(), "collection": "files"},
- 'Missing parameters: "uri" or "host" + "port"',
- ),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "port": 27017,
- "collection": "files",
- },
- 'Missing parameters: "uri" or "host" + "port"',
- ),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "collection": "files",
- },
- 'Missing parameters: "uri" or "host" + "port"',
+ 'Missing parameters: "uri"',
),
(
{"logger_name": "fs_mongo", "path": valid_path(), "uri": "mongo:27017"},
'Missing parameter "collection"',
),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- },
- 'Missing parameter "collection"',
- ),
],
)
def test_fs_connect_with_missing_parameters(config, exp_exception_message):
@@ -367,22 +249,12 @@
},
"MongoClient crashed",
),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "MongoClient crashed",
- ),
],
)
def test_fs_connect_with_invalid_mongoclient(
config, exp_exception_message, monkeypatch
):
- def generate_exception(a, b, c=None):
+ def generate_exception(a, b=None):
raise Exception(exp_exception_message)
monkeypatch.setattr(MongoClient, "__init__", generate_exception)
@@ -405,22 +277,12 @@
},
"Collection unavailable",
),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "Collection unavailable",
- ),
],
)
def test_fs_connect_with_invalid_mongo_collection(
config, exp_exception_message, monkeypatch
):
- def mock_mongoclient_constructor(a, b, c=None):
+ def mock_mongoclient_constructor(a, b=None):
pass
def generate_exception(a, b):
@@ -447,22 +309,12 @@
},
"GridFsBucket crashed",
),
- (
- {
- "logger_name": "fs_mongo",
- "path": valid_path(),
- "host": "mongo",
- "port": 27017,
- "collection": "files",
- },
- "GridFsBucket crashed",
- ),
],
)
def test_fs_connect_with_invalid_gridfsbucket(
config, exp_exception_message, monkeypatch
):
- def mock_mongoclient_constructor(a, b, c=None):
+ def mock_mongoclient_constructor(a, b=None):
pass
def mock_mongoclient_getitem(a, b):