fix(models): fixing parsing of snapshot data

This commit is contained in:
Scott Lyons 2024-08-11 13:36:39 -07:00
commit ada4a7733c
3 changed files with 104 additions and 12 deletions

View file

@ -35,6 +35,4 @@ pub mod errors;
pub use errors::SzurubooruResult;
pub mod models;
#[cfg(test)]
mod tests;
pub mod tokens;

View file

@ -890,6 +890,9 @@ pub enum SnapshotResourceType {
Post,
/// Pool resource
Pool,
/// Pool Category
#[serde(rename = "pool_category")]
PoolCategory,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -905,6 +908,8 @@ pub enum SnapshotCreationDeletionData {
Post(PostResource),
/// A pool resource that was created
Pool(PoolResource),
/// A pool category resource that was created
PoolCategory(PoolCategoryResource),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -922,13 +927,6 @@ pub struct SnapshotModificationData {
pub value: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Data for a merged resource
pub struct SnapshotMergeData {
/// Resource IDs that have been merged
pub merged: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
/// Type representing the data as part of a snapshot
@ -939,7 +937,7 @@ pub enum SnapshotData {
/// Data for a modified resource
Modify(SnapshotModificationData),
/// Data for a merged resource
Merge(SnapshotMergeData),
Merge(Vec<String>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -952,7 +950,7 @@ pub struct SnapshotResource {
/// The resource type
pub resource_type: Option<SnapshotResourceType>,
/// The ID of the snapshot itself
pub id: Option<u32>,
pub id: Option<String>,
/// The user who created this change
pub user: Option<MicroUserResource>,
/// The data associated with this resource change
@ -994,7 +992,7 @@ pub struct AroundPostResult {
#[cfg(test)]
mod tests {
use crate::models::{GlobalInfo, GlobalInfoConfig, TagCategoryResource};
use crate::models::{GlobalInfo, GlobalInfoConfig, SnapshotResource, TagCategoryResource};
use chrono::Datelike;
#[test]
@ -1065,4 +1063,95 @@ mod tests {
.expect("Unable to parse tag category string");
assert_eq!(tag_cat.name, Some("default".to_string()));
}
#[test]
fn test_parse_snapshot() {
let input_str = r#"
{
"operation": "merged",
"type": "pool",
"id": "2",
"user": {
"name": "integration_user",
"avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300"
},
"data": [
"pool",
1
],
"time": "2024-08-11T19:53:34.384644Z"
}
"#;
serde_json::from_str::<SnapshotResource>(input_str)
.expect("Could not parse merged snapshot resource");
let input_str = r#"
{
"operation": "modified",
"type": "pool_category",
"id": "cat_pool_category",
"user": {
"name": "integration_user",
"avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300"
},
"data": {
"type": "object change",
"value": {
"default": {
"type": "primitive change",
"old-value": false,
"new-value": true
}
}
},
"time": "2024-08-11T19:53:33.422437Z"
}
"#;
serde_json::from_str::<SnapshotResource>(input_str)
.expect("Could not parse modified snapshot resource");
let input_str = r#"
{
"operation": "deleted",
"type": "pool",
"id": "3",
"user": {
"name": "integration_user",
"avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300"
},
"data": {
"names": [
"dogs_pool"
],
"category": "cat_pool_category",
"posts": []
},
"time": "2024-08-11T19:53:34.006828Z"
}
"#;
serde_json::from_str::<SnapshotResource>(input_str)
.expect("Could not parse deleted snapshot resource");
let input_str = r#"
{
"operation": "created",
"type": "pool",
"id": "1",
"user": {
"name": "integration_user",
"avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300"
},
"data": {
"names": [
"cats_pool"
],
"category": "cat_pool_category",
"posts": []
},
"time": "2024-08-11T19:53:33.613959Z"
}
"#;
serde_json::from_str::<SnapshotResource>(input_str)
.expect("Could not parse created snapshot resource");
}
}