From c0d08f9c0db8071428f1358e186e83826d68db5e Mon Sep 17 00:00:00 2001 From: Joe Jevnik Date: Fri, 17 Jun 2016 19:28:46 -0400 Subject: [PATCH] TST: Adds wildcard object for assert_equal --- zipline/testing/core.py | 28 +++++++++++++++++++++++++- zipline/testing/predicates.py | 37 ++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/zipline/testing/core.py b/zipline/testing/core.py index dce7815851..1456d7cd24 100644 --- a/zipline/testing/core.py +++ b/zipline/testing/core.py @@ -12,6 +12,7 @@ import os from os.path import abspath, dirname, join, realpath import shutil +from sys import _getframe import tempfile from logbook import TestHandler @@ -23,7 +24,7 @@ from six.moves import filter, map from sqlalchemy import create_engine from testfixtures import TempDirectory -from toolz import concat +from toolz import concat, curry from zipline.assets import AssetFinder, AssetDBWriter from zipline.assets.synthetic import make_simple_equity_info @@ -860,6 +861,7 @@ def __str__(self): ) +@nottest def subtest(iterator, *_names): """ Construct a subtest in a unittest. @@ -1374,3 +1376,27 @@ def patched_read_csv(filepath_or_buffer, *args, **kwargs): with patch.object(module, 'read_csv', patched_read_csv): yield + + +@curry +def ensure_doctest(f, name=None): + """Ensure that an object gets doctested. This is useful for instances + of objects like curry or partial which are not discovered by default. + + Parameters + ---------- + f : any + The thing to doctest. + name : str, optional + The name to use in the doctest function mapping. If this is None, + Then ``f.__name__`` will be used. + + Returns + ------- + f : any + ``f`` unchanged. + """ + _getframe(2).f_globals.setdefault('__test__', {})[ + f.__name__ if name is None else name + ] = f + return f diff --git a/zipline/testing/predicates.py b/zipline/testing/predicates.py index 3bc35346f6..e346b0491d 100644 --- a/zipline/testing/predicates.py +++ b/zipline/testing/predicates.py @@ -39,12 +39,47 @@ from toolz import dissoc, keyfilter import toolz.curried.operator as op +from zipline.testing.core import ensure_doctest from zipline.dispatch import dispatch from zipline.lib.adjustment import Adjustment -from zipline.utils.functional import dzip_exact +from zipline.utils.functional import dzip_exact, instance from zipline.utils.math_utils import tolerant_equals +@instance +@ensure_doctest +class wildcard(object): + """An object that compares equal to any other object. + + This is useful when using :func:`~zipline.testing.predicates.assert_equal` + with a large recursive structure and some fields to be ignored. + + Examples + -------- + >>> wildcard == 5 + True + >>> wildcard == 'ayy' + True + + # reflected + >>> 5 == wildcard + True + >>> 'ayy' == wildcard + True + """ + @staticmethod + def __eq__(other): + return True + + @staticmethod + def __ne__(other): + return False + + def __repr__(self): + return '<%s>' % type(self).__name__ + __str__ = __repr__ + + def keywords(func): """Get the argument names of a function