You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@madlib.apache.org by nj...@apache.org on 2018/04/05 22:48:14 UTC

[3/3] madlib git commit: UnitTests: Raise custom exception for mocked plpy error.

UnitTests: Raise custom exception for mocked plpy error.

Before this commit, all the unit tests that wanted to assert that
plpy.error was called had to assert that an Exception was raised. This
was too generic and did not distinguish between an exception coming from
the plpy mock class vs any other exception.
With this commit, we now raise a custom plpy exception so that we don't
need to assert for the equality of the error messages. Asserting for the
exception is proof enough that plpy.error was called.


Project: http://git-wip-us.apache.org/repos/asf/madlib/repo
Commit: http://git-wip-us.apache.org/repos/asf/madlib/commit/ab83c95b
Tree: http://git-wip-us.apache.org/repos/asf/madlib/tree/ab83c95b
Diff: http://git-wip-us.apache.org/repos/asf/madlib/diff/ab83c95b

Branch: refs/heads/master
Commit: ab83c95be4dde60d47c6d948172e6f77de773dbb
Parents: 00c42af
Author: Nikhil Kak <nk...@pivotal.io>
Authored: Fri Mar 30 11:29:22 2018 -0700
Committer: Nandish Jayaram <nj...@apache.org>
Committed: Thu Apr 5 15:45:50 2018 -0700

----------------------------------------------------------------------
 .../convex/test/unit_tests/plpy_mock.py_in      | 11 ++-
 .../convex/test/unit_tests/test_mlp_igd.py_in   | 34 ++++----
 .../utilities/test/unit_tests/plpy_mock.py_in   | 11 ++-
 .../test_minibatch_preprocessing.py_in          |  6 +-
 .../test/unit_tests/test_utilities.py_in        | 18 +----
 .../test/unit_tests/test_validate_args.py_in    | 84 ++++++++------------
 6 files changed, 74 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/convex/test/unit_tests/plpy_mock.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/convex/test/unit_tests/plpy_mock.py_in b/src/ports/postgres/modules/convex/test/unit_tests/plpy_mock.py_in
index 3058830..dd18649 100644
--- a/src/ports/postgres/modules/convex/test/unit_tests/plpy_mock.py_in
+++ b/src/ports/postgres/modules/convex/test/unit_tests/plpy_mock.py_in
@@ -22,7 +22,7 @@ def __init__(self):
     pass
 
 def error(message):
-    raise Exception(message)
+    raise PLPYException(message)
 
 def execute(query):
     pass
@@ -32,3 +32,12 @@ def warning(query):
 
 def info(query):
     print query
+
+
+class PLPYException(Exception):
+    def __init__(self, message):
+        super(PLPYException, self).__init__()
+        self.message = message
+
+    def __str__(self):
+        return repr(self.message)

http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/convex/test/unit_tests/test_mlp_igd.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/convex/test/unit_tests/test_mlp_igd.py_in b/src/ports/postgres/modules/convex/test/unit_tests/test_mlp_igd.py_in
index d6d1cc1..986687e 100644
--- a/src/ports/postgres/modules/convex/test/unit_tests/test_mlp_igd.py_in
+++ b/src/ports/postgres/modules/convex/test/unit_tests/test_mlp_igd.py_in
@@ -53,16 +53,16 @@ class MLPMiniBatchTestCase(unittest.TestCase):
     @patch('utilities.validate_args.table_exists', return_value=False)
     def test_mlp_preprocessor_input_table_invalid_raises_exception(
                         self, mock1):
-        with self.assertRaises(Exception):
-            self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.MLPMinibatchPreProcessor("input")
 
     @patch('utilities.validate_args.table_exists')
     def test_mlp_preprocessor_summary_invalid_raises_exception(self, mock1):
         tbl_exists_mock = Mock()
         tbl_exists_mock.side_effect = [False, True]
         self.subject.table_exists = tbl_exists_mock
-        with self.assertRaises(Exception):
-            self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.MLPMinibatchPreProcessor("input")
         tbl_exists_mock.assert_any_call("input_summary")
 
 
@@ -71,8 +71,8 @@ class MLPMiniBatchTestCase(unittest.TestCase):
         tbl_exists_mock = Mock()
         tbl_exists_mock.side_effect = [True, False]
         self.subject.table_exists = tbl_exists_mock
-        with self.assertRaises(Exception):
-            self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.MLPMinibatchPreProcessor("input")
         tbl_exists_mock.assert_any_call("input_standardization")
 
     @patch('utilities.validate_args.table_exists')
@@ -80,8 +80,8 @@ class MLPMiniBatchTestCase(unittest.TestCase):
         self.subject.table_exists = Mock()
         self.subject.input_tbl_valid = Mock()
         self.plpy_mock_execute.return_value = [{'key': 'value'}]
-        with self.assertRaises(Exception):
-            self.module = self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.module = self.subject.MLPMinibatchPreProcessor("input")
 
 
     @patch('utilities.validate_args.table_exists')
@@ -92,8 +92,8 @@ class MLPMiniBatchTestCase(unittest.TestCase):
                                                 'dependent_varname': 'value',
                                                 'foo': 'bar'}]
 
-        with self.assertRaises(Exception):
-            self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.MLPMinibatchPreProcessor("input")
 
     @patch('utilities.validate_args.table_exists')
     def test_mlp_preprocessor_indep_var_not_present_raises_exception(self, mock1):
@@ -103,8 +103,8 @@ class MLPMiniBatchTestCase(unittest.TestCase):
                                                 'dependent_varname': 'value',
                                                 'class_values': 'value'}]
 
-        with self.assertRaises(Exception):
-            self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.MLPMinibatchPreProcessor("input")
 
     @patch('utilities.validate_args.table_exists')
     def test_mlp_preprocessor_dep_var_not_present_raises_exception(self, mock1):
@@ -114,8 +114,8 @@ class MLPMiniBatchTestCase(unittest.TestCase):
                                                 'foo': 'value',
                                                 'class_values': 'value'}]
 
-        with self.assertRaises(Exception):
-            self.module = self.subject.MLPPreProcessor("input")
+        with self.assertRaises(plpy.PLPYException):
+            self.module = self.subject.MLPMinibatchPreProcessor("input")
 
     @patch('utilities.validate_args.table_exists')
     def test_mlp_preprocessor_cols_present_returns_dict(self, mock1):
@@ -126,7 +126,7 @@ class MLPMiniBatchTestCase(unittest.TestCase):
                                                 'dependent_varname': 'value',
                                                 'class_values': 'regression',
                                                 'foo': 'bar'}]
-        self.module = self.subject.MLPPreProcessor("input")
+        self.module = self.subject.MLPMinibatchPreProcessor("input")
         self.assertTrue(self.module.preprocessed_summary_dict)
         self.assertEqual(4, len(self.module.preprocessed_summary_dict))
 
@@ -145,11 +145,11 @@ class MLPMiniBatchTestCase(unittest.TestCase):
         self.assertTrue(is_mb_enabled)
 
         self.plpy_mock_execute.return_value = [{'n_x': 1, 'n_y': 2, 'n_z': 4}]
-        with self.assertRaises(Exception):
+        with self.assertRaises(plpy.PLPYException):
             self.subject.check_if_minibatch_enabled('does not matter', 'still does not matter')
 
         self.plpy_mock_execute.return_value = [{'n_x': None, 'n_y': None, 'n_z': None}]
-        with self.assertRaises(Exception):
+        with self.assertRaises(plpy.PLPYException):
             self.subject.check_if_minibatch_enabled('does not matter', 'still does not matter')
 
 

http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/utilities/test/unit_tests/plpy_mock.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/utilities/test/unit_tests/plpy_mock.py_in b/src/ports/postgres/modules/utilities/test/unit_tests/plpy_mock.py_in
index 3058830..dd18649 100644
--- a/src/ports/postgres/modules/utilities/test/unit_tests/plpy_mock.py_in
+++ b/src/ports/postgres/modules/utilities/test/unit_tests/plpy_mock.py_in
@@ -22,7 +22,7 @@ def __init__(self):
     pass
 
 def error(message):
-    raise Exception(message)
+    raise PLPYException(message)
 
 def execute(query):
     pass
@@ -32,3 +32,12 @@ def warning(query):
 
 def info(query):
     print query
+
+
+class PLPYException(Exception):
+    def __init__(self, message):
+        super(PLPYException, self).__init__()
+        self.message = message
+
+    def __str__(self):
+        return repr(self.message)

http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/utilities/test/unit_tests/test_minibatch_preprocessing.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/utilities/test/unit_tests/test_minibatch_preprocessing.py_in b/src/ports/postgres/modules/utilities/test/unit_tests/test_minibatch_preprocessing.py_in
index 5f83e87..548a6dc 100644
--- a/src/ports/postgres/modules/utilities/test/unit_tests/test_minibatch_preprocessing.py_in
+++ b/src/ports/postgres/modules/utilities/test/unit_tests/test_minibatch_preprocessing.py_in
@@ -101,7 +101,7 @@ class MiniBatchPreProcessingTestCase(unittest.TestCase):
         self.assertEqual(2, self.plpy_mock_execute.call_count)
 
     def test_minibatch_preprocessor_multiple_dep_var_raises_exception(self):
-            with self.assertRaises(Exception):
+            with self.assertRaises(plpy.PLPYException):
                 self.module.MiniBatchPreProcessor(self.default_schema_madlib,
                                                                      self.default_source_table,
                                                                      self.default_output_table,
@@ -111,7 +111,7 @@ class MiniBatchPreProcessingTestCase(unittest.TestCase):
                                                                      self.default_buffer_size)
 
     def test_minibatch_preprocessor_buffer_size_zero_fails(self):
-        with self.assertRaises(Exception):
+        with self.assertRaises(plpy.PLPYException):
             self.module.MiniBatchPreProcessor(self.default_schema_madlib,
                                                              self.default_source_table,
                                                              self.default_output_table,
@@ -193,7 +193,7 @@ class MiniBatchQueryFormatterTestCase(unittest.TestCase):
                                             dep_var_array_str)
 
     def test_get_dep_var_array_str_other_type(self):
-        with self.assertRaises(Exception):
+        with self.assertRaises(plpy.PLPYException):
             self.subject.get_dep_var_array_and_classes(self.default_dep_var,
                                                        'other')
 

http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/utilities/test/unit_tests/test_utilities.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/utilities/test/unit_tests/test_utilities.py_in b/src/ports/postgres/modules/utilities/test/unit_tests/test_utilities.py_in
index c40142d..0f38a05 100644
--- a/src/ports/postgres/modules/utilities/test/unit_tests/test_utilities.py_in
+++ b/src/ports/postgres/modules/utilities/test/unit_tests/test_utilities.py_in
@@ -92,7 +92,7 @@ class UtilitiesTestCase(unittest.TestCase):
         self.subject.input_tbl_valid = Mock()
         self.subject.output_tbl_valid = Mock()
         self.subject.is_var_valid = Mock(side_effect = [False, True, True])
-        with self.assertRaises(Exception) as context:
+        with self.assertRaises(plpy.PLPYException):
             self.subject.validate_module_input_params(self.default_source_table,
                                                       self.default_output_table,
                                                       "invalid_indep_var",
@@ -100,33 +100,25 @@ class UtilitiesTestCase(unittest.TestCase):
                                                       self.default_module,
                                                       None)
 
-        expected_exception = "unittest_module error: invalid independent_varname " \
-                             "('invalid_indep_var') for source_table (source)!"
-        self.assertEqual(expected_exception, context.exception.message)
-
     def test_validate_module_input_params_dep_var_invalid(self):
         self.subject.input_tbl_valid = Mock()
         self.subject.output_tbl_valid = Mock()
         self.subject.is_var_valid = Mock(side_effect = [True, False, True])
 
-        with self.assertRaises(Exception) as context:
+        with self.assertRaises(plpy.PLPYException):
             self.subject.validate_module_input_params(self.default_source_table,
                                                       self.default_output_table,
                                                       self.default_ind_var,
                                                       "invalid_dep_var",
                                                       self.default_module, None)
 
-        expected_exception = "unittest_module error: invalid dependent_varname " \
-                             "('invalid_dep_var') for source_table (source)!"
-        self.assertEqual(expected_exception, context.exception.message)
-
     def test_validate_module_input_params_grouping_cols_invalid(self):
         self.subject.input_tbl_valid = Mock()
         self.subject.output_tbl_valid = Mock()
         is_var_valid_mock = Mock()
         is_var_valid_mock.side_effect = [True, True, False]
         self.subject.is_var_valid = is_var_valid_mock
-        with self.assertRaises(Exception) as context:
+        with self.assertRaises(plpy.PLPYException):
             self.subject.validate_module_input_params(self.default_source_table,
                                                   self.default_output_table,
                                                   self.default_ind_var,
@@ -134,10 +126,6 @@ class UtilitiesTestCase(unittest.TestCase):
                                                   self.default_module,
                                                   'invalid_grp_col')
 
-        expected_exception = "unittest_module error: invalid grouping_cols " \
-                             "('invalid_grp_col') for source_table (source)!"
-        self.assertEqual(expected_exception, context.exception.message)
-
     def test_is_var_valid_all_nulls(self):
         self.assertEqual(False, self.subject.is_var_valid(None, None))
 

http://git-wip-us.apache.org/repos/asf/madlib/blob/ab83c95b/src/ports/postgres/modules/utilities/test/unit_tests/test_validate_args.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/utilities/test/unit_tests/test_validate_args.py_in b/src/ports/postgres/modules/utilities/test/unit_tests/test_validate_args.py_in
index 8c59256..f94ee03 100644
--- a/src/ports/postgres/modules/utilities/test/unit_tests/test_validate_args.py_in
+++ b/src/ports/postgres/modules/utilities/test/unit_tests/test_validate_args.py_in
@@ -47,80 +47,58 @@ class ValidateArgsTestCase(unittest.TestCase):
         self.module_patcher.stop()
 
     def test_input_tbl_valid_null_tbl_raises_exception(self):
-        with self.assertRaises(Exception) as context:
+        with self.assertRaises(plpy.PLPYException):
           self.subject.input_tbl_valid(None, "unittest_module")
         
-        self.assertEqual("unittest_module error: NULL/empty input"
-                          " table name!", context.exception.message)
-
     def test_input_tbl_valid_whitespaces_tbl_raises(self):
-        with self.assertRaises(Exception) as context:
+        with self.assertRaises(plpy.PLPYException):
           self.subject.input_tbl_valid("  ", "unittest_module")
         
-        self.assertEqual("unittest_module error: NULL/empty input"
-                          " table name!", context.exception.message)
-
     def test_input_tbl_valid_table_not_exists_raises(self):
-				self.subject.table_exists = Mock(return_value=False)
-				with self.assertRaises(Exception) as context:
-					self.subject.input_tbl_valid("foo", "unittest_module")
-        
-				self.assertEqual("unittest_module error: Input table"
-                          " 'foo' does not exist", context.exception.message)
+        self.subject.table_exists = Mock(return_value=False)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.input_tbl_valid("foo", "unittest_module")
 
     def test_input_tbl_valid_table_tbl_empty_raises(self):
-				self.subject.table_exists = Mock(return_value=True)
-				self.subject.table_is_empty = Mock(return_value=True)
-				with self.assertRaises(Exception) as context:
-					self.subject.input_tbl_valid("foo", "unittest_module")
-        
-				self.assertEqual("unittest_module error: Input table"
-                          " 'foo' is empty!", context.exception.message)
+        self.subject.table_exists = Mock(return_value=True)
+        self.subject.table_is_empty = Mock(return_value=True)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.input_tbl_valid("foo", "unittest_module")
 
     def test_input_tbl_valid_table_tbl_empty_passes(self):
-				self.subject.table_exists = Mock(return_value=True)
-				self.subject.table_is_empty = Mock(return_value=True)
-				self.subject.input_tbl_valid("foo", "unittest_module",
-																				check_empty = False)
+        self.subject.table_exists = Mock(return_value=True)
+        self.subject.table_is_empty = Mock(return_value=True)
+        self.subject.input_tbl_valid("foo", "unittest_module",
+                                     check_empty=False)
 
     def test_input_tbl_valid_table_passes(self):
-				self.subject.table_exists = Mock(return_value=True)
-				self.subject.table_is_empty = Mock(return_value=False)
-				self.subject.input_tbl_valid("foo", "unittest_module")
-        
+        self.subject.table_exists = Mock(return_value=True)
+        self.subject.table_is_empty = Mock(return_value=False)
+        self.subject.input_tbl_valid("foo", "unittest_module")
+
     def test_output_tbl_valid_null_tbl_raises_exception(self):
-        with self.assertRaises(Exception) as context:
-          self.subject.output_tbl_valid(None, "unittest_module")
-        
-        self.assertEqual("unittest_module error: NULL/empty output"
-                          " table name!", context.exception.message)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.output_tbl_valid(None, "unittest_module")
+
 
     def test_output_tbl_valid_whitespaces_tbl_raises_exception(self):
-        with self.assertRaises(Exception) as context:
-          self.subject.output_tbl_valid("  ", "unittest_module")
-        
-        self.assertEqual("unittest_module error: NULL/empty output"
-                          " table name!", context.exception.message)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.output_tbl_valid("  ", "unittest_module")
+
 
     def test_output_tbl_valid_null_raises(self):
-        with self.assertRaises(Exception) as context:
-          self.subject.output_tbl_valid(" null ", "unittest_module")
-        
-        self.assertEqual("unittest_module error: NULL/empty output"
-                          " table name!", context.exception.message)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.output_tbl_valid(" null ", "unittest_module")
+
 
     def test_output_tbl_valid_tbl_exists_raises(self):
-				self.subject.table_exists = Mock(return_value=True)
-				with self.assertRaises(Exception) as context:
-					self.subject.output_tbl_valid(" foo ", "unittest_module")
-        
-				self.assertEqual("unittest_module error: Output table foo"
-												" already exists.\n Drop it before calling"
-												" the function.", context.exception.message)
+        self.subject.table_exists = Mock(return_value=True)
+        with self.assertRaises(plpy.PLPYException):
+            self.subject.output_tbl_valid(" foo ", "unittest_module")
 
     def test_output_tbl_valid_table_passes(self):
-				self.subject.table_exists = Mock(return_value=False)
-				self.subject.output_tbl_valid("foo", "unittest_module")
+        self.subject.table_exists = Mock(return_value=False)
+        self.subject.output_tbl_valid("foo", "unittest_module")
 
 if __name__ == '__main__':
     unittest.main()