mithril_aggregator/database/query/certificate/
get_certificate.rs

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
110
111
112
113
use sqlite::Value;

#[cfg(test)]
use mithril_common::entities::Epoch;
#[cfg(test)]
use mithril_common::StdResult;
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

use crate::database::record::CertificateRecord;

/// Simple queries to retrieve [CertificateRecord] from the sqlite database.
pub struct GetCertificateRecordQuery {
    condition: WhereCondition,
}

impl GetCertificateRecordQuery {
    pub fn all() -> Self {
        Self {
            condition: WhereCondition::default(),
        }
    }

    pub fn by_certificate_id(certificate_id: &str) -> Self {
        Self {
            condition: WhereCondition::new(
                "certificate_id = ?*",
                vec![Value::String(certificate_id.to_owned())],
            ),
        }
    }

    #[cfg(test)]
    pub fn by_epoch(epoch: Epoch) -> StdResult<Self> {
        Ok(Self {
            condition: WhereCondition::new("epoch = ?*", vec![Value::Integer(epoch.try_into()?)]),
        })
    }
}

impl Query for GetCertificateRecordQuery {
    type Entity = CertificateRecord;

    fn filters(&self) -> WhereCondition {
        self.condition.clone()
    }

    fn get_definition(&self, condition: &str) -> String {
        let aliases = SourceAlias::new(&[("{:certificate:}", "c")]);
        let projection = Self::Entity::get_projection().expand(aliases);
        format!("select {projection} from certificate as c where {condition} order by ROWID desc")
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::crypto_helper::tests_setup::setup_certificate_chain;
    use mithril_persistence::sqlite::ConnectionExtensions;

    use crate::database::test_helper::{insert_certificate_records, main_db_connection};

    use super::*;

    #[test]
    fn test_get_certificate_records_by_epoch() {
        let (certificates, _) = setup_certificate_chain(20, 7);

        let connection = main_db_connection().unwrap();
        insert_certificate_records(&connection, certificates.clone());

        let certificate_records: Vec<CertificateRecord> = connection
            .fetch_collect(GetCertificateRecordQuery::by_epoch(Epoch(1)).unwrap())
            .unwrap();
        let expected_certificate_records: Vec<CertificateRecord> = certificates
            .iter()
            .filter_map(|c| (c.epoch == Epoch(1)).then_some(c.to_owned().into()))
            .rev()
            .collect();
        assert_eq!(expected_certificate_records, certificate_records);

        let certificate_records: Vec<CertificateRecord> = connection
            .fetch_collect(GetCertificateRecordQuery::by_epoch(Epoch(3)).unwrap())
            .unwrap();
        let expected_certificate_records: Vec<CertificateRecord> = certificates
            .iter()
            .filter_map(|c| (c.epoch == Epoch(3)).then_some(c.to_owned().into()))
            .rev()
            .collect();
        assert_eq!(expected_certificate_records, certificate_records);

        let cursor = connection
            .fetch(GetCertificateRecordQuery::by_epoch(Epoch(5)).unwrap())
            .unwrap();
        assert_eq!(0, cursor.count());
    }

    #[test]
    fn test_get_all_certificate_records() {
        let (certificates, _) = setup_certificate_chain(5, 2);
        let expected_certificate_records: Vec<CertificateRecord> = certificates
            .iter()
            .map(|c| c.to_owned().into())
            .rev()
            .collect();

        let connection = main_db_connection().unwrap();
        insert_certificate_records(&connection, certificates.clone());

        let certificate_records: Vec<CertificateRecord> = connection
            .fetch_collect(GetCertificateRecordQuery::all())
            .unwrap();
        assert_eq!(expected_certificate_records, certificate_records);
    }
}