Make common methods threading safe. pytest enhancements
[osm/common.git] / osm_common / tests / test_dbmemory.py
index 0b62485..37c2c83 100644 (file)
@@ -8,14 +8,16 @@ from osm_common.dbmemory import DbMemory
 
 __author__ = 'Eduardo Sousa <eduardosousa@av.it.pt>'
 
-@pytest.fixture
-def db_memory():
-    db = DbMemory()
+
+@pytest.fixture(scope="function", params=[True, False])
+def db_memory(request):
+    db = DbMemory(lock=request.param)
     return db
 
-@pytest.fixture
-def db_memory_with_data():
-    db = DbMemory()
+
+@pytest.fixture(scope="function", params=[True, False])
+def db_memory_with_data(request):
+    db = DbMemory(lock=request.param)
 
     db.create("test", {"_id": 1, "data": 1})
     db.create("test", {"_id": 2, "data": 2})
@@ -24,58 +26,63 @@ def db_memory_with_data():
     return db
 
 
-
 def empty_exception_message():
     return 'database exception '
 
-def get_one_exception_message(filter):
-    return "database exception Not found entry with filter='{}'".format(filter)
 
-def get_one_multiple_exception_message(filter):
-    return "database exception Found more than one entry with filter='{}'".format(filter)
+def get_one_exception_message(db_filter):
+    return "database exception Not found entry with filter='{}'".format(db_filter)
+
 
-def del_one_exception_message(filter):
-    return "database exception Not found entry with filter='{}'".format(filter)
+def get_one_multiple_exception_message(db_filter):
+    return "database exception Found more than one entry with filter='{}'".format(db_filter)
+
+
+def del_one_exception_message(db_filter):
+    return "database exception Not found entry with filter='{}'".format(db_filter)
+
+
+def replace_exception_message(value):
+    return "database exception Not found entry with _id='{}'".format(value)
 
-def replace_exception_message(filter):
-    return "database exception Not found entry with filter='{}'".format(filter)
 
 def test_constructor():
     db = DbMemory()
-
     assert db.logger == logging.getLogger('db')
     assert len(db.db) == 0
 
+
 def test_constructor_with_logger():
     logger_name = 'db_local'
-
     db = DbMemory(logger_name=logger_name)
-
     assert db.logger == logging.getLogger(logger_name)
     assert len(db.db) == 0
 
+
 def test_db_connect():
     logger_name = 'db_local'
     config = {'logger_name': logger_name}
-
     db = DbMemory()
     db.db_connect(config)
-
     assert db.logger == logging.getLogger(logger_name)
     assert len(db.db) == 0
 
-@pytest.mark.parametrize("table, filter", [
+
+def test_db_disconnect(db_memory):
+    db_memory.db_disconnect()
+
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {}),
     ("test", {"_id": 1}),
     ("test", {"data": 1}),
-    ("test", {"_id": 1, "data": 1}),
-])
-def test_get_list_with_empty_db(db_memory, table, filter):
-    result = db_memory.get_list(table, filter)
-
+    ("test", {"_id": 1, "data": 1})])
+def test_get_list_with_empty_db(db_memory, table, db_filter):
+    result = db_memory.get_list(table, db_filter)
     assert len(result) == 0
 
-@pytest.mark.parametrize("table, filter, expected_data", [
+
+@pytest.mark.parametrize("table, db_filter, expected_data", [
     ("test", {}, [{"_id": 1, "data": 1}, {"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
     ("test", {"_id": 1}, [{"_id": 1, "data": 1}]),
     ("test", {"data": 1}, [{"_id": 1, "data": 1}]),
@@ -89,27 +96,25 @@ def test_get_list_with_empty_db(db_memory, table, filter):
     ("test_table", {}, []),
     ("test_table", {"_id": 1}, []),
     ("test_table", {"data": 1}, []),
-    ("test_table", {"_id": 1, "data": 1}, []),
-])
-def test_get_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data):
-    result = db_memory_with_data.get_list(table, filter)
-
+    ("test_table", {"_id": 1, "data": 1}, [])])
+def test_get_list_with_non_empty_db(db_memory_with_data, table, db_filter, expected_data):
+    result = db_memory_with_data.get_list(table, db_filter)
     assert len(result) == len(expected_data)
     for data in expected_data:
         assert data in result
 
+
 def test_get_list_exception(db_memory_with_data):
     table = 'test'
-    filter = {}
-
+    db_filter = {}
     db_memory_with_data._find = MagicMock(side_effect=Exception())
-
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.get_list(table, filter)
+        db_memory_with_data.get_list(table, db_filter)
     assert str(excinfo.value) == empty_exception_message()
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, expected_data", [
+
+@pytest.mark.parametrize("table, db_filter, expected_data", [
     ("test", {"_id": 1}, {"_id": 1, "data": 1}),
     ("test", {"_id": 2}, {"_id": 2, "data": 2}),
     ("test", {"_id": 3}, {"_id": 3, "data": 3}),
@@ -118,114 +123,106 @@ def test_get_list_exception(db_memory_with_data):
     ("test", {"data": 3}, {"_id": 3, "data": 3}),
     ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
     ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}),
-    ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3}),
-])
-def test_get_one(db_memory_with_data, table, filter, expected_data):
-    result = db_memory_with_data.get_one(table, filter)
-
+    ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3})])
+def test_get_one(db_memory_with_data, table, db_filter, expected_data):
+    result = db_memory_with_data.get_one(table, db_filter)
     assert result == expected_data
     assert len(db_memory_with_data.db) == 1
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == 3
     assert result in db_memory_with_data.db[table]
 
-@pytest.mark.parametrize("table, filter, expected_data", [
-    ("test", {}, {"_id": 1, "data": 1}),
-])
-def test_get_one_with_multiple_results(db_memory_with_data, table, filter, expected_data):
-    result = db_memory_with_data.get_one(table, filter, fail_on_more=False)
 
+@pytest.mark.parametrize("table, db_filter, expected_data", [
+    ("test", {}, {"_id": 1, "data": 1})])
+def test_get_one_with_multiple_results(db_memory_with_data, table, db_filter, expected_data):
+    result = db_memory_with_data.get_one(table, db_filter, fail_on_more=False)
     assert result == expected_data
     assert len(db_memory_with_data.db) == 1
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == 3
     assert result in db_memory_with_data.db[table]
 
+
 def test_get_one_with_multiple_results_exception(db_memory_with_data):
     table = "test"
-    filter = {}
-
+    db_filter = {}
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.get_one(table, filter)
+        db_memory_with_data.get_one(table, db_filter)
+    assert str(excinfo.value) == (empty_exception_message() + get_one_multiple_exception_message(db_filter))
+    # assert excinfo.value.http_code == http.HTTPStatus.CONFLICT
 
-    assert str(excinfo.value) == (empty_exception_message() + get_one_multiple_exception_message(filter))
-#    assert excinfo.value.http_code == http.HTTPStatus.CONFLICT
 
-@pytest.mark.parametrize("table, filter", [
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"data": 4}),
     ("test", {"_id": 4, "data": 4}),
     ("test_table", {"_id": 4}),
     ("test_table", {"data": 4}),
-    ("test_table", {"_id": 4, "data": 4}),
-])
-def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, filter):
+    ("test_table", {"_id": 4, "data": 4})])
+def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter):
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.get_one(table, filter)
-    assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(filter))
+        db_memory_with_data.get_one(table, db_filter)
+    assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(db_filter))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"data": 4}),
     ("test", {"_id": 4, "data": 4}),
     ("test_table", {"_id": 4}),
     ("test_table", {"data": 4}),
-    ("test_table", {"_id": 4, "data": 4}),
-])
-def test_get_one_with_non_empty_db_none(db_memory_with_data, table, filter):
-    result = db_memory_with_data.get_one(table, filter, fail_on_empty=False)
-    
-    assert result == None
-
-@pytest.mark.parametrize("table, filter", [
+    ("test_table", {"_id": 4, "data": 4})])
+def test_get_one_with_non_empty_db_none(db_memory_with_data, table, db_filter):
+    result = db_memory_with_data.get_one(table, db_filter, fail_on_empty=False)
+    assert result is None
+
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"data": 4}),
     ("test", {"_id": 4, "data": 4}),
     ("test_table", {"_id": 4}),
     ("test_table", {"data": 4}),
-    ("test_table", {"_id": 4, "data": 4}),
-])
-def test_get_one_with_empty_db_exception(db_memory, table, filter):
+    ("test_table", {"_id": 4, "data": 4})])
+def test_get_one_with_empty_db_exception(db_memory, table, db_filter):
     with pytest.raises(DbException) as excinfo:
-        db_memory.get_one(table, filter)
-    assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(filter))
+        db_memory.get_one(table, db_filter)
+    assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(db_filter))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"data": 4}),
     ("test", {"_id": 4, "data": 4}),
     ("test_table", {"_id": 4}),
     ("test_table", {"data": 4}),
-    ("test_table", {"_id": 4, "data": 4}),
-])
-def test_get_one_with_empty_db_none(db_memory, table, filter):
-    result = db_memory.get_one(table, filter, fail_on_empty=False)
-    
-    assert result == None
+    ("test_table", {"_id": 4, "data": 4})])
+def test_get_one_with_empty_db_none(db_memory, table, db_filter):
+    result = db_memory.get_one(table, db_filter, fail_on_empty=False)
+    assert result is None
+
 
 def test_get_one_generic_exception(db_memory_with_data):
     table = 'test'
-    filter = {}
-
+    db_filter = {}
     db_memory_with_data._find = MagicMock(side_effect=Exception())
-
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.get_one(table, filter)
+        db_memory_with_data.get_one(table, db_filter)
     assert str(excinfo.value) == empty_exception_message()
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, expected_data", [
-#    ("test", {}, []),
+
+@pytest.mark.parametrize("table, db_filter, expected_data", [
+    ("test", {}, []),
     ("test", {"_id": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]), 
     ("test", {"_id": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]), 
     ("test", {"_id": 1, "data": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
-    ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]),
-])
-def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data):
-    result = db_memory_with_data.del_list(table, filter)
-
+    ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}])])
+def test_del_list_with_non_empty_db(db_memory_with_data, table, db_filter, expected_data):
+    result = db_memory_with_data.del_list(table, db_filter)
     assert result["deleted"] == (3 - len(expected_data))
     assert len(db_memory_with_data.db) == 1
     assert table in db_memory_with_data.db
@@ -233,49 +230,48 @@ def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected
     for data in expected_data:
         assert data in db_memory_with_data.db[table]
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {}),
     ("test", {"_id": 1}),
     ("test", {"_id": 2}),
     ("test", {"data": 1}),
     ("test", {"data": 2}),
     ("test", {"_id": 1, "data": 1}),
-    ("test", {"_id": 2, "data": 2}),
-])
-def test_del_list_with_empty_db(db_memory, table, filter):
-    result = db_memory.del_list(table, filter)
+    ("test", {"_id": 2, "data": 2})])
+def test_del_list_with_empty_db(db_memory, table, db_filter):
+    result = db_memory.del_list(table, db_filter)
     assert result['deleted'] == 0
 
+
 def test_del_list_generic_exception(db_memory_with_data):
     table = 'test'
-    filter = {}
-
+    db_filter = {}
     db_memory_with_data._find = MagicMock(side_effect=Exception())
-
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.del_list(table, filter)
+        db_memory_with_data.del_list(table, db_filter)
     assert str(excinfo.value) == empty_exception_message()
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, data", [
+
+@pytest.mark.parametrize("table, db_filter, data", [
     ("test", {}, {"_id": 1, "data": 1}),
     ("test", {"_id": 1}, {"_id": 1, "data": 1}),
     ("test", {"data": 1}, {"_id": 1, "data": 1}),
     ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
     ("test", {"_id": 2}, {"_id": 2, "data": 2}),
     ("test", {"data": 2}, {"_id": 2, "data": 2}),
-    ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}),
-])
-def test_del_one(db_memory_with_data, table, filter, data):
-    result = db_memory_with_data.del_one(table, filter)
-
+    ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2})])
+def test_del_one(db_memory_with_data, table, db_filter, data):
+    result = db_memory_with_data.del_one(table, db_filter)
     assert result == {"deleted": 1}
     assert len(db_memory_with_data.db) == 1
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == 2
     assert data not in db_memory_with_data.db[table]
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {}),
     ("test", {"_id": 1}),
     ("test", {"_id": 2}),
@@ -289,15 +285,15 @@ def test_del_one(db_memory_with_data, table, filter, data):
     ("test_table", {"data": 1}),
     ("test_table", {"data": 2}),
     ("test_table", {"_id": 1, "data": 1}),
-    ("test_table", {"_id": 2, "data": 2}),
-])
-def test_del_one_with_empty_db_exception(db_memory, table, filter):
+    ("test_table", {"_id": 2, "data": 2})])
+def test_del_one_with_empty_db_exception(db_memory, table, db_filter):
     with pytest.raises(DbException) as excinfo:
-        db_memory.del_one(table, filter)
-    assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter))
+        db_memory.del_one(table, db_filter)
+    assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(db_filter))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {}),
     ("test", {"_id": 1}),
     ("test", {"_id": 2}),
@@ -311,14 +307,13 @@ def test_del_one_with_empty_db_exception(db_memory, table, filter):
     ("test_table", {"data": 1}),
     ("test_table", {"data": 2}),
     ("test_table", {"_id": 1, "data": 1}),
-    ("test_table", {"_id": 2, "data": 2}),
-])
-def test_del_one_with_empty_db_none(db_memory, table, filter):
-    result = db_memory.del_one(table, filter, fail_on_empty=False)
+    ("test_table", {"_id": 2, "data": 2})])
+def test_del_one_with_empty_db_none(db_memory, table, db_filter):
+    result = db_memory.del_one(table, db_filter, fail_on_empty=False)
+    assert result is None
 
-    assert result == None
 
-@pytest.mark.parametrize("table, filter", [
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"_id": 5}),
     ("test", {"data": 4}),
@@ -331,15 +326,15 @@ def test_del_one_with_empty_db_none(db_memory, table, filter):
     ("test_table", {"data": 1}),
     ("test_table", {"data": 2}),
     ("test_table", {"_id": 1, "data": 1}),
-    ("test_table", {"_id": 2, "data": 2}),
-])
-def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter):
+    ("test_table", {"_id": 2, "data": 2})])
+def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter):
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.del_one(table, filter)
-    assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter))
+        db_memory_with_data.del_one(table, db_filter)
+    assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(db_filter))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter", [
+
+@pytest.mark.parametrize("table, db_filter", [
     ("test", {"_id": 4}),
     ("test", {"_id": 5}),
     ("test", {"data": 4}),
@@ -352,121 +347,98 @@ def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter)
     ("test_table", {"data": 1}),
     ("test_table", {"data": 2}),
     ("test_table", {"_id": 1, "data": 1}),
-    ("test_table", {"_id": 2, "data": 2}),
-])
-def test_del_one_with_non_empty_db_none(db_memory_with_data, table, filter):
-    result = db_memory_with_data.del_one(table, filter, fail_on_empty=False)
+    ("test_table", {"_id": 2, "data": 2})])
+def test_del_one_with_non_empty_db_none(db_memory_with_data, table, db_filter):
+    result = db_memory_with_data.del_one(table, db_filter, fail_on_empty=False)
+    assert result is None
 
-    assert result == None
 
 @pytest.mark.parametrize("fail_on_empty", [
     (True),
-    (False),
-])
+    (False)])
 def test_del_one_generic_exception(db_memory_with_data, fail_on_empty):
     table = 'test'
-    filter = {}
-
+    db_filter = {}
     db_memory_with_data._find = MagicMock(side_effect=Exception())
-
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.del_one(table, filter, fail_on_empty=fail_on_empty)
+        db_memory_with_data.del_one(table, db_filter, fail_on_empty=fail_on_empty)
     assert str(excinfo.value) == empty_exception_message()
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, indata", [
-    ("test", {}, {"_id": 1, "data": 42}),
-    ("test", {}, {"_id": 3, "data": 42}),
-    ("test", {"_id": 1}, {"_id": 3, "data": 42}),
-    ("test", {"_id": 3}, {"_id": 3, "data": 42}),
-    ("test", {"data": 1}, {"_id": 3, "data": 42}),
-    ("test", {"data": 3}, {"_id": 3, "data": 42}),
-    ("test", {"_id": 1, "data": 1}, {"_id": 3, "data": 42}),
-    ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42}),
-])
-def test_replace(db_memory_with_data, table, filter, indata):
-    result = db_memory_with_data.replace(table, filter, indata)
 
+@pytest.mark.parametrize("table, _id, indata", [
+    ("test", 1, {"_id": 1, "data": 42}),
+    ("test", 1, {"_id": 1, "data": 42, "kk": 34}),
+    ("test", 1, {"_id": 1}),
+    ("test", 2, {"_id": 2, "data": 42}),
+    ("test", 2, {"_id": 2, "data": 42, "kk": 34}),
+    ("test", 2, {"_id": 2}),
+    ("test", 3, {"_id": 3, "data": 42}),
+    ("test", 3, {"_id": 3, "data": 42, "kk": 34}),
+    ("test", 3, {"_id": 3})])
+def test_replace(db_memory_with_data, table, _id, indata):
+    result = db_memory_with_data.replace(table, _id, indata)
     assert result == {"updated": 1}
     assert len(db_memory_with_data.db) == 1
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == 3
     assert indata in db_memory_with_data.db[table]
 
-@pytest.mark.parametrize("table, filter, indata", [
-    ("test", {}, {'_id': 1, 'data': 1}),
-    ("test", {}, {'_id': 2, 'data': 1}),
-    ("test", {}, {'_id': 1, 'data': 2}),
-    ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 2, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 2}),
-    ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-])
-def test_replace_without_data_exception(db_memory, table, filter, indata):
+
+@pytest.mark.parametrize("table, _id, indata", [
+    ("test", 1, {"_id": 1, "data": 42}),
+    ("test", 2, {"_id": 2}),
+    ("test", 3, {"_id": 3})])
+def test_replace_without_data_exception(db_memory, table, _id, indata):
     with pytest.raises(DbException) as excinfo:
-        db_memory.replace(table, filter, indata, fail_on_empty=True)
-    assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
+        db_memory.replace(table, _id, indata, fail_on_empty=True)
+    assert str(excinfo.value) == (replace_exception_message(_id))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, indata", [
-    ("test", {}, {'_id': 1, 'data': 1}),
-    ("test", {}, {'_id': 2, 'data': 1}),
-    ("test", {}, {'_id': 1, 'data': 2}),
-    ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 2, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 2}),
-    ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-])
-def test_replace_without_data_none(db_memory, table, filter, indata):
-    result = db_memory.replace(table, filter, indata, fail_on_empty=False)
-    assert result == None
-
-@pytest.mark.parametrize("table, filter, indata", [
-    ("test_table", {}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 2, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 2}),
-    ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-])
-def test_replace_with_data_exception(db_memory_with_data, table, filter, indata):
+
+@pytest.mark.parametrize("table, _id, indata", [
+    ("test", 1, {"_id": 1, "data": 42}),
+    ("test", 2, {"_id": 2}),
+    ("test", 3, {"_id": 3})])
+def test_replace_without_data_none(db_memory, table, _id, indata):
+    result = db_memory.replace(table, _id, indata, fail_on_empty=False)
+    assert result is None
+
+
+@pytest.mark.parametrize("table, _id, indata", [
+    ("test", 11, {"_id": 11, "data": 42}),
+    ("test", 12, {"_id": 12}),
+    ("test", 33, {"_id": 33})])
+def test_replace_with_data_exception(db_memory_with_data, table, _id, indata):
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.replace(table, filter, indata, fail_on_empty=True)
-    assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
+        db_memory_with_data.replace(table, _id, indata, fail_on_empty=True)
+    assert str(excinfo.value) == (replace_exception_message(_id))
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
-@pytest.mark.parametrize("table, filter, indata", [
-    ("test_table", {}, {'_id': 1, 'data': 1}),
-    ("test_table", {}, {'_id': 2, 'data': 1}),
-    ("test_table", {}, {'_id': 1, 'data': 2}),
-    ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
-    ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
-])
-def test_replace_with_data_none(db_memory_with_data, table, filter, indata):
-    result = db_memory_with_data.replace(table, filter, indata, fail_on_empty=False)
-    assert result == None
+
+@pytest.mark.parametrize("table, _id, indata", [
+    ("test", 11, {"_id": 11, "data": 42}),
+    ("test", 12, {"_id": 12}),
+    ("test", 33, {"_id": 33})])
+def test_replace_with_data_none(db_memory_with_data, table, _id, indata):
+    result = db_memory_with_data.replace(table, _id, indata, fail_on_empty=False)
+    assert result is None
+
 
 @pytest.mark.parametrize("fail_on_empty", [
-    (True),
-    (False),
-])
+    True,
+    False])
 def test_replace_generic_exception(db_memory_with_data, fail_on_empty):
     table = 'test'
-    filter = {}
+    _id = {}
     indata = {'_id': 1, 'data': 1}
-
     db_memory_with_data._find = MagicMock(side_effect=Exception())
-
     with pytest.raises(DbException) as excinfo:
-        db_memory_with_data.replace(table, filter, indata, fail_on_empty=fail_on_empty)
+        db_memory_with_data.replace(table, _id, indata, fail_on_empty=fail_on_empty)
     assert str(excinfo.value) == empty_exception_message()
     assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
 
+
 @pytest.mark.parametrize("table, id, data", [
     ("test", "1", {"data": 1}),
     ("test", "1", {"data": 2}),
@@ -483,20 +455,18 @@ def test_replace_generic_exception(db_memory_with_data, fail_on_empty):
     ("test_table", "1", {"data_1": 1, "data_2": 2}),
     ("test_table", "1", {"data_1": 2, "data_2": 1}),
     ("test_table", "2", {"data_1": 1, "data_2": 2}),
-    ("test_table", "2", {"data_1": 2, "data_2": 1}),
-])
+    ("test_table", "2", {"data_1": 2, "data_2": 1})])
 def test_create_with_empty_db_with_id(db_memory, table, id, data):
     data_to_insert = data
     data_to_insert['_id'] = id
-
     returned_id = db_memory.create(table, data_to_insert)
-
     assert returned_id == id
     assert len(db_memory.db) == 1
     assert table in db_memory.db
     assert len(db_memory.db[table]) == 1
     assert data_to_insert in db_memory.db[table]
 
+
 @pytest.mark.parametrize("table, id, data", [
     ("test", "4", {"data": 1}),
     ("test", "5", {"data": 2}),
@@ -513,20 +483,18 @@ def test_create_with_empty_db_with_id(db_memory, table, id, data):
     ("test_table", "4", {"data_1": 1, "data_2": 2}),
     ("test_table", "5", {"data_1": 2, "data_2": 1}),
     ("test_table", "4", {"data_1": 1, "data_2": 2}),
-    ("test_table", "5", {"data_1": 2, "data_2": 1}),
-])
+    ("test_table", "5", {"data_1": 2, "data_2": 1})])
 def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data):
     data_to_insert = data
     data_to_insert['_id'] = id
-
     returned_id = db_memory_with_data.create(table, data_to_insert)
-
     assert returned_id == id
     assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
     assert data_to_insert in db_memory_with_data.db[table]
 
+
 @pytest.mark.parametrize("table, data", [
     ("test", {"data": 1}),
     ("test", {"data": 2}),
@@ -543,20 +511,17 @@ def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data):
     ("test_table", {"data_1": 1, "data_2": 2}),
     ("test_table", {"data_1": 2, "data_2": 1}),
     ("test_table", {"data_1": 1, "data_2": 2}),
-    ("test_table", {"data_1": 2, "data_2": 1}),
-])
+    ("test_table", {"data_1": 2, "data_2": 1})])
 def test_create_with_empty_db_without_id(db_memory, table, data):
     returned_id = db_memory.create(table, data)
-
     assert len(db_memory.db) == 1
     assert table in db_memory.db
     assert len(db_memory.db[table]) == 1
-
     data_inserted = data
     data_inserted['_id'] = returned_id
-
     assert data_inserted in db_memory.db[table]
 
+
 @pytest.mark.parametrize("table, data", [
     ("test", {"data": 1}),
     ("test", {"data": 2}),
@@ -573,27 +538,22 @@ def test_create_with_empty_db_without_id(db_memory, table, data):
     ("test_table", {"data_1": 1, "data_2": 2}),
     ("test_table", {"data_1": 2, "data_2": 1}),
     ("test_table", {"data_1": 1, "data_2": 2}),
-    ("test_table", {"data_1": 2, "data_2": 1}),
-])
+    ("test_table", {"data_1": 2, "data_2": 1})])
 def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data):
     returned_id = db_memory_with_data.create(table, data)
-
     assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
     assert table in db_memory_with_data.db
     assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
-    
     data_inserted = data
     data_inserted['_id'] = returned_id
-
     assert data_inserted in db_memory_with_data.db[table]
 
+
 def test_create_with_exception(db_memory):
     table = "test"
     data = {"_id": 1, "data": 1}
-
     db_memory.db = MagicMock()
     db_memory.db.__contains__.side_effect = Exception()
-
     with pytest.raises(DbException) as excinfo:
         db_memory.create(table, data)
     assert str(excinfo.value) == empty_exception_message()