From 36266ee9be101ef32f2e5b5ff465041cd4727763 Mon Sep 17 00:00:00 2001 From: Aryan Godara Date: Thu, 18 Apr 2024 01:40:49 +0530 Subject: [PATCH] write job tests, create new fixtures --- crates/orchestrator/src/jobs/types.rs | 2 +- crates/orchestrator/tests/common/mod.rs | 26 +++++++++++- crates/orchestrator/tests/config/mod.rs | 24 ++++------- crates/orchestrator/tests/jobs/da_job/mod.rs | 43 ++++++++++++++++++++ crates/orchestrator/tests/jobs/mod.rs | 16 +------- 5 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 crates/orchestrator/tests/jobs/da_job/mod.rs diff --git a/crates/orchestrator/src/jobs/types.rs b/crates/orchestrator/src/jobs/types.rs index 204b05ef..f97c0927 100644 --- a/crates/orchestrator/src/jobs/types.rs +++ b/crates/orchestrator/src/jobs/types.rs @@ -66,7 +66,7 @@ fn unwrap_external_id_failed(expected: &str, got: &ExternalId) -> color_eyre::ey eyre!("wrong ExternalId type: expected {}, got {:?}", expected, got) } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub enum JobType { /// Submitting DA data to the DA layer DataSubmission, diff --git a/crates/orchestrator/tests/common/mod.rs b/crates/orchestrator/tests/common/mod.rs index cb5d0c82..0d38c86e 100644 --- a/crates/orchestrator/tests/common/mod.rs +++ b/crates/orchestrator/tests/common/mod.rs @@ -4,7 +4,19 @@ use rstest::*; use constants::*; use std::env; -use orchestrator::config::{config, Config}; +use std::collections::HashMap; + +use orchestrator::{ + config::{config, Config}, + jobs::types::{ + JobItem, + JobType::DataSubmission, + JobStatus::Created, + ExternalId, + }, +}; + +use ::uuid::Uuid; #[fixture] pub async fn get_or_init_config( @@ -20,3 +32,15 @@ pub async fn get_or_init_config( config().await } + +#[fixture] +pub fn default_job_item() -> JobItem { + JobItem { + id: Uuid::new_v4(), + internal_id: String::from("0"), + job_type: DataSubmission,status: Created, + external_id: ExternalId::Number(0), + metadata: HashMap::new(), + version: 0, + } +} \ No newline at end of file diff --git a/crates/orchestrator/tests/config/mod.rs b/crates/orchestrator/tests/config/mod.rs index c0029443..c7685726 100644 --- a/crates/orchestrator/tests/config/mod.rs +++ b/crates/orchestrator/tests/config/mod.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use crate::common::default_job_item; use super::common::{ get_or_init_config, @@ -7,18 +7,16 @@ use super::common::{ } }; -use orchestrator::config::Config; -use orchestrator::jobs::types::{ - JobItem, - JobType::DataSubmission, - JobStatus::Created, - ExternalId, +use orchestrator::{ + config::Config, + jobs::types::JobItem, }; + use rstest::*; use starknet::providers::Provider; use starknet::core::types::FieldElement; -use ::uuid::Uuid; + #[fixture] fn vec_of_field_elements() -> Vec { @@ -96,16 +94,10 @@ async fn test_config_da_client( #[tokio::test] async fn test_config_database( #[future] get_or_init_config: &Config, + default_job_item: JobItem, ) { let config = get_or_init_config.await; - let job = JobItem { - id: Uuid::new_v4(), - internal_id: String::from("0"), - job_type: DataSubmission,status: Created, - external_id: ExternalId::Number(0), - metadata: HashMap::new(), - version: 0, - }; + let job = default_job_item; let result = config.database().create_job(job).await; assert!(result.is_err()); } \ No newline at end of file diff --git a/crates/orchestrator/tests/jobs/da_job/mod.rs b/crates/orchestrator/tests/jobs/da_job/mod.rs new file mode 100644 index 00000000..376d4f08 --- /dev/null +++ b/crates/orchestrator/tests/jobs/da_job/mod.rs @@ -0,0 +1,43 @@ +use rstest::*; + +use crate::common::{ + get_or_init_config, + default_job_item, +}; + +use orchestrator::{ + config::Config, + jobs::{ + da_job::DaJob, + types::{JobItem, JobType}, + Job + }, +}; + +#[rstest] +#[tokio::test] +async fn test_create_job( + #[future] get_or_init_config: &Config +) { + let config = get_or_init_config.await; + let job = DaJob.create_job(&config, String::from("0")).await; + assert!(job.is_ok()); + + let job = job.unwrap(); + + let job_type = job.job_type; + assert_eq!(job_type, JobType::DataSubmission); + assert_eq!(job.metadata.values().len(), 0, "metadata should be empty"); + assert_eq!(job.external_id.unwrap_string().unwrap(), String::new(), "external_id should be empty string"); +} + +#[rstest] +// #[should_panic] +#[tokio::test] +async fn test_verify_job( + #[future] #[from(get_or_init_config)] config: &Config, + default_job_item: JobItem, +) { + let config = config.await; + assert!(DaJob.verify_job(config, &default_job_item).await.is_err()); +} \ No newline at end of file diff --git a/crates/orchestrator/tests/jobs/mod.rs b/crates/orchestrator/tests/jobs/mod.rs index 89b4b429..eb9d810d 100644 --- a/crates/orchestrator/tests/jobs/mod.rs +++ b/crates/orchestrator/tests/jobs/mod.rs @@ -1,14 +1,2 @@ -use super::common::get_or_init_config; -use orchestrator::config::Config; -use rstest::*; - -#[rstest] -#[tokio::test] -async fn test_abc( - #[future] - #[with( String::from("http://localhost:9944") )] - get_or_init_config: &Config -) { - get_or_init_config.await; - todo!("setting up the structure before writing the tests"); -} \ No newline at end of file +#[cfg(test)] +pub mod da_job;