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 2019/04/18 23:00:23 UTC
[madlib] 02/04: Utilities: Add unit tests for
create_cols_from_array_sql_string()
This is an automated email from the ASF dual-hosted git repository.
njayaram pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/madlib.git
commit 3bf6da956e05a3df052807d3e5c784681287e0cb
Author: Nandish Jayaram <nj...@apache.org>
AuthorDate: Thu Apr 11 17:13:41 2019 -0700
Utilities: Add unit tests for create_cols_from_array_sql_string()
JIRA: MADLIB-1315
Closes #370
Co-authored-by: Ekta Khanna <ek...@pivotal.io>
---
.../utilities/test/unit_tests/test_utilities.py_in | 125 +++++++++++++++++++++
1 file changed, 125 insertions(+)
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 2d2c481..b884eec 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
@@ -247,5 +247,130 @@ class UtilitiesTestCase(unittest.TestCase):
self.assertTrue(s.is_valid_psql_type('boolean[]', s.INTEGER | s.ANY_ARRAY))
self.assertFalse(s.is_valid_psql_type('boolean', s.ANY_ARRAY))
+ def test_create_cols_from_array_sql_string_empty_pylist(self):
+ utils = self.subject
+ self.py_list = None
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'estimated_col'
+ self.coltype = 'dummy'
+ self.has_one_ele = True
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertEqual(out_sql, 'sqlcol[1]+1 AS estimated_col')
+ self.has_one_ele = False
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertEqual(out_sql, 'sqlcol AS estimated_col')
+
+ def test_create_cols_from_array_sql_string_one_ele(self):
+ utils = self.subject
+ self.py_list = ['cat', 'dog']
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'estimated_pred'
+ self.coltype = 'TEXT'
+ self.has_one_ele = True
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertTrue(out_sql, "(ARRAY['cat','dog'])[sqlcol[1]+1]::TEXT AS estimated_pred")
+
+ def test_create_cols_from_array_sql_string_one_ele_with_NULL(self):
+ utils = self.subject
+ self.py_list = [None, 1, 2]
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'estimated_pred'
+ self.coltype = 'INTEGER'
+ self.has_one_ele = True
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertEqual(out_sql, "(ARRAY[ NULL,1,2 ]::INTEGER[])[sqlcol[1]+1]::INTEGER AS estimated_pred")
+
+ def test_create_cols_from_array_sql_string_one_ele_with_many_NULL(self):
+ utils = self.subject
+ self.py_list = [None, 'cat', 'dog', None, None]
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'estimated_pred'
+ self.coltype = 'TEXT'
+ self.has_one_ele = True
+ with self.assertRaises(plpy.PLPYException):
+ utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+
+ def test_create_cols_from_array_sql_string_many_ele(self):
+ utils = self.subject
+ self.py_list = ['cat', 'dog']
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'prob'
+ self.coltype = 'TEXT'
+ self.has_one_ele = False
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertEqual(out_sql, "CAST(sqlcol[1] AS TEXT) AS \"prob_cat\", CAST(sqlcol[2] AS TEXT) AS \"prob_dog\"")
+
+ def test_create_cols_from_array_sql_string_many_ele_with_NULL(self):
+ utils = self.subject
+ self.py_list = [None, 'cat', 'dog']
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'prob'
+ self.coltype = 'TEXT'
+ self.has_one_ele = False
+ out_sql = utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+ self.assertEqual(out_sql, "CAST(sqlcol[1] AS TEXT) AS \"prob_NULL\", CAST(sqlcol[2] AS TEXT) AS \"prob_cat\", CAST(sqlcol[3] AS TEXT) AS \"prob_dog\"")
+
+ def test_create_cols_from_array_sql_string_many_ele_with_many_NULL(self):
+ utils = self.subject
+ self.py_list = [None, 'cat', 'dog', None, None]
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'prob'
+ self.coltype = 'TEXT'
+ self.has_one_ele = False
+ with self.assertRaises(plpy.PLPYException):
+ utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+
+ def test_create_cols_from_array_sql_string_invalid_sql_array(self):
+ utils = self.subject
+ self.py_list = ['cat', 'dog']
+ self.sql_array_col = None
+ self.colname = 'prob'
+ self.coltype = 'TEXT'
+ self.has_one_ele = False
+ with self.assertRaises(plpy.PLPYException):
+ utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+
+ def test_create_cols_from_array_sql_string_invalid_colname(self):
+ utils = self.subject
+ self.py_list = ['cat', 'dog']
+ self.sql_array_col = 'sqlcol'
+ self.colname = ''
+ self.coltype = 'TEXT'
+ self.has_one_ele = False
+ with self.assertRaises(plpy.PLPYException):
+ utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+
+ def test_create_cols_from_array_sql_string_invalid_coltype(self):
+ utils = self.subject
+ self.py_list = ['cat', 'dog']
+ self.sql_array_col = 'sqlcol'
+ self.colname = 'prob'
+ self.coltype = ''
+ self.has_one_ele = False
+ with self.assertRaises(plpy.PLPYException):
+ utils.create_cols_from_array_sql_string(
+ self.py_list, self.sql_array_col, self.colname, self.coltype,
+ self.has_one_ele, "dummy_module")
+
if __name__ == '__main__':
unittest.main()