From 2c9794c5dc468baabe99b2f934e4bdb32e98ce54 Mon Sep 17 00:00:00 2001 From: tierno Date: Wed, 29 Apr 2020 10:24:28 +0000 Subject: [PATCH] adding create_list to dbmongo Change-Id: I72cd3035752d0d053586e966d6f1a5611f60fc09 Signed-off-by: tierno --- osm_common/dbbase.py | 12 +++++++++++- osm_common/dbmemory.py | 4 ++-- osm_common/dbmongo.py | 19 +++++++++++++++++++ osm_common/tests/test_dbbase.py | 7 +++++++ 4 files changed, 39 insertions(+), 3 deletions(-) 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 @@ class DbBase(object): 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 @@ class DbMemory(DbBase): 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 @@ class DbMemory(DbBase): 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 http import HTTPStatus from time import time, sleep from copy import deepcopy from base64 import b64decode +from uuid import uuid4 __author__ = "Alfonso Tierno " @@ -343,6 +344,24 @@ class DbMongo(DbBase): 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 @@ def test_create(db_base): 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) -- 2.25.1