Commit 5405bab4 authored by Christophe Benz's avatar Christophe Benz
Browse files

Use specified error, not exception

parent 95a72f97
......@@ -3,7 +3,6 @@ from io import BytesIO
import pytest
from openpyxl import Workbook
from validata_core import Validator
......@@ -66,6 +65,13 @@ def validate_csv_text(**options):
return Validator().validate(scheme='text', format='csv', **options)
def test_empty_file(schema_abc):
source = ""
report = validate_csv_text(source=source, schema=schema_abc)
assert report["tables"][0]["errors"][0]["code"] == "unknown-csv-dialect"
assert report["tables"][0]["errors"][0]["message"] == "Structure CSV non reconnue"
def test_valid_delimiter(schema_abc):
source = """A,B,C
a,b,c"""
......
......@@ -31,7 +31,7 @@ def replace_at(seq, index, item):
)
def add_error(report, table_index, error):
def prepend_error(report, table_index, error):
return update_in(report, ["tables"], lambda tables: list(
replace_at(tables, table_index, thread_first(
tables[table_index],
......@@ -147,24 +147,6 @@ def amend_report(report):
return report
class ValidataSourceError(Exception):
"""Exception relative to source analysis"""
class MissingHeaderError(ValidataSourceError):
"""Error raised when goodtables return no 'headers' information in report
Validata can't go further in this case
"""
pass
class CantDetectDialectError(ValidataSourceError):
"""Error raised when goodtables can't get csv dialect
Validata can't go further in this case
"""
pass
class Validator:
def load_schema(self, source):
"""Return a `tableschema.Schema` instance from `source`."""
......@@ -206,10 +188,6 @@ class Validator:
options = {**options, "custom_loaders": loaders.custom_loaders}
report = inspector.inspect(source=source, schema=schema_descriptor, **options)
# Goodtables return no 'headers' information :-(
if not report['tables'][0].get('headers'):
raise MissingHeaderError()
if report['tables'][0].get('format') == "csv" and not any(
get_in(['errors', err['code'], 'type'], spec, default=None) == 'source'
for err in report['tables'][0]['errors']
......@@ -217,17 +195,19 @@ class Validator:
standard_csv_delimiter = ","
dialect = csv_helpers.detect_dialect(source, **options)
if dialect is None:
raise CantDetectDialectError()
detected_delimiter = dialect.delimiter
if detected_delimiter != standard_csv_delimiter:
error = goodtables.Error(
code='invalid-column-delimiter',
message_substitutions={
"detected": detected_delimiter,
"expected": standard_csv_delimiter,
},
)
report = add_error(report, table_index=0, error=dict(error))
error = goodtables.Error(code='unknown-csv-dialect')
report = prepend_error(report, table_index=0, error=dict(error))
else:
detected_delimiter = dialect.delimiter
if detected_delimiter != standard_csv_delimiter:
error = goodtables.Error(
code='invalid-column-delimiter',
message_substitutions={
"detected": detected_delimiter,
"expected": standard_csv_delimiter,
},
)
report = prepend_error(report, table_index=0, error=dict(error))
# Translate error messages
report = improve_messages(report, schema_descriptor)
......
......@@ -242,6 +242,12 @@ def year_interval_value(err, schema):
# -> Error message is stored in 'message' key
def unknown_csv_dialect(err, schema):
"""unknown-csv-dialect"""
err['message'] = 'Structure CSV non reconnue'
return u_err(err, 'Structure CSV non reconnue', err['message'])
def invalid_column_delimiter(err, schema):
"""invalid-column-delimiter"""
md = err['message-data']
......@@ -314,6 +320,7 @@ ERROR_MESSAGE_FUNC = {
'unique-constraint': unique_constraint,
# Validata pre-checks
'unknown-csv-dialect': unknown_csv_dialect,
'extra-headers': extra_headers,
'invalid-column-delimiter': invalid_column_delimiter,
'missing-headers': missing_headers,
......@@ -355,6 +362,7 @@ ERROR_MESSAGE_DEFAULT_TITLE = {
'unique-constraint': 'doublons',
# Validata pre-checks
'unknown-csv-dialect': 'structure CSV non reconnue',
'extra-headers': 'en-tête surnuméraire',
'invalid-column-delimiter': 'délimiteur de colonne incorrect',
'missing-headers': 'en-tête manquant',
......
{
"version": "0.0.1",
"errors": {
"unknown-csv-dialect": {
"name": "Unknown CSV dialect",
"type": "source",
"context": "table"
},
"invalid-column-delimiter": {
"name": "Wrong column delimiter",
"type": "source",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment