diff --git a/README.md b/README.md index 79f1ded..89cefad 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ let chain_id: OptionalResult = mesc::get_default_endpoint("xyz_tool"); let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, "xyz_tool"); // get an endpoint by name -let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_query"); +let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_goerli"); // parse a user-provided string into a matching endpoint // (first try 1. endpoint name, then 2. chain id, then 3. network name) diff --git a/python/mesc/interface.py b/python/mesc/interface.py index 6e63cfe..9602135 100644 --- a/python/mesc/interface.py +++ b/python/mesc/interface.py @@ -68,7 +68,7 @@ def get_endpoint_by_network( # get global default for network if chain_id is None: - raise ValueError('chain_id must be a str') + raise ValueError('chain_id must be a str or int') chain_id = str(chain_id) network_defaults = config['network_defaults'] default_name = network_defaults.get(chain_id) @@ -162,6 +162,19 @@ def find_endpoints( return endpoints -def get_global_metadata() -> Mapping[str, Any]: +def get_global_metadata( + *, profile: str | None = None, config: RpcConfig | None = None +) -> Mapping[str, Any]: """return MESC global metadata""" - return load.read_config_data()['global_metadata'] + + if config is None: + config = load.read_config_data() + + if profile is not None: + profile_data = config['profiles'].get(profile) + if profile_data is not None: + if not profile_data['use_mesc']: + return {} + return dict(config['global_metadata'], **profile_data['profile_metadata']) + + return config['global_metadata'] diff --git a/python/mesc/types.py b/python/mesc/types.py index 380822b..c936aeb 100644 --- a/python/mesc/types.py +++ b/python/mesc/types.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing_extensions import Any, MutableMapping, TypedDict, Literal, Sequence +from typing_extensions import Any, MutableMapping, TypedDict, Literal mesc_env_vars = [ @@ -94,7 +94,7 @@ class EndpointNameQuery(TypedDict): class EndpointNetworkQuery(TypedDict): profile: str | None - chain_id: str + chain_id: str | int class UserInputQuery(TypedDict): @@ -105,11 +105,11 @@ class UserInputQuery(TypedDict): class MultiEndpointQuery(TypedDict, total=False): name_contains: str | None url_contains: str | None - chain_id: str | None + chain_id: str | int | None class GlobalMetadataQuery(TypedDict, total=False): - path: Sequence[str] | None + profile: str | None class MescQuery(TypedDict): diff --git a/rust/README.md b/rust/README.md index 609f9fe..f34211f 100644 --- a/rust/README.md +++ b/rust/README.md @@ -72,17 +72,17 @@ let endpoint: OptionalResult = mesc::get_default_endpoint(None); let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, None); // get the default network for a particular tool -let chain_id: OptionalResult = mesc::get_default_endpoint("xyz_tool"); +let chain_id: OptionalResult = mesc::get_default_endpoint(Some("xyz_tool")); // get the default endpoint of a network for a particular tool -let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, "xyz_tool"); +let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, Some("xyz_tool")); // get an endpoint by name -let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_query"); +let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_goerli"); // parse a user-provided string into a matching endpoint // (first try 1. endpoint name, then 2. chain id, then 3. network name) -let endpoint: OptionalResult = mesc::get_endpoint_by_query(user_str, "xyz_tool"); +let endpoint: OptionalResult = mesc::get_endpoint_by_query(user_str, Some("xyz_tool")); // find all endpoints matching given criteria let query = mesc::MultiEndpointQuery::new().chain_id(5); diff --git a/rust/crates/mesc/src/interface.rs b/rust/crates/mesc/src/interface.rs index ae76f0f..2de8e3d 100644 --- a/rust/crates/mesc/src/interface.rs +++ b/rust/crates/mesc/src/interface.rs @@ -44,6 +44,8 @@ pub fn find_endpoints(query: MultiEndpointQuery) -> Result, MescEr } /// get global metadata -pub fn get_global_metadata() -> Result, MescError> { - query::get_global_metadata(&load_config_data()?) +pub fn get_global_metadata( + profile: Option<&str>, +) -> Result, MescError> { + query::get_global_metadata(&load_config_data()?, profile) } diff --git a/rust/crates/mesc/src/query.rs b/rust/crates/mesc/src/query.rs index f6b8af9..1e48523 100644 --- a/rust/crates/mesc/src/query.rs +++ b/rust/crates/mesc/src/query.rs @@ -12,9 +12,13 @@ pub fn get_default_endpoint( ) -> Result, MescError> { // if using a profile, check if that profile has a default endpoint for chain_id if let Some(profile) = profile { - let name = config.profiles.get(profile).and_then(|p| p.default_endpoint.clone()); - if let Some(name) = name { - return get_endpoint_by_name(config, name.as_str()) + if let Some(profile_data) = config.profiles.get(profile) { + if !profile_data.use_mesc { + return Ok(None) + } + if let Some(endpoint_name) = profile_data.default_endpoint.as_deref() { + return get_endpoint_by_name(config, endpoint_name) + } } }; @@ -34,9 +38,13 @@ pub fn get_endpoint_by_network( // if using a profile, check if that profile has a default endpoint for chain_id if let Some(profile) = profile { - let name = config.profiles.get(profile).and_then(|p| p.network_defaults.get(&chain_id)); - if let Some(name) = name { - return get_endpoint_by_name(config, name) + if let Some(profile_data) = config.profiles.get(profile) { + if !profile_data.use_mesc { + return Ok(None) + } + if let Some(endpoint_name) = profile_data.network_defaults.get(&chain_id) { + return get_endpoint_by_name(config, endpoint_name) + } } }; @@ -62,6 +70,14 @@ pub fn get_endpoint_by_query( query: &str, profile: Option<&str>, ) -> Result, MescError> { + if let Some(profile) = profile { + if let Some(profile_data) = config.profiles.get(profile) { + if !profile_data.use_mesc { + return Ok(None) + } + } + } + // by endpoint name if let Some(endpoint) = config.endpoints.get(query) { return Ok(Some(endpoint.clone())); @@ -109,6 +125,19 @@ pub fn find_endpoints( /// get global metadata pub fn get_global_metadata( config: &RpcConfig, + profile: Option<&str>, ) -> Result, MescError> { - Ok(config.global_metadata.clone()) + let mut metadata = config.global_metadata.clone(); + + // load profile metadata + if let Some(profile) = profile { + if let Some(profile_data) = config.profiles.get(profile) { + if !profile_data.use_mesc { + return Ok(HashMap::new()) + } + metadata.extend(profile_data.profile_metadata.clone()) + } + } + + Ok(metadata) } diff --git a/rust/crates/mesc/src/types/queries.rs b/rust/crates/mesc/src/types/queries.rs index e5dddeb..866bbda 100644 --- a/rust/crates/mesc/src/types/queries.rs +++ b/rust/crates/mesc/src/types/queries.rs @@ -115,6 +115,8 @@ pub struct UserInputQuery { /// GlobalMetadataQuery #[derive(Debug, Clone)] pub struct GlobalMetadataQuery { + /// profile + pub profile: Option, /// path pub path: Option>, } diff --git a/rust/crates/mesc_cli/src/cli/args.rs b/rust/crates/mesc_cli/src/cli/args.rs index 5d08e2c..7cbcee8 100644 --- a/rust/crates/mesc_cli/src/cli/args.rs +++ b/rust/crates/mesc_cli/src/cli/args.rs @@ -245,7 +245,19 @@ pub(crate) struct EndpointArgs { /// Arguments for the `metadata` subcommand #[derive(Parser)] -pub(crate) struct MetadataArgs {} +pub(crate) struct MetadataArgs { + /// print metadata of endpoint + #[clap(long)] + pub(crate) endpoint: Option, + + /// print metadata of profile merged with global metadata + #[clap(long)] + pub(crate) profile: Option, + + /// print metadata of profile not merged with global metadata + #[clap(long)] + pub(crate) profile_only: Option, +} /// Arguments for the `url` subcommand #[derive(Parser)] diff --git a/rust/crates/mesc_cli/src/cli/subcommands/help.rs b/rust/crates/mesc_cli/src/cli/subcommands/help.rs index f13b1c2..53f54a5 100644 --- a/rust/crates/mesc_cli/src/cli/subcommands/help.rs +++ b/rust/crates/mesc_cli/src/cli/subcommands/help.rs @@ -185,17 +185,17 @@ let endpoint: OptionalResult = mesc::get_default_endpoint(None); let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, None); // get the default network for a particular tool -let chain_id: OptionalResult = mesc::get_default_endpoint("xyz_tool"); +let chain_id: OptionalResult = mesc::get_default_endpoint(Some("xyz_tool")); // get the default endpoint of a network for a particular tool -let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, "xyz_tool"); +let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, Some("xyz_tool")); // get an endpoint by name -let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_query"); +let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_goerli"); // parse a user-provided string into a matching endpoint // (first try 1. endpoint name, then 2. chain id, then 3. network name) -let endpoint: OptionalResult = mesc::get_endpoint_by_query(user_str, "xyz_tool"); +let endpoint: OptionalResult = mesc::get_endpoint_by_query(user_str, Some("xyz_tool")); // find all endpoints matching given criteria let query = mesc::MultiEndpointQuery::new().chain_id(5); diff --git a/rust/crates/mesc_cli/src/cli/subcommands/metadata.rs b/rust/crates/mesc_cli/src/cli/subcommands/metadata.rs index c14bd41..fda0f5b 100644 --- a/rust/crates/mesc_cli/src/cli/subcommands/metadata.rs +++ b/rust/crates/mesc_cli/src/cli/subcommands/metadata.rs @@ -1,7 +1,33 @@ use crate::{MescCliError, MetadataArgs}; -pub(crate) fn metadata_command(_args: MetadataArgs) -> Result<(), MescCliError> { +pub(crate) fn metadata_command(args: MetadataArgs) -> Result<(), MescCliError> { let config = mesc::load::load_config_data()?; - println!("{}", serde_json::to_string_pretty(&config.global_metadata)?); - Ok(()) + match (args.endpoint, args.profile, args.profile_only) { + (None, None, None) => { + println!("{}", serde_json::to_string_pretty(&config.global_metadata)?); + Ok(()) + } + (Some(endpoint), None, None) => { + if let Some(endpoint) = config.endpoints.get(endpoint.as_str()) { + println!("{}", serde_json::to_string_pretty(&endpoint.endpoint_metadata)?); + Ok(()) + } else { + Err(MescCliError::Error("endpoint not found".to_string())) + } + } + (None, Some(profile), None) => { + let metadata = mesc::get_global_metadata(Some(profile.as_str()))?; + println!("{}", serde_json::to_string_pretty(&metadata)?); + Ok(()) + } + (None, None, Some(profile)) => { + if let Some(profile) = config.profiles.get(profile.as_str()) { + println!("{}", serde_json::to_string_pretty(&profile.profile_metadata)?); + Ok(()) + } else { + Err(MescCliError::Error("profile not found".to_string())) + } + } + _ => Err(MescCliError::Error("cannot specify more than one metadata type".to_string())), + } } diff --git a/rust/crates/mesc_cli/src/metadata.rs b/rust/crates/mesc_cli/src/metadata.rs index ba2a500..8533746 100644 --- a/rust/crates/mesc_cli/src/metadata.rs +++ b/rust/crates/mesc_cli/src/metadata.rs @@ -30,7 +30,7 @@ pub(crate) fn get_theme_font_style(id: &str) -> Result } fn get_cli_theme() -> Result, MescCliError> { - let global_metadata = mesc::get_global_metadata()?; + let global_metadata = mesc::get_global_metadata(Some("mesc_cli"))?; let path: Vec<&str> = vec![]; let cli_theme: HashMap = match global_metadata.get("cli_theme") { Some(value) => get_value_at(value, &path)?, diff --git a/tests/README.md b/tests/README.md index 22de1d8..82841a9 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,9 +1,7 @@ # MESC Tests -A standard set of tests is used to check whether each MESC implementation is compliant with the MESC specification. - -These tests are language-agnostic and can be run against each MESC implementation. +A language-agnostic set of tests is used to check whether each MESC implementation is compliant with the MESC specification. ## Usage @@ -18,8 +16,11 @@ These tests are language-agnostic and can be run against each MESC implementatio | run tests | `pytest` | | run test in parallel mode (much faster) | `pytest -n auto` | | run tests in debug mode (helpful for debugging) | `pytest --pdb` | +| run tests only tests that previously failed | `pytest --lf` | | run tests for specific adapters only | `pytest --adapters adapters/python adapters/cli` | +By default, tests will run for all MESC implementations. If you do not have all of these implementations installed, you will need to use `--adapters` to select only the subset that that you have installed. + ## Adapters diff --git a/tests/adapters/cli b/tests/adapters/cli index d648eff..11db74b 100755 --- a/tests/adapters/cli +++ b/tests/adapters/cli @@ -15,7 +15,9 @@ def run_query(query: MescQuery) -> str: name = cast(str, query["fields"]["name"]) # type: ignore cmd = ["mesc", "endpoint", "--json", "--name", name] elif query["query_type"] == "endpoint_by_network": - chain_id = cast(str, query["fields"]["chain_id"]) # type: ignore + chain_id = query["fields"]["chain_id"] + if isinstance(chain_id, int): + chain_id = str(chain_id) cmd = ["mesc", "endpoint", "--json", "--network", chain_id] elif query["query_type"] == "user_input": user_input = query["fields"]["user_input"] # type: ignore @@ -30,7 +32,10 @@ def run_query(query: MescQuery) -> str: cmd.append(query['fields']['url_contains']) if query["fields"].get('chain_id') is not None: cmd.append('--network') - cmd.append(query['fields']['chain_id']) + chain_id = query["fields"]["chain_id"] + if isinstance(chain_id, int): + chain_id = str(chain_id) + cmd.append(chain_id) elif query['query_type'] == 'global_metadata': cmd = ["mesc", "metadata"] else: diff --git a/tests/adapters/python b/tests/adapters/python index 85bbd25..d6e451a 100755 --- a/tests/adapters/python +++ b/tests/adapters/python @@ -25,7 +25,7 @@ def run_query(query: EndpointQuery) -> Endpoint | Sequence[Endpoint] | None: multi_endpoint_fields = cast(mesc.types.MultiEndpointQuery, query['fields']) return mesc.find_endpoints(**multi_endpoint_fields) elif query["query_type"] == "global_metadata": - return mesc.get_global_metadata() + return mesc.get_global_metadata(**query['fields']) else: raise Exception("invalid query type: " + str(query["query_type"])) diff --git a/tests/generate.py b/tests/generate.py index 856191e..d659e6a 100755 --- a/tests/generate.py +++ b/tests/generate.py @@ -3,9 +3,6 @@ from __future__ import annotations import copy -import json -import os -import time from typing import Any, TypeVar, Sequence from mesc import RpcConfig, Profile, Endpoint @@ -94,14 +91,24 @@ full_config: RpcConfig = { "1": "llamanodes_ethereum", "10": "llamanodes_optimism", }, - "profile_metadata": {}, + "profile_metadata": { + "sample_key": "profile_value", + }, "use_mesc": True, }, + "not_using_mesc": { + "name": "not_using_mesc", + "default_endpoint": None, + "network_defaults": {}, + "profile_metadata": {}, + "use_mesc": False, + }, }, "global_metadata": { "api_keys": { "etherscan": "abc123", }, + "sample_key": "global_value", }, } @@ -128,9 +135,47 @@ def generate_tests() -> list[Test]: generators = [ create_basic_query_tests, create_override_tests, - create_invalid_tests, + create_invalid_query_tests, + create_invalid_config_tests, ] - return [test for generator in generators for test in generator()] + + tests = [test for generator in generators for test in generator()] + + # for tests that query with null profile, also query with non-existent profile + for test in list(tests): + query = test[3] + if ( + query is not None + and "profile" in query.get("fields", {}) + and query["fields"].get("profile") is None + ): + new_test = copy.deepcopy(test) + new_test[3]["fields"]["profile"] = "unknown_profile" # type: ignore + + new_test = (new_test[0] + ", unknown_profile",) + new_test[1:] + tests.append(new_test) + + # every query should return None or empty when a profile has use_mesc=True + for test in list(tests): + query = test[3] + target_output = test[4] + if ( + query is not None + and "profile" in query.get("fields", {}) + and query["fields"].get("profile") is None + and target_output is not None + ): + new_test = copy.deepcopy(test) + new_test[3]["fields"]["profile"] = "not_using_mesc" # type: ignore + if query["query_type"] == "global_metadata": + expected: dict | None = {} + else: + expected = None + new_test = new_test[0:4] + (expected,) + new_test[5:] + new_test = (new_test[0] + ", profile not_using_mesc",) + new_test[1:] + tests.append(new_test) + + return tests T = TypeVar("T") @@ -239,6 +284,17 @@ def create_basic_query_tests() -> list[Test]: full_config["endpoints"]["local_goerli"], True, ), + ( + "query endpoint chain_id by int", + {}, + full_config, + { + "query_type": "endpoint_by_network", + "fields": {"chain_id": 1, "profile": None}, + }, + full_config["endpoints"]["local_ethereum"], + True, + ), ( "get endpoint by network", {}, @@ -353,9 +409,20 @@ def create_basic_query_tests() -> list[Test]: full_config["endpoints"]["local_goerli"], True, ), + ( + "get endpoint by user query, dne", + {}, + full_config, + { + "query_type": "user_input", + "fields": {"user_input": "this_query_has_no_results", "profile": None}, + }, + None, + True, + ), ] - # get endpoint by user query, profile blank + # get endpoint by user query, with default profile tests += [ ( "get endpoint by user query, endpoint name", @@ -423,6 +490,17 @@ def create_basic_query_tests() -> list[Test]: full_config["endpoints"]["local_goerli"], True, ), + ( + "get endpoint by user query, dne", + {}, + full_config, + { + "query_type": "user_input", + "fields": {"user_input": "this_query_has_no_results", "profile": "abc"}, + }, + None, + True, + ), ] # get endpoint by user query, profile full @@ -482,16 +560,33 @@ def create_basic_query_tests() -> list[Test]: full_config["endpoints"]["llamanodes_ethereum"], True, ), - # ( - # "get endpoint by user query, custom network name", - # full_config, - # {"query_type": "user_input", "fields": {"user_input": "testnet", "profile": "xyz"}}, - # full_config['endpoints']['llamanodes_goerli'], - # ), + ( + "get endpoint by user query, custom network name", + {}, + full_config, + { + "query_type": "user_input", + "fields": {"user_input": "testnet", "profile": "xyz"}, + }, + full_config["endpoints"]["local_goerli"], + True, + ), + ( + "get endpoint by user query, dne", + {}, + full_config, + { + "query_type": "user_input", + "fields": {"user_input": "this_query_has_no_results", "profile": "xyz"}, + }, + None, + True, + ), ] # test querying by known network names for chain_id, network_name in known_networks.items(): + # with a default endpoint network_endpoint = copy.deepcopy(blank_endpoint) network_endpoint["chain_id"] = chain_id network_config = copy.deepcopy(full_config) @@ -510,6 +605,23 @@ def create_basic_query_tests() -> list[Test]: ) tests.append(test) + # without a default endpoint + network_config = copy.deepcopy(full_config) + if chain_id in network_config["network_defaults"]: + del network_config["network_defaults"][chain_id] + test_without: Test = ( + "test querying " + network_name + " by name", + {}, + network_config, + { + "query_type": "user_input", + "fields": {"user_input": network_name, "profile": None}, + }, + None, + True, + ) + tests.append(test_without) + # test multi-endpoint queries tests += [ ( @@ -524,6 +636,14 @@ def create_basic_query_tests() -> list[Test]: ], True, ), + ( + "empty fuzzy name query", + {}, + full_config, + {"query_type": "multi_endpoint", "fields": {"name_contains": "yyyyyy"}}, + [], + True, + ), ( "fuzzy url query", {}, @@ -535,6 +655,14 @@ def create_basic_query_tests() -> list[Test]: ], True, ), + ( + "fuzzy url query", + {}, + full_config, + {"query_type": "multi_endpoint", "fields": {"url_contains": "yyyyyy"}}, + [], + True, + ), ( "chain_id query", {}, @@ -546,21 +674,125 @@ def create_basic_query_tests() -> list[Test]: ], True, ), + ( + "chain_id int query", + {}, + full_config, + {"query_type": "multi_endpoint", "fields": {"chain_id": 1}}, + [ + full_config["endpoints"]["local_ethereum"], + full_config["endpoints"]["llamanodes_ethereum"], + ], + True, + ), + ( + "empty chain_id query", + {}, + full_config, + {"query_type": "multi_endpoint", "fields": {"chain_id": "1111"}}, + [], + True, + ), ] - # for tests that query with null profile, also query with non-existent profile - for test in list(tests): - query = test[3] - if "profile" in query.get("fields", {}) and query["fields"]["profile"] is None: # type: ignore - new_test = copy.deepcopy(test) - new_test[3]["fields"]["profile"] = "unknown_profile" # type: ignore - tests.append(new_test) + # test fetching metadata + tests += [ + ( + "get full global metadata, no profile", + {}, + full_config, + {"query_type": "global_metadata", "fields": {"profile": None}}, + full_config["global_metadata"], + True, + ), + ( + "get full global metadata, irrelevant profile", + {}, + full_config, + {"query_type": "global_metadata", "fields": {"profile": "abc"}}, + full_config["global_metadata"], + True, + ), + ( + "get full global metadata, merged with profile", + {}, + full_config, + {"query_type": "global_metadata", "fields": {"profile": "xyz"}}, + dict( + full_config["global_metadata"], + **full_config["profiles"]["xyz"]["profile_metadata"], + ), + True, + ), + ] return tests -# [name, env, config] -def create_invalid_tests() -> list[Test]: +def create_invalid_query_tests() -> list[Test]: + invalid_queries: Sequence[tuple[str, Any]] = [ + ( + "query default endpoint with int profile", + {"query_type": "default_endpoint", "fields": {"profile": 1}}, + ), + ( + "query endpoint name by null", + {"query_type": "endpoint_by_name", "fields": {"name": None}}, + ), + ( + "query endpoint name by int", + {"query_type": "endpoint_by_name", "fields": {"name": 1}}, + ), + ( + "query endpoint chain_id by null", + { + "query_type": "endpoint_by_network", + "fields": {"chain_id": None, "profile": None}, + }, + ), + ( + "query endpoint chain_id by int profile", + { + "query_type": "endpoint_by_network", + "fields": {"chain_id": "1", "profile": 1}, + }, + ), + ( + "query user input by null", + { + "query_type": "user_input", + "fields": {"user_input": None, "profile": None}, + }, + ), + ( + "query user input by bool", + { + "query_type": "user_input", + "fields": {"user_input": True, "profile": None}, + }, + ), + ( + "query user input by int profile", + {"query_type": "user_input", "fields": {"user_input": True, "profile": 1}}, + ), + ] + + tests = [] + for test_name, query in invalid_queries: + test: Test = ( + test_name, + {}, + full_config, + query, + None, + False, + ) + tests.append(test) + + return tests + + +def create_invalid_config_tests() -> list[Test]: tests: list[Test] = [] # unknown default endpoint @@ -591,75 +823,6 @@ def create_invalid_tests() -> list[Test]: ), ) - # invalid queries - invalid_queries: Sequence[tuple[str, Any]] = [ - ( - "query default endpoint with int profile", - {"query_type": "default_endpoint", "fields": {"profile": 1}}, - ), - ( - "query endpoint name by non-existent name", - {"query_type": "default_endpoint", "fields": {"profile": 1}}, - ), - ( - "query endpoint name by null", - {"query_type": "endpoint_by_name", "fields": {"name": None}}, - ), - ( - "query endpoint name by int", - {"query_type": "endpoint_by_name", "fields": {"name": 1}}, - ), - ( - "query endpoint chain_id by null", - { - "query_type": "endpoint_by_network", - "fields": {"chain_id": None, "profile": None}, - }, - ), - ( - "query endpoint chain_id by int", - { - "query_type": "endpoint_by_network", - "fields": {"chain_id": 1, "profile": None}, - }, - ), - ( - "query endpoint chain_id by int profile", - { - "query_type": "endpoint_by_network", - "fields": {"chain_id": "1", "profile": 1}, - }, - ), - ( - "query user input by null", - { - "query_type": "user_input", - "fields": {"user_input": None, "profile": None}, - }, - ), - ( - "query user input by bool", - { - "query_type": "user_input", - "fields": {"user_input": True, "profile": None}, - }, - ), - ( - "query user input by int profile", - {"query_type": "user_input", "fields": {"user_input": True, "profile": 1}}, - ), - ] - for test_name, query in invalid_queries: - test: Test = ( - test_name, - {}, - full_config, - query, - None, - False, - ) - tests.append(test) - # incorrect types tests invalid_type_tests = [ ( @@ -836,7 +999,7 @@ def create_invalid_tests() -> list[Test]: ), ] for test_name, config in invalid_type_tests: - test = ( + test: Test = ( test_name, {}, config, @@ -929,7 +1092,6 @@ def create_invalid_tests() -> list[Test]: return tests -# [name, env, config, query, result] def create_override_tests() -> list[Test]: tests: list[Test] = [] @@ -1200,15 +1362,15 @@ def create_override_tests() -> list[Test]: "override global metadata, change existing key", {"MESC_GLOBAL_METADATA": '{"api_keys": {"etherscan": "new_key"}}'}, full_config, - {"query_type": "global_metadata", "fields": {"path": None}}, - {"api_keys": {"etherscan": "new_key"}}, + {"query_type": "global_metadata", "fields": {}}, + dict(full_config["global_metadata"], api_keys={"etherscan": "new_key"}), True, ), ( "override global metadata, add new key", {"MESC_GLOBAL_METADATA": '{"some_new_key": "value"}'}, full_config, - {"query_type": "global_metadata", "fields": {"path": None}}, + {"query_type": "global_metadata", "fields": {}}, dict(full_config["global_metadata"], some_new_key="value"), True, ), @@ -1216,7 +1378,7 @@ def create_override_tests() -> list[Test]: "override global metadata blank", {"MESC_GLOBAL_METADATA": ""}, full_config, - {"query_type": "global_metadata", "fields": {"path": None}}, + {"query_type": "global_metadata", "fields": {}}, full_config["global_metadata"], True, ), @@ -1282,17 +1444,3 @@ def create_override_tests() -> list[Test]: pass return tests - - -if __name__ == "__main__": - start_time = time.time() - tests = generate_tests() - duration = time.time() - start_time - print("generated", len(tests), "tests in", "%.3f" % duration, "seconds") - - path = "generated/tests.json" - os.makedirs(os.path.dirname(path), exist_ok=True) - with open(path, "w") as f: - json.dump(tests, f) - print() - print("saved to", path) diff --git a/tests/test_mesc.py b/tests/test_mesc.py old mode 100644 new mode 100755 index 0492bc9..d972a57 --- a/tests/test_mesc.py +++ b/tests/test_mesc.py @@ -150,15 +150,15 @@ def test_mesc_output( else: # failure case: incorrect query output print_error_summary(**error_data) - raise OutputDoesNotMatch( - "output does not match", adapter, setup_name, test_name + raise QueryShouldHaveFailed( + "success when expecting failure", adapter, setup_name, test_name ) else: if should_succeed: # failure case: query should have failed print_error_summary(**error_data) - raise QueryShouldHaveFailed( - "success when expecting failure", adapter, setup_name, test_name + raise OutputDoesNotMatch( + "output does not match", adapter, setup_name, test_name ) else: # working properly