Skip to content

Commit

Permalink
update: basic test draft #1
Browse files Browse the repository at this point in the history
  • Loading branch information
heemankv committed Jul 25, 2024
1 parent db472d5 commit d02b19b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 69 deletions.
8 changes: 6 additions & 2 deletions crates/orchestrator/src/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod constants;
use std::collections::HashMap;
use std::sync::Arc;

use httpmock::MockServer;
use ::uuid::Uuid;
use constants::*;
use da_client_interface::MockDaClient;
Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn custom_job_item(default_job_item: JobItem, #[default(String::from("0"))]

/// For implementation of integration tests
#[fixture]
pub async fn build_config() -> color_eyre::Result<()> {
pub async fn build_config() -> color_eyre::Result<MockServer> {
dotenv().ok();

// init starknet client
Expand All @@ -107,7 +108,10 @@ pub async fn build_config() -> color_eyre::Result<()> {
Config::new(Arc::new(provider), da_client, prover_client, settlement_client, database, queue, storage_client);
config_force_init(config).await;

Ok(())
// To be returned by build_config
let server = MockServer::start();

Ok(server)
}

pub async fn drop_database() -> color_eyre::Result<()> {
Expand Down
87 changes: 20 additions & 67 deletions crates/orchestrator/src/tests/jobs/da_job/mod.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,26 @@
use std::collections::HashMap;

use crate::config::{config, config_force_init};
use crate::data_storage::MockDataStorage;
use da_client_interface::{DaVerificationStatus, MockDaClient};
use httpmock::prelude::*;
use httpmock::MockServer;
use rstest::*;
use serde_json::json;
use crate::{config::config, jobs::Job};
use color_eyre::Result;
use crate::tests::common::build_config;
use starknet_core::types::{FieldElement, MaybePendingStateUpdate, StateDiff, StateUpdate};
use uuid::Uuid;

use super::super::common::constants::{ETHEREUM_MAX_BLOB_PER_TXN, ETHEREUM_MAX_BYTES_PER_BLOB};
use super::super::common::{default_job_item, init_config};
use crate::jobs::da_job::DaJob;
use uuid::Uuid;
use std::collections::HashMap;
use crate::jobs::types::{ExternalId, JobItem, JobStatus, JobType};
use crate::jobs::Job;

#[rstest]
#[tokio::test]
async fn test_create_job() {
let config = init_config(None, None, None, None, None, None, None).await;
let job = DaJob.create_job(&config, String::from("0"), HashMap::new()).await;
assert!(job.is_ok());

let job = job.unwrap();

let job_type = job.job_type;
assert_eq!(job_type, JobType::DataSubmission, "job_type should be DataSubmission");
assert!(!(job.id.is_nil()), "id should not be nil");
assert_eq!(job.status, JobStatus::Created, "status should be Created");
assert_eq!(job.version, 0_i32, "version should be 0");
assert_eq!(job.external_id.unwrap_string().unwrap(), String::new(), "external_id should be empty string");
}
// TODO : How to know which DA client we have enabled ?
// feature flag the tests ?

#[rstest]
#[tokio::test]
async fn test_verify_job(#[from(default_job_item)] mut job_item: JobItem) {
let mut da_client = MockDaClient::new();
da_client.expect_verify_inclusion().times(1).returning(|_| Ok(DaVerificationStatus::Verified));

let config = init_config(None, None, None, Some(da_client), None, None, None).await;
assert!(DaJob.verify_job(&config, &mut job_item).await.is_ok());
}

#[rstest]
#[tokio::test]
async fn test_process_job() {
let server = MockServer::start();

let mut da_client = MockDaClient::new();
let mut storage_client = MockDataStorage::new();
async fn test_da_job_process_job( #[future(awt)] build_config: Result<MockServer>) -> color_eyre::Result<()> {

let server = build_config.unwrap();

let config = config().await;
let internal_id = "1";

da_client.expect_max_bytes_per_blob().times(2).returning(move || ETHEREUM_MAX_BYTES_PER_BLOB);
da_client.expect_max_blob_per_txn().times(1).returning(move || ETHEREUM_MAX_BLOB_PER_TXN);
da_client.expect_publish_state_diff().times(1).returning(|_, _| Ok("0xbeef".to_string()));

// Mocking storage client
storage_client.expect_put_data().returning(|_, _| Ok(())).times(1);

let config_init = init_config(
Some(format!("http://localhost:{}", server.port())),
None,
None,
Some(da_client),
None,
None,
Some(storage_client),
)
.await;

config_force_init(config_init).await;

let state_update = MaybePendingStateUpdate::Update(StateUpdate {
block_hash: FieldElement::default(),
new_root: FieldElement::default(),
Expand All @@ -88,14 +38,17 @@ async fn test_process_job() {
let response = json!({ "id": 1,"jsonrpc":"2.0","result": state_update });

let state_update_mock = server.mock(|when, then| {
when.path("/").body_contains("starknet_getStateUpdate");
when.path("http://localhost:3000/").body_contains("starknet_getStateUpdate");
then.status(200).body(serde_json::to_vec(&response).unwrap());
});

state_update_mock.assert();


assert_eq!(
DaJob
.process_job(
config().await.as_ref(),
config.as_ref(),
&mut JobItem {
id: Uuid::default(),
internal_id: internal_id.to_string(),
Expand All @@ -111,5 +64,5 @@ async fn test_process_job() {
"0xbeef"
);

state_update_mock.assert();
}
Ok(())
}

0 comments on commit d02b19b

Please sign in to comment.