Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore fields parameter in assert methods #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class APITestCase(TestCase):
# Set custom snapshot name: `gpg_response`
my_gpg_response = api.client.get('/me?gpg_key')
self.assertMatchSnapshot(my_gpg_response, 'gpg_response')

# Set ignore fields (e.g. 'created_at' : '12-12-2017')
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
self.assertMatchSnapshot(ignore_date_response, ignore_fields=['created_at'])
```

If you want to update the snapshots automatically you can use the `nosetests --snapshot-update`.
Expand All @@ -52,6 +56,10 @@ def test_mything(snapshot):
# Set custom snapshot name: `gpg_response`
my_gpg_response = api.client.get('/me?gpg_key')
snapshot.assert_match(my_gpg_response, 'gpg_response')

# Set ignore fields (e.g. 'created_at' : '12-12-2017')
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
snapshot.assert_match(ignore_date_response, ignore_fields=['created_at'])
```

If you want to update the snapshots automatically you can use the `--snapshot-update` config.
Expand Down
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Usage with unittest/nose
my_gpg_response = api.client.get('/me?gpg_key')
self.assertMatchSnapshot(my_gpg_response, 'gpg_response')

# Set ignore fields (e.g. 'created_at' : '12-12-2017')
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
self.assertMatchSnapshot(ignore_date_response, ignore_fields=['created_at'])

If you want to update the snapshots automatically you can use the
``nosetests --snapshot-update``.

Expand All @@ -61,6 +65,10 @@ Usage with pytest
my_gpg_response = api.client.get('/me?gpg_key')
snapshot.assert_match(my_gpg_response, 'gpg_response')

# Set ignore fields (e.g. 'created_at' : '12-12-2017')
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
snapshot.assert_match(ignore_date_response, ignore_fields=['created_at'])

If you want to update the snapshots automatically you can use the
``--snapshot-update`` config.

Expand Down
22 changes: 18 additions & 4 deletions snapshottest/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ def store(self, data):
def assert_equals(self, value, snapshot):
assert value == snapshot

def assert_match(self, value, name=''):
def assert_match(self, value, name='', ignore_fields=None):
self.remove_fields(value, ignore_fields)
self.curr_snapshot = name or self.snapshot_counter
self.visit()
prev_snapshot = not self.update and self.module[self.test_name]
if prev_snapshot:
self.remove_fields(prev_snapshot, ignore_fields)
try:
self.assert_equals(
PrettyDiff(value, self),
Expand All @@ -236,9 +238,21 @@ def assert_match(self, value, name=''):
def save_changes(self):
self.module.save()


def assert_match_snapshot(value, name=''):
@classmethod
def remove_fields(cls, input, remove_fields_list=None):
if remove_fields_list is None:
remove_fields_list = []
gen = (field for field in remove_fields_list if field in input)
for field in gen:
del input[field]
if isinstance(input, dict):
gen = (value for value in input.values() if isinstance(value, dict))
for value in gen:
cls.remove_fields(value, remove_fields_list)


def assert_match_snapshot(value, name='', ignore_fields=None):
if not SnapshotTest._current_tester:
raise Exception("You need to use assert_match_snapshot in the SnapshotTest context.")

SnapshotTest._current_tester.assert_match(value, name)
SnapshotTest._current_tester.assert_match(value, name, ignore_fields)
4 changes: 2 additions & 2 deletions snapshottest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def tearDown(self):
SnapshotTest._current_tester = None
self._snapshot = None

def assert_match_snapshot(self, value, name=''):
self._snapshot.assert_match(value, name='')
def assert_match_snapshot(self, value, name='', ignore_fields=None):
self._snapshot.assert_match(value, name, ignore_fields)

assertMatchSnapshot = assert_match_snapshot
15 changes: 15 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ def test_pytest_snapshottest_property_test_name(pytest_snapshot_test):
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name 2'


def test_pytest_snapshottest_ignore_fields(pytest_snapshot_test):
ignore_fields_test = {
'url': 'example',
'date': '12-12-2017',
'test': {
'date': '11-12-2017'
}
}

pytest_snapshot_test.assert_match(ignore_fields_test, 'ignore_fields_test', ignore_fields=['date'])
ignore_fields_test.pop('date', None)
ignore_fields_test['test'].pop('date', None)
assert pytest_snapshot_test.module[pytest_snapshot_test.test_name] == ignore_fields_test