sqlx/tests/mysql/describe.rs
Peter Maatman 2a5ea2f71b mysql: always use column alias when available
If a query has a column alias available we should use that name instead
of the column name.

Fixes #385
2020-06-10 00:42:58 -07:00

53 lines
1.4 KiB
Rust

use sqlx::mysql::MySql;
use sqlx::Executor;
use sqlx_core::describe::Column;
use sqlx_test::new;
fn type_names(columns: &[Column<MySql>]) -> Vec<String> {
columns
.iter()
.filter_map(|col| Some(col.type_info.as_ref()?.to_string()))
.collect()
}
#[sqlx_macros::test]
async fn it_describes_simple() -> anyhow::Result<()> {
let mut conn = new::<MySql>().await?;
let d = conn.describe("SELECT * FROM tweet").await?;
let columns = d.columns;
assert_eq!(columns[0].name, "id");
assert_eq!(columns[1].name, "created_at");
assert_eq!(columns[2].name, "text");
assert_eq!(columns[3].name, "owner_id");
assert_eq!(columns[0].not_null, Some(true));
assert_eq!(columns[1].not_null, Some(true));
assert_eq!(columns[2].not_null, Some(true));
assert_eq!(columns[3].not_null, Some(false));
let column_type_names = type_names(&columns);
assert_eq!(column_type_names[0], "BIGINT");
assert_eq!(column_type_names[1], "TIMESTAMP");
assert_eq!(column_type_names[2], "TEXT");
assert_eq!(column_type_names[3], "BIGINT");
Ok(())
}
#[sqlx_macros::test]
async fn uses_alias_name() -> anyhow::Result<()> {
let mut conn = new::<MySql>().await?;
let d = conn
.describe("SELECT text AS tweet_text FROM tweet")
.await?;
let columns = d.columns;
assert_eq!(columns[0].name, "tweet_text");
Ok(())
}