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

Tests with many snapshots are sorted 1, 10, 11, ..., 2, 20, ..., 3, 4, 5, ..., not numeric order #657

Closed
huonw opened this issue Nov 30, 2022 · 5 comments · Fixed by #605
Labels
feature request New feature or request released serializer Syrupy serializer question

Comments

@huonw
Copy link
Contributor

huonw commented Nov 30, 2022

Describe the bug

Snapshots are stored sorted in the .ambr, but this seems to be doing a string comparison on the index, meaning 10 sorts before 2, etc.

To reproduce

# test_many.py
def test_many(snapshot):
    for i in range(25):
        assert i == snapshot

Run pytest --snapshot-update test_many.py:

Resulting __snapshots__/test_many.ambr:

# name: test_many
  0
# ---
# name: test_many.1
  1
# ---
# name: test_many.10
  10
# ---
# name: test_many.11
  11
# ---
# name: test_many.12
  12
# ---
# name: test_many.13
  13
# ---
# name: test_many.14
  14
# ---
# name: test_many.15
  15
# ---
# name: test_many.16
  16
# ---
# name: test_many.17
  17
# ---
# name: test_many.18
  18
# ---
# name: test_many.19
  19
# ---
# name: test_many.2
  2
# ---
# name: test_many.20
  20
# ---
# name: test_many.21
  21
# ---
# name: test_many.22
  22
# ---
# name: test_many.23
  23
# ---
# name: test_many.24
  24
# ---
# name: test_many.3
  3
# ---
# name: test_many.4
  4
# ---
# name: test_many.5
  5
# ---
# name: test_many.6
  6
# ---
# name: test_many.7
  7
# ---
# name: test_many.8
  8
# ---
# name: test_many.9
  9
# ---

Expected behavior

Snapshots should be sorted in numeric order 1, 2, ..., 9, 10, ..., 20, ..., 24.

Some options:

  • formatting the number with leading zeros. If it's possible the leading zeros could be computed dynamically based on how many snapshots are actually used in each test, otherwise could be hard-coded (e.g. 01, 02, ... 09, 10, ... would ensure natural sorting up to 100 snapshots in one test)
  • not doing string comparison, e.g. Store snapshot assertion index in snapshot file  #569 might be related.

Screenshots

N/A

Environment (please complete the following information):

  • OS: macOS
  • Syrupy Version: 3.0.5
  • Python Version: 3.9

Additional context

Thank you for syrupy!

@noahnu
Copy link
Collaborator

noahnu commented Jan 3, 2023

I like the idea of implementing #569 first, removing the index from the name field and then sorting by (name, index).

@noahnu noahnu added this to the v4.0.0 - Simplify Extension API milestone Jan 3, 2023
@noahnu noahnu added feature request New feature or request serializer Syrupy serializer question labels Jan 3, 2023
@noahnu noahnu linked a pull request Jan 26, 2023 that will close this issue
@noahnu
Copy link
Collaborator

noahnu commented Jan 26, 2023

I fixed this in the v4 PR here (not released yet): 4ca0716 however my solution is causing performance issues. I suspect because of the nested tuples in my solution. Will need to improve the performance or come up with another approach before releasing syrupy v4.

I suppose we could try sort only on the text after the last .

@noahnu
Copy link
Collaborator

noahnu commented Feb 2, 2023

So as not to hold up the v4 release, I'm not making this the default behaviour, however it can be accomplished via a custom extension with a couple lines of code: https://github.com/tophat/syrupy/blob/f416e9a408bc252fdeab1888cb8b7f0cd8fdef92/tests/syrupy/extensions/amber_sorted/test_amber_sort.py

@noahnu noahnu closed this as completed in 4ca0716 Feb 2, 2023
tophat-opensource-bot pushed a commit that referenced this issue Feb 2, 2023
# [4.0.0](v3.0.6...v4.0.0) (2023-02-02)

### Bug Fixes

* defer snapshot writes until end of session ([#606](#606)) ([68f1d5f](68f1d5f))
* ensure all pytest options are serializable ([#667](#667)) ([e8ed9f2](e8ed9f2))
* improve pytest-xdist compatibility ([9b9090f](9b9090f))
* lru_cache on snapshot reads ([#629](#629)) ([c1a675f](c1a675f))
* remove legacy path usage to support no:legacypath, closes [#677](#677) ([#684](#684)) ([6385979](6385979))

### Code Refactoring

* simplify data serializer for ambr ([#676](#676)) ([3d296e1](3d296e1))
* write performance improvements, api clarity ([#645](#645)) ([2c31c39](2c31c39))

### Features

* **json:** serialize None as null, close [#622](#622) ([c330680](c330680))
* numerically sort snapshots if possible, close [#657](#657) ([4ca0716](4ca0716))
* **serializer:** preserve key ordering of OrderedDict ([0a2289a](0a2289a))
* support overriding the amber serializer class ([#683](#683)) ([662c93f](662c93f))
* update python version, pytest version ([#658](#658)) ([c360b95](c360b95))

### BREAKING CHANGES

* Serializers may now throw a TaintedSnapshotError which will tell the user to regenerate the snapshot even if the underlying data has not changed. This is to support rolling out more subtle changes to the serializers, such as the introduction of serializer metadata.
* Renamed DataSerializer to AmberDataSerializer.
* **serializer:** Key order is now preserved if using OrderedDict in both the Amber serializer and JSON serializer.
* **json:** The JSONSnapshotExtension now serializes Python's None as "null" rather than "None".
* Raise minimum python version to 3.8.1 and min. pytest version to v7.
* PyTestLocation.filename has been renamed to .basename

* refactor: add test_location kwarg to get_snapshot_name

* refactor: get_snapshot_name is now static as a classmethod

* refactor: remove pre and post read/write hooks
* Pre and post read/write hooks have been removed without replacement to make internal refactor simpler. Please open a GitHub issue if you have a use case for these hooks.

* refactor: rename Fossil to Collection
* The term 'fossil' has been replaced by the clearer term 'collection'.

* refactor: pass test_location to read_snapshot

* refactor: remove singular write_snapshot method

* refactor: dirname property to method

* refactor: pass test_location to discover_snapshots

* refactor: remove usage of self.test_location

* refactor: make write_snapshot a classmethod

* refactor: do not instantiate extension with test_location
* Numerous instance methods have been refactored as classmethods.
@tophat-opensource-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@huonw
Copy link
Contributor Author

huonw commented Feb 6, 2023

A neat solution 👍 Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request released serializer Syrupy serializer question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants