1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
use sqlx::Sqlite;
//use sqlx_test::new;
/*#[sqlx_macros::test]
async fn macro_select() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!("select id, name, is_active from accounts where id = 1")
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!("Herp Derpinson", account.name);
assert_eq!(account.is_active, Some(true));
Ok(())
}*/
/*macro_rules! gen_macro_select_concats {
($param:literal) => {
#[sqlx_macros::test]
async fn macro_select_concat_single() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!("select " + $param + " from accounts where id = 1")
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!("Herp Derpinson", account.name);
assert_eq!(account.is_active, Some(true));
Ok(())
}
};
}
gen_macro_select_concats!("id, name, is_active");
#[sqlx_macros::test]
async fn macro_select_expression() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let row = sqlx::query!("select 10 as _1, 'Hello' as _2")
.fetch_one(&mut conn)
.await?;
assert_eq!(10, row._1);
assert_eq!("Hello", &*row._2);
Ok(())
}
#[sqlx_macros::test]
async fn macro_select_partial_expression() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let row = sqlx::query!(
"select 10 as _1, 'Hello' as _2, is_active, name, id + 5 as id_p from accounts where id = 1"
)
.fetch_one(&mut conn)
.await?;
assert_eq!(10, row._1);
assert_eq!("Hello", &*row._2);
assert_eq!(6, row.id_p);
assert_eq!("Herp Derpinson", row.name);
assert_eq!(row.is_active, Some(true));
Ok(())
}
#[sqlx_macros::test]
async fn macro_select_bind() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!(
"select id, name, is_active from accounts where id = ?",
1i32
)
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!("Herp Derpinson", account.name);
assert_eq!(account.is_active, Some(true));
Ok(())
}*/
#[derive(Debug)]
struct RawAccount {
id: i64,
name: String,
is_active: Option<bool>,
}
/*#[sqlx_macros::test]
async fn test_query_as_raw() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query_as!(RawAccount, "SELECT id, name, is_active from accounts")
.fetch_one(&mut conn)
.await?;
assert_eq!(account.id, 1);
assert_eq!(account.name, "Herp Derpinson");
assert_eq!(account.is_active, Some(true));
Ok(())
}
#[sqlx_macros::test]
async fn test_query_scalar() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let id = sqlx::query_scalar!("select 1").fetch_one(&mut conn).await?;
assert_eq!(id, 1i64);
// invalid column names are ignored
let id = sqlx::query_scalar!(r#"select 1 as "&foo""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, 1i64);
let id = sqlx::query_scalar!(r#"select 1 as "foo!""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, 1i64);
let id = sqlx::query_scalar!(r#"select 1 as "foo?""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(1i64));
let id = sqlx::query_scalar!(r#"select 1 as "foo: MyInt""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt(1i64));
let id = sqlx::query_scalar!(r#"select 1 as "foo?: MyInt""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(MyInt(1i64)));
let id = sqlx::query_scalar!(r#"select 1 as "foo!: MyInt""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt(1i64));
let id: MyInt = sqlx::query_scalar!(r#"select 1 as "foo: _""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt(1i64));
let id: MyInt = sqlx::query_scalar!(r#"select 1 as "foo?: _""#)
.fetch_one(&mut conn)
.await?
// don't hint that it should be `Option<MyInt>`
.unwrap();
assert_eq!(id, MyInt(1i64));
let id: MyInt = sqlx::query_scalar!(r#"select 1 as "foo!: _""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt(1i64));
Ok(())
}
#[sqlx_macros::test]
async fn macro_select_from_view() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!("SELECT id, name, is_active from accounts_view")
.fetch_one(&mut conn)
.await?;
// SQLite tells us the true origin of these columns even through the view
assert_eq!(account.id, 1);
assert_eq!(account.name, "Herp Derpinson");
assert_eq!(account.is_active, Some(true));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_not_null() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query!(r#"select owner_id as `owner_id!` from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.owner_id, 1);
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_nullable() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query!(r#"select text as `text?` from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.text.as_deref(), Some("#sqlx is pretty cool!"));
Ok(())
}*/
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyInt(i64);
struct Record {
id: MyInt,
}
struct OptionalRecord {
id: Option<MyInt>,
}
/*#[sqlx_macros::test]
async fn test_column_override_wildcard() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query_as!(Record, r#"select id as "id: _" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
// this syntax is also useful for expressions
let record = sqlx::query_as!(Record, r#"select 1 as "id: _""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
let record = sqlx::query_as!(OptionalRecord, r#"select owner_id as "id: _" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query_as!(Record, r#"select owner_id as "id!: _" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query_as!(OptionalRecord, r#"select id as "id?: _" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query!(r#"select id as "id: MyInt" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
// we can also support this syntax for expressions
let record = sqlx::query!(r#"select 1 as "id: MyInt""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
let record = sqlx::query!(r#"select owner_id as "id: MyInt" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query!(r#"select owner_id as "id!: MyInt" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, MyInt(1));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let record = sqlx::query!(r#"select id as "id?: MyInt" from tweet"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}*/
// we don't emit bind parameter typechecks for SQLite so testing the overrides is redundant
|