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
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")
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 = []
from time import time, sleep
from copy import deepcopy
from base64 import b64decode
+from uuid import uuid4
__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
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
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)