Skip to content

Commit

Permalink
Fix NotEmptyValidator logging (#40)
Browse files Browse the repository at this point in the history
* Fix tox setup

* Add failing tests

* Handle returning a bool instead of an iterable for self.bad

* Make NotEmptyValidator return a boolean from self.bad when it has failed

* Version 0.0.17 release
  • Loading branch information
di authored Sep 18, 2017
1 parent 577cfae commit 81892a0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

__version__ = '0.0.16'
__version__ = '0.0.17'


class PyTest(TestCommand):
Expand Down
26 changes: 21 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@
# and then run "tox" from this directory.

[tox]
envlist = py27, py35
envlist = begin,py{27,35,36},lint,end

[testenv:py27]
basepython = python2.7

[testenv:py35]
basepython = python3.5

[testenv:py36]
basepython = python3.6

[testenv:lint]
commands = flake8

[testenv]
commands = coverage erase
coverage run --source=vladiate setup.py test
coverage combine
coverage report --include=*
commands = coverage run --source=vladiate setup.py test
deps =
pytest
coverage
flake8

[testenv:begin]
commands = coverage erase

[testenv:end]
commands = coverage report -m --include=*
2 changes: 1 addition & 1 deletion vladiate/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_non_empty_validator_fails():
with pytest.raises(ValidationException):
validator.validate("")

assert validator.bad == set()
assert validator.bad is True


def test_ignore_validator():
Expand Down
21 changes: 20 additions & 1 deletion vladiate/test/test_vlads.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from ..vlad import Vlad
from ..inputs import LocalFile, String
from ..validators import (
EmptyValidator, FloatValidator, SetValidator, UniqueValidator
EmptyValidator, FloatValidator, NotEmptyValidator, SetValidator,
UniqueValidator,
)


Expand Down Expand Up @@ -144,3 +145,21 @@ class TestVlad(Vlad):

assert vlad.validate()
assert vlad.missing_validators == {'Column B'}


def test_when_bad_is_non_iterable():
source = String('Column A,Column B\n,foo')

class TestVlad(Vlad):
validators = {
'Column A': [NotEmptyValidator()],
'Column B': [NotEmptyValidator()],
}

vlad = TestVlad(source=source)

assert not vlad.validate()
assert vlad.validators['Column A'][0].fail_count == 1
assert vlad.validators['Column A'][0].bad
assert vlad.validators['Column B'][0].fail_count == 0
assert not vlad.validators['Column B'][0].bad
6 changes: 3 additions & 3 deletions vladiate/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ class NotEmptyValidator(Validator):

def __init__(self):
self.fail_count = 0
self.failed = False

def validate(self, field, row={}):
if field == '':
self.failed = True
raise ValidationException("Row has emtpy field in column")

@property
def bad(self):
# Return an empty set to conform to the protocol. The 'bad' fields
# would all be empty strings anyways
return set()
return self.failed


class Ignore(Validator):
Expand Down
24 changes: 17 additions & 7 deletions vladiate/vlad.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ def _log_validator_failures(self):
" {} failed {} time(s) ({:.1%}) on field: '{}'".format(
validator.__class__.__name__, validator.fail_count,
validator.fail_count / self.line_count, field_name))
invalid = list(validator.bad)
shown = ["'{}'".format(field) for field in invalid[:99]]
hidden = ["'{}'".format(field) for field in invalid[99:]]
self.logger.error(
" Invalid fields: [{}]".format(", ".join(shown)))
if hidden:
try:
# If self.bad is iterable, it contains the fields which
# caused it to fail
invalid = list(validator.bad)
shown = [
"'{}'".format(field) for field in invalid[:99]
]
hidden = [
"'{}'".format(field)
for field in invalid[99:]
]
self.logger.error(
" ({} more suppressed)".format(len(hidden)))
" Invalid fields: [{}]".format(", ".join(shown)))
if hidden:
self.logger.error(
" ({} more suppressed)".format(len(hidden)))
except TypeError:
pass

def _log_missing_validators(self):
self.logger.error(" Missing validators for:")
Expand Down

0 comments on commit 81892a0

Please sign in to comment.