Merge pull request #154 from shekhirin/shekhirin/fix-settings-serde-tests

test(http): fix and refactor settings assert_(ser|de)_tokens
This commit is contained in:
Clément Renault 2021-04-11 10:52:38 +02:00 committed by GitHub
commit 5efe67f375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 23 deletions

1
Cargo.lock generated
View File

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

View File

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

View File

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