mithril_aggregator/database/query/signer/
get_signer.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
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

use crate::database::record::SignerRecord;

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

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

#[cfg(test)]
mod test_extensions {
    use mithril_persistence::sqlite::WhereCondition;

    use super::*;

    impl GetSignerRecordQuery {
        /// Query to get SignerRecords for a given signer id.
        pub fn by_signer_id(signer_id: String) -> Self {
            Self {
                condition: WhereCondition::new(
                    "signer_id = ?*",
                    vec![sqlite::Value::String(signer_id)],
                ),
            }
        }
    }
}

impl Query for GetSignerRecordQuery {
    type Entity = SignerRecord;

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

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

#[cfg(test)]
mod tests {
    use crate::database::test_helper::{insert_signers, main_db_connection};
    use mithril_persistence::sqlite::ConnectionExtensions;

    use super::*;

    #[test]
    fn test_get_signer_records_by_id() {
        let signer_records_fake = SignerRecord::fake_records(5);

        let connection = main_db_connection().unwrap();
        insert_signers(&connection, signer_records_fake.clone()).unwrap();

        let signer_records: Vec<SignerRecord> = connection
            .fetch_collect(GetSignerRecordQuery::by_signer_id(
                signer_records_fake[0].signer_id.to_owned(),
            ))
            .unwrap();
        let expected_signer_records: Vec<SignerRecord> = vec![signer_records_fake[0].to_owned()];
        assert_eq!(expected_signer_records, signer_records);

        let signer_records: Vec<SignerRecord> = connection
            .fetch_collect(GetSignerRecordQuery::by_signer_id(
                signer_records_fake[2].signer_id.to_owned(),
            ))
            .unwrap();
        let expected_signer_records: Vec<SignerRecord> = vec![signer_records_fake[2].to_owned()];
        assert_eq!(expected_signer_records, signer_records);

        let cursor = connection
            .fetch(GetSignerRecordQuery::by_signer_id(
                "signer-id-not-registered".to_string(),
            ))
            .unwrap();
        assert_eq!(0, cursor.count());
    }

    #[test]
    fn test_get_all_signer_records() {
        let signer_records_fake = SignerRecord::fake_records(5);

        let connection = main_db_connection().unwrap();
        insert_signers(&connection, signer_records_fake.clone()).unwrap();

        let signer_records: Vec<SignerRecord> = connection
            .fetch_collect(GetSignerRecordQuery::all())
            .unwrap();
        let expected_signer_records: Vec<SignerRecord> =
            signer_records_fake.into_iter().rev().collect();
        assert_eq!(expected_signer_records, signer_records);
    }
}