mithril_aggregator/database/query/buffered_single_signature/
get_buffered_single_signature.rs1use sqlite::Value;
2
3use mithril_common::entities::SignedEntityTypeDiscriminants;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::BufferedSingleSignatureRecord;
7
8pub struct GetBufferedSingleSignatureQuery {
10 condition: WhereCondition,
11}
12
13impl GetBufferedSingleSignatureQuery {
14 #[cfg(test)]
15 pub(crate) fn all() -> Self {
16 Self {
17 condition: WhereCondition::default(),
18 }
19 }
20
21 pub fn by_discriminant(signed_entity_type_discriminant: SignedEntityTypeDiscriminants) -> Self {
22 Self {
23 condition: WhereCondition::new(
24 "signed_entity_type_id = ?*",
25 vec![Value::Integer(
26 signed_entity_type_discriminant.index() as i64
27 )],
28 ),
29 }
30 }
31}
32
33impl Query for GetBufferedSingleSignatureQuery {
34 type Entity = BufferedSingleSignatureRecord;
35
36 fn filters(&self) -> WhereCondition {
37 self.condition.clone()
38 }
39
40 fn get_definition(&self, condition: &str) -> String {
41 let aliases = SourceAlias::new(&[("{:buffered_single_signature:}", "b")]);
42 let projection = Self::Entity::get_projection().expand(aliases);
43 format!("select {projection} from buffered_single_signature as b where {condition} order by ROWID desc")
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use mithril_common::entities::SignedEntityTypeDiscriminants::{
50 CardanoImmutableFilesFull, CardanoTransactions, MithrilStakeDistribution,
51 };
52 use mithril_persistence::sqlite::ConnectionExtensions;
53
54 use crate::database::test_helper::{insert_buffered_single_signatures, main_db_connection};
55
56 use super::*;
57
58 #[test]
59 fn test_get_all() {
60 let connection = main_db_connection().unwrap();
61 let records = BufferedSingleSignatureRecord::fakes(&[
62 ("party1", MithrilStakeDistribution),
63 ("party2", CardanoTransactions),
64 ("party3", MithrilStakeDistribution),
65 ]);
66 insert_buffered_single_signatures(&connection, records.clone()).unwrap();
67
68 let stored_records: Vec<BufferedSingleSignatureRecord> = connection
69 .fetch_collect(GetBufferedSingleSignatureQuery::all())
70 .unwrap();
71
72 assert_eq!(
73 records.into_iter().rev().collect::<Vec<_>>(),
74 stored_records
75 );
76 }
77
78 #[test]
79 fn test_get_buffered_single_signature_records_by_discriminant() {
80 let connection = main_db_connection().unwrap();
81 let msd_records = BufferedSingleSignatureRecord::fakes(&[
82 ("party1", MithrilStakeDistribution),
83 ("party2", MithrilStakeDistribution),
84 ]);
85 let ctx_records = BufferedSingleSignatureRecord::fakes(&[("party3", CardanoTransactions)]);
86 insert_buffered_single_signatures(
87 &connection,
88 [msd_records.clone(), ctx_records.clone()].concat(),
89 )
90 .unwrap();
91
92 let stored_msd_records: Vec<BufferedSingleSignatureRecord> = connection
93 .fetch_collect(GetBufferedSingleSignatureQuery::by_discriminant(
94 MithrilStakeDistribution,
95 ))
96 .unwrap();
97 assert_eq!(
98 msd_records.into_iter().rev().collect::<Vec<_>>(),
99 stored_msd_records
100 );
101
102 let stored_ctx_records: Vec<BufferedSingleSignatureRecord> = connection
103 .fetch_collect(GetBufferedSingleSignatureQuery::by_discriminant(
104 CardanoTransactions,
105 ))
106 .unwrap();
107 assert_eq!(
108 ctx_records.into_iter().rev().collect::<Vec<_>>(),
109 stored_ctx_records
110 );
111
112 let cursor = connection
113 .fetch(GetBufferedSingleSignatureQuery::by_discriminant(
114 CardanoImmutableFilesFull,
115 ))
116 .unwrap();
117 assert_eq!(0, cursor.count());
118 }
119}