feat: Initial task runner functionality
This commit is contained in:
parent
713994d848
commit
43bea77392
6 changed files with 244 additions and 35 deletions
|
|
@ -1,14 +1,93 @@
|
|||
use crate::utils::parse::Testcases;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt::format,
|
||||
io::{self, Error, ErrorKind},
|
||||
};
|
||||
|
||||
use crate::utils::parse::{Responses, Testcase, Testcases};
|
||||
use tasks01::poly2block::{self, poly2block};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
mod tasks01;
|
||||
|
||||
pub fn task_distrubute(testcases: Testcases) {
|
||||
for testcase in testcases.testcases {
|
||||
//match testcase {}
|
||||
todo!();
|
||||
pub fn task_deploy(testcase: &Testcase) -> Result<Value, String> {
|
||||
/*
|
||||
* Function to automatially distribute task workloads
|
||||
* TODO: Add functionality to also pass semantics
|
||||
*
|
||||
* */
|
||||
|
||||
let args = &testcase.arguments;
|
||||
|
||||
match testcase.action.as_str() {
|
||||
"poly2block" => {
|
||||
let coefficients: Vec<u8> = args["coefficients"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|x| x.as_u64().unwrap() as u8)
|
||||
.collect();
|
||||
//eprintln!("{:?}", &args["coefficients"]);
|
||||
//eprintln!("{:?}", testcase);
|
||||
//eprintln!("{:?}", coefficients);
|
||||
let result = poly2block(coefficients);
|
||||
let json = json!({"block" : result});
|
||||
Ok(json)
|
||||
}
|
||||
_ => Err(format!(
|
||||
"Fatal error in task distribution. Data was: {:?}",
|
||||
args
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn task_deploy() {
|
||||
todo!();
|
||||
// TODO: Is this obsolete? Might delete later.
|
||||
pub fn task_distrubute(testcases: &Testcases) -> Responses {
|
||||
let mut responses: HashMap<String, Value> = HashMap::new();
|
||||
|
||||
for (id, testcase) in &testcases.testcases {
|
||||
responses.insert(id.to_owned(), task_deploy(testcase).unwrap());
|
||||
}
|
||||
|
||||
Responses {
|
||||
responses: responses,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::utils::parse::parse_json;
|
||||
use std::fs;
|
||||
|
||||
#[test]
|
||||
fn test_task_deploy() {
|
||||
let json = fs::read_to_string("src/test_json/poly2block_example.json").unwrap();
|
||||
let parsed = parse_json(json).unwrap();
|
||||
let testcase = parsed
|
||||
.testcases
|
||||
.get("b856d760-023d-4b00-bad2-15d2b6da22fe")
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
task_deploy(&testcase).is_ok(),
|
||||
"Error: Function result was: {:?}",
|
||||
task_deploy(&testcase)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_task_distribution() {
|
||||
let json = fs::read_to_string("src/test_json/poly2block_example.json").unwrap();
|
||||
let parsed = parse_json(json).unwrap();
|
||||
|
||||
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"block": "ARIAAAAAAAAAAAAAAAAAgA=="}}});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(task_distrubute(&parsed)).unwrap(),
|
||||
serde_json::to_value(expected).unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
mod poly2block;
|
||||
mod block2poly;
|
||||
pub mod block2poly;
|
||||
pub mod poly2block;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,3 +5,4 @@ use serde_json::Value;
|
|||
pub fn poly2block(coefficients: Vec<u8>) -> String {
|
||||
BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes())
|
||||
}
|
||||
|
||||
|
|
|
|||
16
src/test_json/poly2block_example.json
Normal file
16
src/test_json/poly2block_example.json
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"testcases": {
|
||||
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
|
||||
"action": "poly2block",
|
||||
"arguments": {
|
||||
"semantic": "xex",
|
||||
"coefficients": [
|
||||
12,
|
||||
127,
|
||||
9,
|
||||
0
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, io::Result};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Result, Value};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Testcases {
|
||||
|
|
@ -14,11 +14,35 @@ pub struct Testcase {
|
|||
pub arguments: Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Responses {
|
||||
pub responses: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
pub fn parse_json(json: String) -> Result<Testcases> {
|
||||
let deserialised: Testcases = serde_json::from_str(&json)?;
|
||||
Ok(deserialised)
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn generate_response_payload(
|
||||
testcase_id: String,
|
||||
payload: Value,
|
||||
) -> Result<HashMap<String, Value>> {
|
||||
let mut hashmap = HashMap::new();
|
||||
hashmap.insert(testcase_id, payload);
|
||||
Ok(hashmap)
|
||||
}
|
||||
|
||||
pub fn generate_response(payloads: HashMap<String, Value>) -> Result<Value> {
|
||||
let response: Responses = Responses {
|
||||
responses: payloads,
|
||||
};
|
||||
|
||||
Ok(serde_json::to_value(response).unwrap())
|
||||
}
|
||||
*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
|
|
@ -32,17 +56,105 @@ mod tests {
|
|||
fn test_json_parsing() {
|
||||
let json = fs::read_to_string("src/test_json/parse_example.json").unwrap();
|
||||
let parsed = parse_json(json).unwrap();
|
||||
|
||||
/*
|
||||
* Test if struct is deserialised at all
|
||||
* */
|
||||
assert!(
|
||||
!parsed.testcases.is_empty(),
|
||||
"Testcases struct was: {:?}",
|
||||
parsed.testcases
|
||||
);
|
||||
|
||||
/*
|
||||
* Test id the keys are set correctly in the hashmap
|
||||
* */
|
||||
assert!(
|
||||
!parsed
|
||||
.testcases
|
||||
.contains_key("\"b856d760-023d-4b00-bad2-15d2b6da22fe\""),
|
||||
"Testcases first elemient was: {:?}",
|
||||
parsed.testcases
|
||||
);
|
||||
assert!(
|
||||
!parsed
|
||||
.testcases
|
||||
.contains_key("\"254eaee7-05fd-4e0d-8292-9b658a852245\""),
|
||||
"Testcases first element was: {:?}",
|
||||
parsed.testcases
|
||||
);
|
||||
assert!(
|
||||
!parsed
|
||||
.testcases
|
||||
.contains_key("\"affbf4fc-4d2a-41e3-afe0-a79e1d174781\""),
|
||||
"Testcases first element was: {:?}",
|
||||
parsed.testcases
|
||||
);
|
||||
|
||||
/*
|
||||
* Test if the actions are parsed correctly
|
||||
* */
|
||||
let testcase_1 = &parsed
|
||||
.testcases
|
||||
.get("b856d760-023d-4b00-bad2-15d2b6da22fe")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
testcase_1.action, "add_numbers",
|
||||
"Test case was: {:?}",
|
||||
testcase_1.action
|
||||
);
|
||||
}
|
||||
/*
|
||||
#[test]
|
||||
fn test_response_payload_generation() {
|
||||
let testcase_id = "b856d760-023d-4b00-bad2-15d2b6da22fe";
|
||||
let value: Value = json!({"sum" : serde_json::Number::from(666)});
|
||||
let payload = serde_json::to_string(
|
||||
&generate_response_payload(testcase_id.to_owned(), value).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let expected = r#"{"b856d760-023d-4b00-bad2-15d2b6da22fe":{"sum":666}}"#;
|
||||
|
||||
assert_eq!(payload, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_response_generation() {
|
||||
let testcase1_id = "b856d760-023d-4b00-bad2-15d2b6da22fe";
|
||||
let value1: Value = json!({"sum" : serde_json::Number::from(666)});
|
||||
let payload1 = generate_response_payload(testcase1_id.to_owned(), value1).unwrap();
|
||||
let testcase2_id = "b856d760-023d-4b00-bad2-15d2b6da22fe";
|
||||
let value2: Value = json!({"sum" : serde_json::Number::from(666)});
|
||||
let payload2 = generate_response_payload(testcase2_id.to_owned(), value2).unwrap();
|
||||
let testcase3_id = "b856d760-023d-4b00-bad2-15d2b6da22fe";
|
||||
let value3: Value = json!({"sum" : serde_json::Number::from(666)});
|
||||
let payload3 = generate_response_payload(testcase3_id.to_owned(), value3).unwrap();
|
||||
|
||||
let mut responses_vec: HashMap<String, Value> = vec![];
|
||||
|
||||
responses_vec.insert(payload1);
|
||||
responses_vec.push(payload2);
|
||||
responses_vec.push(payload3);
|
||||
let response = generate_response(responses_vec).unwrap();
|
||||
|
||||
let expected = json!(
|
||||
{
|
||||
"responses": {
|
||||
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
|
||||
"sum": 357
|
||||
},
|
||||
"254eaee7-05fd-4e0d-8292-9b658a852245": {
|
||||
"sum": 777
|
||||
},
|
||||
"affbf4fc-4d2a-41e3-afe0-a79e1d174781": {
|
||||
"difference": -120213
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
eprintln!("{}", serde_json::to_string(&response).unwrap());
|
||||
eprintln!("{}", serde_json::to_string(&expected).unwrap());
|
||||
assert_eq!(response, expected);
|
||||
}*/
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue