1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-06 22:32:54 +02:00

working tests

This commit is contained in:
Kelly Brazil
2021-10-25 10:06:01 -07:00
parent 50a3b34016
commit acac039994
3 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1 @@
[{"Name":"Alex","Sex":"M","Age":"41","Height (in)":"74","Weight (lbs)":"170"},{"Name":"Bert","Sex":"M","Age":"42","Height (in)":"68","Weight (lbs)":"166"},{"Name":"Carl","Sex":"M","Age":"32","Height (in)":"70","Weight (lbs)":"155"},{"Name":"Dave","Sex":"M","Age":"39","Height (in)":"72","Weight (lbs)":"167"},{"Name":"Elly","Sex":"F","Age":"30","Height (in)":"66","Weight (lbs)":"124"},{"Name":"Fran","Sex":"F","Age":"33","Height (in)":"66","Weight (lbs)":"115"},{"Name":"Gwen","Sex":"F","Age":"26","Height (in)":"64","Weight (lbs)":"121"},{"Name":"Hank","Sex":"M","Age":"30","Height (in)":"71","Weight (lbs)":"158"},{"Name":"Ivan","Sex":"M","Age":"53","Height (in)":"72","Weight (lbs)":"175"},{"Name":"Jake","Sex":"M","Age":"32","Height (in)":"69","Weight (lbs)":"143"},{"Name":"Kate","Sex":"F","Age":"47","Height (in)":"69","Weight (lbs)":"139"},{"Name":"Luke","Sex":"M","Age":"34","Height (in)":"72","Weight (lbs)":"163"},{"Name":"Myra","Sex":"F","Age":"23","Height (in)":"62","Weight (lbs)":"98"},{"Name":"Neil","Sex":"M","Age":"36","Height (in)":"75","Weight (lbs)":"160"},{"Name":"Omar","Sex":"M","Age":"38","Height (in)":"70","Weight (lbs)":"145"},{"Name":"Page","Sex":"F","Age":"31","Height (in)":"67","Weight (lbs)":"135"},{"Name":"Quin","Sex":"M","Age":"29","Height (in)":"71","Weight (lbs)":"176"},{"Name":"Ruth","Sex":"F","Age":"28","Height (in)":"65","Weight (lbs)":"131"}]

File diff suppressed because one or more lines are too long

147
tests/test_csv_s.py Normal file
View File

@ -0,0 +1,147 @@
import os
import json
import unittest
import jc.parsers.csv_s
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# To create streaming output use:
# $ cat file.csv | jc --csv-s | jello -c > csv-file-streaming.json
class MyTests(unittest.TestCase):
def setUp(self):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_biostats = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-cities.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_cities = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-deniro.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_deniro = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-example.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_example = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-flyrna.tsv'), 'r', encoding='utf-8') as f:
self.generic_csv_flyrna = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-flyrna2.tsv'), 'r', encoding='utf-8') as f:
self.generic_csv_flyrna2 = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-homes-pipe.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_homes_pipe = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-homes.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_homes = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-insurance.csv'), 'r', encoding='utf-8') as f:
self.generic_csv_insurance = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats-streaming.json'), 'r', encoding='utf-8') as f:
self.generic_csv_biostats_streaming_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-cities-streaming.json'), 'r', encoding='utf-8') as f:
self.generic_csv_cities_streaming_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-deniro-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_deniro_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-example-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_example_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-flyrna-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_flyrna_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-flyrna2-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_flyrna2_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-homes-pipe-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_homes_pipe_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-homes-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_homes_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-insurance-streaming.json'), 'r', encoding='utf-8') as f:
# self.generic_csv_insurance_json = json.loads(f.read())
def test_csv_s_nodata(self):
"""
Test CSV parser with no data
"""
self.assertEqual(list(jc.parsers.csv_s.parse('', quiet=True)), [])
# no test for unparsable rows yet - CSV library fails right away if it can't decode the data
# def test_csv_unparsable(self):
# data = 'unparsable data'
# g = jc.parsers.csv_s.parse(data.splitlines(), quiet=True)
# with self.assertRaises(ParseError):
# list(g)
# def test_vmstat_centos_7_7(self):
# """
# Test 'vmstat' on Centos 7.7
# """
# self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat.splitlines(), quiet=True)), self.centos_7_7_vmstat_streaming_json)
def test_csv_s_biostats(self):
"""
Test 'biostats.csv' file
"""
self.assertEqual(list(jc.parsers.csv_s.parse(self.generic_csv_biostats.splitlines(), quiet=True)), self.generic_csv_biostats_streaming_json)
def test_csv_s_cities(self):
"""
Test 'cities.csv' file
"""
self.assertEqual(list(jc.parsers.csv_s.parse(self.generic_csv_cities.splitlines(), quiet=True)), self.generic_csv_cities_streaming_json)
# def test_csv_deniro(self):
# """
# Test 'deniro.csv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_deniro, quiet=True), self.generic_csv_deniro_json)
# def test_csv_example(self):
# """
# Test 'example.csv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_example, quiet=True), self.generic_csv_example_json)
# def test_csv_flyrna(self):
# """
# Test 'flyrna.tsv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_flyrna, quiet=True), self.generic_csv_flyrna_json)
# def test_csv_flyrna2(self):
# """
# Test 'flyrna2.tsv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_flyrna2, quiet=True), self.generic_csv_flyrna2_json)
# def test_csv_homes_pipe(self):
# """
# Test 'homes-pipe.csv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_homes_pipe, quiet=True), self.generic_csv_homes_pipe_json)
# def test_csv_homes(self):
# """
# Test 'homes.csv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_homes, quiet=True), self.generic_csv_homes_json)
# def test_csv_insurance(self):
# """
# Test 'insurance.csv' file
# """
# self.assertEqual(jc.parsers.csv.parse(self.generic_csv_insurance, quiet=True), self.generic_csv_insurance_json)
if __name__ == '__main__':
unittest.main()