Skip to content

Commit

Permalink
tests simplifaction though TES and Task objects reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelnikonorov committed Jul 1, 2024
1 parent 4e415a5 commit ac64bb3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 100 deletions.
16 changes: 0 additions & 16 deletions lib/src/serviceinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@ mod tests {
use crate::test_utils::{ensure_funnel_running, setup};
use tokio;

#[tokio::test]
async fn test_get_service_info() {

This comment has been minimized.

Copy link
@pavelnikonorov

pavelnikonorov Jul 1, 2024

Author Collaborator

old code, the same idea as the following test, so I've removed

// let mut mock_transport = MockTransport::new();

// // Set up the mock to return a specific value when `get` is called
// mock_transport.expect_get()
// .with(eq("http://localhost/service-info"), eq(None))
// .returning(|_, _| Ok(String::from("{\"id\": \"test\", \"name\": \"test\"}")));

// let service_info = ServiceInfo::new(mock_transport);
// let result = service_info.get_service_info().await;

// assert!(result.is_ok());
// assert_eq!(result.unwrap().id, "test");
// assert_eq!(result.unwrap().name, "test");
}
#[tokio::test]
async fn test_get_service_info_from_funnel() {
setup();
Expand Down
125 changes: 45 additions & 80 deletions lib/src/tes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,68 +214,54 @@ mod tests {
use crate::tes::TesState;
use crate::tes::TES;
use crate::test_utils::{ensure_funnel_running, setup};
// use crate::test_utils::{ensure_funnel_running, setup, FUNNEL_PORT};
// use crate::tes::models::TesCreateTaskResponse;

async fn create_task() -> Result<String, Box<dyn std::error::Error>> {
setup();
async fn create_task() -> Result<(Task, TES), Box<dyn std::error::Error>> {

This comment has been minimized.

Copy link
@pavelnikonorov

pavelnikonorov Jul 1, 2024

Author Collaborator

why wouldn't we return the whole Task to avoid recreation of the Task instance?
similarly, for TES, we can return it for reuse further

// setup(); – should be run once in the test function
let mut config = Configuration::default();
let funnel_url = ensure_funnel_running().await;
config.set_base_path(&funnel_url);
let tes = TES::new(&config).await;
let tes = match TES::new(&config).await {
Ok(tes) => tes,
Err(e) => {
println!("Error creating TES instance: {:?}", e);
return Err(e.into());
}
};

let task_json =
std::fs::read_to_string("./lib/sample/grape.tes").expect("Unable to read file");
let task: TesTask = serde_json::from_str(&task_json).expect("JSON was not well-formatted");

let task = tes?.create(task).await?;
Ok(task.id)
let task = tes.create(task).await?;
Ok((task, tes))
}

#[tokio::test]
async fn test_task_create() {
setup();
ensure_funnel_running().await;

let task = create_task().await.expect("Failed to create task");
assert!(!task.is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
let (task, _tes) = create_task().await.expect("Failed to create task");
assert!(!task.id.is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
}

#[tokio::test]
async fn test_task_status() {
setup();

let taskid = &create_task().await.expect("Failed to create task");
assert!(!taskid.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
let mut config = Configuration::default();
let funnel_url = ensure_funnel_running().await;
config.set_base_path(&funnel_url);
match TES::new(&config).await {
Ok(tes) => {
let task = Task::new(taskid.clone(), tes.transport);
let status = task.status().await;
println!("Task: {:?}", status);
// Adding an assertion for the Ok variant
match status {
Ok(state) => {
match state {
TesState::Initializing | TesState::Queued | TesState::Running => {
// Assertion passes if state is Initializing or Queued (When ran locally, the response is Initializing or Queued)
// In Github Workflow, the state is Running
}
_ => {
panic!("Unexpected state: {:?}", state);
}
}
}
Err(err) => {
panic!("Task status returned an error: {:?}", err);
}
}
let (task, _tes) = create_task().await.expect("Failed to create task");
assert!(!task.id.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion

let status = task.status().await;
match status {
Ok(state) => {
assert!(
matches!(state, TesState::Initializing | TesState::Queued | TesState::Running),
"Unexpected state: {:?}",
state
);
}
Err(e) => {
// Handle the error e
println!("Error creating TES instance: {:?}", e);
Err(err) => {
panic!("Task status returned an error: {:?}", err);
}
}
}
Expand All @@ -284,52 +270,31 @@ mod tests {
async fn test_cancel_task() {
setup();

let taskid = &create_task().await.expect("Failed to create task");
assert!(!taskid.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
let mut config = Configuration::default();
let funnel_url = ensure_funnel_running().await;
config.set_base_path(&funnel_url);
match TES::new(&config).await {
Ok(tes) => {
let task = Task::new(taskid.clone(), tes.transport);
let cancel = task.cancel().await;
assert!(cancel.is_ok());
}
Err(e) => {
// Handle the error e
println!("Error creating TES instance: {:?}", e);
}
}
let (task, _tes) = &create_task().await.expect("Failed to create task");
assert!(!task.id.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion

let cancel = task.cancel().await;
assert!(cancel.is_ok());
}

#[tokio::test]
async fn test_list_task() {
setup();

let taskid = &create_task().await.expect("Failed to create task");
assert!(!taskid.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
let mut config = Configuration::default();
let funnel_url = ensure_funnel_running().await;
config.set_base_path(&funnel_url);
match TES::new(&config).await {
Ok(tes) => {
let params: ListTasksParams = ListTasksParams {
name_prefix: None,
state: None,
tag_key: None,
tag_value: None,
page_size: None,
page_token: None,
view: Some("BASIC".to_string()),
};
let (task, tes) = &create_task().await.expect("Failed to create task");
assert!(!task.id.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion

let list = tes.list_tasks(Some(params)).await;
println!("{:?}", list);
}
Err(e) => {
// Handle the error e
println!("Error creating TES instance: {:?}", e);
}
}
let params: ListTasksParams = ListTasksParams {
name_prefix: None,
state: None,
tag_key: None,
tag_value: None,
page_size: None,
page_token: None,
view: Some("BASIC".to_string()),
};

let list = tes.list_tasks(Some(params)).await;
println!("{:?}", list);
}
}
2 changes: 0 additions & 2 deletions lib/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub async fn ensure_funnel_running() -> String {

if output_str.is_empty() {
panic!("Funnel is not running.");
} else {
info!("Funnel is already running.");
}

let funnel_url = format!("{}:{}", FUNNEL_HOST, FUNNEL_PORT);
Expand Down
4 changes: 2 additions & 2 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ done
echo "Funnel server is running."

# Run the tests
cargo test
RUST_BACKTRACE=1 RUST_LOG=debug cargo test

# # Stop and remove the Funnel server container
# docker stop funnel
# docker rm funnel
# docker rm funnel

0 comments on commit ac64bb3

Please sign in to comment.