From 84c1dda39d8aae1335570259aaac3b5e8f41398a Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Wed, 7 Apr 2021 15:06:14 +0300 Subject: [PATCH] test(http): setting enum serialize/deserialize --- Cargo.lock | 10 ++++++ http-ui/Cargo.toml | 3 ++ http-ui/src/main.rs | 61 +++++++++++++++++++++++++++++++++++- milli/src/update/settings.rs | 4 +-- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91e72450a..a76ad8709 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -911,6 +911,7 @@ dependencies = [ "rayon", "serde", "serde_json", + "serde_test", "stderrlog", "structopt", "tempfile", @@ -2079,6 +2080,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_test" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4bb5fef7eaf5a97917567183607ac4224c5b451c15023930f23b937cce879fe" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.6.1" diff --git a/http-ui/Cargo.toml b/http-ui/Cargo.toml index 02a799091..748564c03 100644 --- a/http-ui/Cargo.toml +++ b/http-ui/Cargo.toml @@ -37,3 +37,6 @@ fst = "0.4.5" # Temporary fix for bitvec, remove once fixed. (https://github.com/bitvecto-rs/bitvec/issues/105) funty = "=1.1" + +[dev-dependencies] +serde_test = "1.0.125" diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index 6e9a07855..1b77e443e 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -242,7 +242,7 @@ enum UpdateMetaProgress { }, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(deny_unknown_fields)] #[serde(rename_all = "camelCase")] struct Settings { @@ -993,3 +993,62 @@ async fn main() -> anyhow::Result<()> { let addr = SocketAddr::from_str(&opt.http_listen_addr)?; Ok(warp::serve(routes).run(addr).await) } + +#[cfg(test)] +mod tests { + use serde_test::{assert_de_tokens, assert_ser_tokens, Token}; + + use milli::update::Setting; + + use crate::Settings; + + #[test] + fn serialize_settings() { + let settings = Settings { + displayed_attributes: Setting::Set(vec!["name".to_string()]), + searchable_attributes: Setting::Reset, + faceted_attributes: Setting::NotSet, + criteria: Setting::NotSet, + stop_words: Default::default(), + }; + + assert_ser_tokens(&settings, &[ + Token::Struct { name: "Settings", len: 3 }, + Token::Str("displayedAttributes"), + Token::Some, + Token::Seq { len: Some(1) }, + Token::Str("name"), + Token::SeqEnd, + Token::Str("searchableAttributes"), + Token::None, + Token::Str("facetedAttributes"), + Token::None, + Token::StructEnd, + ]); + } + + #[test] + fn deserialize_settings() { + let settings = Settings { + displayed_attributes: Setting::Set(vec!["name".to_string()]), + searchable_attributes: Setting::Reset, + faceted_attributes: Setting::Reset, + criteria: Setting::NotSet, + stop_words: Setting::NotSet, + }; + + assert_de_tokens(&settings, &[ + Token::Struct { name: "Settings", len: 3 }, + Token::Str("displayedAttributes"), + Token::Some, + Token::Seq { len: Some(1) }, + Token::Str("name"), + Token::SeqEnd, + Token::Str("searchableAttributes"), + Token::None, + Token::Str("facetedAttributes"), + Token::None, + Token::StructEnd, + ]); + } +} diff --git a/milli/src/update/settings.rs b/milli/src/update/settings.rs index f73d0f4d2..5ad942ad6 100644 --- a/milli/src/update/settings.rs +++ b/milli/src/update/settings.rs @@ -14,11 +14,11 @@ use crate::facet::FacetType; use crate::update::{ClearDocuments, IndexDocuments, UpdateIndexingStep}; use crate::update::index_documents::{IndexDocumentsMethod, Transform}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum Setting { Set(T), - NotSet, Reset, + NotSet, } impl Default for Setting {