Skip to content

Instantly share code, notes, and snippets.

@gornostal
Created March 6, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gornostal/6bee1fe0f144282319ef to your computer and use it in GitHub Desktop.
Save gornostal/6bee1fe0f144282319ef to your computer and use it in GitHub Desktop.
conftest.py
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