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
//! A bin that verifies whether the input rows match expected values.
//!
//! ```text
//!   ┌──[verificationsink]──┐
//!  ⇒│<input 0>             │
//!  ⇒│<input 1>             │
//!  ⇒│<input 2>             │
//!  ⇒┊…                     ┊
//!  ⇒│<input n>             │
//!   └──────────────────────┘
//! ```

use super::{
    BinBuildEnvironment, BinDescription, Calculator, FetchItem,
    GetCalibration, Item, Iteration, Result, Scope, SinkBin, SinkNames,
    SinkOnlyBin, SinkOnlyBinDescription,
};
use indexmap::{IndexMap, IndexSet};
use std::collections::VecDeque;

static BIN_TYPE: &str = "verificationsink";

/// A bin that verifies whether the input rows match expected values.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,

    columns: IndexSet<String>,
    expected: VecDeque<Vec<Item>>,

    sources: IndexMap<String, Box<dyn FetchItem>>,
}

impl SinkBin for Bin {}
impl SinkOnlyBin for Bin {}

impl Calculator for Bin {
    fn calculate(&mut self, iteration: &Iteration) -> Result<()> {
        let expected = self.expected.pop_front().unwrap();

        let expected = self
            .columns
            .iter()
            .map(String::to_string)
            .zip(expected.into_iter())
            .collect::<IndexMap<_, _>>();

        let received = self
            .sources
            .iter()
            .map(|(s, ds)| {
                ds.fetch_item(&self.scope).map(|d| (s.to_string(), d))
            })
            .collect::<Result<IndexMap<String, Item>>>()?;

        assert_eq!(received, expected, "iteration: {:?}", iteration);
        Ok(())
    }
}

/// Description for the verification bin.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Description {
    /// The columns of the verification data.
    pub columns: IndexSet<String>,
    /// The expected data rows.
    pub expected: VecDeque<Vec<Item>>,
}

impl BinDescription for Description {
    type Bin = Bin;

    fn check_validity(
        &self,
        _scope: &Scope,
        _get_calibration: &mut dyn GetCalibration,
    ) -> Result<()> {
        Ok(())
    }

    fn bin_type(&self) -> &'static str {
        BIN_TYPE
    }
}

impl SinkNames for Description {
    fn sink_names(&self) -> IndexSet<String> {
        self.columns.iter().map(String::to_string).collect()
    }
}

impl SinkOnlyBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut dyn BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        Ok(Bin {
            scope: scope.clone(),
            columns: self.columns.clone(),
            expected: self.expected.clone(),
            sources: self
                .columns
                .iter()
                .map(|s| env.resolve(s).map(|ds| (s.to_string(), ds)))
                .collect::<Result<IndexMap<_, _>>>()?,
        })
    }
}