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
#[macro_export]
macro_rules! assert_query {
    ($runner:expr, $q:expr, $result:expr) => {
        let result = $runner.query($q).await?;
        assert_eq!(result.to_string(), $result);
    };
}

#[macro_export]
macro_rules! match_connector_result {
    ($runner:expr, $q:expr, $( $($matcher:pat_param)|+ $( if $pred:expr )? => $result:expr ),*) => {
        use query_tests_setup::*;
        use query_tests_setup::ConnectorVersion::*;

        let connector = $runner.connector_version();

        let mut results = match &connector {
            $(
                $( $matcher )|+ $( if $pred )? => $result
            ),*
        };

        let query_result = $runner.query($q).await?.to_string();

        if results.len() == 0 {
            panic!("No results defined for connector {connector}. Query result: {query_result}");
        }

        assert_eq!(
            results.contains(&query_result.as_str()),
            true,
            "Query result: {query_result} is not part of the expected results: {results:?} for connector {connector}",
        );
    };
}

#[macro_export]
macro_rules! is_one_of {
    ($result:expr, $potential_results:expr) => {
        assert_eq!(
            $potential_results.contains(&$result.as_str()),
            true,
            "Query result: {} is not part of the expected results: {:?}",
            $result,
            $potential_results
        );
    };
}

#[macro_export]
macro_rules! run_query {
    ($runner:expr, $q:expr) => {{
        let res = $runner.query($q.to_string()).await?;
        res.assert_success();
        res.to_string()
    }};
}

#[macro_export]
macro_rules! run_query_pretty {
    ($runner:expr, $q:expr) => {{
        let res = $runner.query($q.to_string()).await?;
        res.assert_success();
        res.to_string_pretty()
    }};
}

#[macro_export]
macro_rules! run_query_json {
    ($runner:expr, $q:expr) => {
        serde_json::from_str::<serde_json::Value>($runner.query($q).await?.to_string().as_str()).unwrap()
    };
    ($runner:expr, $q:expr, $path: expr) => {
        query_tests_setup::walk_json(
            &serde_json::from_str::<serde_json::Value>($runner.query($q).await?.to_string().as_str()).unwrap(),
            $path,
        )
        .unwrap()
        .to_owned()
    };
}

#[macro_export]
macro_rules! assert_error {
    ($runner:expr, $q:expr, $code:expr) => {
        $runner.query($q).await?.assert_failure($code, None);
    };
    ($runner:expr, $q:expr, $code:expr, $msg:expr) => {
        $runner.query($q).await?.assert_failure($code, Some($msg.to_string()));
    };
}

#[macro_export]
macro_rules! retry {
    ($body:block, $times:expr) => {{
        use std::time::Duration;
        use tokio::time::sleep;

        let mut retries = $times;

        loop {
            let res = $body.await?;

            if !res.failed() {
                break res;
            }

            if retries > 0 {
                retries -= 1;
                sleep(Duration::from_millis(5)).await;
                continue;
            }

            break res;
        }
    }};
}

#[macro_export]
macro_rules! with_id_excess {
    ($runner:expr, $query_template:expr) => {{
        let max_bind_values = $runner
            .max_bind_values()
            .expect("Test expected to run only for relational databases.");

        let cycle = |argn: usize| (argn % 10 + 1).to_string();
        let id_list = (0..=max_bind_values).map(cycle).collect::<Vec<_>>().join(",");
        $query_template.replace(":id_list:", &id_list)
    }};
}