test(http): setting enum serialize/deserialize

This commit is contained in:
Alexey Shekhirin 2021-04-07 15:06:14 +03:00
parent dc636d190d
commit 84c1dda39d
No known key found for this signature in database
GPG Key ID: AF9A26AA133B5B98
4 changed files with 75 additions and 3 deletions

10
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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,
]);
}
}

View File

@ -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<T> {
Set(T),
NotSet,
Reset,
NotSet,
}
impl<T> Default for Setting<T> {