83 lines
2.6 KiB
Rust
83 lines
2.6 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use uuid::Uuid;
|
|
|
|
use crate::types::LibraryResponseDto;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Library {
|
|
pub id: Uuid,
|
|
pub name: String,
|
|
pub asset_count: i64,
|
|
pub updated_at: DateTime<Utc>,
|
|
pub refreshed_at: Option<DateTime<Utc>>,
|
|
}
|
|
|
|
impl From<LibraryResponseDto> for Library {
|
|
fn from(value: LibraryResponseDto) -> Self {
|
|
Self {
|
|
id: Uuid::parse_str(&value.id).expect("Unable to parse a library's UUID"),
|
|
name: value.name,
|
|
asset_count: value.asset_count,
|
|
updated_at: value.updated_at,
|
|
refreshed_at: value.refreshed_at,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use std::str::FromStr;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn from_library_response_dto_success() {
|
|
let dto = LibraryResponseDto {
|
|
id: String::from("123e4567-e89b-12d3-a456-426655440000"),
|
|
name: String::from("Christmas photos"),
|
|
asset_count: 1246,
|
|
exclusion_patterns: vec![],
|
|
import_paths: vec![],
|
|
owner_id: String::from("abc123"),
|
|
updated_at: DateTime::<Utc>::from_str("2024-11-17T12:55:12Z").unwrap(),
|
|
created_at: DateTime::<Utc>::from_str("2023-10-17T12:55:12Z").unwrap(),
|
|
refreshed_at: Some(DateTime::<Utc>::from_str("2024-11-17T12:53:12Z").unwrap()),
|
|
};
|
|
|
|
let library = Library::from(dto);
|
|
|
|
assert_eq!(
|
|
library.id,
|
|
Uuid::parse_str("123e4567-e89b-12d3-a456-426655440000").unwrap()
|
|
);
|
|
assert_eq!(library.name, "Christmas photos".to_string());
|
|
assert_eq!(library.asset_count, 1246);
|
|
assert_eq!(
|
|
library.updated_at,
|
|
DateTime::<Utc>::from_str("2024-11-17T12:55:12Z").unwrap()
|
|
);
|
|
assert_eq!(
|
|
library.refreshed_at,
|
|
Some(DateTime::<Utc>::from_str("2024-11-17T12:53:12Z").unwrap())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn from_library_response_dto_invalid_uuid_panics() {
|
|
let dto = LibraryResponseDto {
|
|
id: String::from("invalid_uuid"),
|
|
name: String::from("Christmas photos"),
|
|
asset_count: 1246,
|
|
exclusion_patterns: vec![],
|
|
import_paths: vec![],
|
|
owner_id: String::from("abc123"),
|
|
updated_at: DateTime::<Utc>::from_str("2024-11-17T12:55:12Z").unwrap(),
|
|
created_at: DateTime::<Utc>::from_str("2023-10-17T12:55:12Z").unwrap(),
|
|
refreshed_at: Some(DateTime::<Utc>::from_str("2024-11-17T12:53:12Z").unwrap()),
|
|
};
|
|
|
|
let _ = Library::from(dto);
|
|
}
|
|
}
|