Skip to content

Commit 75fbe29

Browse files
committed
WL12412: Standardize count method
This task standardizes the count method for both collections and tables across all connectors. For Connector/Python only the appropriated exception message was changed. Tests were added for regression.
1 parent ca943e1 commit 75fbe29

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/mysqlx/crud.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"""Implementation of the CRUD database objects."""
3030

3131
from .dbdoc import DbDoc
32-
from .errors import ProgrammingError
32+
from .errorcode import ER_NO_SUCH_TABLE
33+
from .errors import OperationalError, ProgrammingError
3334
from .helpers import deprecated, escape, quote_identifier
3435
from .statement import (FindStatement, AddStatement, RemoveStatement,
3536
ModifyStatement, SelectStatement, InsertStatement,
@@ -366,7 +367,15 @@ def count(self):
366367
"""
367368
sql = _COUNT_QUERY.format(quote_identifier(self._schema.name),
368369
quote_identifier(self._name))
369-
return self._connection.execute_sql_scalar(sql)
370+
try:
371+
res = self._connection.execute_sql_scalar(sql)
372+
except OperationalError as err:
373+
if err.errno == ER_NO_SUCH_TABLE:
374+
raise OperationalError(
375+
"Collection '{}' does not exist in schema '{}'"
376+
"".format(self._name, self._schema.name))
377+
raise
378+
return res
370379

371380
def create_index(self, index_name, fields_desc):
372381
"""Creates a collection index.
@@ -514,7 +523,15 @@ def count(self):
514523
"""
515524
sql = _COUNT_QUERY.format(quote_identifier(self._schema.name),
516525
quote_identifier(self._name))
517-
return self._connection.execute_sql_scalar(sql)
526+
try:
527+
res = self._connection.execute_sql_scalar(sql)
528+
except OperationalError as err:
529+
if err.errno == ER_NO_SUCH_TABLE:
530+
raise OperationalError(
531+
"Table '{}' does not exist in schema '{}'"
532+
"".format(self._name, self._schema.name))
533+
raise
534+
return res
518535

519536
def is_view(self):
520537
"""Determine if the underlying object is a view or not.

tests/test_mysqlx_crud.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,19 @@ def test_array_append(self):
19071907

19081908
self.schema.drop_collection(collection_name)
19091909

1910+
def test_count(self):
1911+
collection_name = "collection_test"
1912+
collection = self.schema.create_collection(collection_name)
1913+
collection.add(
1914+
{"_id": "1", "name": "Fred", "age": 21},
1915+
{"_id": "2", "name": "Barney", "age": 28},
1916+
{"_id": "3", "name": "Wilma", "age": 42},
1917+
{"_id": "4", "name": "Betty", "age": 67},
1918+
).execute()
1919+
self.assertEqual(4, collection.count())
1920+
self.schema.drop_collection(collection_name)
1921+
self.assertRaises(mysqlx.OperationalError, collection.count)
1922+
19101923

19111924
@unittest.skipIf(tests.MYSQL_VERSION < (5, 7, 12), "XPlugin not compatible")
19121925
class MySQLxTableTests(tests.MySQLxTests):
@@ -2152,6 +2165,7 @@ def test_count(self):
21522165
self.assertTrue(table.exists_in_database())
21532166
self.assertEqual(table.count(), 1)
21542167
drop_table(self.schema, table_name)
2168+
self.assertRaises(mysqlx.OperationalError, table.count)
21552169

21562170
def test_results(self):
21572171
table_name = "{0}.test".format(self.schema_name)
@@ -2474,6 +2488,8 @@ def test_count(self):
24742488
self.table_name)
24752489
view = create_view(self.schema, self.view_name, defined_as)
24762490
self.assertEqual(view.count(), 1)
2491+
drop_view(self.schema, self.view_name)
2492+
self.assertRaises(mysqlx.OperationalError, view.count)
24772493

24782494
def test_results(self):
24792495
table_name = "{0}.{1}".format(self.schema_name, self.table_name)

0 commit comments

Comments
 (0)