Skip to main content

tank_tests/
identifiers.rs

1use std::sync::LazyLock;
2use tank::{Entity, Executor, expr};
3use tokio::sync::Mutex;
4
5static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
6
7/// Entity whose column names contain characters that require escaping
8/// by the driver's identifier quoting mechanism (e.g. backticks in MySQL).
9#[derive(Entity, Debug, PartialEq)]
10#[tank(name = "special_ids")]
11pub struct SpecialIdentifiers {
12    #[tank(primary_key)]
13    pub id: i32,
14    #[tank(name = "back`tick")]
15    pub backtick_col: String,
16    #[tank(name = "double\"quote")]
17    pub double_quote_col: i32,
18}
19
20pub async fn identifiers(executor: &mut impl Executor) {
21    let _lock = MUTEX.lock().await;
22
23    SpecialIdentifiers::drop_table(executor, true, false)
24        .await
25        .expect("Failed to drop SpecialIdentifiers table");
26    SpecialIdentifiers::create_table(executor, true, true)
27        .await
28        .expect("Failed to create SpecialIdentifiers table");
29
30    let entity = SpecialIdentifiers {
31        id: 1,
32        backtick_col: "hello".to_string(),
33        double_quote_col: 42,
34    };
35    entity
36        .save(executor)
37        .await
38        .expect("Failed to save entity with special identifier characters");
39
40    let found = SpecialIdentifiers::find_one(executor, expr!(SpecialIdentifiers::id == 1))
41        .await
42        .expect("Failed to query")
43        .expect("Entity not found");
44    assert_eq!(found, entity);
45
46    SpecialIdentifiers::delete_many(executor, expr!(SpecialIdentifiers::id == 1))
47        .await
48        .expect("Failed to delete");
49
50    let gone = SpecialIdentifiers::find_one(executor, expr!(SpecialIdentifiers::id == 1))
51        .await
52        .expect("Failed to query after delete");
53    assert!(gone.is_none());
54}