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 3 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
16 changes: 13 additions & 3 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_from_dict(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_from_dict(prev_snapshot, ignore_fields)
try:
self.assert_equals(
PrettyDiff(value, self),
Expand All @@ -236,9 +238,17 @@ def assert_match(self, value, name=''):
def save_changes(self):
self.module.save()

@classmethod
def remove_fields_from_dict(cls, dictionary, remove_fields=None):
if remove_fields is None:
remove_fields = []
for field in remove_fields:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work with nested fields, right?
something like

{
    "content": {
        "created_at": "......."
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, in this case it will not work. What do you suggest in this case since we can have a situation like this:

{
    "created_at": ... ,
    "content": {
        "created_at": "......."
    }
}

Should the remove_fields parameter perhaps be a list if we would want to separate these cases? In this example if you wanted to delete the non-nested created_at you could just pass 'created_at' or ['created_at'] and for the nested ['content', 'created_at'].

The second option would be to globally remove the given field, so if 'created_at' is given as the parameter, it should remove every appearance of it (including nested).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I'm not quite sure myself on what to do in this situation. One approach would be to specify not just keys but the whole path, kind of like "content.items.*.event.created_at" however, for my needs, I just forked this repo and made it delete every key that matches the name recursively.

if field in dictionary:
del dictionary[field]


def assert_match_snapshot(value, name=''):
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
11 changes: 11 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ 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'
}

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