Skip to content

Commit d29f44e

Browse files
committed
fix(test): add tie-breaker to NULL ordering tests
Add secondary sort by id to ensure deterministic ordering among NULL rows. SQL standard only guarantees NULLs precede/follow non-NULLs, not ordering among equal sort keys. Without tie-breaker, tests could flake depending on planner behavior.
1 parent ffb06a8 commit d29f44e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

tests/sqlx/tests/order_by_tests.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,14 @@ async fn order_by_asc_nulls_first_returns_null_record_first(pool: PgPool) -> Res
164164
.execute(&pool)
165165
.await?;
166166

167-
// Test: NULLS FIRST should return id=1
168-
let sql = "SELECT id FROM encrypted ORDER BY e ASC NULLS FIRST";
167+
// Test: NULLS FIRST should return a NULL row first
168+
// Use tie-breaker (id) to ensure deterministic ordering among NULL rows
169+
let sql = "SELECT id FROM encrypted ORDER BY e ASC NULLS FIRST, id";
169170
let row = sqlx::query(sql).fetch_one(&pool).await?;
170171
let first_id: i64 = row.try_get(0)?;
171172
assert_eq!(
172173
first_id, 1,
173-
"ORDER BY e ASC NULLS FIRST should return NULL value (id=1) first"
174+
"ORDER BY e ASC NULLS FIRST, id should return NULL value with lowest id (id=1) first"
174175
);
175176

176177
Ok(())
@@ -265,13 +266,14 @@ async fn order_by_desc_nulls_first_returns_null_value_first(pool: PgPool) -> Res
265266
.execute(&pool)
266267
.await?;
267268

268-
// Test: DESC NULLS FIRST should return id=1
269-
let sql = "SELECT id FROM encrypted ORDER BY e DESC NULLS FIRST";
269+
// Test: DESC NULLS FIRST should return a NULL row first
270+
// Use tie-breaker (id) to ensure deterministic ordering among NULL rows
271+
let sql = "SELECT id FROM encrypted ORDER BY e DESC NULLS FIRST, id";
270272
let row = sqlx::query(sql).fetch_one(&pool).await?;
271273
let first_id: i64 = row.try_get(0)?;
272274
assert_eq!(
273275
first_id, 1,
274-
"ORDER BY e DESC NULLS FIRST should return NULL value (id=1) first"
276+
"ORDER BY e DESC NULLS FIRST, id should return NULL value with lowest id (id=1) first"
275277
);
276278

277279
Ok(())

0 commit comments

Comments
 (0)