gluesql_test_suite/
values.rs

1use {
2    crate::*,
3    gluesql_core::{
4        ast::DataType::{Boolean, Int, Text},
5        error::{EvaluateError, InsertError, SelectError},
6        prelude::{DataType, Payload, Value::*},
7    },
8};
9
10test_case!(values, {
11    let g = get_tester!();
12
13    g.run("CREATE TABLE Items (id INTEGER NOT NULL, name TEXT, status TEXT DEFAULT 'ACTIVE' NOT NULL);").await;
14
15    let test_cases = [
16        (
17            "VALUES (1), (2), (3)",
18            Ok(select!(
19                column1;
20                I64;
21                1;
22                2;
23                3
24            )),
25        ),
26        (
27            "VALUES (1, 'a'), (2, 'b')",
28            Ok(select!(
29                column1 | column2;
30                I64     | Str;
31                1         "a".to_owned();
32                2         "b".to_owned()
33            )),
34        ),
35        (
36            "VALUES (1, 'a'), (2, 'b') ORDER BY column1 DESC",
37            Ok(select!(
38                column1 | column2;
39                I64     | Str;
40                2         "b".to_owned();
41                1         "a".to_owned()
42            )),
43        ),
44        (
45            "VALUES (1), (2) limit 1",
46            Ok(select!(
47                column1;
48                I64;
49                1
50            )),
51        ),
52        (
53            "VALUES (1), (2) offset 1",
54            Ok(select!(
55                column1;
56                I64;
57                2
58            )),
59        ),
60        (
61            "VALUES (1, NULL), (2, NULL)",
62            Ok(select_with_null!(
63                column1 | column2;
64                I64(1)    Null;
65                I64(2)    Null
66            )),
67        ),
68        (
69            "VALUES (1), (2, 'b')",
70            Err(SelectError::NumberOfValuesDifferent.into()),
71        ),
72        (
73            "VALUES (1, 'a'), (2)",
74            Err(SelectError::NumberOfValuesDifferent.into()),
75        ),
76        (
77            "VALUES (1, 'a'), (2, 3)",
78            Err(EvaluateError::NumberParseFailed {
79                literal: "3".to_owned(),
80                data_type: DataType::Text,
81            }
82            .into()),
83        ),
84        (
85            "VALUES (1, 'a'), ('b', 'c')",
86            Err(EvaluateError::TextParseFailed {
87                literal: "b".to_owned(),
88                data_type: DataType::Int,
89            }
90            .into()),
91        ),
92        (
93            "VALUES (1, NULL), (2, 'a'), (3, 4)",
94            Err(EvaluateError::NumberParseFailed {
95                literal: "4".to_owned(),
96                data_type: DataType::Text,
97            }
98            .into()),
99        ),
100        (
101            "CREATE TABLE TableFromValues AS VALUES (1, 'a', True, Null, Null), (2, 'b', False, 3, Null)",
102            Ok(Payload::Create),
103        ),
104        (
105            "SELECT * FROM TableFromValues",
106            Ok(select_with_null!(
107                column1 | column2         | column3    | column4 | column5;
108                I64(1)    Str("a".into())   Bool(true)   Null      Null   ;
109                I64(2)    Str("b".into())   Bool(false)  I64(3)    Null
110            )),
111        ),
112        (
113            "SHOW COLUMNS FROM TableFromValues",
114            Ok(Payload::ShowColumns(vec![
115                ("column1".into(), Int),
116                ("column2".into(), Text),
117                ("column3".into(), Boolean),
118                ("column4".into(), Int),
119                ("column5".into(), Text),
120            ])),
121        ),
122        (
123            "SELECT * FROM (VALUES (1, 'a'), (2, 'b')) AS Derived",
124            Ok(select!(
125                column1 | column2;
126                I64     | Str;
127                1         "a".to_owned();
128                2         "b".to_owned()
129            )),
130        ),
131        (
132            "SELECT column1 AS id, column2 AS name FROM (VALUES (1, 'a'), (2, 'b')) AS Derived",
133            Ok(select!(
134                id      | name;
135                I64     | Str;
136                1         "a".to_owned();
137                2         "b".to_owned()
138            )),
139        ),
140        ("INSERT INTO Items (id) VALUES (1);", Ok(Payload::Insert(1))),
141        (
142            "INSERT INTO Items (id2) VALUES (1);",
143            Err(InsertError::WrongColumnName("id2".to_owned()).into()),
144        ),
145        (
146            "INSERT INTO Items (name) VALUES ('glue');",
147            Err(InsertError::LackOfRequiredColumn("id".to_owned()).into()),
148        ),
149        (
150            "INSERT INTO Items (id) VALUES (3, 'sql')",
151            Err(InsertError::ColumnAndValuesNotMatched.into()),
152        ),
153        (
154            "INSERT INTO Items VALUES (100, 'a', 'b', 1);",
155            Err(InsertError::TooManyValues.into()),
156        ),
157        (
158            "INSERT INTO Nothing VALUES (1);",
159            Err(InsertError::TableNotFound("Nothing".to_owned()).into()),
160        ),
161    ];
162    for (sql, expected) in test_cases {
163        g.test(sql, expected).await;
164    }
165});