Skip to content

Commit

Permalink
Fix bug in deserializing unknown features (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
inahga authored Feb 22, 2024
1 parent 3dfccbb commit 4ed8096
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/entity/aggregator/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashSet;
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum Feature {
TokenHash,
#[serde(untagged)]
Unknown(String),
}

Expand All @@ -27,3 +28,70 @@ impl FromIterator<Feature> for Features {
Self(iter.into_iter().collect())
}
}

#[cfg(test)]
mod tests {
use serde_json::json;

use crate::entity::aggregator::{Feature, Features};

#[test]
fn features_none() {
assert_eq!(
serde_json::from_value::<Features>(json!([])).unwrap(),
Features::from_iter([])
)
}

#[test]
fn features_token_hash() {
assert_eq!(
serde_json::from_value::<Features>(json!(["TokenHash"])).unwrap(),
Features::from_iter([Feature::TokenHash])
);
assert_eq!(
serde_json::from_value::<Features>(json!(["TokenHash", "TokenHash", "TokenHash"]))
.unwrap(),
Features::from_iter([Feature::TokenHash])
);
}

#[test]
fn features_unknown() {
assert_eq!(
serde_json::from_value::<Features>(json!(["UnspecifiedUnknownFeature"])).unwrap(),
Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())])
);

assert_eq!(
serde_json::from_value::<Features>(json!([
"UnspecifiedUnknownFeature",
"UnspecifiedUnknownFeature",
"UnspecifiedUnknownFeature"
]))
.unwrap(),
Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())])
);

assert_eq!(
serde_json::from_value::<Features>(json!([
"UnspecifiedUnknownFeature",
"AnotherUnspecifiedUnknownFeature"
]))
.unwrap(),
Features::from_iter([
Feature::Unknown("UnspecifiedUnknownFeature".to_string()),
Feature::Unknown("AnotherUnspecifiedUnknownFeature".to_string())
])
);

assert_eq!(
serde_json::from_value::<Features>(json!(["UnspecifiedUnknownFeature", "TokenHash"]))
.unwrap(),
Features::from_iter([
Feature::TokenHash,
Feature::Unknown("UnspecifiedUnknownFeature".to_string())
])
);
}
}

0 comments on commit 4ed8096

Please sign in to comment.