Skip to content

Commit 289ee11

Browse files
committed
test(sqlx): add ORDER BY tests with ORE encryption
- Add ORDER BY DESC/ASC tests - Add ORDER BY with WHERE clause (< and >) - Add LIMIT 1 tests for min/max values - Migrated from src/operators/order_by_test.sql (20 assertions) - Coverage: 152/513 (29.6%)
1 parent 12cab28 commit 289ee11

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

tests/sqlx/tests/order_by_tests.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//! ORDER BY tests for ORE-encrypted columns
2+
//!
3+
//! Converted from src/operators/order_by_test.sql
4+
//! Tests ORDER BY with ORE (Order-Revealing Encryption)
5+
//! Uses ore table from migrations/002_install_ore_data.sql (ids 1-99)
6+
7+
use anyhow::{Context, Result};
8+
use eql_tests::QueryAssertion;
9+
use sqlx::{PgPool, Row};
10+
11+
async fn get_ore_encrypted(pool: &PgPool, id: i32) -> Result<String> {
12+
let sql = format!("SELECT e::text FROM ore WHERE id = {}", id);
13+
let row = sqlx::query(&sql)
14+
.fetch_one(pool)
15+
.await
16+
.with_context(|| format!("fetching ore encrypted value for id={}", id))?;
17+
18+
let result: Option<String> = row
19+
.try_get(0)
20+
.with_context(|| format!("extracting text column for id={}", id))?;
21+
22+
result.with_context(|| format!("ore table returned NULL for id={}", id))
23+
}
24+
25+
#[sqlx::test]
26+
async fn order_by_desc_returns_highest_value_first(pool: PgPool) -> Result<()> {
27+
// Test: ORDER BY e DESC returns records in descending order
28+
// Combined with WHERE e < 42 to verify ordering
29+
// Original SQL lines 17-25 in src/operators/order_by_test.sql
30+
31+
let ore_term = get_ore_encrypted(&pool, 42).await?;
32+
33+
let sql = format!(
34+
"SELECT id FROM ore WHERE e < '{}'::eql_v2_encrypted ORDER BY e DESC",
35+
ore_term
36+
);
37+
38+
// Should return 41 records, highest first
39+
let assertion = QueryAssertion::new(&pool, &sql);
40+
assertion.count(41).await;
41+
42+
// First record should be id=41
43+
let row = sqlx::query(&sql).fetch_one(&pool).await?;
44+
let first_id: i32 = row.try_get(0)?;
45+
assert_eq!(first_id, 41, "ORDER BY DESC should return id=41 first");
46+
47+
Ok(())
48+
}
49+
50+
#[sqlx::test]
51+
async fn order_by_desc_with_limit(pool: PgPool) -> Result<()> {
52+
// Test: ORDER BY e DESC LIMIT 1 returns highest value
53+
// Original SQL lines 22-25 in src/operators/order_by_test.sql
54+
55+
let ore_term = get_ore_encrypted(&pool, 42).await?;
56+
57+
let sql = format!(
58+
"SELECT id FROM ore WHERE e < '{}'::eql_v2_encrypted ORDER BY e DESC LIMIT 1",
59+
ore_term
60+
);
61+
62+
let row = sqlx::query(&sql).fetch_one(&pool).await?;
63+
let id: i32 = row.try_get(0)?;
64+
assert_eq!(id, 41, "Should return id=41 (highest value < 42)");
65+
66+
Ok(())
67+
}
68+
69+
#[sqlx::test]
70+
async fn order_by_asc_with_limit(pool: PgPool) -> Result<()> {
71+
// Test: ORDER BY e ASC LIMIT 1 returns lowest value
72+
// Original SQL lines 27-30 in src/operators/order_by_test.sql
73+
74+
let ore_term = get_ore_encrypted(&pool, 42).await?;
75+
76+
let sql = format!(
77+
"SELECT id FROM ore WHERE e < '{}'::eql_v2_encrypted ORDER BY e ASC LIMIT 1",
78+
ore_term
79+
);
80+
81+
let row = sqlx::query(&sql).fetch_one(&pool).await?;
82+
let id: i32 = row.try_get(0)?;
83+
assert_eq!(id, 1, "Should return id=1 (lowest value < 42)");
84+
85+
Ok(())
86+
}
87+
88+
#[sqlx::test]
89+
async fn order_by_asc_with_greater_than(pool: PgPool) -> Result<()> {
90+
// Test: ORDER BY e ASC with WHERE e > 42
91+
// Original SQL lines 33-36 in src/operators/order_by_test.sql
92+
93+
let ore_term = get_ore_encrypted(&pool, 42).await?;
94+
95+
let sql = format!(
96+
"SELECT id FROM ore WHERE e > '{}'::eql_v2_encrypted ORDER BY e ASC",
97+
ore_term
98+
);
99+
100+
// Should return 57 records (43-99)
101+
QueryAssertion::new(&pool, &sql).count(57).await;
102+
103+
Ok(())
104+
}
105+
106+
#[sqlx::test]
107+
async fn order_by_desc_with_greater_than_returns_highest(pool: PgPool) -> Result<()> {
108+
// Test: ORDER BY e DESC LIMIT 1 with e > 42 returns 99
109+
// Original SQL lines 38-41 in src/operators/order_by_test.sql
110+
111+
let ore_term = get_ore_encrypted(&pool, 42).await?;
112+
113+
let sql = format!(
114+
"SELECT id FROM ore WHERE e > '{}'::eql_v2_encrypted ORDER BY e DESC LIMIT 1",
115+
ore_term
116+
);
117+
118+
let row = sqlx::query(&sql).fetch_one(&pool).await?;
119+
let id: i32 = row.try_get(0)?;
120+
assert_eq!(id, 99, "Should return id=99 (highest value > 42)");
121+
122+
Ok(())
123+
}
124+
125+
#[sqlx::test]
126+
async fn order_by_asc_with_greater_than_returns_lowest(pool: PgPool) -> Result<()> {
127+
// Test: ORDER BY e ASC LIMIT 1 with e > 42 returns 43
128+
// Original SQL lines 43-46 in src/operators/order_by_test.sql
129+
130+
let ore_term = get_ore_encrypted(&pool, 42).await?;
131+
132+
let sql = format!(
133+
"SELECT id FROM ore WHERE e > '{}'::eql_v2_encrypted ORDER BY e ASC LIMIT 1",
134+
ore_term
135+
);
136+
137+
let row = sqlx::query(&sql).fetch_one(&pool).await?;
138+
let id: i32 = row.try_get(0)?;
139+
assert_eq!(id, 43, "Should return id=43 (lowest value > 42)");
140+
141+
Ok(())
142+
}

0 commit comments

Comments
 (0)