@@ -14,46 +14,16 @@ This repository provides two crates:
14
14
15
15
## Use the library
16
16
17
- To add the dependency to your project:
18
-
19
- ``` sh
20
- cargo add sqllogictest
21
- ```
22
-
23
- Implement ` DB ` trait for your database structure:
24
-
25
- ``` rust
26
- struct Database {... }
27
-
28
- impl sqllogictest :: DB for Database {
29
- type Error = ... ;
30
- type ColumnType = ... ;
31
- fn run (& mut self , sql : & str ) -> Result <sqllogictest :: DBOutput <Self :: ColumnType >, Self :: Error > {
32
- ...
33
- }
34
- }
35
- ```
36
-
37
- Then create a ` Runner ` on your database instance, and run the tests:
38
-
39
- ``` rust
40
- let db = Database {... };
41
- let mut tester = sqllogictest :: Runner :: new (db );
42
- tester . run_file (" script.slt" ). unwrap ();
43
- ```
44
-
45
- You can also parse the script and execute the records separately:
46
-
47
- ``` rust
48
- let records = sqllogictest :: parse_file (" script.slt" ). unwrap ();
49
- for record in records {
50
- tester . run (record ). unwrap ();
51
- }
52
- ```
17
+ Refer to the [ rustdoc] ( https://docs.rs/sqllogictest/latest/sqllogictest/ ) .
53
18
54
19
## Use the CLI tool
55
20
56
- ![ demo] ( ./docs/demo.gif )
21
+ The CLI tool supports many useful features:
22
+ - Colorful diff output
23
+ - Automatically update test files according to the actual output
24
+ - JUnit format test result report
25
+ - Parallel execution isolated with different databases
26
+ - ...
57
27
58
28
To install the binary:
59
29
@@ -64,11 +34,12 @@ cargo install sqllogictest-bin
64
34
You can use it as follows:
65
35
66
36
``` sh
37
+ # run scripts in `test` directory against postgres with default connection settings
67
38
sqllogictest ' ./test/**/*.slt'
39
+ # run the tests, and update the test files with the actual output!
40
+ sqllogictest ' ./test/**/*.slt' --override
68
41
```
69
42
70
- This command will run scripts in ` test ` directory against postgres with default connection settings.
71
-
72
43
You can find more options in ` sqllogictest --help ` .
73
44
74
45
> ** Note**
@@ -102,15 +73,107 @@ SELECT * FROM foo;
102
73
4 5
103
74
```
104
75
105
- ### Run a statement that should fail
76
+ ### Extension: Run a query/statement that should fail with the expacted error message
77
+
78
+ The syntax:
79
+ - Do not check the error message: ` [statement|query] error `
80
+ - Single line error message (regexp match): ` [statement|query] error <regex> `
81
+ - Multiline error message (exact match): Use ` ---- ` .
106
82
107
83
``` text
108
84
# Ensure that the statement errors and that the error
109
85
# message contains 'Multiple object drop not supported'
110
86
statement error Multiple object drop not supported
111
87
DROP VIEW foo, bar;
88
+
89
+ # The output error message must be the exact match of the expected one to pass the test,
90
+ # except for the leading and trailing whitespaces.
91
+ # Empty lines (not consecutive) are allowed in the expected error message. As a result, the message must end with 2 consecutive empty lines.
92
+ query error
93
+ SELECT 1/0;
94
+ ----
95
+ db error: ERROR: Failed to execute query
96
+
97
+ Caused by these errors:
98
+ 1: Failed to evaluate expression: 1/0
99
+ 2: Division by zero
100
+
101
+
102
+ # The next record begins here after 2 blank lines.
112
103
```
113
104
105
+ ### Extension: Run external shell commands
106
+
107
+ This is useful for manipulating some external resources during the test.
108
+
109
+ ``` text
110
+ system ok
111
+ exit 0
112
+
113
+ # The runner will check the exit code of the command, and this will fail.
114
+ system ok
115
+ exit 1
116
+
117
+ # Check the output of the command. Same as `error`, empty lines (not consecutive) are allowed, and 2 consecutive empty lines ends the result.
118
+ system ok
119
+ echo "Hello\n\nWorld"
120
+ ----
121
+ Hello
122
+
123
+ World
124
+
125
+
126
+ # The next record begins here after 2 blank lines.
127
+
128
+ # Environment variables are supported.
129
+ system ok
130
+ echo $USER
131
+ ----
132
+ xxchan
133
+ ```
134
+
135
+ ### Extension: Environment variable substituion in query and statement
136
+
137
+ It needs to be enabled by adding ` control substitution on ` to the test file.
138
+
139
+ ```
140
+ control substitution on
141
+
142
+ # see https://docs.rs/subst/latest/subst/ for all features
143
+ query TTTT
144
+ SELECT
145
+ '$foo' -- short
146
+ , '${foo}' -- long
147
+ , '${bar:default}' -- default value
148
+ , '${bar:$foo-default}' -- recursive default value
149
+ FROM baz;
150
+ ----
151
+ ...
152
+ ```
153
+
154
+ Besides, there're some special variables supported:
155
+ - ` $__TEST_DIR__ ` : the path to a temporary directory specific to the current test case.
156
+ This can be helpful if you need to manipulate some external resources during the test.
157
+ - ` $__NOW__ ` : the current Unix timestamp in nanoseconds.
158
+
159
+ ```
160
+ control substitution on
161
+
162
+ statement ok
163
+ COPY (SELECT * FROM foo) TO '$__TEST_DIR__/foo.txt';
164
+
165
+ system ok
166
+ echo "foo" > "$__TEST_DIR__/foo.txt"
167
+ ```
168
+
169
+ > [ !NOTE]
170
+ >
171
+ > When substitution is on, special characters need to be excaped, e.g., ` \$ ` and ` \\ ` .
172
+ >
173
+ > ` system ` commands don't support the advanced substitution features of the [ subst] ( https://docs.rs/subst/latest/subst/ ) crate,
174
+ > and excaping is also not needed.
175
+ > Environment variables are supported by the shell, and special variables are still supported by plain string substitution.
176
+
114
177
## Used by
115
178
116
179
- [ RisingLight] ( https://github.com/risinglightdb/risinglight ) : An OLAP database system for educational purpose
0 commit comments