Debian Codegraph @

tests/postgres/describe.rs

raw symbols SCIP
1 use sqlx::{postgres::Postgres, Column, Executor, TypeInfo};
2 use sqlx_test::new;
3
4 #[sqlx_macros::test]
5 async fn it_describes_simple() -> anyhow::Result<()> {
6 let mut conn = new::<Postgres>().await?;
7
8 let d = conn.describe("SELECT * FROM tweet").await?;
9
10 assert_eq!(d.columns()[0].name(), "id");
11 assert_eq!(d.columns()[1].name(), "created_at");
12 assert_eq!(d.columns()[2].name(), "text");
13 assert_eq!(d.columns()[3].name(), "owner_id");
14
15 assert_eq!(d.nullable(0), Some(false));
16 assert_eq!(d.nullable(1), Some(false));
17 assert_eq!(d.nullable(2), Some(false));
18 assert_eq!(d.nullable(3), Some(true));
19
20 assert_eq!(d.columns()[0].type_info().name(), "INT8");
21 assert_eq!(d.columns()[1].type_info().name(), "TIMESTAMPTZ");
22 assert_eq!(d.columns()[2].type_info().name(), "TEXT");
23 assert_eq!(d.columns()[3].type_info().name(), "INT8");
24
25 Ok(())
26 }
27
28 #[sqlx_macros::test]
29 async fn it_describes_expression() -> anyhow::Result<()> {
30 let mut conn = new::<Postgres>().await?;
31
32 let d = conn.describe("SELECT 1::int8 + 10").await?;
33
34 // ?column? will cause the macro to emit an error ad ask the user to explicitly name the type
35 assert_eq!(d.columns()[0].name(), "?column?");
36
37 // postgres cannot infer nullability from an expression
38 // this will cause the macro to emit `Option<_>`
39 assert_eq!(d.nullable(0), None);
40 assert_eq!(d.columns()[0].type_info().name(), "INT8");
41
42 Ok(())
43 }
44
45 #[sqlx_macros::test]
46 async fn it_describes_enum() -> anyhow::Result<()> {
47 let mut conn = new::<Postgres>().await?;
48
49 let d = conn.describe("SELECT 'open'::status as _1").await?;
50
51 assert_eq!(d.columns()[0].name(), "_1");
52
53 let ty = d.columns()[0].type_info();
54
55 assert_eq!(ty.name(), "status");
56
57 assert_eq!(
58 format!("{:?}", ty.kind()),
59 r#"Enum(["new", "open", "closed"])"#
60 );
61
62 Ok(())
63 }
64
65 #[sqlx_macros::test]
66 async fn it_describes_record() -> anyhow::Result<()> {
67 let mut conn = new::<Postgres>().await?;
68
69 let d = conn.describe("SELECT (true, 10::int2)").await?;
70
71 let ty = d.columns()[0].type_info();
72 assert_eq!(ty.name(), "RECORD");
73
74 Ok(())
75 }
76
77 #[sqlx_macros::test]
78 async fn it_describes_composite() -> anyhow::Result<()> {
79 let mut conn = new::<Postgres>().await?;
80
81 let d = conn
82 .describe("SELECT ROW('name',10,500)::inventory_item")
83 .await?;
84
85 let ty = d.columns()[0].type_info();
86
87 assert_eq!(ty.name(), "inventory_item");
88
89 assert_eq!(
90 format!("{:?}", ty.kind()),
91 r#"Composite([("name", PgTypeInfo(Text)), ("supplier_id", PgTypeInfo(Int4)), ("price", PgTypeInfo(Int8))])"#
92 );
93
94 Ok(())
95 }