def _create_demo(infos=None, title=_TITLE): """ Create the demo object with everything setup ready to be launched. Parameters ---------- infos : list of dict, or None List of responses specifying the demo resources. Each response is a dictionary, in the format as specified by an entry point. If none, then responses are loaded from existing entry points installed in the Python environment. title : str, optional Default application title. Returns ------- demo : Demo """ if infos is None: infos = get_responses() resources = [response_to_node(response) for response in infos] logger.info("Found %r resource(s).", len(resources)) return Demo( title=title, model=DemoVirtualDirectory(resources=resources), )
def test_bad_response_replaced(self): # If the response is badly formatted, replace with a placeholder. response = {} with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(response) self.assertFalse(resource.has_children()) self.assertEqual(resource.nice_name, "(Empty)") self.assertIn("Unable to load data.", resource.description) log_content, = watcher.output self.assertIn("KeyError", log_content)
def test_bad_response_missing_version(self): with tempfile.TemporaryDirectory() as temp_dir: response = { "name": "Name", "root": temp_dir, } with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(response) self.assertFalse(resource.has_children()) log_content, = watcher.output self.assertIn("KeyError: \'version\'", log_content)
def test_good_response_to_node(self): with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, "index.rst"), "w"): pass response = { "version": 1, "name": "Amazing Demo", "root": temp_dir, } resource = response_to_node(response) self.assertEqual(resource.name, temp_dir) self.assertEqual(resource.nice_name, "Amazing Demo") self.assertTrue(resource.has_children())
def test_bad_response_bad_name_type(self): with tempfile.TemporaryDirectory() as temp_dir: response = { "version": 1, "name": 1, "root": temp_dir, } with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(response) self.assertFalse(resource.has_children()) log_content, = watcher.output self.assertIn("TraitError", log_content)
def test_bad_response_missing_name(self): with tempfile.TemporaryDirectory() as temp_dir: response = { "version": 1, "root": temp_dir, } with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(response) self.assertFalse(resource.has_children()) self.assertEqual(resource.nice_name, "(Empty)") log_content, = watcher.output self.assertIn("KeyError: \'name\'", log_content)
def test_bad_response_type_error(self): bad_values = [ None, "1", 1, (), ] for bad_value in bad_values: with self.subTest(bad_value=bad_value): with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(bad_value) self.assertFalse(resource.has_children()) self.assertEqual(resource.nice_name, "(Empty)") self.assertIn("Unable to load data.", resource.description) log_content, = watcher.output self.assertIn("TypeError", log_content)
def test_good_response_but_nonexisting_root_to_node(self): # If the response refers to a nonexisting root path, # it is detected and replaced with a placeholder. response = { "version": 1, "name": "Amazing Demo", "root": "I_do_not_exist", } with self.assertLogs(LOGGER_NAME) as watcher: resource = response_to_node(response) self.assertEqual(resource.nice_name, "Amazing Demo") self.assertFalse(resource.has_children()) self.assertIn( "Unable to load data.", resource.description, ) log_content, = watcher.output self.assertIn("TraitError", log_content)