Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sort_with_undefined_rows(self):
first_error = Error('first', cell={'column-number': 1})
second_error = Error('second', cell={'column-number': 2})
assert sorted([second_error, first_error]) == [first_error, second_error]
def test_sort_with_undefined_rows(self):
first_error = Error('first', cell={'column-number': 1})
second_error = Error('second', cell={'column-number': 2})
assert sorted([second_error, first_error]) == [first_error, second_error]
def _compose_error_from_schema_error(error):
message_substitutions = {
'error_message': error,
}
return Error(
'schema-error',
message_substitutions=message_substitutions
)
valid = True
cell['value'] = field.cast_value(value, constraints=False)
except tableschema.exceptions.TableSchemaException:
valid = False
# Skip if valid
if valid:
continue
# Add error
message_substitutions = {
'value': '"{}"'.format(value),
'field_type': '"{}"'.format(field.type),
'field_format': '"{}"'.format(field.format),
}
error = Error(
'type-or-format-error',
cell,
message_substitutions=message_substitutions
)
errors.append(error)
# Remove cell
cells.remove(cell)
return errors
def blank_header(cells, sample=None):
errors = []
for cell in cells:
# Skip if cell have non blank header
if cell.get('header', False):
continue
# Add error
error = Error('blank-header', cell)
errors.append(error)
return errors
row_number=cells[0]['row-number'],
message=message
)
return [error]
# Check value
value = cell.get('value')
if value in self.__blacklist:
message = (
'Value "{value}" in column {column_number} on row {row_number}'
' is blacklisted'
)
message_substitutions = {
'value': value,
}
error = Error(
'blacklisted-value',
cell,
message=message,
message_substitutions=message_substitutions
)
return [error]
def check_row(self, cells):
# Get cell
cell = None
for item in cells:
if self.__column in [item['column-number'], item['header']]:
cell = item
break
# Check cell
if not cell:
message = 'Sequential value check requires column "{column}" to exist'
message = message.format(column=self.__column)
error = Error(
self.__code,
row_number=cells[0]['row-number'],
message=message
)
return [error]
# Get value
try:
value = int(cell.get('value'))
except (TypeError, ValueError):
message = (
'Sequential value check requires column "{column_number}"'
' to be an integer'
)
error = self._create_error(cell, message)
return [error]
# Check integer cutoff
try:
value = int(value)
if value in _TRUNCATED_INTEGER_VALUES:
truncated = True
except Exception:
pass
# Add error
if truncated:
message = (
'Value in column {column_number} for row {row_number}'
' is probably truncated'
)
error = Error(
'truncated-value',
cell,
message=message
)
return [error]
def _create_error(self, cell, message, message_substitutions=None):
return Error(
self.__code,
cell,
message=message,
message_substitutions=message_substitutions
)
def missing_header(cells, sample):
errors = []
for cell in copy(cells):
# Skip if header in cell
if cell.get('header') is not None:
continue
# Add error
message_substitutions = {
'field_name': '"{}"'.format(cell['field'].name),
}
error = Error(
'missing-header',
cell,
message_substitutions=message_substitutions
)
errors.append(error)
# Remove cell
cells.remove(cell)
return errors