65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
use chrono::NaiveDate;
|
|
use uuid::Uuid;
|
|
|
|
use crate::types::PersonResponseDto;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Person {
|
|
pub id: Uuid,
|
|
pub name: String,
|
|
pub date_of_birth: Option<NaiveDate>,
|
|
}
|
|
|
|
impl From<PersonResponseDto> for Person {
|
|
fn from(value: PersonResponseDto) -> Self {
|
|
Self {
|
|
id: Uuid::parse_str(&value.id).expect("Unable to parse a person's UUID"),
|
|
name: value.name,
|
|
date_of_birth: value.birth_date,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn from_person_response_dto_success() {
|
|
let dto = PersonResponseDto {
|
|
id: String::from("123e4567-e89b-12d3-a456-426655440000"),
|
|
name: String::from("John Doe"),
|
|
birth_date: Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()),
|
|
is_hidden: false,
|
|
thumbnail_path: String::from("/foo/bar.jpg"),
|
|
updated_at: None,
|
|
};
|
|
|
|
let person = Person::from(dto);
|
|
|
|
assert_eq!(
|
|
person.id,
|
|
Uuid::parse_str("123e4567-e89b-12d3-a456-426655440000").unwrap()
|
|
);
|
|
assert_eq!(person.name, "John Doe".to_string());
|
|
assert_eq!(
|
|
person.date_of_birth,
|
|
Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn from_person_response_dto_invalid_uuid_panics() {
|
|
let dto = PersonResponseDto {
|
|
id: String::from("invalid_uuid"),
|
|
name: String::from("John Doe"),
|
|
birth_date: Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()),
|
|
is_hidden: false,
|
|
thumbnail_path: String::from("/foo/bar.jpg"),
|
|
updated_at: None,
|
|
};
|
|
|
|
let _ = Person::from(dto);
|
|
}
|
|
}
|