Skip to content

Commit cfa77cb

Browse files
Mavrick-Fclaude
andcommitted
test(assistant): pin which table a spaced name reads
The untrimmed-name test only compared whether `layer` and `table` succeeded, so it passed if both were wrong. It now adds tables named `stats ` and ` Stats` beside `stats` and `Stats`, and checks that each name reads its own table's row through both arguments, and that a spaced name no table has is refused with the expected error. The table lookup in row_source returns its result directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 88bb339 commit cfa77cb

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎crates/assistant/src/tests.rs‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,20 +440,33 @@ fn read_features_reads_a_tools_output_table() {
440440
assert_eq!(by_layer, tools::run(&mut s, &t("read_features", json!({"table": name}))).unwrap(), "{name}");
441441
assert!(by_layer.contains(&format!("\"table\":\"{name}\"")), "{by_layer}");
442442
}
443-
// Names are matched as written, by `layer` as by `table`: no trimming.
444-
for name in ["stats ", " Stats"] {
445-
let (by_layer, by_table) = (
446-
tools::run(&mut s, &t("read_features", json!({"layer": name}))),
447-
tools::run(&mut s, &t("read_features", json!({"table": name}))),
448-
);
449-
assert_eq!(by_layer.is_ok(), by_table.is_ok(), "{name:?}: {by_layer:?} / {by_table:?}");
443+
// Names are matched as written, by `layer` as by `table`: `stats ` and ` Stats` are tables of
444+
// their own beside `stats` and `Stats`, and a name with a space nothing has is neither.
445+
for (name, v) in [("stats ", 3), (" Stats", 4)] {
446+
use rustgis_engine::data::{Dataset, Field, FieldType, GeometryType, Value as V};
447+
let mut ds = Dataset::new(name, GeometryType::None, vec![Field::new("v", FieldType::Integer)]);
448+
ds.push(None, vec![V::Int(v)]);
449+
s.project.add_table(ds);
450+
}
451+
for (name, v) in [("stats", 1.0), ("Stats", 2.0), ("stats ", 3.0), (" Stats", 4.0)] {
452+
for arg in ["layer", "table"] {
453+
let args = Value::Object([(arg.to_string(), json!(name))].into_iter().collect());
454+
let read: Value = serde_json::from_str(&tools::run(&mut s, &t("read_features", args)).unwrap()).unwrap();
455+
assert_eq!((read["table"].as_str(), read["rows"][0]["v"].as_f64()), (Some(name), Some(v)), "{arg} {name:?}: {read}");
456+
}
457+
}
458+
for name in ["stats ", " Stats"] {
459+
let by_layer = tools::run(&mut s, &t("read_features", json!({"layer": name})));
460+
assert!(by_layer.as_ref().is_err_and(|e| e.contains("no layer or standalone table named")), "{name:?}: {by_layer:?}");
461+
let by_table = tools::run(&mut s, &t("read_features", json!({"table": name})));
462+
assert!(by_table.as_ref().is_err_and(|e| e.contains("no standalone table named")), "{name:?}: {by_table:?}");
450463
}
451464
let id = s.project.tables.last().map(|t| t.id).unwrap_or_default();
452465
// A layer with that id reads; otherwise it's an error, never the table.
453466
let by_id = tools::run(&mut s, &t("read_features", json!({"layer": id.to_string()})));
454467
let read_a_layer = match &by_id {
455468
Ok(r) => r.contains("\"layer\"") && !r.contains("\"table\""),
456-
Err(_) => true,
469+
Err(e) => e.contains("no layer or standalone table named"),
457470
};
458471
assert!(read_a_layer, "{by_id:?}");
459472
assert!(tools::run(&mut s, &t("read_features", json!({"table": id.to_string()}))).is_ok());

‎crates/assistant/src/tools.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,15 @@ struct RowSource<'a> {
596596
/// otherwise it is read instead of `layer`.
597597
fn row_source<'a>(s: &'a Session, a: &Value) -> Result<RowSource<'a>, String> {
598598
let table = |i: usize| {
599-
s.project.tables.get(i).map(|t| RowSource { kind: "table", name: &t.name, ds: &t.dataset, selection: None }).ok_or("no such table")
599+
s.project
600+
.tables
601+
.get(i)
602+
.map(|t| RowSource { kind: "table", name: &t.name, ds: &t.dataset, selection: None })
603+
.ok_or_else(|| "no such table".to_string())
600604
};
601605
let given = |v: &&Value| !v.is_null() && v.as_str().is_none_or(|t| !t.trim().is_empty());
602606
if let Some(t) = a.get("table").filter(given) {
603-
return Ok(table(s.table_index(&json!({"table": t}))?)?);
607+
return table(s.table_index(&json!({"table": t}))?);
604608
}
605609
let layer = a.get("layer").cloned().ok_or("layer (or table) is required")?;
606610
let id = match s.layer_id(&json!({"layer": layer})) {
@@ -612,7 +616,7 @@ fn row_source<'a>(s: &'a Session, a: &Value) -> Result<RowSource<'a>, String> {
612616
// `find_table` falls back to an id; keep only a name match, so `layer: "7"` can't read table 7.
613617
let by_name = project::find_table(tables, n).filter(|&i| tables.get(i).is_some_and(|t| t.name.eq_ignore_ascii_case(n)));
614618
return match by_name {
615-
Some(i) => Ok(table(i)?),
619+
Some(i) => table(i),
616620
None => Err(format!("no layer or standalone table named '{n}'")),
617621
};
618622
}

0 commit comments

Comments
 (0)