Skip to content

Commit 82d56f2

Browse files
committed
test(odbc): add test for PostgreSQL BYTEA hex output handling
This commit introduces a new test to verify the handling of BYTEA data type in PostgreSQL, specifically focusing on UTF-8 encoded binary data. The test ensures that data inserted into a temporary table is correctly retrieved and matches the expected output, enhancing the robustness of binary data handling in ODBC connections.
1 parent a1ae1c7 commit 82d56f2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/odbc/odbc.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,54 @@ async fn it_handles_prepared_statement_with_wrong_parameters() -> anyhow::Result
11031103
Ok(())
11041104
}
11051105

1106+
#[tokio::test]
1107+
async fn it_handles_postgres_bytea_hex_output() -> anyhow::Result<()> {
1108+
let mut conn = new::<Odbc>().await?;
1109+
1110+
let dbms = conn.dbms_name().await?;
1111+
if dbms != "PostgreSQL" {
1112+
return Ok(());
1113+
}
1114+
1115+
let utf8_text = "Héllö world! 😀";
1116+
let utf8_bytes = utf8_text.as_bytes().to_vec();
1117+
1118+
conn.execute("DROP TABLE IF EXISTS sqlpage_files").await?;
1119+
conn.execute("CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_files(path VARCHAR(255) NOT NULL PRIMARY KEY, contents BYTEA, last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP)").await?;
1120+
1121+
let insert_stmt = conn
1122+
.prepare("INSERT INTO sqlpage_files(path, contents) VALUES (?, ?)")
1123+
.await?;
1124+
insert_stmt
1125+
.query()
1126+
.bind("unit test file.txt")
1127+
.bind(&utf8_bytes)
1128+
.execute(&mut conn)
1129+
.await?;
1130+
1131+
let select_stmt = conn
1132+
.prepare("SELECT contents from sqlpage_files WHERE path = ?")
1133+
.await?;
1134+
let row = select_stmt
1135+
.query()
1136+
.bind("unit test file.txt")
1137+
.fetch_one(&mut conn)
1138+
.await?;
1139+
1140+
let retrieved = row.try_get_raw(0)?.to_owned().decode::<Vec<u8>>();
1141+
let retrieved_text = String::from_utf8(retrieved.clone())?;
1142+
1143+
assert_eq!(
1144+
retrieved_text, utf8_text,
1145+
"Expected '{}' but got '{}'. Original bytes: {:?}, Retrieved bytes: {:?}",
1146+
utf8_text, retrieved_text, utf8_bytes, retrieved
1147+
);
1148+
1149+
conn.execute("DROP TABLE sqlpage_files").await?;
1150+
1151+
Ok(())
1152+
}
1153+
11061154
#[tokio::test]
11071155
async fn it_works_with_buffered_and_unbuffered_mode() -> anyhow::Result<()> {
11081156
let count = 450;

0 commit comments

Comments
 (0)