Skip to content

Commit d813e6c

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

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

tests/mysql/macros.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +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>, i64)> {
192192
let mut transaction = conn.begin().await?;
193-
sqlx::query!("INSERT INTO tweet(id, text, owner_id) VALUES (1, '#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)
195+
.await?
196+
.last_insert_id();
197+
Ok((transaction, id as i64))
197198
}
198199

199200
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
@@ -211,13 +212,13 @@ struct OptionalRecord {
211212
#[sqlx_macros::test]
212213
async fn test_column_override_wildcard() -> anyhow::Result<()> {
213214
let mut conn = new::<MySql>().await?;
214-
let mut conn = with_test_row(&mut conn).await?;
215+
let (mut conn, id) = with_test_row(&mut conn).await?;
215216

216217
let record = sqlx::query_as!(Record, "select id as `id: _` from tweet")
217218
.fetch_one(&mut conn)
218219
.await?;
219220

220-
assert_eq!(record.id, MyInt(1));
221+
assert_eq!(record.id, MyInt(id));
221222

222223
// this syntax is also useful for expressions
223224
let record = sqlx::query_as!(Record, "select * from (select 1 as `id: _`) records")
@@ -238,7 +239,7 @@ async fn test_column_override_wildcard() -> anyhow::Result<()> {
238239
#[sqlx_macros::test]
239240
async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
240241
let mut conn = new::<MySql>().await?;
241-
let mut conn = with_test_row(&mut conn).await?;
242+
let (mut conn, _) = with_test_row(&mut conn).await?;
242243

243244
let record = sqlx::query_as!(Record, "select owner_id as `id!: _` from tweet")
244245
.fetch_one(&mut conn)
@@ -252,27 +253,27 @@ async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
252253
#[sqlx_macros::test]
253254
async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
254255
let mut conn = new::<MySql>().await?;
255-
let mut conn = with_test_row(&mut conn).await?;
256+
let (mut conn, id) = with_test_row(&mut conn).await?;
256257

257258
let record = sqlx::query_as!(OptionalRecord, "select id as `id?: _` from tweet")
258259
.fetch_one(&mut conn)
259260
.await?;
260261

261-
assert_eq!(record.id, Some(MyInt(1)));
262+
assert_eq!(record.id, Some(MyInt(id)));
262263

263264
Ok(())
264265
}
265266

266267
#[sqlx_macros::test]
267268
async fn test_column_override_exact() -> anyhow::Result<()> {
268269
let mut conn = new::<MySql>().await?;
269-
let mut conn = with_test_row(&mut conn).await?;
270+
let (mut conn, id) = with_test_row(&mut conn).await?;
270271

271272
let record = sqlx::query!("select id as `id: MyInt` from tweet")
272273
.fetch_one(&mut conn)
273274
.await?;
274275

275-
assert_eq!(record.id, MyInt(1));
276+
assert_eq!(record.id, MyInt(id));
276277

277278
// we can also support this syntax for expressions
278279
let record = sqlx::query!("select * from (select 1 as `id: MyInt`) records")
@@ -293,7 +294,7 @@ async fn test_column_override_exact() -> anyhow::Result<()> {
293294
#[sqlx_macros::test]
294295
async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
295296
let mut conn = new::<MySql>().await?;
296-
let mut conn = with_test_row(&mut conn).await?;
297+
let (mut conn, _) = with_test_row(&mut conn).await?;
297298

298299
let record = sqlx::query!("select owner_id as `id!: MyInt` from tweet")
299300
.fetch_one(&mut conn)
@@ -307,13 +308,13 @@ async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
307308
#[sqlx_macros::test]
308309
async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
309310
let mut conn = new::<MySql>().await?;
310-
let mut conn = with_test_row(&mut conn).await?;
311+
let (mut conn, id) = with_test_row(&mut conn).await?;
311312

312313
let record = sqlx::query!("select id as `id?: MyInt` from tweet")
313314
.fetch_one(&mut conn)
314315
.await?;
315316

316-
assert_eq!(record.id, Some(MyInt(1)));
317+
assert_eq!(record.id, Some(MyInt(id)));
317318

318319
Ok(())
319320
}

0 commit comments

Comments
 (0)