From 451b9ba67ebe07e7b089e6624aa8f9d090b3aa2a Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 20 Oct 2023 23:48:06 -0400 Subject: [PATCH] tests --- martin/src/pg/configurator.rs | 303 ++++++++++++++++++++++------------ martin/src/utils/utilities.rs | 15 ++ 2 files changed, 211 insertions(+), 107 deletions(-) mode change 100755 => 100644 martin/src/pg/configurator.rs diff --git a/martin/src/pg/configurator.rs b/martin/src/pg/configurator.rs old mode 100755 new mode 100644 index f6017adda..7a67a1fc2 --- a/martin/src/pg/configurator.rs +++ b/martin/src/pg/configurator.rs @@ -26,18 +26,32 @@ pub type SqlFuncInfoMapMap = InfoMap>; pub type SqlTableInfoMapMapMap = InfoMap>>; #[derive(Debug, PartialEq)] +#[cfg_attr(test, derive(serde::Serialize))] pub struct PgBuilderFuncs { + #[cfg_attr(test, serde(skip_serializing_if = "Option::is_none"))] schemas: Option>, source_id_format: String, } #[derive(Debug, Default, PartialEq)] +#[cfg_attr(test, derive(serde::Serialize))] pub struct PgBuilderTables { + #[cfg_attr( + test, + serde( + skip_serializing_if = "Option::is_none", + serialize_with = "crate::utils::sorted_opt_set" + ) + )] schemas: Option>, source_id_format: String, + #[cfg_attr(test, serde(skip_serializing_if = "Option::is_none"))] id_columns: Option>, + #[cfg_attr(test, serde(skip_serializing_if = "Option::is_none"))] clip_geom: Option, + #[cfg_attr(test, serde(skip_serializing_if = "Option::is_none"))] buffer: Option, + #[cfg_attr(test, serde(skip_serializing_if = "Option::is_none"))] extent: Option, } @@ -78,35 +92,7 @@ impl PgBuilder { pub async fn new(config: &PgConfig, id_resolver: IdResolver) -> Result { let pool = PgPool::new(config).await?; - let auto_tables = if use_auto_publish(config, false) { - let schemas = get_auto_schemas!(config, tables); - let bld = if let Object(PgCfgPublish { - tables: Object(v), .. - }) = &config.auto_publish - { - PgBuilderTables { - schemas, - source_id_format: v - .source_id_format - .as_deref() - .unwrap_or("{table}") - .to_string(), - id_columns: v.id_columns.opt_iter().map(|v| v.cloned().collect()), - clip_geom: v.clip_geom, - buffer: v.buffer, - extent: v.extent, - } - } else { - PgBuilderTables { - schemas, - source_id_format: "{table}".to_string(), - ..Default::default() - } - }; - Some(bld) - } else { - None - }; + let (auto_tables, auto_functions) = calc_auto(config); Ok(Self { pool, @@ -116,26 +102,7 @@ impl PgBuilder { id_resolver, tables: config.tables.clone().unwrap_or_default(), functions: config.functions.clone().unwrap_or_default(), - auto_functions: if use_auto_publish(config, true) { - Some(PgBuilderFuncs { - schemas: get_auto_schemas!(config, functions), - source_id_format: if let Object(PgCfgPublish { - functions: - Object(PgCfgPublishFuncs { - source_id_format: Some(v), - .. - }), - .. - }) = &config.auto_publish - { - v.clone() - } else { - "{function}".to_string() - }, - }) - } else { - None - }, + auto_functions, auto_tables, }) } @@ -410,6 +377,61 @@ fn update_auto_fields(id: &str, inf: &mut TableInfo, auto_tables: &PgBuilderTabl ); } +fn calc_auto(config: &PgConfig) -> (Option, Option) { + let auto_tables = if use_auto_publish(config, false) { + let schemas = get_auto_schemas!(config, tables); + let bld = if let Object(PgCfgPublish { + tables: Object(v), .. + }) = &config.auto_publish + { + PgBuilderTables { + schemas, + source_id_format: v + .source_id_format + .as_deref() + .unwrap_or("{table}") + .to_string(), + id_columns: v.id_columns.opt_iter().map(|v| v.cloned().collect()), + clip_geom: v.clip_geom, + buffer: v.buffer, + extent: v.extent, + } + } else { + PgBuilderTables { + schemas, + source_id_format: "{table}".to_string(), + ..Default::default() + } + }; + Some(bld) + } else { + None + }; + + let auto_functions = if use_auto_publish(config, true) { + Some(PgBuilderFuncs { + schemas: get_auto_schemas!(config, functions), + source_id_format: if let Object(PgCfgPublish { + functions: + Object(PgCfgPublishFuncs { + source_id_format: Some(v), + .. + }), + .. + }) = &config.auto_publish + { + v.clone() + } else { + "{function}".to_string() + }, + }) + } else { + None + }; + + (auto_tables, auto_functions) +} + fn use_auto_publish(config: &PgConfig, for_functions: bool) -> bool { match &config.auto_publish { NoValue => config.tables.is_none() && config.functions.is_none(), @@ -466,97 +488,164 @@ fn by_key(a: &(String, T), b: &(String, T)) -> Ordering { #[cfg(test)] mod tests { use indoc::indoc; + use insta::assert_yaml_snapshot; use super::*; - fn parse_yaml(content: &str) -> PgConfig { - serde_yaml::from_str(content).unwrap() - } - - #[test] - fn test_auto_publish_no_auto() { - let config = parse_yaml("{}"); - assert!(use_auto_publish(&config, false)); - assert!(use_auto_publish(&config, true)); - - let config = parse_yaml("tables: {}"); - assert!(!use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); - - let config = parse_yaml("functions: {}"); - assert!(!use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); + #[derive(serde::Serialize)] + struct AutoCfg { + auto_table: Option, + auto_funcs: Option, } - - #[test] - fn test_auto_publish_bool() { - let config = parse_yaml("auto_publish: true"); - assert!(use_auto_publish(&config, false)); - assert!(use_auto_publish(&config, true)); - - let config = parse_yaml("auto_publish: false"); - assert!(!use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); + fn auto(content: &str) -> AutoCfg { + let cfg: PgConfig = serde_yaml::from_str(content).unwrap(); + let (auto_table, auto_funcs) = calc_auto(&cfg); + AutoCfg { + auto_table, + auto_funcs, + } } #[test] - fn test_auto_publish_obj_bool() { - let config = parse_yaml(indoc! {" + #[allow(clippy::too_many_lines)] + fn test_auto_publish_no_auto() { + let cfg = auto("{}"); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + source_id_format: "{table}" + auto_funcs: + source_id_format: "{function}" + "###); + + let cfg = auto("tables: {}"); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: ~ + auto_funcs: ~ + "###); + + let cfg = auto("functions: {}"); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: ~ + auto_funcs: ~ + "###); + + let cfg = auto("auto_publish: true"); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + source_id_format: "{table}" + auto_funcs: + source_id_format: "{function}" + "###); + + let cfg = auto("auto_publish: false"); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: ~ + auto_funcs: ~ + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public tables: true"}); - assert!(use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); - - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + schemas: + - public + source_id_format: "{table}" + auto_funcs: ~ + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public functions: true"}); - assert!(!use_auto_publish(&config, false)); - assert!(use_auto_publish(&config, true)); - - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: ~ + auto_funcs: + schemas: + - public + source_id_format: "{function}" + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public tables: false"}); - assert!(!use_auto_publish(&config, false)); - assert!(use_auto_publish(&config, true)); - - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: ~ + auto_funcs: + schemas: + - public + source_id_format: "{function}" + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public functions: false"}); - assert!(use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); - } - - #[test] - fn test_auto_publish_obj_obj() { - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + schemas: + - public + source_id_format: "{table}" + auto_funcs: ~ + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public tables: from_schemas: osm id_format: 'foo_{schema}.{table}_bar'"}); - assert!(use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); - - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + schemas: + - osm + - public + source_id_format: "foo_{schema}.{table}_bar" + auto_funcs: ~ + "###); + + let cfg = auto(indoc! {" auto_publish: from_schemas: public tables: from_schemas: osm source_id_format: '{schema}.{table}'"}); - assert!(use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); - - let config = parse_yaml(indoc! {" + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + schemas: + - osm + - public + source_id_format: "{schema}.{table}" + auto_funcs: ~ + "###); + + let cfg = auto(indoc! {" auto_publish: tables: from_schemas: - osm - public"}); - assert!(use_auto_publish(&config, false)); - assert!(!use_auto_publish(&config, true)); + assert_yaml_snapshot!(cfg, @r###" + --- + auto_table: + schemas: + - osm + - public + source_id_format: "{table}" + auto_funcs: ~ + "###); } } diff --git a/martin/src/utils/utilities.rs b/martin/src/utils/utilities.rs index c4893c34c..e3a83f352 100644 --- a/martin/src/utils/utilities.rs +++ b/martin/src/utils/utilities.rs @@ -23,6 +23,21 @@ pub fn sorted_btree_map(value: &HashMap) -> BTreeMa BTreeMap::from_iter(items) } +#[cfg(test)] +pub fn sorted_opt_set( + value: &Option>, + serializer: S, +) -> Result { + value + .as_ref() + .map(|v| { + let mut v: Vec<_> = v.iter().collect(); + v.sort(); + v + }) + .serialize(serializer) +} + pub fn decode_gzip(data: &[u8]) -> Result, std::io::Error> { let mut decoder = GzDecoder::new(data); let mut decompressed = Vec::new();