def test_show_nonexisting_noraise(self): """Test showing a state that does not exist """ buf = io.StringIO() with stdout_redirector(buf): state.show() self.assertEqual(len(buf.getvalue()), 0)
def test_with_block_show(self): """Test with-block of the StateHandler does not cause show to fail """ with tempfile.NamedTemporaryFile() as temp: state.reset_instance() state['a'] = 12 with StateHandler(temp.name, load=True): self.assertEqual(len(state), 1) self.assertEqual(state['a'], 12) buf = io.StringIO() with stdout_redirector(buf): state.show() self.assertNotEqual(len(buf.getvalue()), 0) self.assertRegexpMatches(buf.getvalue(), r"a:12")
def test_show(self): """Test showing the state """ state['a.b'] = 12 state['bla.bli'] = 13 buf = io.StringIO() with stdout_redirector(buf): state.show() self.assertNotEqual(len(buf.getvalue()), 0) self.assertRegexpMatches(buf.getvalue(), r"\[a\]") self.assertRegexpMatches(buf.getvalue(), r"\[bla\]") self.assertRegexpMatches(buf.getvalue(), r"b") self.assertRegexpMatches(buf.getvalue(), r"bli") self.assertRegexpMatches(buf.getvalue(), r"12") self.assertRegexpMatches(buf.getvalue(), r"13")
def show_state(*arguments): """Shows the contents of the state loaded by the configuration or from the file specified as an argument. """ if len(arguments) == 0: state_file = conf['pyexperiment.state_filename'] else: state_file = arguments[0] print_bold("Load state from file '%s'", state_file) try: state.load(state_file, lazy=False, raise_error=True) except IOError as err: print(err) else: if len(state) > 0: state.show() else: print("State empty")
def test_show_lazy(self): """Test showing the state lazily loaded """ state['a.b'] = 12 buf = io.StringIO() with tempfile.NamedTemporaryFile() as temp: state.save(temp.name) state['a.b'] = 13 state.load(temp.name, lazy=True) with stdout_redirector(buf): state.show() self.assertNotEqual(len(buf.getvalue()), 0) self.assertRegexpMatches(buf.getvalue(), r"\[a\]") self.assertRegexpMatches(buf.getvalue(), r"b") self.assertRegexpMatches(buf.getvalue(), r"12") if six.PY2: self.assertNotRegexpMatches(buf.getvalue(), r"13") elif six.PY3: self.assertNotRegex( # pylint: disable=E1101 buf.getvalue(), r"13") else: raise RuntimeError("Python version not supported")