Repository navigation
fix(layout): list only what the definition query keeps in tables and charts #3555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jgregoriorincon
merged 4 commits into
opengeos:main
from
jgregoriorincon:fix/3478-layout-table-definition-query
Dec 1, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
744d7ca
fix(layout): list only what the definition query keeps in tables and …
jgregoriorincon 6d23ac6
fix(layout): count one table's cut on a change, and leave one chart r…
jgregoriorincon 869a338
docs(layout): say what reading a definition query costs and keeps (#3…
jgregoriorincon c4063cb
docs(layout): say when the definition query is read and what a table …
jgregoriorincon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| //! A layout table and chart list only what their layer's definition query keeps (#3478). | ||
|
|
||
| use super::*; | ||
| use rustgis_data::{Dataset, Field, FieldType, Geometry, GeometryType, Value as Data}; | ||
|
|
||
| /// `region`, `n` and whether the query `shown = 1` keeps the row. | ||
| const SHOPS: [(&str, i64, bool); 7] = | ||
| [("E", 40, true), ("N", 90, false), ("S", 10, true), ("E", 70, false), ("N", 30, true), ("W", 80, false), ("S", 20, true)]; | ||
|
|
||
| /// A blank layout "Shops page" over a map with the points layer "Shops" holding `rows`, and the | ||
| /// layer "Pages" of two features to run a series over. | ||
| fn session(rows: &[(&str, i64, bool)]) -> Session { | ||
| let mut s = Session::new(sample::world_project()); | ||
| let point = || Some(Geometry::Point(rustgis_data::Coord::new(0.0, 0.0))); | ||
| let mut ds = Dataset::new( | ||
| "Shops", | ||
| GeometryType::Point, | ||
| vec![Field::new("region", FieldType::Text), Field::new("n", FieldType::Integer), Field::new("shown", FieldType::Integer)], | ||
| ); | ||
| for (region, n, shown) in rows { | ||
| ds.push(point(), vec![Data::Text((*region).into()), Data::Int(*n), Data::Int(i64::from(*shown))]); | ||
| } | ||
| s.map_mut().add_dataset(ds); | ||
| let mut pages = Dataset::new("Pages", GeometryType::Point, vec![Field::new("p", FieldType::Integer)]); | ||
| for p in [1, 2] { | ||
| pages.push(point(), vec![Data::Int(p)]); | ||
| } | ||
| s.map_mut().add_dataset(pages); | ||
| s.run("layout.new", &json!({"name": "Shops page"})).unwrap(); | ||
| s.run("layout.addFrame", &json!({"x": 5, "y": 5, "w": 80, "h": 50})).unwrap(); | ||
| s | ||
| } | ||
|
|
||
| /// The rows the query keeps, as a layer of their own. | ||
| fn kept() -> Vec<(&'static str, i64, bool)> { | ||
| SHOPS.into_iter().filter(|r| r.2).collect() | ||
| } | ||
|
|
||
| fn query(s: &mut Session, text: &str) { | ||
| s.run("layer.definitionQuery", &json!({"layer": "Shops", "where": text})).unwrap(); | ||
| } | ||
|
|
||
| /// Adds an element of Shops, large enough for every row, with `extra` keys; its id. | ||
| fn add(s: &mut Session, kind: &str, extra: &Value) -> u64 { | ||
| let mut p = json!({"kind": kind, "layer": "Shops", "x": 100, "y": 10, "w": 90, "h": 90}); | ||
| if kind == "chart" { | ||
| p["field"] = json!("n"); | ||
| } | ||
| if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) { | ||
| p.extend(e.clone()); | ||
| } | ||
| s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap() | ||
| } | ||
|
|
||
| fn page(s: &mut Session) -> Vec<u8> { | ||
| crate::cmd::layout::layout_png(s, 0, 96.0, None).unwrap() | ||
| } | ||
|
|
||
| /// The page with an element of `extra` keys over all of Shops under `text`. | ||
| fn queried(kind: &str, extra: &Value, text: &str) -> Vec<u8> { | ||
| let mut s = session(&SHOPS); | ||
| query(&mut s, text); | ||
| add(&mut s, kind, extra); | ||
| page(&mut s) | ||
| } | ||
|
|
||
| /// The same element over a layer holding `rows` alone, without a query. | ||
| fn holding(kind: &str, extra: &Value, rows: &[(&str, i64, bool)]) -> Vec<u8> { | ||
| let mut s = session(rows); | ||
| add(&mut s, kind, extra); | ||
| page(&mut s) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_table_and_a_chart_draw_only_what_the_query_keeps() { | ||
| for (kind, extra) in [ | ||
| ("table", json!({})), | ||
| ("table", json!({"sort": "n"})), | ||
| ("table", json!({"sort": "n", "descending": true, "rows": 2})), | ||
| ("chart", json!({"label": "region"})), | ||
| ("chart", json!({"label": "region", "bars": 2, "other": true})), | ||
| ("chart", json!({"group": "region", "statistic": "sum"})), | ||
| ("chart", json!({"group": "region", "statistic": "count"})), | ||
| ("chart", json!({"group": "region", "statistic": "mean", "values": true})), | ||
| ("chart", json!({"group": "region", "statistic": "sum", "stack": "shown"})), | ||
| ] { | ||
| let want = holding(kind, &extra, &kept()); | ||
| assert_ne!(want, holding(kind, &extra, &SHOPS), "{kind} {extra}: the hidden rows show in a drawing"); | ||
| assert_eq!(queried(kind, &extra, "shown = 1"), want, "{kind} {extra}"); | ||
| // A query keeping nothing leaves what an empty layer draws. | ||
| assert_eq!(queried(kind, &extra, "n < 0"), holding(kind, &extra, &[]), "{kind} {extra}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_query_that_is_blank_or_does_not_read_keeps_every_row() { | ||
| for kind in ["table", "chart"] { | ||
| let want = holding(kind, &json!({}), &SHOPS); | ||
| for text in ["", " "] { | ||
| assert_eq!(queried(kind, &json!({}), text), want, "{kind} '{text}'"); | ||
| } | ||
| let mut s = session(&SHOPS); | ||
| s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap().definition_query = Some("n = = 1".into()); | ||
| add(&mut s, kind, &json!({})); | ||
| assert_eq!(page(&mut s), want, "{kind}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_table_of_the_selected_rows_leaves_out_the_hidden_ones() { | ||
| let extra = json!({"selectedOnly": true}); | ||
| let selected = |rows: &[(&str, i64, bool)], text: Option<&str>, selection: Vec<usize>| { | ||
| let mut s = session(rows); | ||
| if let Some(text) = text { | ||
| query(&mut s, text); | ||
| } | ||
| s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap().selection = selection; | ||
| add(&mut s, "table", &extra); | ||
| page(&mut s) | ||
| }; | ||
| // Rows 1 and 3 are hidden; 0 and 4 are the first and third of those kept. | ||
| assert_eq!(selected(&SHOPS, Some("shown = 1"), vec![0, 1, 3, 4]), selected(&kept(), None, vec![0, 2])); | ||
| assert_eq!(selected(&SHOPS, Some("shown = 1"), vec![1, 3]), selected(&kept(), None, vec![])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_series_page_lists_what_the_query_keeps_on_it() { | ||
| for kind in ["table", "chart"] { | ||
| let mut s = session(&SHOPS); | ||
| // The first page keeps the rows up to 10. | ||
| query(&mut s, "n <= @page_number * 10"); | ||
| add(&mut s, kind, &json!({})); | ||
| s.run("layout.series", &json!({"layer": "Pages"})).unwrap(); | ||
| let mut want = session(&[("S", 10, true)]); | ||
| add(&mut want, kind, &json!({})); | ||
| want.run("layout.series", &json!({"layer": "Pages"})).unwrap(); | ||
| assert_eq!(page(&mut s), page(&mut want), "{kind}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn undo_gives_the_hidden_rows_back() { | ||
| for kind in ["table", "chart"] { | ||
| let mut s = session(&SHOPS); | ||
| add(&mut s, kind, &json!({})); | ||
| let all = page(&mut s); | ||
| query(&mut s, "shown = 1"); | ||
| assert_eq!(page(&mut s), holding(kind, &json!({}), &kept()), "{kind}"); | ||
| s.run("edit.undo", &Value::Null).unwrap(); | ||
| assert_eq!(page(&mut s), all, "{kind}"); | ||
| } | ||
| } | ||
|
|
||
| /// What exporting the page says of its tables and charts. | ||
| fn exported(s: &mut Session) -> Vec<String> { | ||
| let dir = std::env::temp_dir().join(format!("rustgis-3478-{}-{:?}", std::process::id(), std::thread::current().id())); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let mark = s.notice_mark(); | ||
| s.run("layout.export", &json!({"path": dir.join("page.pdf"), "dpi": 36})).unwrap(); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("Table") || t.contains("Chart")).collect() | ||
| } | ||
|
|
||
| #[test] | ||
| fn what_is_said_to_be_left_out_counts_the_kept_rows_alone() { | ||
| let mut s = session(&SHOPS); | ||
| query(&mut s, "shown = 1"); | ||
| // Four rows kept of the six asked for: all of them fit, and nothing is cut. | ||
| add(&mut s, "table", &json!({"rows": 6})); | ||
| let bars = add(&mut s, "chart", &json!({"bars": 3})); | ||
| let said = exported(&mut s); | ||
| assert_eq!(said.len(), 1, "{said:?}"); | ||
| assert!(said[0].contains(&format!("({bars}): the chart draws 3 of 4 bars")), "{said:?}"); | ||
| // A box for one row of the four. | ||
| let small = add(&mut s, "table", &json!({"h": 11})); | ||
| let said = exported(&mut s); | ||
| assert!(said.iter().any(|t| t.contains(&format!("({small}): its box shows 1 of 4 rows"))), "{said:?}"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.