0

I'm currently trying to create a simple integration test that for example try the signup endpoint. Coming from many other backend languages I'm used to rollback database after each test.

How can I do this using sqlx? Is there any way to start sqlx with some kind of test transaction ?

I don't find anything on this.


    #[actix_rt::test]
    async fn signup_test() {
        let params = SignupRequest {
            login: "bruce8@wayne.com".into(),
            password: "testtest123".into(),
        };
        let app_state = AppState::init().await;

        let mut app = test::init_service(
            App::new()
                .app_data(web::Data::new(app_state.clone()))
                .configure(configure),
        )
        .await;
        let req = test::TestRequest::post() //
            .insert_header(("content-type", "application/json"))
            .set_json(params)
            .uri("/auth")
            .to_request();
        let resp = test::call_service(&mut app, req).await;
        log::info!("----> {}", resp.status());

        assert!(resp.status().is_success());
    }
mcfly
  • 774
  • 1
  • 8
  • 18
  • 1
    Your question mentions sqlx (which, as with any transactional db client, supports transaction rollback with [`Transaction::rollback`](https://docs.rs/sqlx/latest/sqlx/struct.Transaction.html#method.rollback)—but of course, that just aborts the transaction from ever being committed and such may be a necessary step for your tests; furthermore aborted transactions also consume database resources, both ephemeral and permanent, so may not be ideal for testing). However, here you don't appear to be using sqlx but rather an HTTP endpoint—does that endpoint expose an abort/rollback feature? – eggyal Feb 22 '22 at 10:24
  • More generally, one would typically test against a non-live system where aborting/rolling back is not required. And/or reset the test database to a particular state for each test run. And/or test against mocks rather than real systems. – eggyal Feb 22 '22 at 10:26
  • I'm using sqlx, just didn't show the code as it was quite standard. I'm quite used to rollback data for each test to create many scenarios. – mcfly Feb 22 '22 at 10:31

0 Answers0