Repository: madlib
Updated Branches:
  refs/heads/master b2efa7d0b -> 259e00416


Utilities: Add functions for postgres character/boolean type comparison.

This commit adds two functions to check if a given type matches
one of the predefined postgres character or boolean types.


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

Branch: refs/heads/master
Commit: c902cb60fcbc436fadee8902bb24cbbd02eaad0b
Parents: b2efa7d
Author: Nikhil Kak <n...@pivotal.io>
Authored: Fri Apr 6 11:35:16 2018 -0700
Committer: Nandish Jayaram <njaya...@apache.org>
Committed: Fri Apr 13 17:16:38 2018 -0700

----------------------------------------------------------------------
 .../test/unit_tests/test_utilities.py_in        | 20 ++++++++++++++++
 .../postgres/modules/utilities/utilities.py_in  | 24 ++++++++++++++++++++
 2 files changed, 44 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/madlib/blob/c902cb60/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 0f38a05..7ad6253 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
@@ -196,5 +196,25 @@ class UtilitiesTestCase(unittest.TestCase):
                                               {'class': 'c'}]),
                          {'class': ['a', 'b', 'c']})
 
+    def test_is_psql_char_type(self):
+        self.assertTrue(self.subject.is_psql_char_type('text'))
+        self.assertTrue(self.subject.is_psql_char_type('varchar'))
+        self.assertTrue(self.subject.is_psql_char_type('character varying'))
+        self.assertTrue(self.subject.is_psql_char_type('char'))
+        self.assertTrue(self.subject.is_psql_char_type('character'))
+
+        self.assertFalse(self.subject.is_psql_char_type('c1har'))
+        self.assertFalse(self.subject.is_psql_char_type('varchar1'))
+        self.assertFalse(self.subject.is_psql_char_type('1character'))
+
+    def test_is_psql_char_type_excludes_list(self):
+        self.assertTrue(self.subject.is_psql_char_type('text', 
['varchar','char']))
+        self.assertFalse(self.subject.is_psql_char_type('text', 
['text','char']))
+        self.assertFalse(self.subject.is_psql_char_type('varchar', 'varchar'))
+
+    def test_is_psql_boolean_type(self):
+        self.assertTrue(self.subject.is_psql_boolean_type('boolean'))
+        self.assertFalse(self.subject.is_psql_boolean_type('not boolean'))
+
 if __name__ == '__main__':
     unittest.main()

http://git-wip-us.apache.org/repos/asf/madlib/blob/c902cb60/src/ports/postgres/modules/utilities/utilities.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/utilities/utilities.py_in 
b/src/ports/postgres/modules/utilities/utilities.py_in
index 324ed6d..4131a80 100644
--- a/src/ports/postgres/modules/utilities/utilities.py_in
+++ b/src/ports/postgres/modules/utilities/utilities.py_in
@@ -210,6 +210,30 @@ def is_psql_int_type(arg, exclude=None):
     return (arg in to_check_types)
 # -------------------------------------------------------------------------
 
+
+def is_psql_char_type(arg, exclude_list=[]):
+    """
+    This function checks if the given arg is one of the predefined postgres
+    character types
+    :param arg:
+    :param exclude: Optionally exclude one or more types from the comparison
+    :return: True if it is one of the character types, else False.
+
+    """
+    if not isinstance(exclude_list, list) :
+        exclude_list = [exclude_list]
+
+    text_types = set(['text', 'varchar', 'character varying', 'char', 
'character'])
+    return arg in text_types - set(exclude_list)
+
+def is_psql_boolean_type(arg):
+        """
+        This function checks if the given arg is one of type postgres boolean
+        :param arg:
+        :return: True if it is boolean, else False.
+        """
+        return arg == 'boolean'
+
 def is_string_formatted_as_array_expression(string_to_match):
     """
     Return true if the string is formatted as array[<something>], else false

Reply via email to