add source::reader_async get/stream tests
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
08acb4e71b
commit
ef98b1fea4
@ -448,6 +448,118 @@ pub mod reader_async {
|
||||
SourceQuestionsZipReaderAsync::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::source::SourceQuestion;
|
||||
|
||||
use super::*;
|
||||
use async_zip::{base::write::ZipFileWriter, ZipEntryBuilder};
|
||||
use core::fmt::Debug;
|
||||
use futures_util::StreamExt;
|
||||
use std::path::Path;
|
||||
use tempfile::tempdir;
|
||||
use tokio::fs;
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
async fn write_sample_zip<P>(path: P)
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let batch = sample_batch();
|
||||
let z_file = fs::File::create(path).await.expect("crerate zip file");
|
||||
let mut zip_file = ZipFileWriter::with_tokio(z_file);
|
||||
let entry =
|
||||
ZipEntryBuilder::new("test.json".into(), async_zip::Compression::Zstd).build();
|
||||
zip_file
|
||||
.write_entry_whole(entry, serde_json::to_vec(&batch).unwrap().as_slice())
|
||||
.await
|
||||
.expect("write entry");
|
||||
zip_file.close().await.expect("close zip");
|
||||
}
|
||||
|
||||
async fn assert_data_rref_eq<T>((x, y): (T, &T))
|
||||
where
|
||||
T: PartialEq + Debug,
|
||||
{
|
||||
assert_eq!(x, *y);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_source_questions_stream() {
|
||||
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).await;
|
||||
|
||||
let mut z_file = fs::File::open(tmpfile_zip).await.expect("open zip file");
|
||||
let zip_file = ZipFileReader::with_tokio(&mut z_file)
|
||||
.await
|
||||
.expect("open zip file reader");
|
||||
|
||||
let expected_count = expected_batch.questions.len();
|
||||
let expected_stream = futures::stream::iter(expected_batch.questions.iter());
|
||||
let mut actual_source = zip_file.source_questions();
|
||||
let actual_stream = actual_source.stream();
|
||||
let mut actual_count: usize = 0;
|
||||
|
||||
actual_stream
|
||||
.flat_map(|x| futures::stream::iter(x.1.expect("parse batch").questions))
|
||||
.zip(expected_stream)
|
||||
.map(|x| {
|
||||
actual_count += 1;
|
||||
x
|
||||
})
|
||||
.for_each(assert_data_rref_eq::<SourceQuestion>)
|
||||
.await;
|
||||
assert_eq!(actual_count, expected_count);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async 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).await;
|
||||
|
||||
let mut z_file = fs::File::open(tmpfile_zip).await.expect("open zip file");
|
||||
let zip_file = ZipFileReader::with_tokio(&mut z_file)
|
||||
.await
|
||||
.expect("open zip file reader");
|
||||
|
||||
let mut source = zip_file.source_questions();
|
||||
assert_eq!(source.len(), 1);
|
||||
|
||||
let actual = source.get(0).await.expect("get batch");
|
||||
assert_eq!(actual.0, "test.json");
|
||||
assert_eq!(actual.1.expect("parse batch"), expected_batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(any(feature = "convert_async", feature = "source_async"))]
|
||||
pub use reader_async::{ReadSourceQuestionsBatchesAsync, SourceQuestionsZipReaderAsync};
|
||||
|
Loading…
Reference in New Issue
Block a user