Skip to content

Commit

Permalink
feat: add simple props filter helper (#290)
Browse files Browse the repository at this point in the history
* docs: update readme to show props example

* feat: add simple props filter helper

* chore: sort imports
  • Loading branch information
iamogbz authored Jul 12, 2020
1 parent 65c5d8f commit d76cc07
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,34 @@ It should return `true` or `false` if the property should be excluded or include

Syrupy comes with built-in helpers that can be used to make easy work of using the filter options.

###### `props(prop_name, *prop_name)`

Easy way to build a filter that excludes based on string based property names.

Takes an argument list of property names, with support for indexed iterables.

```py
from syrupy.filters import props

def test_bar(snapshot):
actual = {
"id": uuid.uuid4(),
"list": [1,2,3],
}
assert actual == snapshot(exclude=props("id", "1"))
```

```ambr
# name: test_bar
<class 'dict'> {
'list': <class 'list'> [
1,
3,
],
}
---
```

###### `paths(path_string, *path_strings)`

Easy way to build a filter that uses full path strings delimited with `.`.
Expand All @@ -168,7 +196,7 @@ def test_bar(snapshot):
"date": datetime.now(),
"list": [1,2,3],
}
assert actual == snapshot(exclude=paths("date_created", "list.1"))
assert actual == snapshot(exclude=paths("date", "list.1"))
```

```ambr
Expand Down
11 changes: 11 additions & 0 deletions src/syrupy/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ def path_filter(prop: "PropertyName", path: "PropertyPath") -> bool:
return any(path_str == p for p in (path_string, *path_strings))

return path_filter


def props(prop_name: str, *prop_names: str) -> "PropertyFilter":
"""
Factory to create filter using list of props
"""

def prop_filter(prop: "PropertyName", path: "PropertyPath") -> bool:
return any(str(prop) == p for p in (prop_name, *prop_names))

return prop_filter
22 changes: 20 additions & 2 deletions tests/__snapshots__/test_filters.ambr
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
# name: test_filters_error_prop
# name: test_filters_error_prop[path_filter]
<class 'WithNested'> {
include_me='prop value',
nested=<class 'CustomClass'> {
include_me='prop value',
},
}
---
# name: test_filters_expected_path
# name: test_filters_error_prop[prop_filter]
<class 'WithNested'> {
include_me='prop value',
nested=<class 'CustomClass'> {
include_me='prop value',
},
}
---
# name: test_filters_expected_paths
<class 'dict'> {
'list': <class 'list'> [
2,
],
'nested': <class 'dict'> {
'other': 'value',
},
}
---
# name: test_filters_expected_props
<class 'dict'> {
'list': <class 'list'> [
2,
Expand Down
29 changes: 25 additions & 4 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import pytest

from syrupy.filters import paths
from syrupy.filters import (
paths,
props,
)


def test_filters_path_noop(snapshot):
with pytest.raises(TypeError, match="required positional argument"):
paths()


def test_filters_expected_path(snapshot):
def test_filters_expected_paths(snapshot):
actual = {
0: "some value",
"date": datetime.datetime.now(),
Expand All @@ -20,7 +23,25 @@ def test_filters_expected_path(snapshot):
assert actual == snapshot(exclude=paths("0", "date", "nested.id", "list.0"))


def test_filters_error_prop(snapshot):
def test_filters_prop_noop(snapshot):
with pytest.raises(TypeError, match="required positional argument"):
props()


def test_filters_expected_props(snapshot):
actual = {
0: "some value",
"date": datetime.datetime.now(),
"nested": {"id": 4, "other": "value"},
"list": [1, 2],
}
assert actual == snapshot(exclude=props("0", "date", "id"))


@pytest.mark.parametrize(
"predicate", [paths("exclude_me", "nested.exclude_me"), props("exclude_me")]
)
def test_filters_error_prop(snapshot, predicate):
class CustomClass:
@property
def include_me(self):
Expand All @@ -33,4 +54,4 @@ def exclude_me(self):
class WithNested(CustomClass):
nested = CustomClass()

assert WithNested() == snapshot(exclude=paths("exclude_me", "nested.exclude_me"))
assert WithNested() == snapshot(exclude=predicate)

0 comments on commit d76cc07

Please sign in to comment.