Skip to content

Commit

Permalink
docs: documented the preserving of snapshot parts
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Apr 3, 2024
1 parent e4aa722 commit ffd04c3
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 118 deletions.
149 changes: 147 additions & 2 deletions docs/eq_snapshot.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## General

A snapshot can be compared against any value with `==`.
The value gets recorded if the snapshot is undefined (`snapshot()`)
The value can be recorded with `--inline-snapshot=create` if the snapshot is empty.

Example:

Expand All @@ -19,9 +19,154 @@ Example:
assert 2 + 2 == snapshot(4)
```

=== "value changed"
<!-- inline-snapshot: outcome-failed=1 -->
```python
def test_something():
assert 2 + 3 == snapshot(4)
```

=== "--inline-snapshot=fix"
<!-- inline-snapshot: fix -->
```python
def test_something():
assert 2 + 3 == snapshot(5)
```

The value can later be changed with `--inline-snapshot=fix` if the value the snapshot is compared with has changed.

## dirty-equals

It might be, that larger snapshots with many lists and dictionaries contain some values which change frequently and are not relevant for the test.

Example:

=== "original code"
<!-- inline-snapshot: outcome-passed=1 outcome-errors=1 -->
```python
from inline_snapshot import snapshot
import datetime


def get_data():
return {
"date": datetime.datetime.now(),
"payload": "some data",
}


def test_function():
assert get_data() == snapshot()
```

=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
from inline_snapshot import snapshot
import datetime


def get_data():
return {
"date": datetime.datetime.now(),
"payload": "some data",
}


def test_function():
assert get_data() == snapshot(
{"date": datetime.datetime(2024, 3, 14, 1, 0), "payload": "some data"}
)
```

They might be part of larger data structures and be difficult to normalize.

inline-snapshot tries to change only the values which it has to change in order to make the equality comparison pass.
This allows to replace parts of the snapshot with [dirty-equals](https://dirty-equals.helpmanual.io/latest/) expressions.
This expressions are preserved as long as the `==` comparison with them is `True`.


Example:

=== "using IsDatetime()"
<!-- inline-snapshot: outcome-passed=1 -->
```python
from inline_snapshot import snapshot
from dirty_equals import IsDatetime
import datetime


def get_data():
return {
"date": datetime.datetime.now(),
"payload": "some data",
}


def test_function():
assert get_data() == snapshot(
{
"date": IsDatetime(),
"payload": "some data",
}
)
```

=== "changed payload"
<!-- inline-snapshot: outcome-failed=1 -->
```python
from inline_snapshot import snapshot
from dirty_equals import IsDatetime
import datetime


def get_data():
return {
"date": datetime.datetime.now(),
"payload": "data changed for some good reason",
}


def test_function():
assert get_data() == snapshot(
{
"date": IsDatetime(),
"payload": "some data",
}
)
```


=== "--inline-snapshot=fix"
<!-- inline-snapshot: fix -->
```python
from inline_snapshot import snapshot
from dirty_equals import IsDatetime
import datetime


def get_data():
return {
"date": datetime.datetime.now(),
"payload": "data changed for some good reason",
}


def test_function():
assert get_data() == snapshot(
{
"date": IsDatetime(),
"payload": "data changed for some good reason",
}
)
```


## pytest options

It interacts with the following `--inline-snapshot` flags:

- `create` create a new value if the snapshot value is undefined.
- `fix` record the new value and store it in the source code if it is different from the current one.
- `fix` record the value parts and store them in the source code if it is different from the current one.
- `update` update parts of the value if their representation has changed.
Parts which are replaced with dirty-equals expressions are not updated.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ theme:
custom_dir: docs/theme
features:
- toc.follow
- content.code.annotate
palette:
- media: (prefers-color-scheme)

Expand Down
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def test(session):
"coverage",
"pytest-xdist",
"coverage-enable-subprocess",
"dirty-equals",
"time-machine",
)
session.env["COVERAGE_PROCESS_START"] = str(
Path(__file__).parent / "pyproject.toml"
Expand Down
Loading

0 comments on commit ffd04c3

Please sign in to comment.