mithril_aggregator/database/query/signer/
get_signer.rs1use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
2
3use crate::database::record::SignerRecord;
4
5pub struct GetSignerRecordQuery {
7 condition: WhereCondition,
8}
9
10impl GetSignerRecordQuery {
11 pub fn all() -> Self {
12 Self {
13 condition: WhereCondition::default(),
14 }
15 }
16}
17
18#[cfg(test)]
19mod test_extensions {
20 use mithril_persistence::sqlite::WhereCondition;
21
22 use super::*;
23
24 impl GetSignerRecordQuery {
25 pub fn by_signer_id(signer_id: String) -> Self {
27 Self {
28 condition: WhereCondition::new(
29 "signer_id = ?*",
30 vec![sqlite::Value::String(signer_id)],
31 ),
32 }
33 }
34 }
35}
36
37impl Query for GetSignerRecordQuery {
38 type Entity = SignerRecord;
39
40 fn filters(&self) -> WhereCondition {
41 self.condition.clone()
42 }
43
44 fn get_definition(&self, condition: &str) -> String {
45 let aliases = SourceAlias::new(&[("{:signer:}", "s")]);
46 let projection = Self::Entity::get_projection().expand(aliases);
47 format!("select {projection} from signer as s where {condition} order by ROWID desc")
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use crate::database::test_helper::{insert_signers, main_db_connection};
54 use mithril_persistence::sqlite::ConnectionExtensions;
55
56 use super::*;
57
58 #[test]
59 fn test_get_signer_records_by_id() {
60 let signer_records_fake = SignerRecord::fake_records(5);
61
62 let connection = main_db_connection().unwrap();
63 insert_signers(&connection, signer_records_fake.clone()).unwrap();
64
65 let signer_records: Vec<SignerRecord> = connection
66 .fetch_collect(GetSignerRecordQuery::by_signer_id(
67 signer_records_fake[0].signer_id.to_owned(),
68 ))
69 .unwrap();
70 let expected_signer_records: Vec<SignerRecord> = vec![signer_records_fake[0].to_owned()];
71 assert_eq!(expected_signer_records, signer_records);
72
73 let signer_records: Vec<SignerRecord> = connection
74 .fetch_collect(GetSignerRecordQuery::by_signer_id(
75 signer_records_fake[2].signer_id.to_owned(),
76 ))
77 .unwrap();
78 let expected_signer_records: Vec<SignerRecord> = vec![signer_records_fake[2].to_owned()];
79 assert_eq!(expected_signer_records, signer_records);
80
81 let cursor = connection
82 .fetch(GetSignerRecordQuery::by_signer_id(
83 "signer-id-not-registered".to_string(),
84 ))
85 .unwrap();
86 assert_eq!(0, cursor.count());
87 }
88
89 #[test]
90 fn test_get_all_signer_records() {
91 let signer_records_fake = SignerRecord::fake_records(5);
92
93 let connection = main_db_connection().unwrap();
94 insert_signers(&connection, signer_records_fake.clone()).unwrap();
95
96 let signer_records: Vec<SignerRecord> = connection
97 .fetch_collect(GetSignerRecordQuery::all())
98 .unwrap();
99 let expected_signer_records: Vec<SignerRecord> =
100 signer_records_fake.into_iter().rev().collect();
101 assert_eq!(expected_signer_records, signer_records);
102 }
103}