Add 'count' to DB utilities 59/7959/3
authordelacruzramo <pedro.delacruzramos@altran.com>
Tue, 17 Sep 2019 14:05:17 +0000 (16:05 +0200)
committerdelacruzramo <pedro.delacruzramos@altran.com>
Thu, 19 Sep 2019 12:46:03 +0000 (14:46 +0200)
Change-Id: I1a137ceeea21c70cf9358a7bc812ca4e2fe5a240
Signed-off-by: delacruzramo <pedro.delacruzramos@altran.com>
osm_common/dbbase.py
osm_common/dbmemory.py
osm_common/dbmongo.py
osm_common/tests/test_dbmemory.py

index e9152e5..1319fd8 100644 (file)
@@ -88,6 +88,16 @@ class DbBase(object):
         """
         raise DbException("Method 'get_list' not implemented")
 
+    def count(self, table, q_filter=None):
+        """
+        Count the number of entries matching q_filter
+        :param table: collection or table
+        :param q_filter: Filter
+        :return: number of entries found (can be zero)
+        :raise: DbException on error
+        """
+        raise DbException("Method 'count' not implemented")
+
     def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True):
         """
         Obtain one entry matching q_filter
index c994640..d12d03d 100644 (file)
@@ -161,6 +161,22 @@ class DbMemory(DbBase):
         except Exception as e:  # TODO refine
             raise DbException(str(e))
 
+    def count(self, table, q_filter=None):
+        """
+        Count the number of entries matching q_filter
+        :param table: collection or table
+        :param q_filter: Filter
+        :return: number of entries found (can be zero)
+        :raise: DbException on error
+        """
+        try:
+            with self.lock:
+                return sum(1 for x in self._find(table, self._format_filter(q_filter)))
+        except DbException:
+            raise
+        except Exception as e:  # TODO refine
+            raise DbException(str(e))
+
     def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True):
         """
         Obtain one entry matching q_filter
index 5334ef9..86ec7b7 100644 (file)
@@ -221,6 +221,25 @@ class DbMongo(DbBase):
         except Exception as e:  # TODO refine
             raise DbException(e)
 
+    def count(self, table, q_filter=None):
+        """
+        Count the number of entries matching q_filter
+        :param table: collection or table
+        :param q_filter: Filter
+        :return: number of entries found (can be zero)
+        :raise: DbException on error
+        """
+        try:
+            with self.lock:
+                collection = self.db[table]
+                db_filter = self._format_filter(q_filter)
+                count = collection.count(db_filter)
+            return count
+        except DbException:
+            raise
+        except Exception as e:  # TODO refine
+            raise DbException(e)
+
     def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True):
         """
         Obtain one entry matching q_filter
index e89560b..552e8f1 100644 (file)
@@ -223,6 +223,8 @@ def test_get_list(db_memory_with_many_data, db_filter, expected_ids):
     assert len(db_memory_with_many_data.db) == 1
     assert "test" in db_memory_with_many_data.db
     assert len(db_memory_with_many_data.db["test"]) == 8
+    result = db_memory_with_many_data.count("test", db_filter)
+    assert result == len(expected_ids)
 
 
 @pytest.mark.parametrize("table, db_filter, expected_data", [