Skip to content

Commit

Permalink
Exclude by list index
Browse files Browse the repository at this point in the history
  • Loading branch information
kvs8 committed Nov 3, 2023
1 parent 9027ec2 commit 6ebf1d9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ Result:
}
```

- Exclude list item by index:
```python
exclude_body = ['items.1']
```
Result:
```json
{
"meta": {
"api_version": "1.0.0",
"issue_date": "20230926"
},
"items": [
{
"id": "1_abc",
"name": "chair"
}
]
}
```

- Exclude string value by regular expression
```python
exclude_body = ['items.*.id:\d+']
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/excluder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,37 @@
[r"data:\d+"],
{"data": 1},
id='cutting out a non-string value by a regular expression'
), pytest.param(
),
pytest.param(
{"data": "1"},
[r"data:\D*"],
{"data": "1"},
id='cutting out a value by a regular expression without matches'
),
pytest.param(
{"data": [{"id": 123}, {"id": 124}]},
["data.1"],
{"data": [{"id": 123}]},
id='cutting list item by index'
),
pytest.param(
{"data": [{"id": 123}, {"id": 124}]},
["data.-1"],
{"data": [{"id": 123}, {"id": 124}]},
id='cutting list item by negative index'
),
pytest.param(
{"data": [{"id": 123}, {"id": 124}]},
["data.2"],
{"data": [{"id": 123}, {"id": 124}]},
id='cutting list item by non-existent index'
),
pytest.param(
{"data": [{"id": 123}, {"id": 124}]},
["data.id"],
{"data": [{"id": 123}, {"id": 124}]},
id='cutting list item by not numeric index'
),
])
def test_exclude(data: Dict[str, Any], excludes: List[str], expected_data: Dict[str, Any]):
filter_data(excludes, data)
Expand Down
2 changes: 2 additions & 0 deletions vedro_replay/exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def _exclude(self, data: Dict[str, Any], excluded_path: List[str]) -> None:
data[current_path_part] = result[0]
else:
del data[current_path_part]
elif isinstance(data, list) and current_path_part.isdigit() and len(data) > int(current_path_part):
data.pop(int(current_path_part))
return

if isinstance(data, list) and current_path_part == '*':
Expand Down

0 comments on commit 6ebf1d9

Please sign in to comment.