From b177fbaa5a3a091e66a716790e61f63fa33deac2 Mon Sep 17 00:00:00 2001 From: Morgan Mccauley Date: Mon, 5 Aug 2024 10:30:46 +1200 Subject: [PATCH] test: Test indexer fetching from registry --- coordinator/src/registry.rs | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/coordinator/src/registry.rs b/coordinator/src/registry.rs index ae15dac0..9f0824d3 100644 --- a/coordinator/src/registry.rs +++ b/coordinator/src/registry.rs @@ -262,3 +262,81 @@ impl RegistryImpl { anyhow::bail!("Invalid registry response") } } + +#[cfg(test)] +mod tests { + use super::*; + + use mockall::predicate::*; + + #[tokio::test] + async fn parses_non_existant_config() { + let mut mock_json_rpc_client = JsonRpcClientWrapper::default(); + mock_json_rpc_client + .expect_call::() + .with(always()) + .returning(|_| { + Ok(near_jsonrpc_client::methods::query::RpcQueryResponse { + kind: QueryResponseKind::CallResult(near_primitives::views::CallResult { + result: "null".as_bytes().to_vec(), + logs: vec![], + }), + block_height: Default::default(), + block_hash: Default::default(), + }) + }); + + let registry = RegistryImpl::new("registry".parse().unwrap(), mock_json_rpc_client); + + let config = registry + .fetch_indexer(&"account".parse().unwrap(), "function") + .await + .unwrap(); + + assert!(config.is_none()); + } + + #[tokio::test] + async fn parses_existing_config() { + let mut mock_json_rpc_client = JsonRpcClientWrapper::default(); + mock_json_rpc_client + .expect_call::() + .with(always()) + .returning(|_| { + Ok(near_jsonrpc_client::methods::query::RpcQueryResponse { + kind: QueryResponseKind::CallResult(near_primitives::views::CallResult { + result: serde_json::json!({ + "code": "code", + "schema": "schema", + "rule": { + "affected_account_id": "queryapi.dataplatform.near", + "kind": "ACTION_ANY", + "status": "SUCCESS" + }, + "start_block": { + "HEIGHT": 0, + }, + "updated_at_block_height": 0, + "created_at_block_height": 0, + "deleted_at_block_height": 0 + }) + .to_string() + .as_bytes() + .to_vec(), + logs: vec![], + }), + block_height: Default::default(), + block_hash: Default::default(), + }) + }); + + let registry = RegistryImpl::new("registry".parse().unwrap(), mock_json_rpc_client); + + let config = registry + .fetch_indexer(&"account".parse().unwrap(), "function") + .await + .unwrap(); + + assert!(config.is_some()); + } +}