Skip to content

Commit 4ccd455

Browse files
authored
Migrate datafusion/sql tests to insta, part6 (#15578)
* WIP * migrate tests in `diagnostic.rs` to `insta` * Migrate simple test case in `plan_to_sql.rs` to `insta` * Migrate `sql_round_trip` test cases in `plan_to_sql.rs` to `insta` * Migrate table reference tests in `plan_to_sql.rs` to use `insta` for snapshot testing * Refactor roundtrip expression tests in `plan_to_sql.rs` to use snapshot assertions with `insta` * Update roundtrip expression tests in `plan_to_sql.rs` to use updated snapshot assertions * resolve comments
1 parent 362fcdf commit 4ccd455

File tree

2 files changed

+530
-319
lines changed

2 files changed

+530
-319
lines changed

datafusion/sql/tests/cases/diagnostic.rs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use datafusion_functions::string;
19+
use insta::assert_snapshot;
1920
use std::{collections::HashMap, sync::Arc};
2021

2122
use datafusion_common::{Diagnostic, Location, Result, Span};
@@ -136,7 +137,7 @@ fn test_table_not_found() -> Result<()> {
136137
let query = "SELECT * FROM /*a*/personx/*a*/";
137138
let spans = get_spans(query);
138139
let diag = do_query(query);
139-
assert_eq!(diag.message, "table 'personx' not found");
140+
assert_snapshot!(diag.message, @"table 'personx' not found");
140141
assert_eq!(diag.span, Some(spans["a"]));
141142
Ok(())
142143
}
@@ -146,7 +147,7 @@ fn test_unqualified_column_not_found() -> Result<()> {
146147
let query = "SELECT /*a*/first_namex/*a*/ FROM person";
147148
let spans = get_spans(query);
148149
let diag = do_query(query);
149-
assert_eq!(diag.message, "column 'first_namex' not found");
150+
assert_snapshot!(diag.message, @"column 'first_namex' not found");
150151
assert_eq!(diag.span, Some(spans["a"]));
151152
Ok(())
152153
}
@@ -156,7 +157,7 @@ fn test_qualified_column_not_found() -> Result<()> {
156157
let query = "SELECT /*a*/person.first_namex/*a*/ FROM person";
157158
let spans = get_spans(query);
158159
let diag = do_query(query);
159-
assert_eq!(diag.message, "column 'first_namex' not found in 'person'");
160+
assert_snapshot!(diag.message, @"column 'first_namex' not found in 'person'");
160161
assert_eq!(diag.span, Some(spans["a"]));
161162
Ok(())
162163
}
@@ -166,14 +167,11 @@ fn test_union_wrong_number_of_columns() -> Result<()> {
166167
let query = "/*whole+left*/SELECT first_name FROM person/*left*/ UNION ALL /*right*/SELECT first_name, last_name FROM person/*right+whole*/";
167168
let spans = get_spans(query);
168169
let diag = do_query(query);
169-
assert_eq!(
170-
diag.message,
171-
"UNION queries have different number of columns"
172-
);
170+
assert_snapshot!(diag.message, @"UNION queries have different number of columns");
173171
assert_eq!(diag.span, Some(spans["whole"]));
174-
assert_eq!(diag.notes[0].message, "this side has 1 fields");
172+
assert_snapshot!(diag.notes[0].message, @"this side has 1 fields");
175173
assert_eq!(diag.notes[0].span, Some(spans["left"]));
176-
assert_eq!(diag.notes[1].message, "this side has 2 fields");
174+
assert_snapshot!(diag.notes[1].message, @"this side has 2 fields");
177175
assert_eq!(diag.notes[1].span, Some(spans["right"]));
178176
Ok(())
179177
}
@@ -183,15 +181,9 @@ fn test_missing_non_aggregate_in_group_by() -> Result<()> {
183181
let query = "SELECT id, /*a*/first_name/*a*/ FROM person GROUP BY id";
184182
let spans = get_spans(query);
185183
let diag = do_query(query);
186-
assert_eq!(
187-
diag.message,
188-
"'person.first_name' must appear in GROUP BY clause because it's not an aggregate expression"
189-
);
184+
assert_snapshot!(diag.message, @"'person.first_name' must appear in GROUP BY clause because it's not an aggregate expression");
190185
assert_eq!(diag.span, Some(spans["a"]));
191-
assert_eq!(
192-
diag.helps[0].message,
193-
"Either add 'person.first_name' to GROUP BY clause, or use an aggregare function like ANY_VALUE(person.first_name)"
194-
);
186+
assert_snapshot!(diag.helps[0].message, @"Either add 'person.first_name' to GROUP BY clause, or use an aggregare function like ANY_VALUE(person.first_name)");
195187
Ok(())
196188
}
197189

@@ -200,10 +192,10 @@ fn test_ambiguous_reference() -> Result<()> {
200192
let query = "SELECT /*a*/first_name/*a*/ FROM person a, person b";
201193
let spans = get_spans(query);
202194
let diag = do_query(query);
203-
assert_eq!(diag.message, "column 'first_name' is ambiguous");
195+
assert_snapshot!(diag.message, @"column 'first_name' is ambiguous");
204196
assert_eq!(diag.span, Some(spans["a"]));
205-
assert_eq!(diag.notes[0].message, "possible column a.first_name");
206-
assert_eq!(diag.notes[1].message, "possible column b.first_name");
197+
assert_snapshot!(diag.notes[0].message, @"possible column a.first_name");
198+
assert_snapshot!(diag.notes[1].message, @"possible column b.first_name");
207199
Ok(())
208200
}
209201

@@ -213,11 +205,11 @@ fn test_incompatible_types_binary_arithmetic() -> Result<()> {
213205
"SELECT /*whole+left*/id/*left*/ + /*right*/first_name/*right+whole*/ FROM person";
214206
let spans = get_spans(query);
215207
let diag = do_query(query);
216-
assert_eq!(diag.message, "expressions have incompatible types");
208+
assert_snapshot!(diag.message, @"expressions have incompatible types");
217209
assert_eq!(diag.span, Some(spans["whole"]));
218-
assert_eq!(diag.notes[0].message, "has type UInt32");
210+
assert_snapshot!(diag.notes[0].message, @"has type UInt32");
219211
assert_eq!(diag.notes[0].span, Some(spans["left"]));
220-
assert_eq!(diag.notes[1].message, "has type Utf8");
212+
assert_snapshot!(diag.notes[1].message, @"has type Utf8");
221213
assert_eq!(diag.notes[1].span, Some(spans["right"]));
222214
Ok(())
223215
}
@@ -227,7 +219,7 @@ fn test_field_not_found_suggestion() -> Result<()> {
227219
let query = "SELECT /*whole*/first_na/*whole*/ FROM person";
228220
let spans = get_spans(query);
229221
let diag = do_query(query);
230-
assert_eq!(diag.message, "column 'first_na' not found");
222+
assert_snapshot!(diag.message, @"column 'first_na' not found");
231223
assert_eq!(diag.span, Some(spans["whole"]));
232224
assert_eq!(diag.notes.len(), 1);
233225

@@ -243,7 +235,7 @@ fn test_field_not_found_suggestion() -> Result<()> {
243235
})
244236
.collect();
245237
suggested_fields.sort();
246-
assert_eq!(suggested_fields[0], "person.first_name");
238+
assert_snapshot!(suggested_fields[0], @"person.first_name");
247239
Ok(())
248240
}
249241

@@ -253,7 +245,7 @@ fn test_ambiguous_column_suggestion() -> Result<()> {
253245
let spans = get_spans(query);
254246
let diag = do_query(query);
255247

256-
assert_eq!(diag.message, "column 'id' is ambiguous");
248+
assert_snapshot!(diag.message, @"column 'id' is ambiguous");
257249
assert_eq!(diag.span, Some(spans["whole"]));
258250

259251
assert_eq!(diag.notes.len(), 2);
@@ -281,8 +273,8 @@ fn test_invalid_function() -> Result<()> {
281273
let query = "SELECT /*whole*/concat_not_exist/*whole*/()";
282274
let spans = get_spans(query);
283275
let diag = do_query(query);
284-
assert_eq!(diag.message, "Invalid function 'concat_not_exist'");
285-
assert_eq!(diag.notes[0].message, "Possible function 'concat'");
276+
assert_snapshot!(diag.message, @"Invalid function 'concat_not_exist'");
277+
assert_snapshot!(diag.notes[0].message, @"Possible function 'concat'");
286278
assert_eq!(diag.span, Some(spans["whole"]));
287279
Ok(())
288280
}
@@ -292,10 +284,7 @@ fn test_scalar_subquery_multiple_columns() -> Result<(), Box<dyn std::error::Err
292284
let spans = get_spans(query);
293285
let diag = do_query(query);
294286

295-
assert_eq!(
296-
diag.message,
297-
"Too many columns! The subquery should only return one column"
298-
);
287+
assert_snapshot!(diag.message, @"Too many columns! The subquery should only return one column");
299288

300289
let expected_span = Some(Span {
301290
start: spans["x"].start,
@@ -327,10 +316,7 @@ fn test_in_subquery_multiple_columns() -> Result<(), Box<dyn std::error::Error>>
327316
let spans = get_spans(query);
328317
let diag = do_query(query);
329318

330-
assert_eq!(
331-
diag.message,
332-
"Too many columns! The subquery should only return one column"
333-
);
319+
assert_snapshot!(diag.message, @"Too many columns! The subquery should only return one column");
334320

335321
let expected_span = Some(Span {
336322
start: spans["id"].start,
@@ -360,16 +346,10 @@ fn test_unary_op_plus_with_column() -> Result<()> {
360346
let query = "SELECT +/*whole*/first_name/*whole*/ FROM person";
361347
let spans = get_spans(query);
362348
let diag = do_query(query);
363-
assert_eq!(diag.message, "+ cannot be used with Utf8");
349+
assert_snapshot!(diag.message, @"+ cannot be used with Utf8");
364350
assert_eq!(diag.span, Some(spans["whole"]));
365-
assert_eq!(
366-
diag.notes[0].message,
367-
"+ can only be used with numbers, intervals, and timestamps"
368-
);
369-
assert_eq!(
370-
diag.helps[0].message,
371-
"perhaps you need to cast person.first_name"
372-
);
351+
assert_snapshot!(diag.notes[0].message, @"+ can only be used with numbers, intervals, and timestamps");
352+
assert_snapshot!(diag.helps[0].message, @"perhaps you need to cast person.first_name");
373353
Ok(())
374354
}
375355

@@ -379,15 +359,9 @@ fn test_unary_op_plus_with_non_column() -> Result<()> {
379359
let query = "SELECT +'a'";
380360
let diag = do_query(query);
381361
assert_eq!(diag.message, "+ cannot be used with Utf8");
382-
assert_eq!(
383-
diag.notes[0].message,
384-
"+ can only be used with numbers, intervals, and timestamps"
385-
);
362+
assert_snapshot!(diag.notes[0].message, @"+ can only be used with numbers, intervals, and timestamps");
386363
assert_eq!(diag.notes[0].span, None);
387-
assert_eq!(
388-
diag.helps[0].message,
389-
"perhaps you need to cast Utf8(\"a\")"
390-
);
364+
assert_snapshot!(diag.helps[0].message, @r#"perhaps you need to cast Utf8("a")"#);
391365
assert_eq!(diag.helps[0].span, None);
392366
assert_eq!(diag.span, None);
393367
Ok(())

0 commit comments

Comments
 (0)