def setup_class(cls): """Setup the test class.""" md_path = os.path.join(ROOT_DIR, "docs", "generic-skills-step-by-step.md") code_blocks = extract_code_blocks(filepath=md_path, filter_="python") cls.generic_seller = code_blocks[0:11] cls.generic_buyer = code_blocks[11:len(code_blocks)]
def setup_class(cls): """Setup the test class.""" BaseAEATestCase.setup_class() doc_path = os.path.join(ROOT_DIR, MD_FILE) cls.code_blocks = extract_code_blocks(filepath=doc_path, filter="python") test_code_path = os.path.join(CUR_PATH, PY_FILE) cls.python_file = extract_python_code(test_code_path)
def test_read_md_file(): """Compare the extracted code with the python file.""" doc_path = os.path.join(ROOT_DIR, MD_FILE) code_blocks = extract_code_blocks(filepath=doc_path, filter_="python") test_code_path = os.path.join(CUR_PATH, PY_FILE) python_file = extract_python_code(test_code_path) assert code_blocks[-1] == python_file, "Files must be exactly the same."
def test_code_blocks_exist(self): """Test that all the code-blocks exist in the python file.""" path = Path(ROOT_DIR, "tests", "test_docs", "test_bash_yaml", "md_files") logger.info(os.listdir(path)) for file in os.listdir(path): if not file.endswith(".md"): continue bash_file = read_md_file(filepath=Path(path, file)) md_path = os.path.join(ROOT_DIR, "docs", file.replace("bash-", "")) bash_code_blocks = extract_code_blocks(filepath=md_path, filter="bash") for blocks in bash_code_blocks: assert (blocks in bash_file ), "[{}]: FAILED. Code must be identical".format(file) logger.info("[{}]: PASSED".format(file)) yaml_code_blocks = extract_code_blocks(filepath=md_path, filter="yaml") for blocks in yaml_code_blocks: assert (blocks in bash_file ), "[{}]: FAILED. Code must be identical".format(file) logger.info("[{}]: PASSED".format(file))
def _test_blocks(self, filename: str, filter_: str): """Test code blocks of a certain type determined by 'filter_' param.""" bash_file = Path(self.BASH_DIR_PATH, filename).read_text(encoding="utf-8") md_path = os.path.join(ROOT_DIR, "docs", filename.replace("bash-", "")) bash_code_blocks = extract_code_blocks(filepath=md_path, filter_=filter_) for blocks in bash_code_blocks: assert blocks in bash_file, "[{}]: FAILED. Code must be identical".format( filename) logger.info( f"[{filename}]: PASSED. Tested {len(bash_code_blocks)} '{filter_}' blocks." )
def test_update_skill_and_run(self): """Test that the resource folder contains scaffold handlers.py module.""" self.initialize_aea(AUTHOR) # generate random location location = { "latitude": round(uniform(-90, 90), 2), # nosec "longitude": round(uniform(-180, 180), 2), # nosec } simple_service_registration_aea = "simple_service_registration" self.fetch_agent( "fetchai/simple_service_registration:0.10.0", simple_service_registration_aea, ) self.set_agent_context(simple_service_registration_aea) # add non-funded key self.generate_private_key(COSMOS) self.generate_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE_CONNECTION) self.add_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE) self.add_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE_CONNECTION, connection=True) self.replace_private_key_in_file(NON_FUNDED_COSMOS_PRIVATE_KEY_1, COSMOS_PRIVATE_KEY_FILE_CONNECTION) default_routing = { "fetchai/oef_search:0.4.0": "fetchai/soef:0.6.0", } # replace location setting_path = "vendor.fetchai.skills.simple_service_registration.models.strategy.args.location" self.force_set_config(setting_path, location) search_aea = "search_aea" self.create_agents(search_aea) self.set_agent_context(search_aea) skill_name = "my_search" skill_id = AUTHOR + "/" + skill_name + ":" + DEFAULT_VERSION self.scaffold_item("skill", skill_name) self.add_item("connection", "fetchai/p2p_libp2p:0.7.0") self.add_item("connection", "fetchai/soef:0.6.0") self.set_config("agent.default_connection", "fetchai/p2p_libp2p:0.7.0") setting_path = "agent.default_routing" self.force_set_config(setting_path, default_routing) # manually change the files: path = Path(self.t, search_aea, "skills", skill_name, "behaviours.py") original = Path(AEA_DIR, "skills", "scaffold", "behaviours.py") assert filecmp.cmp(path, original) with open(path, "w") as file: file.write(self.code_blocks[0]) # block one is behaviour path = Path(self.t, search_aea, "skills", skill_name, "handlers.py") original = Path(AEA_DIR, "skills", "scaffold", "handlers.py") assert filecmp.cmp(path, original) with open(path, "w") as file: file.write(self.code_blocks[1]) # block two is handler path = Path(self.t, search_aea, "skills", skill_name, "my_model.py") original = Path(AEA_DIR, "skills", "scaffold", "my_model.py") assert filecmp.cmp(path, original) with open(path, "w") as file: file.write(self.code_blocks[2]) # block three is dialogues path_new = Path(self.t, search_aea, "skills", skill_name, "dialogues.py") os.rename(path, path_new) path = Path(self.t, search_aea, "skills", skill_name, "skill.yaml") yaml_code_block = extract_code_blocks(self.doc_path, filter="yaml") with open(path, "w") as file: file.write(yaml_code_block[0]) # block one is yaml # update fingerprint self.fingerprint_item("skill", skill_id) # add keys self.generate_private_key(COSMOS) self.generate_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE_CONNECTION) self.add_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE) self.add_private_key(COSMOS, COSMOS_PRIVATE_KEY_FILE_CONNECTION, connection=True) # fund key self.generate_wealth(COSMOS) # set p2p configs setting_path = "vendor.fetchai.connections.p2p_libp2p.config" self.force_set_config(setting_path, NON_GENESIS_CONFIG) # replace location setting_path = "skills.{}.behaviours.my_search_behaviour.args.location".format( skill_name) self.force_set_config(setting_path, location) # run agents self.set_agent_context(simple_service_registration_aea) simple_service_registration_aea_process = self.run_agent() check_strings = ( "Downloading golang dependencies. This may take a while...", "Finished downloading golang dependencies.", "Starting libp2p node...", "Connecting to libp2p node...", "Successfully connected to libp2p node!", "My libp2p addresses:", ) missing_strings = self.missing_from_output( simple_service_registration_aea_process, check_strings, timeout=240, is_terminating=False, ) assert ( missing_strings == [] ), "Strings {} didn't appear in simple_service_registration_aea output.".format( missing_strings) self.set_agent_context(search_aea) search_aea_process = self.run_agent() check_strings = ( "Downloading golang dependencies. This may take a while...", "Finished downloading golang dependencies.", "Starting libp2p node...", "Connecting to libp2p node...", "Successfully connected to libp2p node!", "My libp2p addresses:", ) missing_strings = self.missing_from_output(search_aea_process, check_strings, timeout=240, is_terminating=False) assert (missing_strings == [] ), "Strings {} didn't appear in search_aea output.".format( missing_strings) check_strings = ( "registering agent on SOEF.", "registering service on SOEF.", ) missing_strings = self.missing_from_output( simple_service_registration_aea_process, check_strings, is_terminating=False, ) assert ( missing_strings == [] ), "Strings {} didn't appear in simple_service_registration_aea output.".format( missing_strings) check_strings = ( "sending search request to OEF search node, search_count=", "number of search requests sent=", "found number of agents=1, received search count=", ) missing_strings = self.missing_from_output(search_aea_process, check_strings, is_terminating=False) assert (missing_strings == [] ), "Strings {} didn't appear in search_aea output.".format( missing_strings) self.terminate_agents(simple_service_registration_aea_process, search_aea_process) assert (self.is_successfully_terminated() ), "Agents weren't successfully terminated." wait_for_localhost_ports_to_close([9000, 9001])
def setup_class(cls): """Setup the test class.""" AEATestCaseMany.setup_class() cls.doc_path = os.path.join(ROOT_DIR, MD_FILE) cls.code_blocks = extract_code_blocks(filepath=cls.doc_path, filter="python")
def test_update_skill_and_run(self): """Test that the resource folder contains scaffold handlers.py module.""" self.initialize_aea(AUTHOR) simple_service_registration_aea = "simple_service_registration" self.fetch_agent("fetchai/simple_service_registration:0.7.0", simple_service_registration_aea) search_aea = "search_aea" self.create_agents(search_aea) self.set_agent_context(search_aea) skill_name = "my_search" skill_id = AUTHOR + "/" + skill_name + ":" + DEFAULT_VERSION self.scaffold_item("skill", skill_name) self.add_item("connection", "fetchai/oef:0.6.0") self.set_config("agent.default_connection", "fetchai/oef:0.6.0") # manually change the files: path = Path(self.t, search_aea, "skills", skill_name, "behaviours.py") original = Path(AEA_DIR, "skills", "scaffold", "behaviours.py") assert filecmp.cmp(path, original) with open(path, "w") as file: file.write(self.code_blocks[0]) # block one is behaviour path = Path(self.t, search_aea, "skills", skill_name, "handlers.py") original = Path(AEA_DIR, "skills", "scaffold", "handlers.py") assert filecmp.cmp(path, original) with open(path, "w") as file: file.write(self.code_blocks[1]) # block two is handler path = Path(self.t, search_aea, "skills", skill_name, "my_model.py") os.remove(path) path = Path(self.t, search_aea, "skills", skill_name, "skill.yaml") yaml_code_block = extract_code_blocks(self.doc_path, filter="yaml") with open(path, "w") as file: file.write(yaml_code_block[0]) # block one is yaml # update fingerprint self.fingerprint_item("skill", skill_id) # run agents self.set_agent_context(simple_service_registration_aea) simple_service_registration_aea_process = self.run_agent( "--connections", "fetchai/oef:0.6.0") self.set_agent_context(search_aea) search_aea_process = self.run_agent("--connections", "fetchai/oef:0.6.0") check_strings = ( "updating services on OEF service directory.", "unregistering services from OEF service directory.", ) missing_strings = self.missing_from_output( simple_service_registration_aea_process, check_strings, is_terminating=False, ) assert ( missing_strings == [] ), "Strings {} didn't appear in simple_service_registration_aea output.".format( missing_strings) check_strings = ( "sending search request to OEF search node, search_count=", "number of search requests sent=", "found number of agents=1, received search count=", ) missing_strings = self.missing_from_output(search_aea_process, check_strings, is_terminating=False) assert (missing_strings == [] ), "Strings {} didn't appear in search_aea output.".format( missing_strings) self.terminate_agents(simple_service_registration_aea_process, search_aea_process) assert (self.is_successfully_terminated() ), "Agents weren't successfully terminated."