add source::reader_sync get/iter tests
This commit is contained in:
parent
7e633577b1
commit
08acb4e71b
@ -239,6 +239,99 @@ pub mod reader_sync {
|
|||||||
SourceQuestionsZipReader::new(self)
|
SourceQuestionsZipReader::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::source::SourceQuestion;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use std::fs;
|
||||||
|
use std::{io::Write, iter, path::Path};
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
|
use super::SourceQuestionsBatch;
|
||||||
|
|
||||||
|
fn sample_batch() -> SourceQuestionsBatch {
|
||||||
|
SourceQuestionsBatch {
|
||||||
|
description: "Тестовый".into(),
|
||||||
|
date: "00-000-2000".into(),
|
||||||
|
questions: vec![
|
||||||
|
SourceQuestion {
|
||||||
|
id: "Вопрос 1".into(),
|
||||||
|
description: "Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2".into(),
|
||||||
|
answer: "42".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
SourceQuestion {
|
||||||
|
id: "Вопрос 2".into(),
|
||||||
|
description: "Зимой и летом одним цветом".into(),
|
||||||
|
answer: "ёлка".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_sample_zip<P>(path: P)
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let batch = sample_batch();
|
||||||
|
let z_file = fs::File::create(path).expect("crerate zip file");
|
||||||
|
let mut zip_file = zip::ZipWriter::new(z_file);
|
||||||
|
let options =
|
||||||
|
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Zstd);
|
||||||
|
zip_file
|
||||||
|
.start_file("test.json", options)
|
||||||
|
.expect("zip start file");
|
||||||
|
zip_file
|
||||||
|
.write(serde_json::to_vec(&batch).unwrap().as_slice())
|
||||||
|
.expect("write entry");
|
||||||
|
zip_file.finish().expect("finish zip file");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_source_questions_get() {
|
||||||
|
let expected_batch = sample_batch();
|
||||||
|
let dir = tempdir().expect("tempdir");
|
||||||
|
|
||||||
|
// write sample
|
||||||
|
let tmpfile_zip = dir.path().join("test.zip");
|
||||||
|
write_sample_zip(&tmpfile_zip);
|
||||||
|
|
||||||
|
let z_file = fs::File::open(tmpfile_zip).expect("open zip file");
|
||||||
|
let zip_file = zip::ZipArchive::new(z_file).expect("open zip file reader");
|
||||||
|
|
||||||
|
let mut source = zip_file.source_questions();
|
||||||
|
assert_eq!(source.len(), 1);
|
||||||
|
|
||||||
|
let actual = source.nth(0).expect("get batch");
|
||||||
|
assert_eq!(actual.0, "test.json");
|
||||||
|
assert_eq!(actual.1.expect("parse batch"), expected_batch);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_source_questions_iter() {
|
||||||
|
let expected_batch = sample_batch();
|
||||||
|
let dir = tempdir().expect("tempdir");
|
||||||
|
|
||||||
|
// write sample
|
||||||
|
let tmpfile_zip = dir.path().join("test.zip");
|
||||||
|
write_sample_zip(&tmpfile_zip);
|
||||||
|
|
||||||
|
let z_file = fs::File::open(tmpfile_zip).expect("open zip file");
|
||||||
|
let zip_file = zip::ZipArchive::new(z_file).expect("open zip file reader");
|
||||||
|
|
||||||
|
let source = zip_file.source_questions();
|
||||||
|
assert_eq!(source.len(), 1);
|
||||||
|
|
||||||
|
let expected_iter = iter::once((String::from("test.json"), Ok(expected_batch)));
|
||||||
|
|
||||||
|
assert!(source
|
||||||
|
.map(|x| (x.0, x.1.map_err(|e| e.to_string())))
|
||||||
|
.eq(expected_iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "convert", feature = "source"))]
|
#[cfg(any(feature = "convert", feature = "source"))]
|
||||||
|
Loading…
Reference in New Issue
Block a user