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

feat: implement testing utilities #2001

Merged
merged 1 commit into from
Aug 2, 2022
Merged

feat: implement testing utilities #2001

merged 1 commit into from
Aug 2, 2022

Conversation

abonander
Copy link
Collaborator

@abonander abonander commented Jul 28, 2022

The star of the show is #[sqlx::test] which is now available for general use. When used to decorate a zero-parameter async 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 or PoolConnection parameter, or PoolOptions and ConnectOptions parameters, it will provide per-test database connections using DATABASE_URL as the master connection parameters. The databases are automatically migrated if it finds a migrations/ folder in your project (you can also specify a different path or refer to an embedded migrator from sqlx::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.md

To 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 as INSERT 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.

@abonander abonander force-pushed the ab/testing-utils branch 12 times, most recently from 1c9f28e to a2b2902 Compare August 1, 2022 21:11
@abonander abonander marked this pull request as ready for review August 1, 2022 21:11
@abonander abonander force-pushed the ab/testing-utils branch 15 times, most recently from 86ec65e to b49b5be Compare August 2, 2022 00:34
@abonander abonander force-pushed the ab/testing-utils branch 17 times, most recently from 4a83aa0 to e1c7cf8 Compare August 2, 2022 21:01
@abonander abonander merged commit 054f619 into main Aug 2, 2022
@abonander abonander deleted the ab/testing-utils branch August 2, 2022 21:38
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.
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

Successfully merging this pull request may close these issues.

#[sqlx::test]
1 participant