()
| 4 | |
| 5 | #[test] |
| 6 | fn test_decimal_ordering_integration() { |
| 7 | let conn = Connection::open_in_memory().unwrap(); |
| 8 | |
| 9 | // Initialize metadata and functions |
| 10 | TypeMetadata::init(&conn).unwrap(); |
| 11 | register_all_functions(&conn).unwrap(); |
| 12 | |
| 13 | // Create a table with decimal values |
| 14 | conn.execute( |
| 15 | "CREATE TABLE prices ( |
| 16 | id INTEGER PRIMARY KEY, |
| 17 | item TEXT, |
| 18 | amount TEXT |
| 19 | )", |
| 20 | [], |
| 21 | ).unwrap(); |
| 22 | |
| 23 | // Register as DECIMAL type |
| 24 | conn.execute( |
| 25 | "INSERT INTO __pgsqlite_schema (table_name, column_name, pg_type, sqlite_type) |
| 26 | VALUES ('prices', 'amount', 'NUMERIC', 'DECIMAL')", |
| 27 | [], |
| 28 | ).unwrap(); |
| 29 | |
| 30 | // Insert test data - these would sort incorrectly as text |
| 31 | let test_data = vec![ |
| 32 | ("item1", "100"), |
| 33 | ("item2", "20"), |
| 34 | ("item3", "3"), |
| 35 | ("item4", "1000"), |
| 36 | ("item5", "99.99"), |
| 37 | ("item6", "-5"), |
| 38 | ("item7", "0.5"), |
| 39 | ]; |
| 40 | |
| 41 | for (item, amount) in test_data { |
| 42 | conn.execute( |
| 43 | "INSERT INTO prices (item, amount) VALUES (?1, ?2)", |
| 44 | [item, amount], |
| 45 | ).unwrap(); |
| 46 | } |
| 47 | |
| 48 | // Test ordering with CAST (simulating what our rewriter would do) |
| 49 | let mut stmt = conn.prepare("SELECT item, amount FROM prices ORDER BY CAST(amount AS REAL)").unwrap(); |
| 50 | let results: Vec<(String, String)> = stmt.query_map([], |row| { |
| 51 | Ok((row.get(0)?, row.get(1)?)) |
| 52 | }).unwrap().collect::<Result<Vec<_>, _>>().unwrap(); |
| 53 | |
| 54 | // Verify correct numeric ordering |
| 55 | assert_eq!(results[0].0, "item6"); // -5 |
| 56 | assert_eq!(results[1].0, "item7"); // 0.5 |
| 57 | assert_eq!(results[2].0, "item3"); // 3 |
| 58 | assert_eq!(results[3].0, "item2"); // 20 |
| 59 | assert_eq!(results[4].0, "item5"); // 99.99 |
| 60 | assert_eq!(results[5].0, "item1"); // 100 |
| 61 | assert_eq!(results[6].0, "item4"); // 1000 |
| 62 | |
| 63 | // Test descending order |
nothing calls this directly
no test coverage detected