def test_random(self): random_records = make_random_csv_records_naive() for ir, rec in enumerate(random_records): expected_fields = rec[0] escaped_entry = rec[1] expected_warning = rec[2] test_fields, test_warning = csv_utils.split_quoted_str( escaped_entry, ',') test_fields_preserved, test_warning_preserved = csv_utils.split_quoted_str( escaped_entry, ',', True) self.assertEqual(','.join(test_fields_preserved), escaped_entry) self.assertEqual(expected_warning, test_warning) self.assertEqual(test_warning_preserved, test_warning) self.assertEqual(test_fields, csv_utils.unquote_fields(test_fields_preserved)) if not expected_warning: self.assertEqual(expected_fields, test_fields)
def test_split(self): self.assertEqual(csv_utils.split_quoted_str(' aaa, " aaa, bbb " , ccc , ddd ', ',', True)[0], [' aaa', ' " aaa, bbb " ', ' ccc ', ' ddd ']) self.assertEqual(csv_utils.split_quoted_str(' aaa, " aaa, bbb " , ccc , ddd ', ',', False)[0], [' aaa', ' aaa, bbb ', ' ccc ', ' ddd ']) test_cases = list() test_cases.append(('hello,world', (['hello', 'world'], False))) test_cases.append(('hello,"world"', (['hello', 'world'], False))) test_cases.append(('"abc"', (['abc'], False))) test_cases.append(('abc', (['abc'], False))) test_cases.append(('', ([''], False))) test_cases.append((',', (['', ''], False))) test_cases.append((',,,', (['', '', '', ''], False))) test_cases.append((',"",,,', (['', '', '', '', ''], False))) test_cases.append(('"","",,,""', (['', '', '', '', ''], False))) test_cases.append(('"aaa,bbb",', (['aaa,bbb', ''], False))) test_cases.append(('"aaa,bbb",ccc', (['aaa,bbb', 'ccc'], False))) test_cases.append(('"aaa,bbb","ccc"', (['aaa,bbb', 'ccc'], False))) test_cases.append(('"aaa,bbb","ccc,ddd"', (['aaa,bbb', 'ccc,ddd'], False))) test_cases.append((' "aaa,bbb" , "ccc,ddd" ', (['aaa,bbb', 'ccc,ddd'], False))) test_cases.append(('"aaa,bbb",ccc,ddd', (['aaa,bbb', 'ccc', 'ddd'], False))) test_cases.append(('"a"aa" a,bbb",ccc,ddd', (['"a"aa" a', 'bbb"', 'ccc', 'ddd'], True))) test_cases.append(('"aa, bb, cc",ccc",ddd', (['aa, bb, cc', 'ccc"', 'ddd'], True))) test_cases.append(('hello,world,"', (['hello', 'world', '"'], True))) test_cases.append((' aaa, " aaa, bbb " , ccc , ddd ', ([' aaa', ' aaa, bbb ', ' ccc ', ' ddd '], False))) test_cases.append((' aaa ,bbb ,ccc , ddd ', ([' aaa ', 'bbb ', 'ccc ', ' ddd '], False))) for tc in test_cases: src = tc[0] expected_dst = tc[1] warning_expected = expected_dst[1] test_dst = csv_utils.split_quoted_str(tc[0], ',') self.assertEqual(expected_dst, test_dst, msg='\nsrc: {}\ntest_dst: {}\nexpected_dst: {}\n'.format(src, test_dst, expected_dst)) test_dst_preserved = csv_utils.split_quoted_str(tc[0], ',', True) self.assertEqual(test_dst[1], test_dst_preserved[1]) self.assertEqual(','.join(test_dst_preserved[0]), tc[0], 'preserved split failure') if not warning_expected: self.assertEqual(test_dst[0], csv_utils.unquote_fields(test_dst_preserved[0]))