Skip to main content

Library that eases partial matching of objects.

Project description

Any Valid
=========

The AnyValid class is a wrapper class to be used with, for example,
`formencode.validators <http://www.formencode.org/en/latest/modules/validators.html#module-formencode.validators>`__,
that lets you partially specify what kind of objects it should match. To
see the usefulness of this, it might be easier to first have a look at
mock's ANY object (`from unittest.mock's
documentation <http://docs.python.org/3/library/unittest.mock.html#any>`__):

Sometimes you may need to make assertions about some of the
arguments in a call to mock, but either not care about some of the
arguments or want to pull them individually out of call\_args and
make more complex assertions on them.

To ignore certain arguments you can pass in objects that compare
equal to everything. Calls to assert\_called\_with() and
assert\_called\_once\_with() will then succeed no matter what was
passed in.

.. code:: python

>>
>> mock = Mock(return_value=None)
>> mock('foo', bar=object())
>> mock.assert_called_once_with('foo', bar=ANY)

ANY can also be used in comparisons with call lists like
mock\_calls:

.. code:: python

>>
>> m = MagicMock(return_value=None)
>> m(1)
>> m(1, 2)
>> m(object())
>> m.mock_calls == [call(1), call(1, 2), ANY]
True

Now, what if you would like to make certain assertions about an
argument, but perhaps don't know the exact value, or want to avoid
certain values (for example ``None``). This is where AnyValid might come
in handy. It provides a really simple way to leverage all the great work
that has been put into formencode's validators, so that your testing
code can make advanced assertions while being easy to read and maintain.

Examples
========

Simple argument matching:

.. code:: python

>>>
>>> from mock import Mock
>>> from any_valid import AnyValid, Int, String
>>>
>>> def check_call(foo, bar):
... try:
... mock = Mock(return_value=None)
... mock(foo, bar=bar)
... mock.assert_called_once_with(AnyValid(String(min_lenght=3)),
... bar=AnyValid(Int(min=2)))
... except AssertionError:
... return False
... return True
...
>>> check_call('fo', 1)
False
>>> check_call(8, 0)
False
>>> check_call('foo', 2)
True

Matching a loosely defined dict argument:

.. code:: python

>>> from any_valid import AnyValid, Number, OneOf
>>> valid_input = { ... 'core_temperature': AnyValid(Number(min=35, max=41.5)),
... 'protocol': AnyValid(OneOf(['https', 'http'])),
... }
>>> mock = Mock(return_value=None)
>>> mock({'core_temperature': 36.8, 'protocol': 'https'})
>>> mock.assert_called_with(valid_input)
>>>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

AnyValid-1.0.0.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file AnyValid-1.0.0.tar.gz.

File metadata

  • Download URL: AnyValid-1.0.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for AnyValid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 53cb486804e87986de9e0e278b5a9fa6c458222625ea6c9711c7b2aefe275097
MD5 e2aa202bbc1c090a1b0473b4cc6575a5
BLAKE2b-256 2130560dea258fd45b63cd64fdb0e839bc9d9b845e7861337df872c6bc317ce9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page