use serde::{Deserialize, Serialize};
use crate::rest::{ReadOnlyResource, ResourceOperation, ResourcePath, RestResource};
use crate::HttpMethod;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct Currency {
#[serde(skip_serializing)]
pub currency: Option<String>,
#[serde(skip_serializing)]
pub rate_updated_at: Option<String>,
#[serde(skip_serializing)]
pub enabled: Option<bool>,
}
impl RestResource for Currency {
type Id = String;
type FindParams = ();
type AllParams = ();
type CountParams = ();
const NAME: &'static str = "Currency";
const PLURAL: &'static str = "currencies";
const PATHS: &'static [ResourcePath] = &[
ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "currencies"),
];
fn get_id(&self) -> Option<Self::Id> {
self.currency.clone()
}
}
impl ReadOnlyResource for Currency {}
#[cfg(test)]
mod tests {
use super::*;
use crate::rest::{get_path, ReadOnlyResource, ResourceOperation, RestResource};
#[test]
fn test_currency_implements_read_only_resource() {
fn assert_read_only<T: ReadOnlyResource>() {}
assert_read_only::<Currency>();
}
#[test]
fn test_currency_deserialization() {
let json = r#"{
"currency": "CAD",
"rate_updated_at": "2024-01-15T10:30:00-05:00",
"enabled": true
}"#;
let currency: Currency = serde_json::from_str(json).unwrap();
assert_eq!(currency.currency, Some("CAD".to_string()));
assert_eq!(
currency.rate_updated_at,
Some("2024-01-15T10:30:00-05:00".to_string())
);
assert_eq!(currency.enabled, Some(true));
}
#[test]
fn test_currency_list_only_paths() {
let all_path = get_path(Currency::PATHS, ResourceOperation::All, &[]);
assert!(all_path.is_some());
assert_eq!(all_path.unwrap().template, "currencies");
let find_path = get_path(Currency::PATHS, ResourceOperation::Find, &["id"]);
assert!(find_path.is_none());
let count_path = get_path(Currency::PATHS, ResourceOperation::Count, &[]);
assert!(count_path.is_none());
let create_path = get_path(Currency::PATHS, ResourceOperation::Create, &[]);
assert!(create_path.is_none());
let update_path = get_path(Currency::PATHS, ResourceOperation::Update, &["id"]);
assert!(update_path.is_none());
let delete_path = get_path(Currency::PATHS, ResourceOperation::Delete, &["id"]);
assert!(delete_path.is_none());
}
#[test]
fn test_currency_constants() {
assert_eq!(Currency::NAME, "Currency");
assert_eq!(Currency::PLURAL, "currencies");
}
#[test]
fn test_currency_get_id_returns_code() {
let currency_with_code = Currency {
currency: Some("USD".to_string()),
rate_updated_at: None,
enabled: Some(true),
};
assert_eq!(currency_with_code.get_id(), Some("USD".to_string()));
let currency_without_code = Currency::default();
assert_eq!(currency_without_code.get_id(), None);
}
#[test]
fn test_currency_all_fields_are_read_only() {
let currency = Currency {
currency: Some("EUR".to_string()),
rate_updated_at: Some("2024-01-15T10:30:00-05:00".to_string()),
enabled: Some(true),
};
let json = serde_json::to_value(¤cy).unwrap();
assert_eq!(json, serde_json::json!({}));
}
}