mithril_aggregator/database/query/buffered_single_signature/
get_buffered_single_signature.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
114
115
116
117
118
119
use sqlite::Value;

use mithril_common::entities::SignedEntityTypeDiscriminants;
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

use crate::database::record::BufferedSingleSignatureRecord;

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

impl GetBufferedSingleSignatureQuery {
    #[cfg(test)]
    pub(crate) fn all() -> Self {
        Self {
            condition: WhereCondition::default(),
        }
    }

    pub fn by_discriminant(signed_entity_type_discriminant: SignedEntityTypeDiscriminants) -> Self {
        Self {
            condition: WhereCondition::new(
                "signed_entity_type_id = ?*",
                vec![Value::Integer(
                    signed_entity_type_discriminant.index() as i64
                )],
            ),
        }
    }
}

impl Query for GetBufferedSingleSignatureQuery {
    type Entity = BufferedSingleSignatureRecord;

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

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

#[cfg(test)]
mod tests {
    use mithril_common::entities::SignedEntityTypeDiscriminants::{
        CardanoImmutableFilesFull, CardanoTransactions, MithrilStakeDistribution,
    };
    use mithril_persistence::sqlite::ConnectionExtensions;

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

    use super::*;

    #[test]
    fn test_get_all() {
        let connection = main_db_connection().unwrap();
        let records = BufferedSingleSignatureRecord::fakes(&[
            ("party1", MithrilStakeDistribution),
            ("party2", CardanoTransactions),
            ("party3", MithrilStakeDistribution),
        ]);
        insert_buffered_single_signatures(&connection, records.clone()).unwrap();

        let stored_records: Vec<BufferedSingleSignatureRecord> = connection
            .fetch_collect(GetBufferedSingleSignatureQuery::all())
            .unwrap();

        assert_eq!(
            records.into_iter().rev().collect::<Vec<_>>(),
            stored_records
        );
    }

    #[test]
    fn test_get_buffered_single_signature_records_by_discriminant() {
        let connection = main_db_connection().unwrap();
        let msd_records = BufferedSingleSignatureRecord::fakes(&[
            ("party1", MithrilStakeDistribution),
            ("party2", MithrilStakeDistribution),
        ]);
        let ctx_records = BufferedSingleSignatureRecord::fakes(&[("party3", CardanoTransactions)]);
        insert_buffered_single_signatures(
            &connection,
            [msd_records.clone(), ctx_records.clone()].concat(),
        )
        .unwrap();

        let stored_msd_records: Vec<BufferedSingleSignatureRecord> = connection
            .fetch_collect(GetBufferedSingleSignatureQuery::by_discriminant(
                MithrilStakeDistribution,
            ))
            .unwrap();
        assert_eq!(
            msd_records.into_iter().rev().collect::<Vec<_>>(),
            stored_msd_records
        );

        let stored_ctx_records: Vec<BufferedSingleSignatureRecord> = connection
            .fetch_collect(GetBufferedSingleSignatureQuery::by_discriminant(
                CardanoTransactions,
            ))
            .unwrap();
        assert_eq!(
            ctx_records.into_iter().rev().collect::<Vec<_>>(),
            stored_ctx_records
        );

        let cursor = connection
            .fetch(GetBufferedSingleSignatureQuery::by_discriminant(
                CardanoImmutableFilesFull,
            ))
            .unwrap();
        assert_eq!(0, cursor.count());
    }
}