feat: implement testing utilities - #2001
Merged
Merged
Conversation
This was referenced Jul 28, 2022
Closed
abonander
force-pushed
the
ab/testing-utils
branch
12 times, most recently
from
August 1, 2022 21:11
1c9f28e to
a2b2902
Compare
abonander
marked this pull request as ready for review
August 1, 2022 21:11
abonander
force-pushed
the
ab/testing-utils
branch
15 times, most recently
from
August 2, 2022 00:34
86ec65e to
b49b5be
Compare
abonander
force-pushed
the
ab/testing-utils
branch
17 times, most recently
from
August 2, 2022 21:01
4a83aa0 to
e1c7cf8
Compare
abonander
force-pushed
the
ab/testing-utils
branch
from
August 2, 2022 21:02
e1c7cf8 to
c066726
Compare
abonander
added a commit
that referenced
this pull request
Aug 3, 2022
I intended to add subcommands to `sqlx-cli` to manage test databases but I wanted to get #2001 finished and out the door so we can start using it ASAP.
abonander
added a commit
that referenced
this pull request
Aug 3, 2022
I intended to add subcommands to `sqlx-cli` to manage test databases but I wanted to get #2001 finished and out the door so we can start using it ASAP.
abonander
added a commit
that referenced
this pull request
Aug 3, 2022
I intended to add subcommands to `sqlx-cli` to manage test databases but I wanted to get #2001 finished and out the door so we can start using it ASAP.
abonander
added a commit
that referenced
this pull request
Aug 3, 2022
I intended to add subcommands to `sqlx-cli` to manage test databases but I wanted to get #2001 finished and out the door so we can start using it ASAP.
11 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The star of the show is
#[sqlx::test]which is now available for general use. When used to decorate a zero-parameterasync fnit operates identically to#[tokio::test](though it doesn't recognize the control arguments that it uses) or#[async_std::test].However, if you add a
PoolorPoolConnectionparameter, orPoolOptionsandConnectOptionsparameters, it will provide per-test database connections usingDATABASE_URLas the master connection parameters. The databases are automatically migrated if it finds amigrations/folder in your project (you can also specify a different path or refer to an embedded migrator fromsqlx::migrate!()), and you can also specify the names of test fixtures (SQL scripts to insert test data) to apply.There's extensive examples in the documentation, which I've also tried out the ability to write it in a separate Markdown file using
include_str!()so it's easier to maintain and easier to read in-tree (lines prefixed with#are hidden by Rustdoc to help with brevity): https://github.com/launchbadge/sqlx/blob/ab/testing-utils/src/macros/test.mdTo demonstrate the attribute in action, I've created an example showing how it can be used with Axum to do API testing entirely in-process (with the exception of the database itself, of course): https://github.com/launchbadge/sqlx/blob/ab/testing-utils/examples/postgres/axum-social-with-tests/tests/comment.rs
closes #330
There's also some WIP code in there about automatic fixture capture. I conceived of a
sqlx-clicommand to automatically dump the contents of a database asINSERTstatements to make it easier to write test fixtures, and potentially in the future generate fixtures by diffing two different snapshots of a database. Because we have engineers at Launchbadge who are eager to try out#[sqlx::test], I've decided to shelve that for now to expedite getting this branch merged and released, but I'm keeping the code in-tree to make it easier to get back to later.Also I'm making the executive decision to upgrade our minimum officially supported versions of MySQL, MariaDB and Postgres. The previous minimums have been end-of-lifed and don't seem to support the tests I wrote for this feature (since I'm used to working with newer versions anyway). Older versions may very well still work if you don't use the new features in this PR, but they won't be tested anymore.
Postgres 9.6 -> 10
MySQL 5.6 -> 5.7
MariaDB 10.2 -> 10.3
This PR doesn't add support to the MSSQL driver as we still plan to break it out into a separate offering.