feat: implement testing utilities #2001
Merged
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 fn
it 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
Pool
orPoolConnection
parameter, orPoolOptions
andConnectOptions
parameters, it will provide per-test database connections usingDATABASE_URL
as 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-cli
command to automatically dump the contents of a database asINSERT
statements 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.