Created
March 6, 2015 11:59
-
-
Save gornostal/6bee1fe0f144282319ef to your computer and use it in GitHub Desktop.
conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
class DictHasValus(dict): | |
""" | |
DictHasValus({('foo', 'bar'): 'hello'}) == {'foo': {'bar': 'hello'}} | |
""" | |
def __eq__(self, other): | |
try: | |
return all([self.find(k, other) == v for k, v in self.items()]) | |
except KeyError: | |
return False | |
def find(self, keys, other): | |
if type(keys) != tuple: | |
keys = (keys,) | |
return reduce(lambda d, key: d[key], keys, other) | |
pytest.DictHasValus = DictHasValus | |
def test_DictWithValues(): | |
assert DictHasValus({'test': 'hello'}) == {'test': 'hello', 'other': False} | |
assert DictHasValus({('test',): 'hello'}) == {'test': 'hello'} | |
assert DictHasValus({('foo', 'bar'): 'hello'}) == {'foo': {'bar': 'hello', 1: 2}} | |
assert DictHasValus({(1, 'bar'): 'hello'}) == {1: {'bar': 'hello'}} | |
assert DictHasValus({'test': 'hello'}) != {'test': 'wrong val', 'other': False} | |
assert DictHasValus({('foo', 'bar'): 'hello'}) != {'foo': None} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment