tank_tests/
multiple.rs

1#![allow(dead_code)]
2use std::sync::LazyLock;
3use tank::AsValue;
4use tank::{Driver, Entity, Executor, QueryResult, SqlWriter, stream::TryStreamExt};
5use tokio::sync::Mutex;
6
7#[derive(Debug, Entity, PartialEq)]
8struct One {
9    a1: u32,
10    string: String,
11    c1: u64,
12}
13#[derive(Debug, Entity, PartialEq)]
14struct Two {
15    a2: u32,
16    string: String,
17}
18#[derive(Debug, Entity, PartialEq)]
19struct Three {
20    string: String,
21}
22static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
23
24pub async fn multiple<E: Executor>(executor: &mut E) {
25    let _lock = MUTEX.lock().await;
26
27    let mut sql = String::new();
28    let writer = executor.driver().sql_writer();
29    sql.push_str("    \n\n  \n \n\t\t\n   \n    ");
30    // 1
31    writer.write_drop_table::<One>(&mut sql, true);
32    sql.push_str("\t\t");
33    // 2
34    writer.write_drop_table::<Two>(&mut sql, true);
35    // 3
36    writer.write_drop_table::<Three>(&mut sql, true);
37    // 4
38    writer.write_create_table::<One>(&mut sql, true);
39    sql.push('\n');
40    // 5
41    writer.write_create_table::<Two>(&mut sql, true);
42    // 6
43    writer.write_create_table::<Three>(&mut sql, true);
44    sql.push_str(" ");
45    // 7
46    writer.write_insert(
47        &mut sql,
48        [
49            &Two {
50                a2: 21,
51                string: "aaa".into(),
52            },
53            &Two {
54                a2: 22,
55                string: "bbb".into(),
56            },
57            &Two {
58                a2: 23,
59                string: "eee".into(),
60            },
61        ],
62        false,
63    );
64    sql.push_str("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
65    // 8
66    writer.write_insert(
67        &mut sql,
68        [
69            &Three {
70                string: "ddd".into(),
71            },
72            &Three {
73                string: "ccc".into(),
74            },
75        ],
76        false,
77    );
78    // 9
79    writer.write_select(&mut sql, [Three::string], Three::table(), &true, None);
80    // 10
81    writer.write_insert(
82        &mut sql,
83        [&One {
84            a1: 11,
85            string: "zzz".into(),
86            c1: 512,
87        }],
88        false,
89    );
90    // 11
91    writer.write_select(
92        &mut sql,
93        [One::a1, One::string, One::c1],
94        One::table(),
95        &true,
96        None,
97    );
98    // 12
99    writer.write_select(&mut sql, [Two::a2, Two::string], Two::table(), &true, None);
100    sql.push_str("            \t    \t\t  \n \n \n \t    \n\n\n ");
101    let result = executor
102        .run(sql)
103        .try_collect::<Vec<_>>()
104        .await
105        .expect("Could not run the composite query");
106    // 12 statements but one select returns 3 rows and another one returns 2 rows (12 - 2 + 3 + 2 = 15)
107    assert_eq!(result.len(), 15);
108    let mut result = result
109        .into_iter()
110        .filter_map(|v| match v {
111            QueryResult::Row(row) => Some(row),
112            QueryResult::Affected(..) => None,
113        })
114        .collect::<Vec<_>>();
115    result.sort_by(|a, b| {
116        let a = a
117            .get_column("string")
118            .map(|v| String::try_from_value(v.clone()))
119            .expect("Does not have column \"string\"")
120            .expect("The column called `string` is not a VARCHAR");
121        let b = b
122            .get_column("string")
123            .map(|v| String::try_from_value(v.clone()))
124            .expect("Does not have column \"string\"")
125            .expect("The column called `string` is not a VARCHAR");
126        a.cmp(&b)
127    });
128    assert_eq!(result.len(), 6);
129    let mut result = result.into_iter().peekable();
130    assert_eq!(*result.peek().unwrap().labels, ["a2", "string"]);
131    assert_eq!(
132        Two::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
133        Two {
134            a2: 21,
135            string: "aaa".into()
136        }
137    );
138    result.next();
139    assert_eq!(*result.peek().unwrap().labels, ["a2", "string"]);
140    assert_eq!(
141        Two::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
142        Two {
143            a2: 22,
144            string: "bbb".into()
145        }
146    );
147    result.next();
148    assert_eq!(*result.peek().unwrap().labels, ["string"]);
149    assert_eq!(
150        Three::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
151        Three {
152            string: "ccc".into(),
153        }
154    );
155    result.next();
156    assert_eq!(*result.peek().unwrap().labels, ["string"]);
157    assert_eq!(
158        Three::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
159        Three {
160            string: "ddd".into(),
161        }
162    );
163    result.next();
164    assert_eq!(*result.peek().unwrap().labels, ["a2", "string"]);
165    assert_eq!(
166        Two::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
167        Two {
168            a2: 23,
169            string: "eee".into()
170        }
171    );
172    result.next();
173    assert_eq!(*result.peek().unwrap().labels, ["a1", "string", "c1"]);
174    assert_eq!(
175        One::from_row(result.peek().unwrap().clone()).expect("The row was not an entity Two"),
176        One {
177            a1: 11,
178            string: "zzz".into(),
179            c1: 512,
180        }
181    );
182    result.next();
183}