Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 412448d

Browse files
authored
Merge pull request #1 from say/ignore-fields
Add ignore fields parameter
2 parents 0b4656a + fc1b150 commit 412448d

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class APITestCase(TestCase):
3535
# Set custom snapshot name: `gpg_response`
3636
my_gpg_response = api.client.get('/me?gpg_key')
3737
self.assertMatchSnapshot(my_gpg_response, 'gpg_response')
38+
39+
# Set ignore fields (e.g. 'created_at' : '12-12-2017')
40+
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
41+
self.assertMatchSnapshot(ignore_date_response, ignore_fields=['created_at'])
3842
```
3943

4044
If you want to update the snapshots automatically you can use the `nosetests --snapshot-update`.
@@ -52,6 +56,10 @@ def test_mything(snapshot):
5256
# Set custom snapshot name: `gpg_response`
5357
my_gpg_response = api.client.get('/me?gpg_key')
5458
snapshot.assert_match(my_gpg_response, 'gpg_response')
59+
60+
# Set ignore fields (e.g. 'created_at' : '12-12-2017')
61+
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
62+
snapshot.assert_match(ignore_date_response, ignore_fields=['created_at'])
5563
```
5664

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

README.rst

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Usage with unittest/nose
4141
my_gpg_response = api.client.get('/me?gpg_key')
4242
self.assertMatchSnapshot(my_gpg_response, 'gpg_response')
4343
44+
# Set ignore fields (e.g. 'created_at' : '12-12-2017')
45+
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
46+
self.assertMatchSnapshot(ignore_date_response, ignore_fields=['created_at'])
47+
4448
If you want to update the snapshots automatically you can use the
4549
``nosetests --snapshot-update``.
4650

@@ -61,6 +65,10 @@ Usage with pytest
6165
my_gpg_response = api.client.get('/me?gpg_key')
6266
snapshot.assert_match(my_gpg_response, 'gpg_response')
6367
68+
# Set ignore fields (e.g. 'created_at' : '12-12-2017')
69+
ignore_date_response = {'created_at' : '01-01-2018', 'url': '/me'}
70+
snapshot.assert_match(ignore_date_response, ignore_fields=['created_at'])
71+
6472
If you want to update the snapshots automatically you can use the
6573
``--snapshot-update`` config.
6674

setup.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
with open('README.rst') as f:
66
readme = f.read()
77

8-
tests_require = ['six', 'pytest>=3.1.0', 'pytest-cov', 'nose', 'django>=1.10.6']
8+
tests_require = [
9+
'six',
10+
'pytest>=3.1.0',
11+
'pytest-cov<2.6.0',
12+
'nose',
13+
'django>=1.10.6',
14+
]
915

1016
setup(
1117
name='snapshottest',

snapshottest/module.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ def assert_value_matches_snapshot(self, test_value, snapshot_value):
231231
def assert_equals(self, value, snapshot):
232232
assert value == snapshot
233233

234-
def assert_match(self, value, name=''):
234+
def assert_match(self, value, name='', ignore_fields=None):
235+
self.remove_fields(value, ignore_fields)
235236
self.curr_snapshot = name or self.snapshot_counter
236237
self.visit()
237238
if self.update:
@@ -243,6 +244,7 @@ def assert_match(self, value, name=''):
243244
self.store(value) # first time this test has been seen
244245
else:
245246
try:
247+
self.remove_fields(prev_snapshot, ignore_fields)
246248
self.assert_value_matches_snapshot(value, prev_snapshot)
247249
except AssertionError:
248250
self.fail()
@@ -254,9 +256,21 @@ def assert_match(self, value, name=''):
254256
def save_changes(self):
255257
self.module.save()
256258

257-
258-
def assert_match_snapshot(value, name=''):
259+
@classmethod
260+
def remove_fields(cls, input, remove_fields_list=None):
261+
if remove_fields_list is None:
262+
remove_fields_list = []
263+
gen = (field for field in remove_fields_list if field in input)
264+
for field in gen:
265+
del input[field]
266+
if isinstance(input, dict):
267+
gen = (value for value in input.values() if isinstance(value, dict))
268+
for value in gen:
269+
cls.remove_fields(value, remove_fields_list)
270+
271+
272+
def assert_match_snapshot(value, name='', ignore_fields=None):
259273
if not SnapshotTest._current_tester:
260274
raise Exception("You need to use assert_match_snapshot in the SnapshotTest context.")
261275

262-
SnapshotTest._current_tester.assert_match(value, name)
276+
SnapshotTest._current_tester.assert_match(value, name, ignore_fields)

snapshottest/unittest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def tearDown(self):
101101
SnapshotTest._current_tester = None
102102
self._snapshot = None
103103

104-
def assert_match_snapshot(self, value, name=''):
105-
self._snapshot.assert_match(value, name=name)
104+
def assert_match_snapshot(self, value, name='', ignore_fields=None):
105+
self._snapshot.assert_match(value, name, ignore_fields)
106106

107107
assertMatchSnapshot = assert_match_snapshot

tests/test_pytest.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,31 @@ def test_property_test_name(self, pytest_snapshot_test):
3535

3636

3737
def test_pytest_snapshottest_property_test_name(pytest_snapshot_test):
38-
pytest_snapshot_test.assert_match('counter')
39-
assert pytest_snapshot_test.test_name == \
40-
'test_pytest_snapshottest_property_test_name 1'
38+
pytest_snapshot_test.assert_match('counter')
39+
assert pytest_snapshot_test.test_name == \
40+
'test_pytest_snapshottest_property_test_name 1'
4141

42-
pytest_snapshot_test.assert_match('named', 'named_test')
43-
assert pytest_snapshot_test.test_name == \
44-
'test_pytest_snapshottest_property_test_name named_test'
42+
pytest_snapshot_test.assert_match('named', 'named_test')
43+
assert pytest_snapshot_test.test_name == \
44+
'test_pytest_snapshottest_property_test_name named_test'
4545

46-
pytest_snapshot_test.assert_match('counter')
47-
assert pytest_snapshot_test.test_name == \
48-
'test_pytest_snapshottest_property_test_name 2'
46+
pytest_snapshot_test.assert_match('counter')
47+
assert pytest_snapshot_test.test_name == \
48+
'test_pytest_snapshottest_property_test_name 2'
49+
50+
51+
def test_pytest_snapshottest_ignore_fields(pytest_snapshot_test):
52+
ignore_fields_test = {
53+
'url': 'example',
54+
'date': '12-12-2017',
55+
'test': {
56+
'date': '11-12-2017'
57+
}
58+
}
59+
60+
pytest_snapshot_test.assert_match(
61+
ignore_fields_test, 'ignore_fields_test', ignore_fields=['date']
62+
)
63+
ignore_fields_test.pop('date', None)
64+
ignore_fields_test['test'].pop('date', None)
65+
assert pytest_snapshot_test.module[pytest_snapshot_test.test_name] == ignore_fields_test

0 commit comments

Comments
 (0)