url_cleaner_engine/
testing.rs

1//! A basic and not very good testing framework.
2
3use serde::{Serialize, Deserialize};
4
5use crate::types::*;
6use crate::glue::*;
7use crate::util::*;
8
9/// Tests.
10#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
11pub struct Tests {
12    /// The individual [`TestSet`]s.
13    pub sets: Vec<TestSet>
14}
15
16impl Tests {
17    /// Do the tests. Panicking if any fail.
18    /// # Panics
19    /// If any call to [`TestSet::do`] panics, "returns" that panic.
20    pub fn r#do(self, cleaner: &Cleaner) {
21        for set in self.sets {
22            set.r#do(cleaner)
23        }
24    }
25}
26
27/// Rules for how to construct a [`Job`] from a [`Cleaner`] and the [`Test`]s to run on it.
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29pub struct TestSet {
30    /// The [`ParamsDiff`] to apply to the [`Cleaner`].
31    #[serde(default, skip_serializing_if = "is_default")]
32    pub params_diff: Option<ParamsDiff>,
33    /// The [`JobContext`] to give to the [`Job`].
34    #[serde(default, skip_serializing_if = "is_default")]
35    pub job_context: JobContext,
36    /// The [`Test`]s to run.
37    pub tests: Vec<Test>
38}
39
40impl TestSet {
41    /// Do the tests, panicking if any fail.
42    /// # Panics
43    /// If a call to [`Job::next`] returns an error, panics.
44    ///
45    /// If a call to [`Task::do`] returns an error, panics.
46    ///
47    /// If any test fails, panics.
48    pub fn r#do(self, cleaner: &Cleaner) {
49        let mut cleaner = cleaner.borrowed();
50
51        println!(
52            "TestSet\n  params_diff: {}\n  job_context: {}",
53            serde_json::to_string(&self.params_diff).expect("Serialization to never fail."),
54            serde_json::to_string(&self.job_context).expect("Serialization to never fail.")
55        );
56
57        if let Some(params_diff) = self.params_diff {
58            params_diff.apply_once(&mut cleaner.params);
59        }
60
61        for test in self.tests {
62            println!("    Test: {}", serde_json::to_string(&test).expect("Serialition to never fail."));
63            let task = Task {
64                config: test.task_config,
65                job_context: &self.job_context,
66                cleaner: &cleaner,
67                #[cfg(feature = "cache")]
68                cache: CacheHandle {
69                    cache: &Default::default(),
70                    config: Default::default()
71                },
72                unthreader: &Default::default()
73            };
74            let result1 = task.r#do().expect("The test to execute succesfully.");
75            assert_eq!(result1, test.result, "The test to return the expected value.");
76            if test.test_idempotence {
77                let task = Task {
78                    config: result1.clone().into(),
79                    job_context: &self.job_context,
80                    cleaner: &cleaner,
81
82                #[cfg(feature = "cache")]
83                    cache: CacheHandle {
84                        cache: &Default::default(),
85                        config: Default::default()
86                    },
87                    unthreader: &Default::default()
88                };
89                let result2 = task.r#do().expect("The idempotence test to be succeed.");
90                assert_eq!(result2, result1, "Idempotence to be upheld.");
91            }
92        }
93    }
94}
95
96/// An individual test.
97#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
98pub struct Test {
99    /// The [`TaskConfig`].
100    pub task_config: TaskConfig,
101    /// The expected result.
102    pub result: BetterUrl,
103    /// If [`true`], test idempotence.
104    ///
105    /// Defaults to [`true`].
106    #[serde(default = "get_true", skip_serializing_if = "is_true")]
107    pub test_idempotence: bool
108}