-
Notifications
You must be signed in to change notification settings - Fork 46
feat: Enhance data regression fixture with JSON support and utilities #243
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
Open
MitchellAcoustics
wants to merge
14
commits into
ESSS:master
Choose a base branch
from
MitchellAcoustics:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aabb940
Adds JSON support to data regression fixture
MitchellAcoustics fa7a1f4
Adds expected JSON regression files
MitchellAcoustics 4555f36
Add recursive dictionary sorting utility and integrate it into data r…
MitchellAcoustics 1658c31
Update JSON output formatting in data regression fixture to use 4-spa…
MitchellAcoustics e62d1c5
Merge remote-tracking branch 'origin/master' into enable-json-io
MitchellAcoustics ed8e9b5
Refactor JSON dumping in DataRegressionFixture to use 2-space indenta…
MitchellAcoustics 28913a8
Update src/pytest_regressions/data_regression.py
MitchellAcoustics 8e40b08
Update src/pytest_regressions/common.py
MitchellAcoustics 93e5cd4
Add JSON sorting test to validate sort_dict_by_keys functionality
b44d3db
Remove stale and unused baseline test data files
Copilot 3220ee0
Merge pull request #1 from MitchellAcoustics/enable-json-io
MitchellAcoustics 87e433a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2a7ea5e
Make indent configurable in data regression functions
MitchellAcoustics 918f3ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import json | ||
| import os | ||
| from collections.abc import Callable | ||
| from collections.abc import MutableMapping | ||
|
|
@@ -13,6 +14,7 @@ | |
| from .common import check_text_files | ||
| from .common import perform_regression_check | ||
| from .common import round_digits_in_data | ||
| from .common import sort_dict_by_keys | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pytest_datadir.plugin import LazyDataDir | ||
|
|
@@ -41,6 +43,9 @@ def check( | |
| basename: str | None = None, | ||
| fullpath: Optional["os.PathLike[str]"] = None, | ||
| round_digits: int | None = None, | ||
| extension: str = ".yml", | ||
| *, | ||
| indent: int = 2, | ||
| ) -> None: | ||
| """ | ||
| Checks the given dict against a previously recorded version, or generate a new file. | ||
|
|
@@ -58,34 +63,51 @@ def check( | |
| :param round_digits: | ||
| If given, round all floats in the dict to the given number of digits. | ||
|
|
||
| :param extension: Extension of the file. Defaults to ".yml". | ||
| If equal to ".json", expects `data_dict` to be JSON serializable | ||
| and dumps it using standard `json.dump`. | ||
|
Comment on lines
+66
to
+68
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I commented in the issue, lets use an enum instead. 👍 |
||
|
|
||
| ``basename`` and ``fullpath`` are exclusive. | ||
| """ | ||
| __tracebackhide__ = True | ||
|
|
||
| if round_digits is not None: | ||
| round_digits_in_data(data_dict, round_digits) | ||
|
|
||
| data_dict = sort_dict_by_keys(data_dict) | ||
|
|
||
| def dump(filename: Path) -> None: | ||
| """Dump dict contents to the given filename""" | ||
|
|
||
| dumped_str = yaml.dump_all( | ||
| [data_dict], | ||
| Dumper=RegressionYamlDumper, | ||
| default_flow_style=False, | ||
| allow_unicode=True, | ||
| indent=2, | ||
| encoding="utf-8", | ||
| ) | ||
| with filename.open("wb") as f: | ||
| f.write(dumped_str) | ||
| if extension.lower() in [".yml", ".yaml"]: | ||
| dumped_str = yaml.dump_all( | ||
| [data_dict], | ||
| Dumper=RegressionYamlDumper, | ||
| default_flow_style=False, | ||
| allow_unicode=True, | ||
| indent=indent, | ||
| encoding="utf-8", | ||
| ) | ||
| with filename.open("wb") as f: | ||
| f.write(dumped_str) | ||
| elif extension.lower() == ".json": | ||
| dumped_str = json.dumps( | ||
| data_dict, indent=indent, sort_keys=True, ensure_ascii=False | ||
| ) | ||
| with filename.open("w", encoding="utf-8") as f: | ||
| f.write(dumped_str) | ||
| else: | ||
| raise NotImplementedError( | ||
| f"file extension `{extension}` is not supported by data_regression; " | ||
| "supported extensions are '.yml', '.yaml', '.json'" | ||
| ) | ||
|
Comment on lines
+81
to
+102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use a match against the enum, with an |
||
|
|
||
| perform_regression_check( | ||
| datadir=self.datadir, | ||
| original_datadir=self.original_datadir, | ||
| request=self.request, | ||
| check_fn=partial(check_text_files, encoding="UTF-8"), | ||
| dump_fn=dump, | ||
| extension=".yml", | ||
| extension=extension, | ||
| basename=basename, | ||
| fullpath=fullpath, | ||
| force_regen=self.force_regen, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "contents": "Foo", | ||
| "value": 11 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "contents": "Foo", | ||
| "value": 11 | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "content": { | ||
| "value": 1.12, | ||
| "value1": "toto" | ||
| }, | ||
| "value": 1.23, | ||
| "values": [ | ||
| 1.12, | ||
| 2.35 | ||
| ] | ||
| } |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should we always sort the dict keys? Dicts are order preserving, in fact I can think of a few situations where users might want to regress against the original order, without sorting.
Let's remove key sorting altogether from this change, users can sort the dict themselves if they require that, I think.