Skip to content
This repository was archived by the owner on May 24, 2025. It is now read-only.

Commit c648a2f

Browse files
committed
refactor(tests): build job item moved to common
1 parent f5a123e commit c648a2f

File tree

5 files changed

+93
-200
lines changed

5 files changed

+93
-200
lines changed

crates/orchestrator/src/tests/database/mod.rs

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
use chrono::{SubsecRound, Utc};
21
use rstest::*;
3-
use uuid::Uuid;
4-
5-
use crate::constants::{BLOB_DATA_FILE_NAME, CAIRO_PIE_FILE_NAME, PROGRAM_OUTPUT_FILE_NAME, SNOS_OUTPUT_FILE_NAME};
6-
use crate::jobs::metadata::{
7-
CommonMetadata, DaMetadata, JobMetadata, JobSpecificMetadata, ProvingInputType, ProvingMetadata, SnosMetadata,
8-
StateUpdateMetadata,
9-
};
10-
use crate::jobs::types::{ExternalId, JobItem, JobItemUpdates, JobStatus, JobType};
2+
3+
use crate::jobs::metadata::JobSpecificMetadata;
4+
use crate::jobs::types::{JobItemUpdates, JobStatus, JobType};
115
use crate::jobs::JobError;
126
use crate::tests::config::{ConfigType, TestConfigBuilder};
7+
use crate::tests::utils::build_job_item;
138

149
#[rstest]
1510
#[tokio::test]
@@ -245,63 +240,3 @@ async fn database_test_update_job() {
245240
panic!("Job not found in Database.")
246241
}
247242
}
248-
249-
// Test Util Functions
250-
// ==========================================
251-
252-
pub fn build_job_item(job_type: JobType, job_status: JobStatus, internal_id: u64) -> JobItem {
253-
let metadata = match job_type {
254-
JobType::StateTransition => JobMetadata {
255-
common: CommonMetadata::default(),
256-
specific: JobSpecificMetadata::StateUpdate(StateUpdateMetadata {
257-
blocks_to_settle: vec![internal_id],
258-
snos_output_paths: vec![format!("{}/{}", internal_id, SNOS_OUTPUT_FILE_NAME)],
259-
program_output_paths: vec![format!("{}/{}", internal_id, PROGRAM_OUTPUT_FILE_NAME)],
260-
blob_data_paths: vec![format!("{}/{}", internal_id, BLOB_DATA_FILE_NAME)],
261-
last_failed_block_no: None,
262-
tx_hashes: Vec::new(),
263-
}),
264-
},
265-
JobType::SnosRun => JobMetadata {
266-
common: CommonMetadata::default(),
267-
specific: JobSpecificMetadata::Snos(SnosMetadata {
268-
block_number: internal_id,
269-
full_output: false,
270-
cairo_pie_path: Some(format!("{}/{}", internal_id, CAIRO_PIE_FILE_NAME)),
271-
snos_output_path: Some(format!("{}/{}", internal_id, SNOS_OUTPUT_FILE_NAME)),
272-
program_output_path: Some(format!("{}/{}", internal_id, PROGRAM_OUTPUT_FILE_NAME)),
273-
snos_fact: None,
274-
}),
275-
},
276-
JobType::ProofCreation => JobMetadata {
277-
common: CommonMetadata::default(),
278-
specific: JobSpecificMetadata::Proving(ProvingMetadata {
279-
block_number: internal_id,
280-
input_path: Some(ProvingInputType::CairoPie(format!("{}/{}", internal_id, CAIRO_PIE_FILE_NAME))),
281-
ensure_on_chain_registration: None,
282-
download_proof: None,
283-
}),
284-
},
285-
JobType::DataSubmission => JobMetadata {
286-
common: CommonMetadata::default(),
287-
specific: JobSpecificMetadata::Da(DaMetadata {
288-
block_number: internal_id,
289-
blob_data_path: Some(format!("{}/{}", internal_id, BLOB_DATA_FILE_NAME)),
290-
tx_hash: None,
291-
}),
292-
},
293-
_ => panic!("Invalid job type"),
294-
};
295-
296-
JobItem {
297-
id: Uuid::new_v4(),
298-
internal_id: internal_id.to_string(),
299-
job_type,
300-
status: job_status,
301-
external_id: ExternalId::Number(0),
302-
metadata,
303-
version: 0,
304-
created_at: Utc::now().round_subsecs(0),
305-
updated_at: Utc::now().round_subsecs(0),
306-
}
307-
}

crates/orchestrator/src/tests/jobs/mod.rs

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ use mongodb::bson::doc;
66
use omniqueue::QueueError;
77
use rstest::rstest;
88
use tokio::time::sleep;
9-
use uuid::Uuid;
109

11-
use super::database::build_job_item;
12-
use crate::constants::{BLOB_DATA_FILE_NAME, CAIRO_PIE_FILE_NAME, PROGRAM_OUTPUT_FILE_NAME, SNOS_OUTPUT_FILE_NAME};
10+
use crate::constants::CAIRO_PIE_FILE_NAME;
1311
use crate::jobs::job_handler_factory::mock_factory;
1412
use crate::jobs::metadata::{
15-
CommonMetadata, DaMetadata, JobMetadata, JobSpecificMetadata, ProvingInputType, ProvingMetadata, SnosMetadata,
16-
StateUpdateMetadata,
13+
CommonMetadata, JobMetadata, JobSpecificMetadata, ProvingInputType, ProvingMetadata, SnosMetadata,
1714
};
18-
use crate::jobs::types::{ExternalId, JobItem, JobStatus, JobType, JobVerificationStatus};
15+
use crate::jobs::types::{ExternalId, JobStatus, JobType, JobVerificationStatus};
1916
use crate::jobs::{create_job, handle_job_failure, process_job, retry_job, verify_job, Job, JobError, MockJob};
2017
use crate::queue::job_queue::QueueNameForJobType;
2118
use crate::queue::QueueType;
2219
use crate::tests::common::MessagePayloadType;
2320
use crate::tests::config::{ConfigType, TestConfigBuilder};
21+
use crate::tests::utils::build_job_item;
2422

2523
#[cfg(test)]
2624
pub mod da_job;
@@ -35,13 +33,12 @@ pub mod state_update_job;
3533
pub mod snos_job;
3634

3735
use assert_matches::assert_matches;
38-
use chrono::{SubsecRound, Utc};
3936

4037
/// Tests `create_job` function when job is not existing in the db.
4138
#[rstest]
4239
#[tokio::test]
4340
async fn create_job_job_does_not_exists_in_db_works() {
44-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Created, "0".to_string());
41+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Created, 0);
4542
let mut job_handler = MockJob::new();
4643

4744
// Adding expectation for creation of new job.
@@ -94,7 +91,7 @@ async fn create_job_job_does_not_exists_in_db_works() {
9491
#[rstest]
9592
#[tokio::test]
9693
async fn create_job_job_exists_in_db_works() {
97-
let job_item = build_job_item_by_type_and_status(JobType::ProofCreation, JobStatus::Created, "0".to_string());
94+
let job_item = build_job_item(JobType::ProofCreation, JobStatus::Created, 0);
9895

9996
let services = TestConfigBuilder::new()
10097
.configure_database(ConfigType::Actual)
@@ -190,7 +187,7 @@ async fn process_job_with_job_exists_in_db_and_valid_job_processing_status_works
190187
let database_client = services.config.database();
191188

192189
// Create a job with proper metadata structure
193-
let job_item = build_job_item_by_type_and_status(job_type.clone(), job_status.clone(), "1".to_string());
190+
let job_item = build_job_item(job_type.clone(), job_status.clone(), 1);
194191

195192
let mut job_handler = MockJob::new();
196193

@@ -244,7 +241,7 @@ async fn process_job_handles_panic() {
244241
let database_client = services.config.database();
245242

246243
// Create a job with proper metadata structure
247-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Created, "1".to_string());
244+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Created, 1);
248245

249246
// Creating job in database
250247
database_client.create_job(job_item.clone()).await.unwrap();
@@ -286,7 +283,7 @@ async fn process_job_handles_panic() {
286283
#[tokio::test]
287284
async fn process_job_with_job_exists_in_db_with_invalid_job_processing_status_errors() {
288285
// Creating a job with Completed status which is invalid processing.
289-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Completed, "1".to_string());
286+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Completed, 1);
290287

291288
// building config
292289
let services = TestConfigBuilder::new()
@@ -320,7 +317,7 @@ async fn process_job_with_job_exists_in_db_with_invalid_job_processing_status_er
320317
#[tokio::test]
321318
async fn process_job_job_does_not_exists_in_db_works() {
322319
// Creating a valid job which is not existing in the db.
323-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Created, "1".to_string());
320+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Created, 1);
324321

325322
// building config
326323
let services = TestConfigBuilder::new()
@@ -366,7 +363,7 @@ async fn process_job_two_workers_process_same_job_works() {
366363
.await;
367364
let db_client = services.config.database();
368365

369-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Created, "1".to_string());
366+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Created, 1);
370367

371368
// Creating the job in the db
372369
db_client.create_job(job_item.clone()).await.unwrap();
@@ -422,7 +419,7 @@ async fn process_job_job_handler_returns_error_works() {
422419
.await;
423420
let db_client = services.config.database();
424421

425-
let job_item = build_job_item_by_type_and_status(JobType::SnosRun, JobStatus::Created, "1".to_string());
422+
let job_item = build_job_item(JobType::SnosRun, JobStatus::Created, 1);
426423

427424
// Creating the job in the db
428425
db_client.create_job(job_item.clone()).await.unwrap();
@@ -439,8 +436,7 @@ async fn process_job_job_handler_returns_error_works() {
439436
#[rstest]
440437
#[tokio::test]
441438
async fn verify_job_with_verified_status_works() {
442-
let job_item =
443-
build_job_item_by_type_and_status(JobType::DataSubmission, JobStatus::PendingVerification, "1".to_string());
439+
let job_item = build_job_item(JobType::DataSubmission, JobStatus::PendingVerification, 1);
444440

445441
// building config
446442
let services = TestConfigBuilder::new()
@@ -486,8 +482,7 @@ async fn verify_job_with_verified_status_works() {
486482
#[rstest]
487483
#[tokio::test]
488484
async fn verify_job_with_rejected_status_adds_to_queue_works() {
489-
let job_item =
490-
build_job_item_by_type_and_status(JobType::DataSubmission, JobStatus::PendingVerification, "1".to_string());
485+
let job_item = build_job_item(JobType::DataSubmission, JobStatus::PendingVerification, 1);
491486

492487
// building config
493488
let services = TestConfigBuilder::new()
@@ -541,8 +536,7 @@ async fn verify_job_with_rejected_status_works() {
541536
let database_client = services.config.database();
542537

543538
// Create a job with proper metadata structure
544-
let mut job_item =
545-
build_job_item_by_type_and_status(JobType::DataSubmission, JobStatus::PendingVerification, "1".to_string());
539+
let mut job_item = build_job_item(JobType::DataSubmission, JobStatus::PendingVerification, 1);
546540

547541
// Set process_attempt_no to 1 to simulate max attempts reached
548542
job_item.metadata.common.process_attempt_no = 1;
@@ -593,8 +587,7 @@ async fn verify_job_with_pending_status_adds_to_queue_works() {
593587
let database_client = services.config.database();
594588

595589
// Create a job with proper metadata structure
596-
let job_item =
597-
build_job_item_by_type_and_status(JobType::DataSubmission, JobStatus::PendingVerification, "1".to_string());
590+
let job_item = build_job_item(JobType::DataSubmission, JobStatus::PendingVerification, 1);
598591

599592
// Creating job in database
600593
database_client.create_job(job_item.clone()).await.unwrap();
@@ -646,8 +639,7 @@ async fn verify_job_with_pending_status_works() {
646639
let database_client = services.config.database();
647640

648641
// Create a job with proper metadata structure
649-
let mut job_item =
650-
build_job_item_by_type_and_status(JobType::DataSubmission, JobStatus::PendingVerification, "1".to_string());
642+
let mut job_item = build_job_item(JobType::DataSubmission, JobStatus::PendingVerification, 1);
651643

652644
// Set verification_attempt_no to 1 to simulate max attempts reached
653645
job_item.metadata.common.verification_attempt_no = 1;
@@ -684,54 +676,6 @@ async fn verify_job_with_pending_status_works() {
684676
assert_matches!(consumed_messages_verification_queue, QueueError::NoData);
685677
}
686678

687-
fn build_job_item_by_type_and_status(job_type: JobType, job_status: JobStatus, internal_id: String) -> JobItem {
688-
let block_number = internal_id.parse::<u64>().unwrap_or(0);
689-
690-
// Create appropriate specific metadata based on job type
691-
let specific_metadata = match job_type {
692-
JobType::SnosRun => JobSpecificMetadata::Snos(SnosMetadata {
693-
block_number,
694-
full_output: false,
695-
cairo_pie_path: Some(format!("{}/{}", block_number, CAIRO_PIE_FILE_NAME)),
696-
snos_output_path: Some(format!("{}/{}", block_number, SNOS_OUTPUT_FILE_NAME)),
697-
program_output_path: Some(format!("{}/{}", block_number, PROGRAM_OUTPUT_FILE_NAME)),
698-
snos_fact: None,
699-
}),
700-
JobType::DataSubmission => JobSpecificMetadata::Da(DaMetadata {
701-
block_number,
702-
blob_data_path: Some(format!("{}/{}", block_number, BLOB_DATA_FILE_NAME)),
703-
tx_hash: None,
704-
}),
705-
JobType::ProofCreation => JobSpecificMetadata::Proving(ProvingMetadata {
706-
block_number,
707-
input_path: Some(ProvingInputType::CairoPie(format!("{}/{}", block_number, CAIRO_PIE_FILE_NAME))),
708-
ensure_on_chain_registration: None,
709-
download_proof: None,
710-
}),
711-
JobType::StateTransition => JobSpecificMetadata::StateUpdate(StateUpdateMetadata {
712-
blocks_to_settle: vec![block_number],
713-
snos_output_paths: vec![format!("{}/{}", block_number, SNOS_OUTPUT_FILE_NAME)],
714-
program_output_paths: vec![format!("{}/{}", block_number, PROGRAM_OUTPUT_FILE_NAME)],
715-
blob_data_paths: vec![format!("{}/{}", block_number, BLOB_DATA_FILE_NAME)],
716-
last_failed_block_no: None,
717-
tx_hashes: Vec::new(),
718-
}),
719-
_ => panic!("Unsupported job type: {:?}", job_type),
720-
};
721-
722-
JobItem {
723-
id: Uuid::new_v4(),
724-
internal_id,
725-
job_type,
726-
status: job_status,
727-
external_id: ExternalId::Number(0),
728-
metadata: JobMetadata { common: CommonMetadata::default(), specific: specific_metadata },
729-
version: 0,
730-
created_at: Utc::now().round_subsecs(0),
731-
updated_at: Utc::now().round_subsecs(0),
732-
}
733-
}
734-
735679
#[rstest]
736680
#[case(JobType::DataSubmission, JobStatus::Completed)] // code should panic here, how can completed move to dl queue ?
737681
#[case(JobType::SnosRun, JobStatus::PendingVerification)]

crates/orchestrator/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ pub mod queue;
1010
pub mod alerts;
1111
pub mod common;
1212
mod data_storage;
13+
pub mod utils;
1314
pub mod workers;

crates/orchestrator/src/tests/server/job_routes.rs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@ use std::net::SocketAddr;
33
use std::sync::Arc;
44
use std::time::Duration;
55

6-
use chrono::{SubsecRound as _, Utc};
76
use hyper::{Body, Request};
87
use mockall::predicate::eq;
98
use rstest::*;
109
use starknet::providers::jsonrpc::HttpTransport;
1110
use starknet::providers::JsonRpcClient;
1211
use url::Url;
1312
use utils::env_utils::get_env_var_or_panic;
14-
use uuid::Uuid;
1513

1614
use crate::config::Config;
1715
use crate::jobs::job_handler_factory::mock_factory;
18-
use crate::jobs::metadata::{
19-
CommonMetadata, DaMetadata, JobMetadata, JobSpecificMetadata, ProvingMetadata, SnosMetadata, StateUpdateMetadata,
20-
};
21-
use crate::jobs::types::{ExternalId, JobItem, JobStatus, JobType};
16+
use crate::jobs::types::{JobStatus, JobType};
2217
use crate::jobs::{Job, MockJob};
2318
use crate::queue::init_consumers;
2419
use crate::queue::job_queue::{JobQueueMessage, QueueNameForJobType};
2520
use crate::routes::types::ApiResponse;
2621
use crate::tests::config::{ConfigType, TestConfigBuilder};
22+
use crate::tests::utils::build_job_item;
2723

2824
#[fixture]
2925
async fn setup_trigger() -> (SocketAddr, Arc<Config>) {
@@ -216,55 +212,3 @@ async fn test_init_consumer() {
216212
let services = TestConfigBuilder::new().build().await;
217213
assert!(init_consumers(services.config).await.is_ok());
218214
}
219-
220-
// Test Util Functions
221-
// ==========================================
222-
223-
pub fn build_job_item(job_type: JobType, job_status: JobStatus, internal_id: u64) -> JobItem {
224-
// Create appropriate job-specific metadata based on job type
225-
let specific_metadata = match job_type {
226-
JobType::DataSubmission => {
227-
JobSpecificMetadata::Da(DaMetadata { block_number: internal_id, blob_data_path: None, tx_hash: None })
228-
}
229-
JobType::SnosRun => JobSpecificMetadata::Snos(SnosMetadata {
230-
block_number: internal_id,
231-
full_output: false,
232-
cairo_pie_path: None,
233-
snos_output_path: None,
234-
program_output_path: None,
235-
snos_fact: None,
236-
}),
237-
JobType::ProofCreation => JobSpecificMetadata::Proving(ProvingMetadata {
238-
block_number: internal_id,
239-
input_path: None,
240-
ensure_on_chain_registration: None,
241-
download_proof: None,
242-
}),
243-
JobType::StateTransition => JobSpecificMetadata::StateUpdate(StateUpdateMetadata {
244-
blocks_to_settle: vec![internal_id],
245-
snos_output_paths: vec![],
246-
program_output_paths: vec![],
247-
blob_data_paths: vec![],
248-
last_failed_block_no: None,
249-
tx_hashes: vec![],
250-
}),
251-
JobType::ProofRegistration => JobSpecificMetadata::Proving(ProvingMetadata {
252-
block_number: internal_id,
253-
input_path: None,
254-
ensure_on_chain_registration: None,
255-
download_proof: None,
256-
}),
257-
};
258-
259-
JobItem {
260-
id: Uuid::new_v4(),
261-
internal_id: internal_id.to_string(),
262-
job_type,
263-
status: job_status,
264-
external_id: ExternalId::Number(0),
265-
metadata: JobMetadata { common: CommonMetadata::default(), specific: specific_metadata },
266-
version: 0,
267-
created_at: Utc::now().round_subsecs(0),
268-
updated_at: Utc::now().round_subsecs(0),
269-
}
270-
}

0 commit comments

Comments
 (0)