-
Notifications
You must be signed in to change notification settings - Fork 0
test: coverage for indexes list and get_none_if_not_found #62
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ use crate::api::ApiClient; | |
| use rayon::prelude::*; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::collections::HashMap; | ||
| use std::ops::ControlFlow; | ||
|
|
||
| #[derive(Deserialize, Serialize)] | ||
| struct Index { | ||
|
|
@@ -52,16 +53,42 @@ struct ConnectionsBody { | |
| connections: Vec<ConnectionRef>, | ||
| } | ||
|
|
||
| fn connection_lookup(api: &ApiClient) -> HashMap<String, String> { | ||
| let body: ConnectionsBody = api.get("/connections"); | ||
| fn connection_label_to_id_map(connections: &[ConnectionRef]) -> HashMap<String, String> { | ||
| let mut m = HashMap::new(); | ||
| for c in body.connections { | ||
| m.insert(c.id.clone(), c.id.clone()); | ||
| m.insert(c.name.clone(), c.id); | ||
| for c in connections { | ||
| m.insert(c.name.clone(), c.id.clone()); | ||
| } | ||
| m | ||
| } | ||
|
|
||
| fn connection_lookup(api: &ApiClient) -> HashMap<String, String> { | ||
| let body: ConnectionsBody = api.get("/connections"); | ||
| connection_label_to_id_map(&body.connections) | ||
| } | ||
|
|
||
| /// How to continue after merging one `/information_schema` page. | ||
| fn information_schema_followup( | ||
| has_more: bool, | ||
| next_cursor: Option<String>, | ||
| ) -> ControlFlow<(), String> { | ||
| if !has_more { | ||
| return ControlFlow::Break(()); | ||
| } | ||
| let Some(c) = next_cursor else { | ||
| return ControlFlow::Break(()); | ||
| }; | ||
| ControlFlow::Continue(c) | ||
| } | ||
|
|
||
| fn sort_info_tables(tables: &mut [InfoTable]) { | ||
| tables.sort_by(|a, b| { | ||
| a.connection | ||
| .cmp(&b.connection) | ||
| .then_with(|| a.schema.cmp(&b.schema)) | ||
| .then_with(|| a.table.cmp(&b.table)) | ||
| }); | ||
| } | ||
|
|
||
| fn collect_tables( | ||
| api: &ApiClient, | ||
| connection_id: Option<&str>, | ||
|
|
@@ -86,17 +113,12 @@ fn collect_tables( | |
| } | ||
| let body: InfoListResponse = api.get_with_params("/information_schema", ¶ms); | ||
| out.extend(body.tables); | ||
| if !body.has_more { | ||
| break; | ||
| match information_schema_followup(body.has_more, body.next_cursor) { | ||
| ControlFlow::Break(()) => break, | ||
| ControlFlow::Continue(c) => cursor = Some(c), | ||
| } | ||
| cursor = body.next_cursor; | ||
| } | ||
| out.sort_by(|a, b| { | ||
| a.connection | ||
| .cmp(&b.connection) | ||
| .then_with(|| a.schema.cmp(&b.schema)) | ||
| .then_with(|| a.table.cmp(&b.table)) | ||
| }); | ||
| sort_info_tables(&mut out); | ||
| out | ||
| } | ||
|
|
||
|
|
@@ -266,3 +288,153 @@ pub fn create( | |
| println!("{}", "Index created.".green()); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn information_schema_followup_terminates_when_not_has_more() { | ||
| assert!(matches!( | ||
| information_schema_followup(false, Some("c".into())), | ||
| ControlFlow::Break(()) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn information_schema_followup_breaks_when_more_but_no_cursor() { | ||
| assert!(matches!( | ||
| information_schema_followup(true, None), | ||
| ControlFlow::Break(()) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn information_schema_followup_continues_with_cursor() { | ||
| assert!(matches!( | ||
| information_schema_followup(true, Some("next".into())), | ||
| ControlFlow::Continue(ref s) if s == "next" | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sort_info_tables_orders_by_connection_schema_table() { | ||
| let mut tables = vec![ | ||
| InfoTable { | ||
| connection: "b".into(), | ||
| schema: "s".into(), | ||
| table: "t2".into(), | ||
| }, | ||
| InfoTable { | ||
| connection: "a".into(), | ||
| schema: "s".into(), | ||
| table: "t1".into(), | ||
| }, | ||
| ]; | ||
| sort_info_tables(&mut tables); | ||
| assert_eq!(tables[0].table, "t1"); | ||
| assert_eq!(tables[1].table, "t2"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn connection_label_to_id_map_maps_names_only() { | ||
| let connections = vec![ | ||
| ConnectionRef { | ||
| id: "conn-id".into(), | ||
| name: "Warehouse".into(), | ||
| }, | ||
| ConnectionRef { | ||
| id: "other".into(), | ||
| name: "Lake".into(), | ||
| }, | ||
| ]; | ||
| let m = connection_label_to_id_map(&connections); | ||
| assert_eq!(m.get("Warehouse").map(String::as_str), Some("conn-id")); | ||
| assert_eq!(m.get("Lake").map(String::as_str), Some("other")); | ||
| assert!(!m.contains_key("conn-id")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn collect_tables_single_page() { | ||
| let mut server = mockito::Server::new(); | ||
| let mock = server | ||
| .mock("GET", "/information_schema") | ||
| .match_header("Authorization", "Bearer k") | ||
| .match_header("X-Workspace-Id", "ws1") | ||
| .with_status(200) | ||
| .with_body( | ||
| r#"{"tables":[ | ||
| {"connection":"c1","schema":"public","table":"z"}, | ||
| {"connection":"c1","schema":"public","table":"a"} | ||
| ],"has_more":false,"next_cursor":null}"#, | ||
| ) | ||
| .create(); | ||
|
|
||
| let api = ApiClient::test_new(&server.url(), "k", Some("ws1")); | ||
| let tables = collect_tables(&api, None, None, None); | ||
| mock.assert(); | ||
| assert_eq!(tables.len(), 2); | ||
| assert_eq!(tables[0].table, "a"); | ||
| assert_eq!(tables[1].table, "z"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn list_one_table_scan_returns_empty_on_404() { | ||
| let mut server = mockito::Server::new(); | ||
| let mock = server | ||
| .mock( | ||
| "GET", | ||
| mockito::Matcher::Regex(r"^/connections/.+/tables/.+/.+/indexes$".into()), | ||
| ) | ||
| .match_header("Authorization", "Bearer k") | ||
| .with_status(404) | ||
| .create(); | ||
|
|
||
| let api = ApiClient::test_new(&server.url(), "k", Some("ws")); | ||
| let rows = list_one_table_scan(&api, "cid", "sch", "tbl"); | ||
| mock.assert(); | ||
| assert!(rows.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn list_one_table_returns_indexes() { | ||
| let mut server = mockito::Server::new(); | ||
| let mock = server | ||
| .mock("GET", "/connections/cid/tables/sch/tbl/indexes") | ||
| .match_header("Authorization", "Bearer k") | ||
| .with_status(200) | ||
| .with_body( | ||
| r#"{"indexes":[{ | ||
| "index_name":"ix1", | ||
| "index_type":"btree", | ||
| "columns":["c1"], | ||
| "metric":null, | ||
| "status":"ready", | ||
| "created_at":"2020-01-01T00:00:00Z", | ||
| "updated_at":"2020-01-01T00:00:00Z" | ||
| }]}"#, | ||
| ) | ||
| .create(); | ||
|
|
||
| let api = ApiClient::test_new(&server.url(), "k", None); | ||
| let rows = list_one_table(&api, "cid", "sch", "tbl"); | ||
| mock.assert(); | ||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(rows[0].index_name, "ix1"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn list_one_table_scan_returns_indexes_on_200() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: Unlike the other mockito tests in this file, this one doesn't call |
||
| let mut server = mockito::Server::new(); | ||
| let mock = server | ||
| .mock("GET", "/connections/x/tables/s/t/indexes") | ||
| .with_status(200) | ||
| .with_body(r#"{"indexes":[]}"#) | ||
| .create(); | ||
|
|
||
| let api = ApiClient::test_new(&server.url(), "k", None); | ||
| let rows = list_one_table_scan(&api, "x", "s", "t"); | ||
| mock.assert(); | ||
| assert!(rows.is_empty()); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The test only exercises connection-level ordering (entries differ at
connection). Schema and table tiebreaking are never reached, so the test name oversells the coverage. Consider adding a second pair that shares the same connection but differs on schema or table to actually verify the composite key. (not blocking)