MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / test_parse_error_codes

Function test_parse_error_codes

src/environmentd/tests/sql.rs:2353–2407  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2351
2352#[mz_ore::test]
2353fn test_parse_error_codes() {
2354 // Parse/cast and overflow failures should report data-exception (class 22)
2355 // SQLSTATEs rather than the catch-all XX000 (INTERNAL_ERROR). Regression
2356 // test for the fix tracked in SQL-326.
2357 let server = test_util::TestHarness::default().start_blocking();
2358 let mut client = server.connect(postgres::NoTls).unwrap();
2359
2360 let cases: &[(&str, &SqlState)] = &[
2361 // Out-of-range datetime value -> datetime_field_overflow (22008).
2362 (
2363 "SELECT '14714-12-31 18:01:00+00 BC'::date",
2364 &SqlState::DATETIME_FIELD_OVERFLOW,
2365 ),
2366 // Malformed datetime string -> invalid_datetime_format (22007).
2367 (
2368 "SELECT 'not a date'::date",
2369 &SqlState::INVALID_DATETIME_FORMAT,
2370 ),
2371 // Out-of-range float value -> numeric_value_out_of_range (22003).
2372 (
2373 "SELECT '1e400'::float8",
2374 &SqlState::NUMERIC_VALUE_OUT_OF_RANGE,
2375 ),
2376 // Malformed integer string -> invalid_text_representation (22P02).
2377 (
2378 "SELECT 'abc'::integer",
2379 &SqlState::INVALID_TEXT_REPRESENTATION,
2380 ),
2381 // Division by zero -> division_by_zero (22012). Previously XX000.
2382 ("SELECT 1 / 0", &SqlState::DIVISION_BY_ZERO),
2383 // Square root of a negative -> invalid_argument_for_power_function
2384 // (2201F). Previously XX000.
2385 (
2386 "SELECT sqrt(-1.0)",
2387 &SqlState::INVALID_ARGUMENT_FOR_POWER_FUNCTION,
2388 ),
2389 // More than one row from a scalar subquery -> cardinality_violation
2390 // (21000). Previously XX000.
2391 (
2392 "SELECT (SELECT generate_series(1, 2))",
2393 &SqlState::CARDINALITY_VIOLATION,
2394 ),
2395 ];
2396
2397 for (query, expected) in cases {
2398 let err = client.query_one(*query, &[]).unwrap_err().unwrap_db_error();
2399 assert_eq!(
2400 err.code(),
2401 *expected,
2402 "unexpected SQLSTATE {} for query `{query}`: {}",
2403 err.code().code(),
2404 err.message(),
2405 );
2406 }
2407}
2408
2409#[mz_ore::test]
2410#[allow(clippy::disallowed_methods)]

Callers

nothing calls this directly

Calls 5

start_blockingMethod · 0.80
unwrapMethod · 0.80
unwrap_db_errorMethod · 0.80
connectMethod · 0.45
query_oneMethod · 0.45

Tested by

no test coverage detected