From 594c13ccb4c50e162b2b1abb1c08f2d5be4eb86c Mon Sep 17 00:00:00 2001 From: steveny2k Date: Sat, 31 Dec 2016 00:17:23 -0800 Subject: [PATCH] add function duplicatedColumnNames to address issue: hide traceback from users #3; correct unit tests --- validate.py | 12 +++++++++--- validator tests.py | 9 +++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/validate.py b/validate.py index af53cfa..ce18b22 100644 --- a/validate.py +++ b/validate.py @@ -43,14 +43,20 @@ def validate_columns(input_file): raise NoDataError("Data file contains fewer than 2 rows!") +def validate_columns_simple_output(input_file): + try: + validate_columns(input_file) + except Exception as e: + print e if __name__ == '__main__': if len(sys.argv) is 1: print "no arguments privided; running the program against some sample CSV files" - validate_columns("test_files/valid.csv") - validate_columns("test_files/rowLengthMismatch.csv") - validate_columns("test_files/tooFewColumns.csv") + validate_columns_simple_output("test_files/valid.csv") + validate_columns_simple_output("test_files/rowLengthMismatch.csv") + validate_columns_simple_output("test_files/duplicatedColumnNames.csv") + validate_columns_simple_output("test_files/tooFewColumns.csv") else: for arg in sys.argv[1:]: if os.path.isfile(arg): diff --git a/validator tests.py b/validator tests.py index d2262e2..456ef37 100644 --- a/validator tests.py +++ b/validator tests.py @@ -1,7 +1,8 @@ import unittest import sys -from validate import validate_columns +from validate import validate_columns, validate_columns_simple_output, \ + DuplicateColumnError, NoDataError, ColumnMismatchError class TestCSVValidator(unittest.TestCase): @@ -10,15 +11,15 @@ def test_empty_arg(self): validate_columns('') def test_row_lengths(self): - with self.assertRaises(ValueError): + with self.assertRaises(NoDataError): validate_columns('test_files/tooFewColumns.csv') def test_duplicated_column_names(self): - with self.assertRaises(ValueError): + with self.assertRaises(DuplicateColumnError): validate_columns('test_files/duplicatedColumnNames.csv') def test_row_length_mismatch(self): - with self.assertRaises(ValueError): + with self.assertRaises(ColumnMismatchError): validate_columns('test_files/rowLengthMismatch.csv') if __name__ == '__main__':