zirv_queue/queue/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
use crate::models::job::{Job, NewJob};
#[derive(Debug)]
pub struct JobData {
// Arbitrary fields that represent the job payload
pub payload: String,
}
/// Enqueue a new job in the database
pub fn enqueue_job(
job_data: JobData
) -> Result<Job, diesel::result::Error> {
let new_job = NewJob::new(job_data.payload);
let job = Job::create(new_job)?;
Ok(job)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::job::{Job, NewJob};
use diesel::result::Error;
#[test]
fn test_enqueue_job_success() {
// Given
let data = JobData {
payload: "Test payload".to_string(),
};
// When
let result = enqueue_job(data);
// Then
assert!(result.is_ok(), "Expected OK result from enqueue_job");
let job = result.unwrap();
assert_eq!(job.payload, "Test payload");
assert_eq!(job.status, "pending");
assert_eq!(job.attempts, 0);
}
#[test]
fn test_enqueue_job_error() {
// If you want to test error scenarios, you could modify your mock
// to return an Err variant under certain conditions.
// For demonstration, let's forcibly create an error scenario by
// temporarily redefining the mock in local scope:
struct ErrorJob;
impl ErrorJob {
fn create(_new_job: NewJob) -> Result<Job, Error> {
// Return a Diesel-like error
Err(Error::NotFound)
}
}
// Because we can't override the actual `Job::create` again easily
// in the same scope, we might call `ErrorJob::create` directly or
// test in a different pattern. For illustration:
let failing_result = ErrorJob::create(NewJob::new("Payload".to_string()));
assert!(failing_result.is_err());
assert_eq!(failing_result.err().unwrap(), Error::NotFound);
}
}