mithril_aggregator/database/query/epoch_settings/
delete_epoch_settings.rsuse sqlite::Value;
use mithril_common::entities::Epoch;
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
use crate::database::record::EpochSettingsRecord;
pub struct DeleteEpochSettingsQuery {
condition: WhereCondition,
}
impl Query for DeleteEpochSettingsQuery {
type Entity = EpochSettingsRecord;
fn filters(&self) -> WhereCondition {
self.condition.clone()
}
fn get_definition(&self, condition: &str) -> String {
let projection = Self::Entity::get_projection()
.expand(SourceAlias::new(&[("{:epoch_setting:}", "epoch_setting")]));
format!("delete from epoch_setting where {condition} returning {projection}")
}
}
impl DeleteEpochSettingsQuery {
#[cfg(test)]
pub fn by_epoch(epoch: Epoch) -> Self {
let epoch_settings_id_value = Value::Integer(epoch.try_into().unwrap());
Self {
condition: WhereCondition::new("epoch_setting_id = ?*", vec![epoch_settings_id_value]),
}
}
pub fn below_epoch_threshold(epoch_threshold: Epoch) -> Self {
let epoch_settings_id_value = Value::Integer(epoch_threshold.try_into().unwrap());
Self {
condition: WhereCondition::new("epoch_setting_id < ?*", vec![epoch_settings_id_value]),
}
}
}
#[cfg(test)]
mod tests {
use crate::database::query::GetEpochSettingsQuery;
use crate::database::test_helper::{insert_epoch_settings, main_db_connection};
use mithril_persistence::sqlite::ConnectionExtensions;
use super::*;
#[test]
fn test_delete_by_epoch() {
let connection = main_db_connection().unwrap();
insert_epoch_settings(&connection, &[1, 2]).unwrap();
let cursor = connection
.fetch(DeleteEpochSettingsQuery::by_epoch(Epoch(2)))
.unwrap();
assert_eq!(1, cursor.count());
let cursor = connection
.fetch(GetEpochSettingsQuery::by_epoch(Epoch(1)).unwrap())
.unwrap();
assert_eq!(1, cursor.count());
let cursor = connection
.fetch(GetEpochSettingsQuery::by_epoch(Epoch(2)).unwrap())
.unwrap();
assert_eq!(0, cursor.count());
}
#[test]
fn test_delete_below_threshold() {
let connection = main_db_connection().unwrap();
insert_epoch_settings(&connection, &[1, 2]).unwrap();
let cursor = connection
.fetch(DeleteEpochSettingsQuery::below_epoch_threshold(Epoch(2)))
.unwrap();
assert_eq!(1, cursor.count());
let cursor = connection
.fetch(GetEpochSettingsQuery::by_epoch(Epoch(1)).unwrap())
.unwrap();
assert_eq!(0, cursor.count());
let cursor = connection
.fetch(GetEpochSettingsQuery::by_epoch(Epoch(2)).unwrap())
.unwrap();
assert_eq!(1, cursor.count());
}
}