test(http): combine settings assert_(ser|de)_tokens into 1 test

This commit is contained in:
Alexey Shekhirin 2021-04-10 12:13:59 +03:00
parent 0d09c64dde
commit 3af8fa194c
No known key found for this signature in database
GPG Key ID: AF9A26AA133B5B98
3 changed files with 60 additions and 23 deletions

1
Cargo.lock generated
View File

@ -904,6 +904,7 @@ dependencies = [
"grenad", "grenad",
"heed", "heed",
"log", "log",
"maplit",
"meilisearch-tokenizer", "meilisearch-tokenizer",
"memmap", "memmap",
"milli", "milli",

View File

@ -39,4 +39,5 @@ fst = "0.4.5"
funty = "=1.1" funty = "=1.1"
[dev-dependencies] [dev-dependencies]
maplit = "1.0.2"
serde_test = "1.0.125" serde_test = "1.0.125"

View File

@ -996,58 +996,93 @@ async fn main() -> anyhow::Result<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_test::{assert_de_tokens, assert_ser_tokens, Token}; use maplit::{btreeset,hashmap};
use serde_test::{assert_tokens, Token};
use milli::update::Setting; use milli::update::Setting;
use crate::Settings; use crate::Settings;
#[test] #[test]
fn serialize_settings() { fn serde_settings_set() {
let settings = Settings { let settings = Settings {
displayed_attributes: Setting::Set(vec!["name".to_string()]), displayed_attributes: Setting::Set(vec!["name".to_string()]),
searchable_attributes: Setting::Reset, searchable_attributes: Setting::Set(vec!["age".to_string()]),
faceted_attributes: Setting::NotSet, faceted_attributes: Setting::Set(hashmap! { "age".into() => "integer".into() }),
criteria: Setting::NotSet, criteria: Setting::Set(vec!["asc(age)".to_string()]),
stop_words: Default::default(), stop_words: Setting::Set(btreeset! { "and".to_string() }),
}; };
assert_ser_tokens(&settings, &[ assert_tokens(&settings, &[
Token::Struct { name: "Settings", len: 3 }, Token::Struct { name: "Settings", len: 5 },
Token::Str("displayedAttributes"), Token::Str("displayedAttributes"),
Token::Some, Token::Some,
Token::Seq { len: Some(1) }, Token::Seq { len: Some(1) },
Token::Str("name"), Token::Str("name"),
Token::SeqEnd, Token::SeqEnd,
Token::Str("searchableAttributes"), Token::Str("searchableAttributes"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::SeqEnd,
Token::Str("facetedAttributes"),
Token::Some,
Token::Map { len: Some(1) },
Token::Str("age"),
Token::Str("integer"),
Token::MapEnd,
Token::Str("criteria"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Str("asc(age)"),
Token::SeqEnd,
Token::Str("stopWords"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Str("and"),
Token::SeqEnd,
Token::StructEnd,
]);
}
#[test]
fn serde_settings_reset() {
let settings = Settings {
displayed_attributes: Setting::Reset,
searchable_attributes: Setting::Reset,
faceted_attributes: Setting::Reset,
criteria: Setting::Reset,
stop_words: Setting::Reset,
};
assert_tokens(&settings, &[
Token::Struct { name: "Settings", len: 5 },
Token::Str("displayedAttributes"),
Token::None,
Token::Str("searchableAttributes"),
Token::None, Token::None,
Token::Str("facetedAttributes"), Token::Str("facetedAttributes"),
Token::None, Token::None,
Token::Str("criteria"),
Token::None,
Token::Str("stopWords"),
Token::None,
Token::StructEnd, Token::StructEnd,
]); ]);
} }
#[test] #[test]
fn deserialize_settings() { fn serde_settings_notset() {
let settings = Settings { let settings = Settings {
displayed_attributes: Setting::Set(vec!["name".to_string()]), displayed_attributes: Setting::NotSet,
searchable_attributes: Setting::Reset, searchable_attributes: Setting::NotSet,
faceted_attributes: Setting::Reset, faceted_attributes: Setting::NotSet,
criteria: Setting::NotSet, criteria: Setting::NotSet,
stop_words: Setting::NotSet, stop_words: Setting::NotSet,
}; };
assert_de_tokens(&settings, &[ assert_tokens(&settings, &[
Token::Struct { name: "Settings", len: 3 }, Token::Struct { name: "Settings", len: 0 },
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, Token::StructEnd,
]); ]);
} }