-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Setup
Versions
- Rust: 1.62.1
- Diesel: 2.0.0-rc.1
- Database: PG 11
- Operating System macOS 12.4
Feature Flags
- diesel: ["postgres", "r2d2", "chrono"]
Problem Description
In my 1.4.8 diesel usage I had a connection customizer that set the begin_test_transaction
Which I injected into my "test-only" connection pool which has a max-size of 1.
Then in my tests I was able to grab the DB connection perform inserts, then drop the connection and pass the pool to the code being tested (e.g. API requests) and it would use the SAME Connection & transaction block.
What are you trying to accomplish?
Happy testing with a single-connection pool.
What is the expected output?
The tests to run correctly.
What is the actual output?
The transaction is rolled back once the connection is returned to the pool and thus breaking the tests. as the code being tests creates a new separate transaction.
Are you seeing any additional errors?
Steps to reproduce
Connection customizer to call begin_test_transaction
#[derive(Debug, Clone, Copy)]
struct TestConnectionCustomizer;
impl<C, E> CustomizeConnection<C, E> for TestConnectionCustomizer
where
C: diesel::Connection,
{
fn on_acquire(&self, conn: &mut C) -> Result<(), E> {
conn.begin_test_transaction()
.expect("Failed to start test transaction");
Ok(())
}
}Sample test
#[tokio::test]
async fn my_test() {
let pool = test_pool();
let mut db = pool.get().unwrap();
let user = UserFactory::default().insert(&mut db);
drop(db);
let obj = ObjectToTest::new(&pool);
obj.some_method(user.id);
let mut db = pool.get().unwrap();
let updated_user = users::table.filter(users::id.eq(user.id)).first(&mut db).unwrap();
drop(db);
assert_eq!(user.name, new_user.name);
}This code works under Diesel 1.4.8 but does not in Diesel 2.0.0-rc.1
Here is raw SQL queries that were run
- Diesel 1.4.8
execute <unnamed>: SET TIME ZONE 'UTC'
execute <unnamed>: SET CLIENT_ENCODING TO 'UTF8'
statement: BEGIN
execute <unnamed>: SELECT 1
execute <unnamed>: INSERT INTO "users" ("name") VALUES ($1) RETURNING "users"."id", "users"."name", "users"."created_at", "users"."updated_at"
execute <unnamed>: SELECT 1
execute <unnamed>: UPDATE "users" SET "name" = $1 WHERE "users"."id" = $2 RETURNING "users"."id", "users"."name", "users"."created_at", "users"."updated_at"
execute <unnamed>: SELECT 1
execute __diesel_stmt_0: SELECT "users"."id", "users"."name", "users"."created_at", "users"."updated_at" FROM "users" WHERE "users"."id" = $1 LIMIT $2
-- implicit close & rollback
- Diesel 2.0.0-rc.1
execute <unnamed>: SET TIME ZONE 'UTC'
execute <unnamed>: SET CLIENT_ENCODING TO 'UTF8'
statement: BEGIN
execute <unnamed>: SELECT 1
execute <unnamed>: INSERT INTO "users" ("name") VALUES ($1) RETURNING "users"."id", "users"."name", "users"."created_at", "users"."updated_at"
-- implicit close & rollback
execute <unnamed>: SET TIME ZONE 'UTC'
execute <unnamed>: SET CLIENT_ENCODING TO 'UTF8'
statement: BEGIN
execute <unnamed>: SELECT 1
execute <unnamed>: UPDATE "users" SET "name" = $1 WHERE "users"."id" = $2 RETURNING "users"."id", "users"."name", "users"."created_at", "users"."updated_at"
-- implicit close & rollback
execute <unnamed>: SET TIME ZONE 'UTC'
execute <unnamed>: SET CLIENT_ENCODING TO 'UTF8'
statement: BEGIN
execute <unnamed>: SELECT 1
execute __diesel_stmt_0: SELECT "users"."id", "users"."name", "users"."created_at", "users"."updated_at" FROM "users" WHERE "users"."id" = $1 LIMIT $2
-- implicit close & rollback
Checklist
- I have already looked over the issue tracker and the disussion forum for similar possible closed issues.
- This issue can be reproduced on Rust's stable channel. (Your issue will be
closed if this is not the case) - This issue can be reproduced without requiring a third party crate