adding create_list to dbmongo
Change-Id: I72cd3035752d0d053586e966d6f1a5611f60fc09
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
diff --git a/osm_common/dbbase.py b/osm_common/dbbase.py
index 614fa29..7a98c76 100644
--- a/osm_common/dbbase.py
+++ b/osm_common/dbbase.py
@@ -137,10 +137,20 @@
Add a new entry at database
:param table: collection or table
:param indata: content to be added
- :return: database id of the inserted element. Raises a DbException on error
+ :return: database '_id' of the inserted element. Raises a DbException on error
"""
raise DbException("Method 'create' not implemented")
+ def create_list(self, table, indata_list):
+ """
+ Add several entries at once
+ :param table: collection or table
+ :param indata_list: list of elements to insert. Each element must be a dictionary.
+ An '_id' key based on random uuid is added at each element if missing
+ :return: list of inserted '_id's. Exception on error
+ """
+ raise DbException("Method 'create_list' not implemented")
+
def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None):
"""
Modifies an entry at database
diff --git a/osm_common/dbmemory.py b/osm_common/dbmemory.py
index d3b4019..196d5d9 100644
--- a/osm_common/dbmemory.py
+++ b/osm_common/dbmemory.py
@@ -414,7 +414,7 @@
Add a new entry at database
:param table: collection or table
:param indata: content to be added
- :return: database id of the inserted element. Raises a DbException on error
+ :return: database '_id' of the inserted element. Raises a DbException on error
"""
try:
id = indata.get("_id")
@@ -434,7 +434,7 @@
Add a new entry at database
:param table: collection or table
:param indata_list: list content to be added
- :return: database ids of the inserted element. Raises a DbException on error
+ :return: list of inserted 'id's. Raises a DbException on error
"""
try:
_ids = []
diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py
index 16da6e7..e40d0e4 100644
--- a/osm_common/dbmongo.py
+++ b/osm_common/dbmongo.py
@@ -23,6 +23,7 @@
from time import time, sleep
from copy import deepcopy
from base64 import b64decode
+from uuid import uuid4
__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
@@ -343,6 +344,24 @@
except Exception as e: # TODO refine
raise DbException(e)
+ def create_list(self, table, indata_list):
+ """
+ Add several entries at once
+ :param table: collection or table
+ :param indata_list: content list to be added.
+ :return: the list of inserted '_id's. Exception on error
+ """
+ try:
+ for item in indata_list:
+ if item.get("_id") is None:
+ item["_id"] = str(uuid4())
+ with self.lock:
+ collection = self.db[table]
+ data = collection.insert_many(indata_list)
+ return data.inserted_ids
+ except Exception as e: # TODO refine
+ raise DbException(e)
+
def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None):
"""
Modifies an entry at database
diff --git a/osm_common/tests/test_dbbase.py b/osm_common/tests/test_dbbase.py
index ca1336d..1abd1c7 100644
--- a/osm_common/tests/test_dbbase.py
+++ b/osm_common/tests/test_dbbase.py
@@ -71,6 +71,13 @@
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
+def test_create_list(db_base):
+ with pytest.raises(DbException) as excinfo:
+ db_base.create_list(None, None)
+ assert str(excinfo.value).startswith(exception_message("Method 'create_list' not implemented"))
+ assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
+
+
def test_del_list(db_base):
with pytest.raises(DbException) as excinfo:
db_base.del_list(None, None)