def test_exception(self): console = MockConsole() self.wizard.console = console err = RuntimeError("baz") self.wizard.exception('foo', err) assert 'foo' in console.log_text assert "RuntimeError: baz" in console.log_text
def test_ask_custom_eval(self, inputs, type_, expected): console = MockConsole(inputs) self.wizard.console = console result = self.wizard.ask_custom_eval('foo', type_=type_) assert result == expected assert console.input_call_count == 1 assert 'foo' in console.log_text
def test_ask_save(self, inputs): expected = {'y': True, 'n': False}[inputs] console = MockConsole(['foo', inputs]) self.wizard.console = console result = self.wizard._ask_save() assert result is expected assert "Before quitting" in self.wizard.console.log_text assert "Sorry" in self.wizard.console.log_text
def test_ask_do_another(self, inputs, expected): console = MockConsole(inputs) self.wizard.console = console result = self.wizard._ask_do_another("CV") assert result == expected assert "another CV" in console.log_text if len(inputs) > 1: assert "Sorry" in console.log_text
def test_ask_custom_eval_bad_input(self): console = MockConsole(['bar', '2+2']) self.wizard.console = console result = self.wizard.ask_custom_eval('foo') assert result == 4 assert console.input_call_count == 2 assert "I couldn't understand" in console.log_text assert "NameError" in console.log_text
def test_ask_load_error(self): console = MockConsole(["abc", "100"]) self.wizard.console = console loaded = self.wizard.ask_load("foo", int) assert loaded == 100 assert "foo" in console.log_text assert "ValueError" in console.log_text assert "base 10" in console.log_text
def test_ask_help(self): console = MockConsole(['?helpme', 'foo']) self.wizard.console = console def helper(result): return f"You said: {result[1:]}" result = self.wizard.ask('bar', helper=helper) assert result == 'foo' assert 'You said: helpme' in console.log_text
def test_save_to_file(self, toy_engine): console = MockConsole([]) self.wizard.console = console self.wizard.register(toy_engine, 'Engine', 'engines') storage = MockStorage() self.wizard.save_to_file(storage) assert len(storage.cvs) == len(storage.volumes) == 0 assert len(storage.networks) == len(storage.schemes) == 0 assert len(storage.engines) == 1 assert storage.engines[toy_engine.name] == toy_engine assert storage.engines[0] == toy_engine assert "Everything has been stored" in self.wizard.console.log_text
def test_ask_empty(self, autohelp): # if the use response in an empty string, we should repeat the # question (possible giving autohelp). This fixes a regression where # an empty string would cause an uncaught exception. console = MockConsole(['', 'foo']) self.wizard.console = console result = self.wizard.ask("question", helper=lambda x: "say_help", autohelp=autohelp) assert result == "foo" if autohelp: assert "say_help" in self.wizard.console.log_text
def test_run_wizard_quit(self): console = MockConsole() self.wizard.console = console self.wizard._patched = True step = mock.Mock(func=mock.Mock(side_effect=QuitWizard()), display_name='Engine', store_name='engines', minimum=1, maximum=1) self.wizard.steps = [step] mock_ask_save = mock.Mock(return_value=False) with mock.patch.object(Wizard, '_ask_save', mock_ask_save): self.wizard.run_wizard() assert "Goodbye!" in self.wizard.console.log_text
def test_ask_enumerate(self, bad_choice): inputs = {False: '1', True: ['10', '1']}[bad_choice] console = MockConsole(inputs) self.wizard.console = console selected = self.wizard.ask_enumerate('foo', options=['bar', 'baz']) assert selected == 'bar' assert 'foo' in console.log_text assert '1. bar' in console.log_text assert '2. baz' in console.log_text assert console.input_call_count == len(inputs) if bad_choice: assert "'10'" in console.log_text assert 'not a valid option' in console.log_text else: assert "'10'" not in console.log_text assert 'not a valid option' not in console.log_text
def test_obj_selector(self, inputs, expected): create_func = lambda wiz: 'new' console = MockConsole(inputs) self.wizard.console = console self.wizard.cvs = {'foo': 'bar'} def mock_register(obj, name, store): console.print("registered") return obj self.wizard.register = mock_register sel = self.wizard.obj_selector('cvs', "CV", create_func) assert sel == expected assert "CV" in console.log_text if inputs == '2': assert "registered" in console.log_text
def test_ask_enumerate_dict(self, user_inp): inputs = { 'int': ["1"], 'str': ["bar"], 'help': ["?", "1"], "bad": ["99", "1"] }[user_inp] console = MockConsole(inputs) self.wizard.console = console selected = self.wizard.ask_enumerate_dict(question="foo", options={ "bar": "bar_func", "baz": "baz_func" }) assert selected == "bar_func" assert 'foo' in console.log_text assert '1. bar' in console.log_text assert '2. baz' in console.log_text assert console.input_call_count == len(inputs) if user_inp == 'help': assert "no help available" in console.log_text elif user_inp == "bad": assert "'99'" in console.log_text assert "not a valid option" in console.log_text
def test_ask(self): console = MockConsole('foo') self.wizard.console = console result = self.wizard.ask('bar') assert result == 'foo' assert 'bar' in console.log_text
def test_ask_load(self): console = MockConsole(['100']) self.wizard.console = console loaded = self.wizard.ask_load("foo", int) assert loaded == 100 assert "foo" in console.log_text
def _generic_speak_test(self, func_name): console = MockConsole() self.wizard.console = console func = getattr(self.wizard, func_name) func('foo') assert 'foo' in console.log_text
def _get_storage(self, inputs, expected): console = MockConsole(inputs) self.wizard.console = console storage = self.wizard.get_storage() assert storage.backend.filename == expected assert console.input_call_count == len(inputs)