Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Added support for empty and no headers sources (#195)
Browse files Browse the repository at this point in the history
* use inspect.signature instead of deprecated predecessor

* added failed test for empty source

* added failing test for no headers source

* added support for empty and no headers sources

* fixed py2 tests
  • Loading branch information
roll committed May 31, 2017
1 parent faf5ddd commit 848ae9c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Empty file added data/empty.csv
Empty file.
3 changes: 3 additions & 0 deletions data/invalid_no_headers.csv
@@ -0,0 +1,3 @@
1,english
2,中国人
3,german,extra
32 changes: 20 additions & 12 deletions goodtables/inspector.py
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import six
import inspect
import datetime
import operator
Expand Down Expand Up @@ -133,7 +134,7 @@ def __inspect_table(self, table):
# Prepare vars
errors = []
warnings = []
headers = None
headers = []
row_number = 0
fatal_error = False
checks = copy(self.__checks)
Expand All @@ -147,6 +148,8 @@ def __inspect_table(self, table):
stream.open()
sample = stream.sample
headers = stream.headers
if headers is None:
headers = [None] * len(sample[0]) if sample else []
if _filter_checks(checks, type='schema'):
if schema is None and self.__infer_schema:
schema = Schema(infer(headers, sample))
Expand Down Expand Up @@ -174,13 +177,14 @@ def __inspect_table(self, table):

# Head checks
if not fatal_error:
head_checks = _filter_checks(checks, context='head')
for check in head_checks:
if not columns:
break
check['func'](errors, columns, sample)
for error in errors:
error['row'] = None
if None not in headers:
head_checks = _filter_checks(checks, context='head')
for check in head_checks:
if not columns:
break
check['func'](errors, columns, sample)
for error in errors:
error['row'] = None

# Body checks
if not fatal_error:
Expand All @@ -191,7 +195,7 @@ def __inspect_table(self, table):
extended_rows = stream.iter(extended=True)
while True:
try:
row_number, headers, row = next(extended_rows)
row_number, _, row = next(extended_rows)
except StopIteration:
break
except Exception as exception:
Expand Down Expand Up @@ -235,6 +239,7 @@ def __inspect_table(self, table):
stop = datetime.datetime.now()

# Compose report
headers = headers if None not in headers else None
errors = errors[:self.__error_limit]
errors = _sort_errors(errors)
report = copy(extra)
Expand Down Expand Up @@ -319,10 +324,13 @@ def _prepare_checks(setup, custom, order_fields, infer_fields):

# Bind options
for check in checks:
args, _, _, _ = inspect.getargspec(check['func'])
if 'order_fields' in args:
if six.PY2:
parameters, _, _, _ = inspect.getargspec(check['func'])
else:
parameters = inspect.signature(check['func']).parameters
if 'order_fields' in parameters:
check['func'] = partial(check['func'], order_fields=order_fields)
if 'infer_fields' in args:
if 'infer_fields' in parameters:
check['func'] = partial(check['func'], infer_fields=infer_fields)

return checks
Expand Down
20 changes: 20 additions & 0 deletions tests/test_inspector.py
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import pytest
from goodtables import Inspector


Expand Down Expand Up @@ -179,3 +180,22 @@ def test_inspector_warnings_table_and_error_limit():
assert len(report['warnings']) == 2
assert 'table(s) limit' in report['warnings'][0]
assert 'error(s) limit' in report['warnings'][1]


# Empty source

def test_inspector_empty_source():
inspector = Inspector()
report = inspector.inspect('data/empty.csv')
assert report['tables'][0]['row-count'] == 0
assert report['tables'][0]['error-count'] == 0


# No headers source

def test_inspector_no_headers():
inspector = Inspector()
report = inspector.inspect('data/invalid_no_headers.csv', headers=None)
assert report['tables'][0]['row-count'] == 3
assert report['tables'][0]['error-count'] == 1
assert report['tables'][0]['errors'][0]['code'] == 'extra-value'

0 comments on commit 848ae9c

Please sign in to comment.