File size: 8,309 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
//! Read and write the `archathon_solution_json` file.
use super::arc_json_model;
use serde::{Deserialize, Serialize};
use std::{path::Path, fs};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Prediction {
pub prediction_id: u8,
pub output: arc_json_model::Grid,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestItem {
pub output_id: u8,
pub number_of_predictions: u8,
pub predictions: Vec<Prediction>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskItem {
pub task_name: String,
#[serde(rename = "test")]
pub test_vec: Vec<TestItem>,
}
/// Wrapper for the `archathon_solution_json` file.
#[derive(Clone, Debug)]
pub struct ArcathonSolutionJsonFile {
pub task_vec: Vec<TaskItem>,
}
impl ArcathonSolutionJsonFile {
/// Load the `archaton_solution_json` file.
pub fn load(path_solution_teamid_json: &Path) -> anyhow::Result<Self> {
let solution_teamid_json_string: String = match fs::read_to_string(path_solution_teamid_json) {
Ok(value) => value,
Err(error) => {
return Err(anyhow::anyhow!("Something went wrong reading the file: {:?} error: {:?}", path_solution_teamid_json, error));
}
};
let tasks: Vec<TaskItem> = match serde_json::from_str(&solution_teamid_json_string) {
Ok(value) => value,
Err(error) => {
return Err(anyhow::anyhow!("Could not parse archaton_solution_json file, path: {:?} error: {:?} json: {:?}", path_solution_teamid_json, error, solution_teamid_json_string));
}
};
let instance = Self {
task_vec: tasks,
};
Ok(instance)
}
pub fn empty() -> Self {
Self {
task_vec: vec!(),
}
}
/// Save the `archaton_solution_json` file.
///
/// Returns the file size in bytes.
pub fn save(&self, path_solution_dir: &Path, path_solution_teamid_json: &Path) -> anyhow::Result<usize> {
if !path_solution_dir.exists() {
match fs::create_dir(path_solution_dir) {
Ok(_) => {},
Err(error) => {
return Err(anyhow::anyhow!("Unable to create solution directory: {:?}, error: {:?}", path_solution_dir, error));
}
}
}
let json: String = match serde_json::to_string(&self.task_vec) {
Ok(value) => value,
Err(error) => {
return Err(anyhow::anyhow!("Unable to serialize task_vec to json: {:?}", error));
}
};
let bytes: usize = json.len();
match fs::write(&path_solution_teamid_json, json) {
Ok(()) => {},
Err(error) => {
return Err(anyhow::anyhow!("Unable to save solutions file. path: {:?} error: {:?}", path_solution_teamid_json, error));
}
}
Ok(bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::arc::path_testdata;
use std::path::PathBuf;
fn mock_prediction() -> Prediction {
Prediction {
prediction_id: 8,
output: vec![vec![1, 2], vec![3, 4]]
}
}
#[test]
fn test_10000_prediction_to_json() {
let instance = mock_prediction();
let json: String = serde_json::to_string(&instance).expect("string");
assert_eq!(json, "{\"prediction_id\":8,\"output\":[[1,2],[3,4]]}");
}
fn mock_testitem() -> TestItem {
let mut predictions = Vec::<Prediction>::new();
predictions.push(mock_prediction());
predictions.push(mock_prediction());
TestItem {
output_id: 23,
number_of_predictions: 2,
predictions,
}
}
#[test]
fn test_10001_testitem_to_json() {
let instance = mock_testitem();
let json: String = serde_json::to_string(&instance).expect("string");
assert_eq!(json, "{\"output_id\":23,\"number_of_predictions\":2,\"predictions\":[{\"prediction_id\":8,\"output\":[[1,2],[3,4]]},{\"prediction_id\":8,\"output\":[[1,2],[3,4]]}]}");
}
fn mock_taskitem() -> TaskItem {
let mut test_vec = Vec::<TestItem>::new();
test_vec.push(mock_testitem());
test_vec.push(mock_testitem());
TaskItem {
task_name: "mock_taskitem".to_string(),
test_vec
}
}
#[test]
fn test_10002_taskitem_to_json() {
let instance = mock_taskitem();
let json: String = serde_json::to_string(&instance).expect("string");
assert_eq!(json.starts_with("{"), true);
assert_eq!(json.ends_with("}"), true);
assert_eq!(json.contains("mock_taskitem"), true);
assert_eq!(json.contains("number_of_predictions"), true);
assert_eq!(json.contains("prediction_id"), true);
assert_eq!(json.contains("[[1,2],[3,4]]"), true);
}
fn mock_task_vec() -> Vec<TaskItem> {
let mut task_vec = Vec::<TaskItem>::new();
task_vec.push(mock_taskitem());
task_vec.push(mock_taskitem());
task_vec
}
#[test]
fn test_20000_save_solutions_json() -> anyhow::Result<()> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_20000_save_solutions_json");
fs::create_dir(&basedir)?;
let path_solutions_json: PathBuf = basedir.join("solutions.json");
let task_vec = mock_task_vec();
let tasks = ArcathonSolutionJsonFile {
task_vec
};
// Act
let returned_bytes: usize = tasks.save(&basedir, &path_solutions_json)?;
// Assert
let filesize: u64 = path_solutions_json.metadata()?.len();
assert_eq!(returned_bytes as u64, filesize);
assert_eq!(returned_bytes, 659);
let json: String = fs::read_to_string(&path_solutions_json)?;
assert_eq!(json.starts_with("[{"), true);
assert_eq!(json.ends_with("}]"), true);
assert_eq!(json.contains("mock_taskitem"), true);
assert_eq!(json.contains("number_of_predictions"), true);
assert_eq!(json.contains("prediction_id"), true);
assert_eq!(json.contains("[[1,2],[3,4]]"), true);
Ok(())
}
#[test]
fn test_30000_read_solutions_json() {
// Arrange
let path: PathBuf = path_testdata("arcathon_solution_format").expect("ok");
// Act
let tasks_instance: ArcathonSolutionJsonFile = ArcathonSolutionJsonFile::load(&path).expect("ok");
// Assert
let tasks: Vec<TaskItem> = tasks_instance.task_vec.clone();
assert_eq!(tasks.len(), 2);
assert_eq!(tasks[0].task_name, "12997ef3");
assert_eq!(tasks[1].task_name, "13713586");
assert_eq!(tasks[0].test_vec.len(), 2);
assert_eq!(tasks[1].test_vec.len(), 1);
assert_eq!(tasks[0].test_vec[0].number_of_predictions, 3);
assert_eq!(tasks[0].test_vec[0].predictions.len(), 3);
assert_eq!(tasks[0].test_vec[1].number_of_predictions, 3);
assert_eq!(tasks[0].test_vec[1].predictions.len(), 3);
assert_eq!(tasks[1].test_vec[0].number_of_predictions, 3);
assert_eq!(tasks[1].test_vec[0].predictions.len(), 3);
}
#[test]
fn test_30001_read_solutions_json() {
// Arrange
let path: PathBuf = path_testdata("solution_notXORdinary").expect("ok");
// Act
let tasks_instance: ArcathonSolutionJsonFile = ArcathonSolutionJsonFile::load(&path).expect("ok");
// Assert
let tasks: Vec<TaskItem> = tasks_instance.task_vec.clone();
assert_eq!(tasks.len(), 2);
assert_eq!(tasks[0].task_name, "3428a4f5");
assert_eq!(tasks[1].task_name, "f2829549");
assert_eq!(tasks[0].test_vec.len(), 2);
assert_eq!(tasks[1].test_vec.len(), 1);
assert_eq!(tasks[0].test_vec[0].number_of_predictions, 1);
assert_eq!(tasks[0].test_vec[0].predictions.len(), 1);
assert_eq!(tasks[0].test_vec[1].number_of_predictions, 1);
assert_eq!(tasks[0].test_vec[1].predictions.len(), 1);
assert_eq!(tasks[1].test_vec[0].number_of_predictions, 1);
assert_eq!(tasks[1].test_vec[0].predictions.len(), 1);
}
}
|