Skip to content

Simplify aggregator integration tests #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.3.37"
version = "0.3.38"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
69 changes: 69 additions & 0 deletions mithril-aggregator/src/database/provider/signed_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ impl<'client> SignedEntityRecordProvider<'client> {
))
}

fn condition_by_certificate_id(
&self,
certificate_id: &str,
) -> Result<WhereCondition, StdError> {
Ok(WhereCondition::new(
"certificate_id = ?*",
vec![Value::String(certificate_id.to_owned())],
))
}

fn condition_by_signed_entity_type(
&self,
signed_entity_type: &SignedEntityTypeDiscriminants,
Expand All @@ -183,6 +193,17 @@ impl<'client> SignedEntityRecordProvider<'client> {
Ok(signed_entity_record)
}

/// Get [record][SignedEntityRecord] for a given `certificate_id`.
pub fn get_by_certificate_id(
&self,
certificate_id: &str,
) -> Result<EntityCursor<SignedEntityRecord>, StdError> {
let filters = self.condition_by_certificate_id(certificate_id)?;
let signed_entity_record = self.find(filters)?;

Ok(signed_entity_record)
}

/// Get SignedEntityRecords for a given signed entity type.
pub fn get_by_signed_entity_type(
&self,
Expand Down Expand Up @@ -290,6 +311,12 @@ pub trait SignedEntityStorer: Sync + Send {
signed_entity_id: &str,
) -> StdResult<Option<SignedEntityRecord>>;

/// Get signed entity type by certificate id
async fn get_signed_entity_by_certificate_id(
&self,
certificate_hash: &str,
) -> StdResult<Option<SignedEntityRecord>>;

/// Get last signed entities by signed entity type
async fn get_last_signed_entities_by_type(
&self,
Expand Down Expand Up @@ -334,6 +361,20 @@ impl SignedEntityStorer for SignedEntityStoreAdapter {
Ok(signed_entity)
}

async fn get_signed_entity_by_certificate_id(
&self,
certificate_id: &str,
) -> StdResult<Option<SignedEntityRecord>> {
let connection = &*self.connection.lock().await;
let provider = SignedEntityRecordProvider::new(connection);
let mut cursor = provider
.get_by_certificate_id(certificate_id)
.map_err(|e| AdapterError::GeneralError(format!("{e}")))?;
let signed_entity = cursor.next();

Ok(signed_entity)
}

async fn get_last_signed_entities_by_type(
&self,
signed_entity_type_id: &SignedEntityTypeDiscriminants,
Expand Down Expand Up @@ -483,6 +524,19 @@ mod tests {
assert_eq!(vec![Value::String("signed-ent-123".to_string())], values);
}

#[test]
fn get_signed_entity_record_by_signed_certificate_id() {
let connection = Connection::open(":memory:").unwrap();
let provider = SignedEntityRecordProvider::new(&connection);
let condition = provider
.condition_by_certificate_id("certificate_id")
.unwrap();
let (filter, values) = condition.expand();

assert_eq!("certificate_id = ?1".to_string(), filter);
assert_eq!(vec![Value::String("certificate_id".to_string())], values);
}

#[test]
fn insert_signed_entity_record() {
let snapshots = fake_data::snapshots(1);
Expand Down Expand Up @@ -551,6 +605,21 @@ mod tests {
assert_eq!(expected_signed_entity_records, signed_entity_records);
}

#[tokio::test]
async fn test_get_signed_entity_record_by_certificate_id() {
let expected_record = fake_signed_entity_records(1).remove(0);
let connection = Connection::open(":memory:").unwrap();
setup_signed_entity_db(&connection, vec![expected_record.clone()]).unwrap();
let store = SignedEntityStoreAdapter::new(Arc::new(Mutex::new(connection)));

let record = store
.get_signed_entity_by_certificate_id(&expected_record.certificate_id)
.await
.expect("querying signed entity record by certificate id should not fail");

assert_eq!(Some(expected_record), record);
}

#[test]
fn test_insert_signed_entity_record() {
let signed_entity_records = fake_signed_entity_records(5);
Expand Down
4 changes: 3 additions & 1 deletion mithril-aggregator/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ pub enum SimulateFromChainParams {

#[doc(hidden)]
impl DependencyManager {
/// `TEST METHOD ONLY`
///
/// Get the first two epochs that will be used by a newly started aggregator
async fn get_genesis_epochs(&self) -> (Epoch, Epoch) {
pub async fn get_genesis_epochs(&self) -> (Epoch, Epoch) {
let current_epoch = self
.chain_observer
.get_current_epoch()
Expand Down
Loading