def load_config_from_file(self): loader = ConfigLoader() filename = askopenfilename( initialdir=self.get_state()['home_directory'], title='select .cfg file', defaultextension='.cfg') if not filename: return None loader.set_state_from_file(filename) self._load_new_state(loader)
def test_load_config_from_file_loads_from_file(self, mock_filename): filename = os.path.join(TESTS_FILES, 'tst.cfg') mock_filename.return_value = filename save_config_to_filename({'probability_plural_noun': 0.0}, filename) main = MainFrame() default_state = main.get_state() main.load_config_from_file() self.assertNotEqual(main.get_state(), default_state) loader = ConfigLoader() loader.set_state_from_file(filename) self.assertEqual(loader.state, main.get_state()) os.remove(filename)
def test_ConfigLoader_set_state_from_file_no_errors(self): filename = os.path.join(TESTS_FILES, 'test.cfg') save_config_to_filename({'error_probability': 1.0}, filename) # default 0.20 loader = ConfigLoader() loader.set_state_from_file(filename) default_loader = ConfigLoader() loader_state = loader.state default_state = default_loader.state self.assertNotEqual(loader_state, default_state) self.assertEqual(loader_state['error_probability'], 1.0) loader_state['error_probability'] = 0.20 self.assertEqual(loader_state, default_state) # cleanup os.remove(filename)
def test_export_config_file_saves_correctly(self, mock_filename): filename = os.path.join(TESTS_FILES, 'tst.cfg') if os.path.exists(filename): os.remove(filename) main = MainFrame() default_state = main.get_state() save_config({'font_size': 15}) main.load_config() current_state = main.get_state() ConfigLoader().revert_to_default() config_state = ConfigLoader().state expected_state = config_state.copy() expected_state['font_size'] = 15 # test current state self.assertEqual(default_state, config_state) self.assertEqual(current_state, expected_state) self.assertNotEqual(default_state, current_state) mock_filename.return_value = filename main.export_config_file() saved_config = ConfigLoader() saved_config.set_state_from_file(filename) config_file_state = ConfigLoader().state self.assertEqual(saved_config.state, current_state) self.assertEqual(main.get_state(), current_state) self.assertEqual(config_file_state, default_state) mock_filename.assert_called_with( initialdir=current_state['home_directory'], title='select .cfg file', initialfile='exported_config.cfg', defaultextension='.cfg') os.remove(filename)