Skip to content

Commit

Permalink
Add tests for ProgressUpdateBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Dec 8, 2023
1 parent 68a909b commit 6a6bc3d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub enum Client {
},

/// The 'Job' is malformed.
#[error("progress update hash is malformed: {desc}")]
#[error("progress update is malformed: {desc}")]
ProgressUpdateMalformed {
/// Details on what is missing or incorrect about an instance of 'ProgressUpdate'
desc: String,
Expand Down
73 changes: 73 additions & 0 deletions src/proto/single/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,77 @@ mod test {
assert_ne!(job1.jid, job2.jid);
assert_ne!(job1.created_at, job2.created_at);
}

#[test]
fn test_progress_updaet_needs_jid() {
let result = ProgressUpdateBuilder::default().build();
let err = result.unwrap_err();
assert_eq!(
err.to_string(),
"progress update is malformed: `jid` must be initialized"
)
}

#[test]
fn test_percent_validated_on_progress_update() {
let tracked = utils::gen_random_jid();
let result = ProgressUpdateBuilder::default()
.jid(tracked)
.percent(120)
.build();
let err = result.unwrap_err();
assert_eq!(
err.to_string(),
"progress update is malformed: `percent` indicates job execution progress and should be in the range from 0 to 100 inclusive"
)
}

#[test]
fn test_progress_update_can_be_created_with_builder() {
let tracked = utils::gen_random_jid();
let progress = ProgressUpdateBuilder::default()
.jid(tracked.clone())
.build()
.unwrap();

assert_eq!(progress.jid, tracked);
assert!(progress.desc.is_none());
assert!(progress.percent.is_none());
assert!(progress.reserve_until.is_none());

let extra_time_needed = chrono::Duration::seconds(300);
let extend = Utc::now() + extra_time_needed;
let progress = ProgressUpdateBuilder::default()
.jid(tracked.clone())
.desc("Resizing the image...".to_string())
.percent(67)
.reserve_until(extend)
.build()
.unwrap();
let serialized = serde_json::to_string(&progress).unwrap();
assert!(serialized.contains("jid"));
assert!(serialized.contains(&tracked));

assert!(serialized.contains("desc"));
assert!(serialized.contains("Resizing the image..."));

assert!(serialized.contains("percent"));
assert!(serialized.contains("67"));

assert!(serialized.contains("reserve_until"));
assert!(serialized.contains(&extend.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)));

let progress = ProgressUpdateBuilder::default()
.jid(tracked.clone())
.build()
.unwrap();
let serialized =serde_json::to_string(&progress).unwrap();

assert!(serialized.contains("jid"));
assert!(serialized.contains(&tracked));

assert!(!serialized.contains("percent"));
assert!(!serialized.contains("desc"));
assert!(!serialized.contains("reserve_until"));
}
}

0 comments on commit 6a6bc3d

Please sign in to comment.