Skip to content

Commit

Permalink
Rewrite allowances using allow_iter() and allow_each() to simplify
Browse files Browse the repository at this point in the history
the implementation, make behavior consistent for all allowances,
and provide a basis for user-defined allowances.  Also remove old
code and provide backwards compatible patches for api_dev1 sub-module.

Note: This commit is way too big.  It would have been better to make
a dozen smaller edits.
  • Loading branch information
shawnbrown committed Jul 25, 2016
1 parent f8bca62 commit 9fc92cd
Show file tree
Hide file tree
Showing 7 changed files with 1,008 additions and 1,053 deletions.
26 changes: 24 additions & 2 deletions datatest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from .sources.excel import ExcelSource
from .sources.pandas import PandasSource

from .compare import CompareSet
from .compare import CompareDict

from .error import DataError

from .differences import Extra
Expand All @@ -19,8 +22,15 @@
from .differences import NotProperSubset
from .differences import NotProperSuperset

from .compare import CompareSet
from .compare import CompareDict
from .allow import allow_iter
from .allow import allow_each
from .allow import allow_only
from .allow import allow_any
from .allow import allow_extra
from .allow import allow_missing
from .allow import allow_limit
from .allow import allow_deviation
from .allow import allow_percent_deviation

from .runner import mandatory
from .runner import skip
Expand All @@ -42,6 +52,7 @@
'BaseSource',
'SqliteSource',
'CsvSource',
'AdapterSource',
'MultiSource',
'ExcelSource',
'PandasSource',
Expand All @@ -60,6 +71,17 @@
'Invalid',
'Deviation',

# Allowance context mangers.
'allow_iter',
'allow_each',
'allow_only',
'allow_any',
'allow_extra',
'allow_missing',
'allow_limit',
'allow_deviation',
'allow_percent_deviation',

# Test runner and command-line program.
'mandatory',
'skip',
Expand Down
47 changes: 46 additions & 1 deletion datatest/__past__/api_dev1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import absolute_import
import inspect
import datatest
from datatest.utils import itertools
from datatest import DataTestCase


Expand All @@ -17,7 +18,6 @@
DataTestCase.assertDataNotRegex = DataTestCase.assertSubjectNotRegex
datatest.DataAssertionError = datatest.DataError


_wrapped_find_data_source = DataTestCase._find_data_source
@staticmethod
def _find_data_source(name):
Expand Down Expand Up @@ -55,9 +55,54 @@ def _normalize_required(self, required, method, *args, **kwds):
DataTestCase._normalize_required = _normalize_required


# This method was removed entirely.
def _assertDataCount(self, column, keys, required=None, msg=None, **kwds_filter):
subject_dict = self.subject.count(column, keys, **kwds_filter)
required = self._normalize_required(required, 'sum', column, keys, **kwds_filter)
msg = msg or 'row counts different than {0!r} sums'.format(column)
self.assertEqual(subject_dict, required, msg)
DataTestCase.assertDataCount = _assertDataCount


# Function signature and behavior was changed.
def _allowAny(self, number=None, msg=None, **kwds_filter):
if number:
return datatest.allow_limit(number, msg, **kwds_filter)
return datatest.allow_any(msg, **kwds_filter)
DataTestCase.allowAny = _allowAny


# Function signature and behavior was changed.
def _allowMissing(self, number=None, msg=None):
def function(iterable):
t1, t2 = itertools.tee(iterable)
not_allowed = []
count = 0
for x in t1:
if not isinstance(x, datatest.Missing):
not_allowed.append(x)
else:
count += 1
if count > number:
return t2 # <- EXIT! Exceeds limit, return all.
return not_allowed
return datatest.allow_iter(function, msg)
DataTestCase.allowMissing = _allowMissing


# Function signature and behavior was changed.
def _allowExtra(self, number=None, msg=None):
def function(iterable):
t1, t2 = itertools.tee(iterable)
not_allowed = []
count = 0
for x in t1:
if not isinstance(x, datatest.Extra):
not_allowed.append(x)
else:
count += 1
if count > number:
return t2 # <- EXIT! Exceeds limit, return all.
return not_allowed
return datatest.allow_iter(function, msg)
DataTestCase.allowExtra = _allowExtra

0 comments on commit 9fc92cd

Please sign in to comment.