use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(deny_unknown_fields)]
pub struct AnnotationUpsert {
pub source_key: String,
pub stream_id: Uuid,
pub time: DateTime<Utc>,
pub category: String,
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub standard_curve_id: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnnotationMapping {
pub source_key: String,
pub id: Option<Uuid>,
pub status: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(deny_unknown_fields)]
pub struct NoteUpsert {
pub source_key: String,
pub site_name: String,
pub text: String,
pub verified: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct NoteMapping {
pub source_key: String,
pub id: Option<Uuid>,
pub status: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn annotation_upsert_round_trips() {
let up = AnnotationUpsert {
source_key: "corrections:12".into(),
stream_id: Uuid::nil(),
time: Utc::now(),
category: "audit".into(),
text: "corrected with the January curve".into(),
standard_curve_id: Some(Uuid::nil()),
};
let json = serde_json::to_value(&up).unwrap();
let back: AnnotationUpsert = serde_json::from_value(json).unwrap();
assert_eq!(back.source_key, up.source_key);
assert_eq!(back.standard_curve_id, up.standard_curve_id);
}
#[test]
fn an_annotation_without_a_curve_round_trips() {
let up = AnnotationUpsert {
source_key: "corrections:13".into(),
stream_id: Uuid::nil(),
time: Utc::now(),
category: "audit".into(),
text: "no curve".into(),
standard_curve_id: None,
};
let json = serde_json::to_value(&up).unwrap();
assert!(json.get("standard_curve_id").is_none());
let back: AnnotationUpsert = serde_json::from_value(json).unwrap();
assert!(back.standard_curve_id.is_none());
}
#[test]
fn note_upsert_round_trips() {
let up = NoteUpsert {
source_key: "notes:4".into(),
site_name: "FP1".into(),
text: "gauge replaced".into(),
verified: true,
};
let back: NoteUpsert = serde_json::from_value(serde_json::to_value(&up).unwrap()).unwrap();
assert_eq!(back.site_name, "FP1");
assert!(back.verified);
}
}