Skip to content

Commit

Permalink
Deserializer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cichaczem committed Apr 25, 2024
1 parent aa4f3cd commit 564e234
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/serde_utils/base64_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,53 @@ where

match base64_strings {
Some(base64_vec) => {
let decoded_vec: Vec<Vec<u8>> = base64_vec
let decoded_vec: Result<Vec<Vec<u8>>, _> = base64_vec
.into_iter()
.map(|base64_str| {
general_purpose::STANDARD.decode(base64_str).unwrap()
general_purpose::STANDARD
.decode(base64_str)
.map_err(serde::de::Error::custom)
})
.collect();

Ok(Some(decoded_vec))
Ok(Some(decoded_vec?))
}
None => Ok(None),
}
}

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Test {
#[serde(with = "super")]
blobs: Option<Vec<Vec<u8>>>,
}

#[test]
fn test_deserialize_with_valid_input() {
let blobs =
Some(["Hello", "world!"].map(|b| b.as_bytes().to_vec()).to_vec());
let test = Test {
blobs: blobs.clone(),
};

let s = serde_json::to_string(&test).unwrap();

let test: Test = serde_json::from_str(&s).unwrap();
assert_eq!(test.blobs, blobs);
}

#[test]
fn test_deserialize_with_null_input() {
let test = Test { blobs: None };

let s = serde_json::to_string(&test).unwrap();

let test: Test = serde_json::from_str(&s).unwrap();
assert_eq!(test.blobs, None);
}
}

0 comments on commit 564e234

Please sign in to comment.