source tests: reuse fn sample_batch()

This commit is contained in:
Dmitry Belyaev 2023-08-13 12:34:22 +03:00
parent ed37fee697
commit 8e87e5e17b
1 changed files with 11 additions and 53 deletions

View File

@ -218,37 +218,12 @@ pub mod reader_sync {
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::source::SourceQuestion; use super::super::test::sample_batch;
use super::*; use super::*;
use std::fs; use std::fs;
use std::{io::Write, iter, path::Path}; use std::{io::Write, iter, path::Path};
use tempfile::tempdir; 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) fn write_sample_zip<P>(path: P)
where where
P: AsRef<Path>, P: AsRef<Path>,
@ -282,7 +257,7 @@ pub mod reader_sync {
let mut source = zip_file.source_questions(); let mut source = zip_file.source_questions();
assert_eq!(source.len(), 1); assert_eq!(source.len(), 1);
let actual = source.nth(0).expect("get batch"); let actual = source.next().expect("get batch");
assert_eq!(actual.0, "test.json"); assert_eq!(actual.0, "test.json");
assert_eq!(actual.1.expect("parse batch"), expected_batch); assert_eq!(actual.1.expect("parse batch"), expected_batch);
} }
@ -430,6 +405,7 @@ pub mod reader_async {
mod test { mod test {
use crate::source::SourceQuestion; use crate::source::SourceQuestion;
use super::super::test::sample_batch;
use super::*; use super::*;
use async_zip::{base::write::ZipFileWriter, ZipEntryBuilder}; use async_zip::{base::write::ZipFileWriter, ZipEntryBuilder};
use core::fmt::Debug; use core::fmt::Debug;
@ -438,28 +414,6 @@ pub mod reader_async {
use tempfile::tempdir; use tempfile::tempdir;
use tokio::fs; 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) async fn write_sample_zip<P>(path: P)
where where
P: AsRef<Path>, P: AsRef<Path>,
@ -547,9 +501,8 @@ mod test {
use insta::assert_yaml_snapshot; use insta::assert_yaml_snapshot;
use serde_json::json; use serde_json::json;
#[test] pub fn sample_batch() -> SourceQuestionsBatch {
fn test_batch_ser() { SourceQuestionsBatch {
let batch = SourceQuestionsBatch {
description: "Тестовый".into(), description: "Тестовый".into(),
date: "00-000-2000".into(), date: "00-000-2000".into(),
questions: vec![ questions: vec![
@ -567,7 +520,12 @@ mod test {
}, },
], ],
..Default::default() ..Default::default()
}; }
}
#[test]
fn test_batch_ser() {
let batch = sample_batch();
assert_yaml_snapshot!(batch, @r#" assert_yaml_snapshot!(batch, @r#"
--- ---