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
use async_trait::async_trait;
use std::cell::RefCell;
use std::fmt::Debug;
use std::rc::Rc;
use crate::executor::{execute, Payload};
use crate::parse::parse;
use crate::result::Result;
use crate::store::{AlterTable, Store, StoreMut};
pub async fn run<T: 'static + Debug, U: Store<T> + StoreMut<T> + AlterTable>(
cell: Rc<RefCell<Option<U>>>,
sql: &str,
) -> Result<Payload> {
println!("[Run] {}", sql);
let mut storage = cell.replace(None).unwrap();
let query = &parse(sql).unwrap()[0];
let result = match execute(storage, query).await {
Ok((s, payload)) => {
storage = s;
Ok(payload)
}
Err((s, error)) => {
storage = s;
Err(error)
}
};
cell.replace(Some(storage));
result
}
#[async_trait]
pub trait Tester<T: 'static + Debug, U: Store<T> + StoreMut<T> + AlterTable> {
fn new(namespace: &str) -> Self;
fn get_cell(&mut self) -> Rc<RefCell<Option<U>>>;
}
#[macro_export]
macro_rules! test_case {
($name: ident, $content: expr) => {
pub async fn $name<T, U>(mut tester: impl tests::Tester<T, U>)
where
T: 'static + std::fmt::Debug,
U: Store<T> + StoreMut<T> + AlterTable,
{
use std::rc::Rc;
let cell = tester.get_cell();
#[allow(unused_macros)]
macro_rules! run {
($sql: expr) => {
tests::run(Rc::clone(&cell), $sql).await.unwrap()
};
}
#[allow(unused_macros)]
macro_rules! count {
($count: expr, $sql: expr) => {
match tests::run(Rc::clone(&cell), $sql).await.unwrap() {
Payload::Select { rows, .. } => assert_eq!($count, rows.len()),
Payload::Delete(num) => assert_eq!($count, num),
Payload::Update(num) => assert_eq!($count, num),
_ => panic!("compare is only for Select, Delete and Update"),
};
};
}
#[allow(unused_macros)]
macro_rules! test {
($expected: expr, $sql: expr) => {
let found = tests::run(Rc::clone(&cell), $sql).await;
assert_eq!($expected, found);
};
}
$content.await
}
};
}