2024-05-13 15:37:53 +02:00
|
|
|
use crate::repl::tests::{fail_test, run_test, TestResult};
|
2022-12-21 23:54:39 +01:00
|
|
|
|
2023-09-26 18:49:28 +02:00
|
|
|
// Tests for null / null / Value::Nothing
|
2022-12-21 23:54:39 +01:00
|
|
|
#[test]
|
|
|
|
fn nothing_fails_string() -> TestResult {
|
2023-09-26 18:49:28 +02:00
|
|
|
fail_test("let nil = null; $nil.foo", "doesn't support cell paths")
|
2022-12-21 23:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nothing_fails_int() -> TestResult {
|
2023-09-26 18:49:28 +02:00
|
|
|
fail_test("let nil = null; $nil.3", "doesn't support cell paths")
|
2022-12-21 23:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests for records
|
|
|
|
#[test]
|
|
|
|
fn record_single_field_success() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'}.foo == 'bar'", "true")
|
|
|
|
}
|
|
|
|
|
2023-03-16 04:50:58 +01:00
|
|
|
#[test]
|
|
|
|
fn record_single_field_optional_success() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'}.foo? == 'bar'", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_works_with_cell_path_success() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'} | get foo?", "bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_works_with_cell_path_missing_data() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'} | get foobar? | to nuon", "null")
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:39 +01:00
|
|
|
#[test]
|
|
|
|
fn record_single_field_failure() -> TestResult {
|
|
|
|
fail_test("{foo: 'bar'}.foobar", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_int_failure() -> TestResult {
|
|
|
|
fail_test("{foo: 'bar'}.3", "")
|
|
|
|
}
|
|
|
|
|
2023-03-16 04:50:58 +01:00
|
|
|
#[test]
|
|
|
|
fn record_single_field_optional() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'}.foobar? | to nuon", "null")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-22 21:54:19 +01:00
|
|
|
fn record_single_field_optional_short_circuits() -> TestResult {
|
|
|
|
// Check that we return null as soon as the `.foobar?` access
|
|
|
|
// fails instead of erroring on the `.baz` access
|
|
|
|
run_test("{foo: 'bar'}.foobar?.baz | to nuon", "null")
|
2023-03-16 04:50:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_multiple_optional_fields() -> TestResult {
|
|
|
|
run_test("{foo: 'bar'}.foobar?.baz? | to nuon", "null")
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:39 +01:00
|
|
|
#[test]
|
|
|
|
fn nested_record_field_success() -> TestResult {
|
|
|
|
run_test("{foo: {bar: 'baz'} }.foo.bar == 'baz'", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_record_field_failure() -> TestResult {
|
|
|
|
fail_test("{foo: {bar: 'baz'} }.foo.asdf", "")
|
|
|
|
}
|
|
|
|
|
2023-03-16 04:50:58 +01:00
|
|
|
#[test]
|
|
|
|
fn nested_record_field_optional() -> TestResult {
|
|
|
|
run_test("{foo: {bar: 'baz'} }.foo.asdf? | to nuon", "null")
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:39 +01:00
|
|
|
#[test]
|
|
|
|
fn record_with_nested_list_success() -> TestResult {
|
|
|
|
run_test("{foo: [{bar: 'baz'}]}.foo.0.bar == 'baz'", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_with_nested_list_int_failure() -> TestResult {
|
|
|
|
fail_test("{foo: [{bar: 'baz'}]}.foo.3.bar", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_with_nested_list_column_failure() -> TestResult {
|
|
|
|
fail_test("{foo: [{bar: 'baz'}]}.foo.0.asdf", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests for lists
|
|
|
|
#[test]
|
|
|
|
fn list_single_field_success() -> TestResult {
|
|
|
|
run_test("[{foo: 'bar'}].foo.0 == 'bar'", "true")?;
|
|
|
|
// test field access both ways
|
|
|
|
run_test("[{foo: 'bar'}].0.foo == 'bar'", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_single_field_failure() -> TestResult {
|
|
|
|
fail_test("[{foo: 'bar'}].asdf", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the scenario where the requested column is not present in all rows
|
|
|
|
#[test]
|
2023-01-02 23:45:43 +01:00
|
|
|
fn jagged_list_access_fails() -> TestResult {
|
|
|
|
fail_test("[{foo: 'bar'}, {}].foo", "cannot find column")?;
|
|
|
|
fail_test("[{}, {foo: 'bar'}].foo", "cannot find column")
|
2022-12-21 23:54:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 04:50:58 +01:00
|
|
|
#[test]
|
|
|
|
fn jagged_list_optional_access_succeeds() -> TestResult {
|
|
|
|
run_test("[{foo: 'bar'}, {}].foo?.0", "bar")?;
|
|
|
|
run_test("[{foo: 'bar'}, {}].foo?.1 | to nuon", "null")?;
|
|
|
|
|
|
|
|
run_test("[{}, {foo: 'bar'}].foo?.0 | to nuon", "null")?;
|
|
|
|
run_test("[{}, {foo: 'bar'}].foo?.1", "bar")
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:39 +01:00
|
|
|
// test that accessing a nonexistent row fails
|
|
|
|
#[test]
|
|
|
|
fn list_row_access_failure() -> TestResult {
|
|
|
|
fail_test("[{foo: 'bar'}, {foo: 'baz'}].2", "")
|
|
|
|
}
|
|
|
|
|
2023-03-16 04:50:58 +01:00
|
|
|
#[test]
|
|
|
|
fn list_row_optional_access_succeeds() -> TestResult {
|
|
|
|
run_test("[{foo: 'bar'}, {foo: 'baz'}].2? | to nuon", "null")?;
|
|
|
|
run_test("[{foo: 'bar'}, {foo: 'baz'}].3? | to nuon", "null")
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:39 +01:00
|
|
|
// regression test for an old bug
|
|
|
|
#[test]
|
|
|
|
fn do_not_delve_too_deep_in_nested_lists() -> TestResult {
|
2023-01-02 23:45:43 +01:00
|
|
|
fail_test("[[{foo: bar}]].foo", "cannot find column")
|
2022-12-21 23:54:39 +01:00
|
|
|
}
|
2023-03-17 03:19:41 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_literals() -> TestResult {
|
|
|
|
run_test("let cell_path = $.a.b; {a: {b: 3}} | get $cell_path", "3")
|
|
|
|
}
|
2023-03-22 21:54:19 +01:00
|
|
|
|
|
|
|
// Test whether cell path access short-circuits properly
|
|
|
|
#[test]
|
|
|
|
fn deeply_nested_cell_path_short_circuits() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"{foo: [{bar: 'baz'}]}.foo.3?.bar.asdfdafg.234.foobar | to nuon",
|
|
|
|
"null",
|
|
|
|
)
|
|
|
|
}
|
2023-09-06 20:22:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_type() -> TestResult {
|
|
|
|
run_test("$.a.b | describe", "cell-path")
|
|
|
|
}
|