Skip to content

Commit f5c0c4a

Browse files
committed
test(mysql): fix integration tests
Signed-off-by: Atkins Chang <[email protected]>
1 parent 7934e0d commit f5c0c4a

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

tests/mysql/macros.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,13 @@ async fn test_column_override_nullable() -> anyhow::Result<()> {
188188

189189
async fn with_test_row<'a>(
190190
conn: &'a mut MySqlConnection,
191-
) -> anyhow::Result<Transaction<'a, MySql>> {
191+
) -> anyhow::Result<(Transaction<'a, MySql>, MyInt)> {
192192
let mut transaction = conn.begin().await?;
193-
sqlx::query!("INSERT INTO tweet(text, owner_id) VALUES ('#sqlx is pretty cool!', 1)")
193+
let id = sqlx::query!("INSERT INTO tweet(text, owner_id) VALUES ('#sqlx is pretty cool!', 1)")
194194
.execute(&mut transaction)
195-
.await?;
196-
Ok(transaction)
197-
}
198-
199-
async fn last_insert_id(conn: &mut MySqlConnection) -> anyhow::Result<MyInt> {
200-
let result = sqlx::query!("SELECT last_insert_id() AS last_insert_id")
201-
.fetch_one(conn)
202-
.await?;
203-
Ok(MyInt(result.last_insert_id as i64))
195+
.await?
196+
.last_insert_id();
197+
Ok((transaction, MyInt(id as i64)))
204198
}
205199

206200
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
@@ -218,8 +212,7 @@ struct OptionalRecord {
218212
#[sqlx_macros::test]
219213
async fn test_column_override_wildcard() -> anyhow::Result<()> {
220214
let mut conn = new::<MySql>().await?;
221-
let mut conn = with_test_row(&mut conn).await?;
222-
let id = last_insert_id(&mut conn).await?;
215+
let (mut conn, id) = with_test_row(&mut conn).await?;
223216

224217
let record = sqlx::query_as!(Record, "select id as `id: _` from tweet")
225218
.fetch_one(&mut conn)
@@ -246,7 +239,7 @@ async fn test_column_override_wildcard() -> anyhow::Result<()> {
246239
#[sqlx_macros::test]
247240
async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
248241
let mut conn = new::<MySql>().await?;
249-
let mut conn = with_test_row(&mut conn).await?;
242+
let (mut conn, _) = with_test_row(&mut conn).await?;
250243

251244
let record = sqlx::query_as!(Record, "select owner_id as `id!: _` from tweet")
252245
.fetch_one(&mut conn)
@@ -260,8 +253,7 @@ async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
260253
#[sqlx_macros::test]
261254
async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
262255
let mut conn = new::<MySql>().await?;
263-
let mut conn = with_test_row(&mut conn).await?;
264-
let id = last_insert_id(&mut conn).await?;
256+
let (mut conn, id) = with_test_row(&mut conn).await?;
265257

266258
let record = sqlx::query_as!(OptionalRecord, "select id as `id?: _` from tweet")
267259
.fetch_one(&mut conn)
@@ -275,8 +267,7 @@ async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
275267
#[sqlx_macros::test]
276268
async fn test_column_override_exact() -> anyhow::Result<()> {
277269
let mut conn = new::<MySql>().await?;
278-
let mut conn = with_test_row(&mut conn).await?;
279-
let id = last_insert_id(&mut conn).await?;
270+
let (mut conn, id) = with_test_row(&mut conn).await?;
280271

281272
let record = sqlx::query!("select id as `id: MyInt` from tweet")
282273
.fetch_one(&mut conn)
@@ -303,7 +294,7 @@ async fn test_column_override_exact() -> anyhow::Result<()> {
303294
#[sqlx_macros::test]
304295
async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
305296
let mut conn = new::<MySql>().await?;
306-
let mut conn = with_test_row(&mut conn).await?;
297+
let (mut conn, _) = with_test_row(&mut conn).await?;
307298

308299
let record = sqlx::query!("select owner_id as `id!: MyInt` from tweet")
309300
.fetch_one(&mut conn)
@@ -317,8 +308,7 @@ async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
317308
#[sqlx_macros::test]
318309
async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
319310
let mut conn = new::<MySql>().await?;
320-
let mut conn = with_test_row(&mut conn).await?;
321-
let id = last_insert_id(&mut conn).await?;
311+
let (mut conn, id) = with_test_row(&mut conn).await?;
322312

323313
let record = sqlx::query!("select id as `id?: MyInt` from tweet")
324314
.fetch_one(&mut conn)

0 commit comments

Comments
 (0)