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

feat: add simple props filter helper #290

Merged
merged 4 commits into from
Jul 12, 2020
Merged
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
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)