Skip to content

Commit

Permalink
[Python] Refactor tests to use assertRaisesRegex
Browse files Browse the repository at this point in the history
The Python unittest.TestCase class offers the assertRaises() and
assertRaisesRegex() functions to test that an exception is raised when
callable is called [1].

Currently assertRaises(exception) is used in the Python
client tests, to assert the type of a given error. This patch refactors
usages of assertRaises() to assertRaisesRegex(exception, regex) in order
to assert the error message as well.

In Python3.2 assertRaisesRegexp has been renamed to assertRaisesRegex
(without the trailing 'p') [2].  To be able to use assertRaisesRegex
throughout the Python codebase, a compatibility class called
CompatUnitTest is added.

[1] https://docs.python.org/3/library/unittest.html#unittest.TestCase
[2] https://docs.python.org/3.9/library/unittest.html
    #unittest.TestCase.assertRaisesRegex

Change-Id: I41e2d69996ee0ed0f0418ae184d95239f2739efb
Reviewed-on: http://gerrit.cloudera.org:8080/19633
Tested-by: Kudu Jenkins
Reviewed-by: Attila Bukor <[email protected]>
  • Loading branch information
martongreber authored and attilabukor committed Apr 13, 2023
1 parent 8af7b88 commit 08bd36a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 51 deletions.
12 changes: 11 additions & 1 deletion python/kudu/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# flake8: noqa

import itertools

import contextlib
try:
import numpy as np
except ImportError:
Expand Down Expand Up @@ -97,6 +97,16 @@ def dict_iter(o):
return list(o.items())


if sys.version_info < (3, 1):
class CompatUnitTest(unittest.TestCase):
@contextlib.contextmanager
def assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs):
with self.assertRaisesRegexp(expected_exception, expected_regex, *args, **kwargs):
yield
else:
class CompatUnitTest(unittest.TestCase):
pass

integer_types = six.integer_types
if np is not None:
integer_types += (np.integer,)
58 changes: 39 additions & 19 deletions python/kudu/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# specific language governing permissions and limitations
# under the License.

from kudu.compat import unittest, long
from kudu.compat import CompatUnitTest, long
from kudu.tests.common import KuduTestBase
from kudu.client import (Partitioning,
RangePartition,
Expand All @@ -32,7 +32,7 @@
from pytz import utc


class TestClient(KuduTestBase, unittest.TestCase):
class TestClient(KuduTestBase, CompatUnitTest):

def setUp(self):
pass
Expand Down Expand Up @@ -102,7 +102,8 @@ def test_delete_table(self):
assert not self.client.table_exists(name)

# Should raise a more meaningful exception at some point
with self.assertRaises(kudu.KuduNotFound):
error_msg = 'the table does not exist: table_name: "{0}"'.format(name)
with self.assertRaisesRegex(kudu.KuduNotFound, error_msg):
self.client.delete_table(name)

def test_table_nonexistent(self):
Expand Down Expand Up @@ -476,10 +477,12 @@ def test_insert_with_auto_incrementing_column(self):
# inserts. In this case, at the second position it would like to get an int64 (the type
# of the auto-incrementing counter), therefore we get type error. (Specifying the
# auto-incremeintg counter is obviously rejected from the server side)
with self.assertRaises(TypeError):
error_msg = 'an integer is required'
with self.assertRaisesRegex(TypeError, error_msg):
op = table.new_insert((1,'text'))

with self.assertRaises(TypeError):
error_msg = 'an integer is required'
with self.assertRaisesRegex(TypeError, error_msg):
op = table.new_insert([1,'text'])

finally:
Expand Down Expand Up @@ -534,8 +537,10 @@ def test_session_flush_modes(self):
self.client.new_session(flush_mode='sync')
self.client.new_session(flush_mode='background')

with self.assertRaises(ValueError):
self.client.new_session(flush_mode='foo')
flush_mode = 'foo'
error_msg = 'Invalid flush mode: {0}'.format(flush_mode)
with self.assertRaisesRegex(ValueError, error_msg):
self.client.new_session(flush_mode=flush_mode)

def test_session_mutation_buffer_settings(self):
self.client.new_session(flush_mode=kudu.FLUSH_AUTO_BACKGROUND,
Expand All @@ -553,16 +558,20 @@ def test_session_mutation_buffer_settings(self):
def test_session_mutation_buffer_errors(self):
session = self.client.new_session(flush_mode=kudu.FLUSH_AUTO_BACKGROUND)

with self.assertRaises(OverflowError):
error_msg = 'can\'t convert negative value to unsigned int'
with self.assertRaisesRegex(OverflowError, error_msg):
session.set_mutation_buffer_max_num(-1)

with self.assertRaises(kudu.errors.KuduInvalidArgument):
error_msg = '120: watermark must be between 0 and 100 inclusive'
with self.assertRaisesRegex(kudu.errors.KuduInvalidArgument, error_msg):
session.set_mutation_buffer_flush_watermark(1.2)

with self.assertRaises(OverflowError):
error_msg = 'can\'t convert negative value to unsigned int'
with self.assertRaisesRegex(OverflowError, error_msg):
session.set_mutation_buffer_flush_interval(-1)

with self.assertRaises(OverflowError):
error_msg = 'can\'t convert negative value to size_t'
with self.assertRaisesRegex(OverflowError, error_msg):
session.set_mutation_buffer_space(-1)

def test_connect_timeouts(self):
Expand Down Expand Up @@ -599,11 +608,13 @@ def test_bad_partialrow(self):
op[key[0]] = 'test'

# Test incorrectly typed data
with self.assertRaises(TypeError):
error_msg = 'an integer is required'
with self.assertRaisesRegex(TypeError, error_msg):
op['int_val'] = 'incorrect'

# Test setting NULL in a not-null column
with self.assertRaises(kudu.errors.KuduInvalidArgument):
error_msg = 'column not nullable: key'
with self.assertRaisesRegex(kudu.errors.KuduInvalidArgument, error_msg):
op['key'] = None

def test_alter_table_rename(self):
Expand Down Expand Up @@ -821,22 +832,30 @@ def test_alter_table_auto_incrementing_column(self):
# negatives
alterer = self.client.new_table_alterer(table)
alterer.drop_column(col_name)
with self.assertRaises(KuduInvalidArgument):
error_msg = 'can\'t drop column: {0}'\
.format(Schema.get_auto_incrementing_column_name())
with self.assertRaisesRegex(KuduInvalidArgument, error_msg):
alterer.alter()

alterer = self.client.new_table_alterer(table)
alterer.add_column(col_name)
with self.assertRaises(KuduInvalidArgument):
error_msg = 'can\'t add a column with reserved name: {0}'\
.format(Schema.get_auto_incrementing_column_name())
with self.assertRaisesRegex(KuduInvalidArgument, error_msg):
alterer.alter()

alterer = self.client.new_table_alterer(table)
alterer.alter_column(col_name, "new_column_name")
with self.assertRaises(KuduInvalidArgument):
error_msg = 'can\'t change name for column: {0}'\
.format(Schema.get_auto_incrementing_column_name())
with self.assertRaisesRegex(KuduInvalidArgument, error_msg):
alterer.alter()

alterer = self.client.new_table_alterer(table)
alterer.alter_column(col_name).remove_default()
with self.assertRaises(KuduInvalidArgument):
error_msg = 'can\'t change remove default for column: {0}'\
.format(Schema.get_auto_incrementing_column_name())
with self.assertRaisesRegex(KuduInvalidArgument, error_msg):
alterer.alter()

# positives
Expand Down Expand Up @@ -869,11 +888,12 @@ def test_require_encryption(self):
def test_require_authn(self):
# Kerberos is not enabled on the cluster, so requiring
# authentication is expected to fail.
with self.assertRaises(kudu.KuduBadStatus):
error_msg = 'client requires authentication, but server does not have Kerberos enabled'
with self.assertRaisesRegex(kudu.KuduBadStatus, error_msg):
client = kudu.connect(self.master_hosts, self.master_ports,
require_authentication=True)

class TestMonoDelta(unittest.TestCase):
class TestMonoDelta(CompatUnitTest):

def test_empty_ctor(self):
delta = kudu.TimeDelta()
Expand Down
1 change: 0 additions & 1 deletion python/kudu/tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from __future__ import division

from kudu.compat import unittest
from kudu.tests.util import TestScanBase
from kudu.tests.common import KuduTestBase, TimeoutError
import kudu
Expand Down
1 change: 0 additions & 1 deletion python/kudu/tests/test_scantoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# specific language governing permissions and limitations
# under the License.

from kudu.compat import unittest
from kudu.tests.util import TestScanBase
from kudu.tests.common import KuduTestBase
import kudu
Expand Down
Loading

0 comments on commit 08bd36a

Please sign in to comment.