1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-15 01:24:29 +02:00

add csv utf-8 bom tests

This commit is contained in:
Kelly Brazil
2022-10-25 11:46:31 -07:00
parent 888b6bd6d5
commit fd5cbbb4d5
5 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1 @@
[{"col1":"a","col2":"b","col3":"c"},{"col1":"x","col2":"y","col3":"z"},{"col1":"abc","col2":"def","col3":"ghi"},{"col1":"1","col2":"2","col3":"3"},{"col1":"foo","col2":"bar","col3":"baz"}]

View File

@ -0,0 +1,6 @@
col1,col2,col3
a,b,c
x,y,z
abc,def,ghi
1,2,3
foo,bar,baz
1 col1 col2 col3
2 a b c
3 x y z
4 abc def ghi
5 1 2 3
6 foo bar baz

View File

@ -0,0 +1 @@
[{"col1":"a","col2":"b","col3":"c"},{"col1":"x","col2":"y","col3":"z"},{"col1":"abc","col2":"def","col3":"ghi"},{"col1":"1","col2":"2","col3":"3"},{"col1":"foo","col2":"bar","col3":"baz"}]

View File

@ -39,6 +39,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.csv'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.csv'), 'r', encoding='utf-8') as f:
generic_csv_doubleqouted = f.read() generic_csv_doubleqouted = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-utf-8-bom.csv'), 'r', encoding='utf-8') as f:
generic_csv_utf8_bom = f.read()
# output # output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats.json'), 'r', encoding='utf-8') as f:
generic_csv_biostats_json = json.loads(f.read()) generic_csv_biostats_json = json.loads(f.read())
@ -70,6 +73,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.json'), 'r', encoding='utf-8') as f:
generic_csv_doubleqouted_json = json.loads(f.read()) generic_csv_doubleqouted_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-utf-8-bom.json'), 'r', encoding='utf-8') as f:
generic_csv_utf8_bom_json = json.loads(f.read())
def test_csv_nodata(self): def test_csv_nodata(self):
""" """
@ -131,12 +137,18 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(jc.parsers.csv.parse(self.generic_csv_insurance, quiet=True), self.generic_csv_insurance_json) self.assertEqual(jc.parsers.csv.parse(self.generic_csv_insurance, quiet=True), self.generic_csv_insurance_json)
def test_doubleqouted(self): def test_csv_doubleqouted(self):
""" """
Test 'csv-doubleqouted.csv' file Test 'csv-doubleqouted.csv' file
""" """
self.assertEqual(jc.parsers.csv.parse(self.generic_csv_doubleqouted, quiet=True), self.generic_csv_doubleqouted_json) self.assertEqual(jc.parsers.csv.parse(self.generic_csv_doubleqouted, quiet=True), self.generic_csv_doubleqouted_json)
def test_csv_utf8_bom(self):
"""
Test 'csv-utf-8-bom.csv' file to ensure the first column is correct if UTF-8 BOM bytes are present
"""
self.assertEqual(jc.parsers.csv.parse(self.generic_csv_utf8_bom, quiet=True), self.generic_csv_utf8_bom_json)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -44,6 +44,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.csv'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.csv'), 'r', encoding='utf-8') as f:
generic_csv_doubleqouted = f.read() generic_csv_doubleqouted = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-utf-8-bom.csv'), 'r', encoding='utf-8') as f:
generic_csv_utf8_bom = f.read()
# output # output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats-streaming.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats-streaming.json'), 'r', encoding='utf-8') as f:
generic_csv_biostats_streaming_json = json.loads(f.read()) generic_csv_biostats_streaming_json = json.loads(f.read())
@ -75,6 +78,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted-streaming.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted-streaming.json'), 'r', encoding='utf-8') as f:
generic_csv_doublequoted_streaming_json = json.loads(f.read()) generic_csv_doublequoted_streaming_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-utf-8-bom-streaming.json'), 'r', encoding='utf-8') as f:
generic_csv_utf8_bom_streaming_json = json.loads(f.read())
def test_csv_s_nodata(self): def test_csv_s_nodata(self):
""" """
@ -153,6 +159,12 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(list(jc.parsers.csv_s.parse(self.generic_csv_doubleqouted.splitlines(), quiet=True)), self.generic_csv_doublequoted_streaming_json) self.assertEqual(list(jc.parsers.csv_s.parse(self.generic_csv_doubleqouted.splitlines(), quiet=True)), self.generic_csv_doublequoted_streaming_json)
def test_csv_s_utf8_bom(self):
"""
Test 'csv-utf-8-bom.csv' file to ensure the first column is correct if UTF-8 BOM bytes are present
"""
self.assertEqual(list(jc.parsers.csv_s.parse(self.generic_csv_utf8_bom.splitlines(), quiet=True)), self.generic_csv_utf8_bom_streaming_json)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()