| 1 |
use sqlx::mysql::MySql; |
| 2 |
use sqlx::{Column, Executor, Type, TypeInfo}; |
| 3 |
use sqlx_test::new; |
| 4 |
|
| 5 |
#[sqlx_macros::test] |
| 6 |
async fn it_describes_simple() -> anyhow::Result<()> { |
| 7 |
let mut conn = new::<MySql>().await?; |
| 8 |
|
| 9 |
let d = conn.describe("SELECT * FROM tweet").await?; |
| 10 |
|
| 11 |
assert_eq!(d.columns()[0].name(), "id"); |
| 12 |
assert_eq!(d.columns()[1].name(), "created_at"); |
| 13 |
assert_eq!(d.columns()[2].name(), "text"); |
| 14 |
assert_eq!(d.columns()[3].name(), "owner_id"); |
| 15 |
|
| 16 |
assert_eq!(d.nullable(0), Some(false)); |
| 17 |
assert_eq!(d.nullable(1), Some(false)); |
| 18 |
assert_eq!(d.nullable(2), Some(false)); |
| 19 |
assert_eq!(d.nullable(3), Some(true)); |
| 20 |
|
| 21 |
assert_eq!(d.columns()[0].type_info().name(), "BIGINT"); |
| 22 |
assert_eq!(d.columns()[1].type_info().name(), "TIMESTAMP"); |
| 23 |
assert_eq!(d.columns()[2].type_info().name(), "TEXT"); |
| 24 |
assert_eq!(d.columns()[3].type_info().name(), "BIGINT"); |
| 25 |
|
| 26 |
Ok(()) |
| 27 |
} |
| 28 |
|
| 29 |
#[sqlx_macros::test] |
| 30 |
async fn test_boolean() -> anyhow::Result<()> { |
| 31 |
let mut conn = new::<MySql>().await?; |
| 32 |
|
| 33 |
conn.execute( |
| 34 |
r#" |
| 35 |
CREATE TEMPORARY TABLE with_bit_and_tinyint ( |
| 36 |
id INT PRIMARY KEY AUTO_INCREMENT, |
| 37 |
value_bit_1 BIT(1), |
| 38 |
value_bool BOOLEAN, |
| 39 |
bit_n BIT(64), |
| 40 |
value_int TINYINT |
| 41 |
); |
| 42 |
"#, |
| 43 |
) |
| 44 |
.await?; |
| 45 |
|
| 46 |
let d = conn.describe("SELECT * FROM with_bit_and_tinyint").await?; |
| 47 |
|
| 48 |
assert_eq!(d.column(2).name(), "value_bool"); |
| 49 |
assert_eq!(d.column(2).type_info().name(), "BOOLEAN"); |
| 50 |
|
| 51 |
assert_eq!(d.column(1).name(), "value_bit_1"); |
| 52 |
assert_eq!(d.column(1).type_info().name(), "BIT"); |
| 53 |
|
| 54 |
assert!(<bool as Type<MySql>>::compatible(&d.column(1).type_info())); |
| 55 |
assert!(<bool as Type<MySql>>::compatible(&d.column(2).type_info())); |
| 56 |
|
| 57 |
Ok(()) |
| 58 |
} |
| 59 |
|
| 60 |
#[sqlx_macros::test] |
| 61 |
async fn uses_alias_name() -> anyhow::Result<()> { |
| 62 |
let mut conn = new::<MySql>().await?; |
| 63 |
|
| 64 |
let d = conn |
| 65 |
.describe("SELECT text AS tweet_text FROM tweet") |
| 66 |
.await?; |
| 67 |
|
| 68 |
assert_eq!(d.columns()[0].name(), "tweet_text"); |
| 69 |
|
| 70 |
Ok(()) |
| 71 |
} |