def test_get_config(self): """Verify returning the application's configuration""" expected_configuration = config.Configure(pathfinder.config_path()).config expected_configuration.read(pathfinder.config_path()) returned_configuration = model.get_config().config returned_configuration.read(pathfinder.config_path()) for section in expected_configuration.sections(): self.assertListEqual(expected_configuration.items(section), returned_configuration.items(section))
def test_get_size(self): """Verify returning the size of the main app window set in config""" cfg = config.Configure(pathfinder.config_path()) str_win_size = cfg.get_app_option_list("Window Size") expected_win_size = [300, 600] if str_win_size is not None: expected_win_size = [int(dimsize) for dimsize in str_win_size] self.assertListEqual(expected_win_size, self.model.get_window_size())
def test_get_coords(self): """Verify returning the UL corner of the main app window set in config""" cfg = config.Configure(pathfinder.config_path()) str_coords = cfg.get_app_option_list("Coordinates") expected_coords = (0, 0) if str_coords is not None: expected_coords = [int(coord) for coord in str_coords] self.assertListEqual(expected_coords, self.model.get_coords())
def test_set_preview_state(self): """Verify setting the current setting for displaying plot thumbnails""" cfg = config.Configure(pathfinder.config_path()) original_preview_state = cfg.get_app_option_boolean("Enable Preview") new_preview_state = not original_preview_state self.assertEqual(original_preview_state, self.model.get_preview_state()) self.model.set_preview_state(new_preview_state) self.assertEqual(new_preview_state, self.model.get_preview_state()) self.model.set_preview_state(original_preview_state)
def setUp(self): self.sample_data = np.array(self.random_data()) self.sample_data_basename = "sample.dat" self.sample_data_file = os.path.join(os.path.dirname(__file__), self.sample_data_basename) with h5py.File(self.sample_data_file, 'w') as fidout: fidout.create_dataset(self.sample_data_basename, data=self.sample_data) self.mock_controller = "" self.model = model.MainModel(self.mock_controller) cfg = config.Configure(pathfinder.config_path()) self.original_loglevel = cfg.get_app_option("log level")
def test_get_loglevel(self): """Verify returning the log level from config""" cfg = config.Configure(pathfinder.config_path()) log_level = cfg.get_app_option("log level") available_log_levels = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL} log_level = available_log_levels.get(log_level, logging.WARNING) self.assertEqual(log_level, model.get_loglevel())
def log_path(): """Returns the path to the log file. If not already set, sets to user's home directory/nditoolbox.log and sets the default in the config file.""" _config = config.Configure(config_path()) logpath_key = "Log File" if _config.has_app_option(logpath_key): return _config.get_app_option(logpath_key) else: default_logpath = os.path.normcase( os.path.join(os.path.expanduser('~'), 'nditoolbox.log')) _config.set_app_option({logpath_key: default_logpath}) return default_logpath
def test_set_size(self): """Verify setting the size of the main app window in config""" cfg = config.Configure(pathfinder.config_path()) str_win_size = cfg.get_app_option_list("Window Size") original_win_size = [300, 600] if str_win_size is not None: original_win_size = [int(dimsize) for dimsize in str_win_size] self.assertListEqual(original_win_size, self.model.get_window_size()) new_win_size = [800, 1024] self.model.set_window_size(new_win_size) self.assertListEqual(new_win_size, self.model.get_window_size()) self.model.set_window_size(original_win_size)
def test_no_cfg_file(self): """Verify Configure handles non-existent config file""" no_such_file = os.path.join(os.path.dirname(__file__), "_config.cfg") self.assertFalse(os.path.exists(no_such_file)) cfg = config.Configure(no_such_file) self.assertIsNone(cfg.get_app_option('path')) cfg.set_app_option({'path': pathfinder.app_path()}) # App option should now be set in newly-created file self.assertTrue(os.path.exists(no_such_file)) self.assertEqual(cfg.get_app_option('path'), pathfinder.app_path()) if os.path.exists(no_such_file): os.remove(no_such_file)
def user_path(): """Returns the path for storing user data. If not already set, returns user's home directory/nditoolbox and sets the default in the config file.""" _config = config.Configure(config_path()) upath_key = "User Path" if _config.has_app_option(upath_key): return _config.get_app_option(upath_key) else: default_upath = os.path.normcase( os.path.join(os.path.expanduser('~'), 'nditoolbox')) _config.set_app_option({upath_key: default_upath}) return default_upath
def test_set_loglevel(self): """Verify setting the log level in config""" cfg = config.Configure(pathfinder.config_path()) log_levels = ['debug', 'info', 'warning', 'error', 'critical', None, 'abc'] acceptable_log_levels = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL} for level in log_levels: model.set_loglevel(level) if level in acceptable_log_levels: self.assertEqual(acceptable_log_levels[level], model.get_loglevel())
def test_set_coords(self): """Verify setting the UL corner of the main app window in config""" cfg = config.Configure(pathfinder.config_path()) str_coords = cfg.get_app_option_list("Coordinates") original_coords = (0, 0) if str_coords is not None: original_coords = [int(coord) for coord in str_coords] self.assertListEqual(original_coords, self.model.get_coords()) new_coords_int = [3, 5] self.model.set_coords(new_coords_int) self.assertListEqual(new_coords_int, self.model.get_coords()) new_coords_str = ["9", "-1"] self.model.set_coords(new_coords_str) self.assertListEqual([int(coord) for coord in new_coords_str], self.model.get_coords()) self.model.set_coords(original_coords)
def test_get_preview_state(self): """Verify returning the current setting for displaying plot thumbnails""" cfg = config.Configure(pathfinder.config_path()) preview_state = cfg.get_app_option_boolean("Enable Preview") self.assertEqual(preview_state, self.model.get_preview_state())
def get_config(): """Returns a Configure instance pointing to the application's default configuration file.""" return config.Configure(pathfinder.config_path())
def setUp(self): self.config_path = os.path.join(os.path.dirname(__file__), "tst_config.cfg") self.config = config.Configure(self.config_path)