Skip to content

Commit 94c274d

Browse files
committed
Update sqlx dependencies to version 0.6.49-beta.4 and refactor database interaction in tests for improved clarity and consistency.
1 parent d4dd9c2 commit 94c274d

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ panic = "abort"
1818
codegen-units = 2
1919

2020
[dependencies]
21-
sqlx = { package = "sqlx-oldapi", version = "0.6.49-beta.3", default-features = false, features = [
21+
sqlx = { package = "sqlx-oldapi", version = "0.6.49-beta.4", default-features = false, features = [
2222
"any",
2323
"runtime-tokio-rustls",
2424
"migrate",

src/filesystem.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,12 @@ async fn test_sql_file_read_utf8() -> anyhow::Result<()> {
342342
let config = app_config::tests::test_config();
343343
let state = AppState::init(&config).await?;
344344
let create_table_sql = DbFsQueries::get_create_table_sql(state.db.info.database_type);
345-
state
346-
.db
347-
.connection
348-
.execute(format!("DROP TABLE IF EXISTS sqlpage_files; {create_table_sql}").as_str())
349-
.await?;
345+
let db = &state.db;
346+
let conn = &db.connection;
347+
conn.execute("DROP TABLE IF EXISTS sqlpage_files").await?;
348+
conn.execute(create_table_sql).await?;
350349

351-
let dbms = state.db.info.kind;
350+
let dbms = db.info.kind;
352351
let insert_sql = format!(
353352
"INSERT INTO sqlpage_files(path, contents) VALUES ({}, {})",
354353
make_placeholder(dbms, 1),
@@ -357,10 +356,10 @@ async fn test_sql_file_read_utf8() -> anyhow::Result<()> {
357356
sqlx::query(&insert_sql)
358357
.bind("unit test file.txt")
359358
.bind("Héllö world! 😀".as_bytes())
360-
.execute(&state.db.connection)
359+
.execute(conn)
361360
.await?;
362361

363-
let fs = FileSystem::init("/", &state.db).await;
362+
let fs = FileSystem::init("/", &db).await;
364363
let actual = fs
365364
.read_to_string(&state, "unit test file.txt".as_ref(), false)
366365
.await?;

src/webserver/database/sql_to_json.rs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,24 @@ mod tests {
149149
let db_url = test_database_url();
150150
let mut c = sqlx::AnyConnection::connect(&db_url).await?;
151151
let row = sqlx::query(
152-
"SELECT \
153-
123.456 as one_value, \
154-
1 as two_values, \
155-
2 as two_values, \
156-
'x' as three_values, \
157-
'y' as three_values, \
158-
'z' as three_values \
159-
",
152+
r#"SELECT
153+
123.456 as "ONE_VALUE",
154+
1 as "TWO_VALUES",
155+
2 as "TWO_VALUES",
156+
'x' as "THREE_VALUES",
157+
'y' as "THREE_VALUES",
158+
'z' as "THREE_VALUES"
159+
"#,
160160
)
161161
.fetch_one(&mut c)
162162
.await?;
163-
assert_eq!(
164-
row_to_json(&row),
165-
serde_json::json!({
166-
"one_value": 123.456,
167-
"two_values": [1,2],
168-
"three_values": ["x","y","z"],
169-
})
163+
expect_json_object_equal(
164+
&row_to_json(&row),
165+
&serde_json::json!({
166+
"ONE_VALUE": 123.456,
167+
"TWO_VALUES": [1,2],
168+
"THREE_VALUES": ["x","y","z"],
169+
}),
170170
);
171171
Ok(())
172172
}
@@ -466,7 +466,7 @@ mod tests {
466466
fn expect_json_object_equal(actual: &Value, expected: &Value) {
467467
use std::fmt::Write;
468468

469-
if actual == expected {
469+
if json_values_equal(actual, expected) {
470470
return;
471471
}
472472
let actual = actual.as_object().unwrap();
@@ -480,7 +480,7 @@ mod tests {
480480
for key in all_keys {
481481
let actual_value = actual.get(key).unwrap_or(&Value::Null);
482482
let expected_value = expected.get(key).unwrap_or(&Value::Null);
483-
if actual_value == expected_value {
483+
if json_values_equal(actual_value, expected_value) {
484484
continue;
485485
}
486486
writeln!(
@@ -494,4 +494,33 @@ mod tests {
494494
"JSON objects are not equal:\n\n{comparison_string}"
495495
);
496496
}
497+
498+
/// Compare JSON values, treating integers and floats that are numerically equal as equal
499+
fn json_values_equal(a: &Value, b: &Value) -> bool {
500+
use Value::*;
501+
502+
match (a, b) {
503+
(Null, Null) => true,
504+
(Bool(a), Bool(b)) => a == b,
505+
(Number(a), Number(b)) => {
506+
// Treat integers and floats as equal if they represent the same numerical value
507+
a.as_f64() == b.as_f64()
508+
}
509+
(String(a), String(b)) => a == b,
510+
(Array(a), Array(b)) => {
511+
a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| json_values_equal(a, b))
512+
}
513+
(Object(a), Object(b)) => {
514+
if a.len() != b.len() {
515+
return false;
516+
}
517+
a.iter().all(|(key, value)| {
518+
b.get(key).map_or(false, |expected_value| {
519+
json_values_equal(value, expected_value)
520+
})
521+
})
522+
}
523+
_ => false,
524+
}
525+
}
497526
}

0 commit comments

Comments
 (0)