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

API to read snapshots #353

Open
max-sixty opened this issue Feb 20, 2023 · 8 comments
Open

API to read snapshots #353

max-sixty opened this issue Feb 20, 2023 · 8 comments

Comments

@max-sixty
Copy link
Collaborator

max-sixty commented Feb 20, 2023

Edit: to avoid reading this wall of text, see this comment for a summary

Below is the original comment

---

We have a process in PRQL to extract, compile, and test the examples in our book / docs. Currently we're extracting each PRQL example, making our own snapshot of the PRQL, and then running insta snapshots on the result of compiling that PRQL to SQL.

I wonder whether this is a common case, and we could replace "making our own snapshots of it" with "make an insta snapshot and then read it". For that, we'd need some sort of API where we could do something like read_snapshots!(prefix) -> [(id, snapshot_text)] or similar


Full details:

Another use case for something like this would be testing a pipeline without having to start from the beginning. For example, to test our compilation, we could have a snapshot of each stage of the compiler, and be able to test the final step by reading a snapshot of the result in the middle, running the final stage, and asserting it matches a snapshotted final result. Currently with insta we can snapshot each stage, but it requires starting from the beginning.


This is an idea I've been brewing on today when writing some of this code, so possibly it's not complete / there is a simpler way to achieve the goal / it's actually fairly rare to need to start with an intermediate result.

@max-sixty
Copy link
Collaborator Author

Another approach here would be an API for writing & managing files that weren't .snap files.

So in the case above, we could write files like https://github.com/PRQL/prql/blob/2ecdfaac4796e12772afd0394268fdd9f366ab02/book/tests/prql/transforms/from_text-0.prql, and use insta rather than code like this: https://github.com/PRQL/prql/blob/2ecdfaac4796e12772afd0394268fdd9f366ab02/book/tests/snapshot.rs#L123-L159.

@max-sixty
Copy link
Collaborator Author

I realize there's a wall of text above, and my thinking has coalesced, so to summarize on the APi I'm thinking of:

  • read_snapshot, which returns the text of the body of the snapshot
    • Would have to think about whether it deserializes a yaml snapshot, and how to best specify the snapshot name / path
  • settings.use_plain, which would write a snapshot without any heading; just the text, so other processes can read it without needing to parse it

@NfNitLoop
Copy link

+1 for something like this.

Use case: We're using async-graphql, which is a code-first graphql library. But we need to generate the SDL schema for our GraphQL API so other languages/libraries can interact with our API.

I reached for Insta thinking I could just make an insta test and pass it my schema.sdl() string and a file path, which would let me use that file as both:

  • The canonical graphql.schema declaration for the API.
  • An Insta test to make sure no one has forgotten to re-run the command to snapshot (and code review) the new API changes.

@mitsuhiko
Copy link
Owner

I mean you can load snapshots even today with the Snapshot::from_file method. But actually determining the name will be tricky. I would love to see a mocked up idea of how you would try to use this.

@xamgore
Copy link

xamgore commented May 18, 2024

In another thread I show an example of testing library implementation with support for snapshots.

@mitsuhiko
Copy link
Owner

I dumped some ideas over there for how this could work if the snapshot macro were to be changed: #456 (comment)

@max-sixty
Copy link
Collaborator Author

max-sixty commented Jul 30, 2024

One simple incremental approach would be to have an assert_snapshot_raw!(path, value) which:

  • Writes value to path
  • ...no metadata, no header, no .snap extension
  • Uses all the insta snapshot machinery when the file doesn't match / is missing

It's a bit less orthogonal than other approaches — generally the macro type concerns the format of the snapshot rather than its type. But it would be useful.


Another approach would be .with_settings({raw=True, path="./foo/bar.html"}). This is a bit more orthogonal, a bit more work to implement.

@max-sixty
Copy link
Collaborator Author

For future travelers — #610 does some of this, since it can write an arbitrary value to an arbitrary file. (it doesn't allow literally reading snapshots though, still need some mapping to the file path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants