Esempio n. 1
0
    def test_main_overrides_verbosity(self):
        """Test running main called with --verbosity works as expected
        """
        called = [False]

        def foo_fun():
            """Foo function
            """
            called[0] = True

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--verbosity", "DEBUG", "foo_fun"]

        self.assertFalse(called[0])

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.verbosity'], 'DEBUG')

        called[0] = False
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--verbosity", "WARNING", "foo_fun"]

        self.assertFalse(called[0])

        experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.verbosity'], 'WARNING')
Esempio n. 2
0
    def test_main_runs_other_function(self):
        """Test running main with default command and other function
        """
        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test",
            "custom_function2"]

        run = [False, False]

        def custom_function():
            """User function
            """
            run[0] = True

        def custom_function2():
            """User function2
            """
            run[1] = True

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(default=custom_function,
                            commands=[custom_function2])

        self.assertEqual(len(buf.getvalue()), 0)
        self.assertFalse(run[0])
        self.assertTrue(run[1])
Esempio n. 3
0
    def test_main_shows_commands(self):
        """Test running main shows commands
        """
        def default_function():
            """Default function
            """
            pass

        def custom_function1():
            """User function
            """
            pass

        def custom_function2():
            """User function
            """
            pass

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_commands"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(default=default_function,
                            commands=[custom_function1, custom_function2])

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r"default_function")
        self.assertRegexpMatches(buf.getvalue(), r"custom_function1")
        self.assertRegexpMatches(buf.getvalue(), r"custom_function2")
Esempio n. 4
0
    def test_print_timings_correct(self):
        """Test timing is about right
        """
        buf = io.StringIO()

        # Nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        log.initialize()
        # Still, nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        for _ in range(3):
            with log.timed("Foo", level=logging.FATAL):
                time.sleep(0.01)

        with stdout_redirector(buf):
            log.print_timings()

        # Should print correct stats
        self.assertRegexpMatches(buf.getvalue(), r'\'Foo\'')
        self.assertRegexpMatches(buf.getvalue(), r'3 times')
        self.assertRegexpMatches(buf.getvalue(), r'total = 0.03')
        self.assertRegexpMatches(buf.getvalue(), r'median = 0.01')

        log.close()
        # Correct timings should be logged three times
        self.assertRegexpMatches(self.log_stream.getvalue(), r'Foo')
        self.assertEqual(len(re.findall(r'Foo',
                                        self.log_stream.getvalue())), 3)
        self.assertRegexpMatches(self.log_stream.getvalue(), r'took 0.01')
        self.assertEqual(len(re.findall(r'took 0.01',
                                        self.log_stream.getvalue())), 3)
Esempio n. 5
0
    def test_example_messsage_with_args(self):
        """Make sure print_examples interpolates message with arguments
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            printers.print_examples("Foo %d", 3 + 4)

        self.assertRegexpMatches(buf.getvalue(), r".*%s.*" % "Foo 7")
Esempio n. 6
0
    def test_examples_print_something(self):
        """Make sure calling print_examples prints something
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            printers.print_examples()

        self.assertTrue(len(buf.getvalue()) > 0)
Esempio n. 7
0
    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)
Esempio n. 8
0
    def test_reset_seqence_appears(self):
        """Test printing actually produces the reset sequence
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer("foo")

        # We will get the assertion later (by  dependency injection)
        self.assertRegexpMatches(buf.getvalue(), "%s" % re.escape(printers.RESET_SEQ))  # pylint: disable=no-member
Esempio n. 9
0
    def test_message_sequence_appears(self):
        """Test printing actually prints the message
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer("foo")

        # We will get the assertion later (by dependency injection)
        self.assertRegexpMatches(buf.getvalue(), "foo")  # pylint: disable=no-member
    def test_captures_stdout(self):
        """Test capturing stdout from print
        """
        message = "This should be captured..."

        buf = io.StringIO()
        with stdout_redirector(buf):
            print(message)

        self.assertEqual(buf.getvalue(), message + '\n')
Esempio n. 11
0
    def test_reset_seqence_appears(self):
        """Test printing actually produces the reset sequence
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer("foo")

        # We will get the assertion later (by  dependency injection)
        self.assertRegexpMatches(  # pylint: disable=no-member
            buf.getvalue(), "%s" % re.escape(printers.RESET_SEQ))
Esempio n. 12
0
    def test_message_interpolates_args(self):
        """Test printing actually interpolates the arguments correctly
        """
        message = "str: %s, int: %d, float %f"
        arguments = ("bla", 12, 3.14)
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer(message, *arguments)

        # We will get the assertion later (by dependency injection)
        self.assertRegexpMatches(buf.getvalue(), r".*%s.*" % (message % arguments))  # pylint: disable=no-member
Esempio n. 13
0
    def test_color_sequence_appears(self):
        """Test printing actually produces the color sequence
        """
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer("foo")

        # We will get the assertion later (by dependency injection)
        # pylint: disable=no-member
        self.assertIsNotNone(self.COLOR_SEQ)
        self.assertRegexpMatches(buf.getvalue(),
                                 "%s" % re.escape(self.COLOR_SEQ))
Esempio n. 14
0
    def test_message_interpolates_args(self):
        """Test printing actually interpolates the arguments correctly
        """
        message = "str: %s, int: %d, float %f"
        arguments = ('bla', 12, 3.14)
        buf = io.StringIO()
        with stdout_redirector(buf):
            self.printer(message, *arguments)

        # We will get the assertion later (by dependency injection)
        self.assertRegexpMatches(  # pylint: disable=no-member
            buf.getvalue(), r'.*%s.*' % (message % arguments))
Esempio n. 15
0
    def test_main_shows_config(self):
        """Test running main shows the configuration
        """
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_config"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main()
        self.assertRegexpMatches(buf.getvalue(), r"\[pyexperiment\]")
        self.assertRegexpMatches(buf.getvalue(), r"n_processes")
Esempio n. 16
0
    def test_main_shows_no_test(self):
        """Test running main complains if there are no tests
        """
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_tests"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(tests=[])

        self.assertRegexpMatches(buf.getvalue(), r"No tests available")
Esempio n. 17
0
    def test_main_not_enough_arguments(self):
        """Test running main without command
        """
        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main()

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r"[Nn]ot enough arguments")
    def test_main_shows_config(self):
        """Test running main shows the configuration
        """
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_config"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main()
        self.assertRegexpMatches(buf.getvalue(), r"\[pyexperiment\]")
        self.assertRegexpMatches(buf.getvalue(), r"n_processes")
    def test_main_shows_no_test(self):
        """Test running main complains if there are no tests
        """
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_tests"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(tests=[])

        self.assertRegexpMatches(buf.getvalue(), r"No tests available")
    def test_main_not_enough_arguments(self):
        """Test running main without command
        """
        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main()

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r"[Nn]ot enough arguments")
Esempio n. 21
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")
Esempio n. 22
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")
Esempio n. 23
0
    def test_main_shows_other_state(self):
        """Test running main shows state from file
        """
        with tempfile.NamedTemporaryFile() as temp:
            state['foo'] = 42
            state.save(temp.name)

            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "show_state", temp.name]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main()
            self.assertRegexpMatches(buf.getvalue(), r"foo")
            self.assertRegexpMatches(buf.getvalue(), r"42")
Esempio n. 24
0
    def test_main_shows_test(self):
        """Test running main shows tests when needed
        """
        class ExampleTest(unittest.TestCase):
            """Test case for the test
            """
            pass

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_tests"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(tests=[ExampleTest])

        self.assertRegexpMatches(buf.getvalue(), r"ExampleTest")
Esempio n. 25
0
    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 test_main_shows_other_state(self):
        """Test running main shows state from file
        """
        with tempfile.NamedTemporaryFile() as temp:
            state['foo'] = 42
            state.save(temp.name)

            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "show_state", temp.name
            ]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main()
            self.assertRegexpMatches(buf.getvalue(), r"foo")
            self.assertRegexpMatches(buf.getvalue(), r"42")
Esempio n. 27
0
    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")
Esempio n. 28
0
    def test_main_prints_timings_simple(self):
        """Test running main logs timings as expected with --print_timings
        """
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--print-timings"]

        def hello():
            """Logs a message
            """
            with log.timed("bla"):
                pass

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(default=hello)

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r'bla')
Esempio n. 29
0
    def test_main_prints_result(self):
        """Test running main prints the result of a function
        """
        def custom_function():
            """User function
            """
            return "Foo"

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "custom_function"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r'Foo')
Esempio n. 30
0
    def test_main_saves_config(self):
        """Test running main saves the configuration
        """
        with tempfile.NamedTemporaryFile() as temp:
            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "save_config", temp.name]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main()

            lines = open(temp.name).readlines()
            self.assertNotEqual(len(lines), 0)
            self.assertRegexpMatches("".join(lines), r"\[pyexperiment\]")
            self.assertRegexpMatches("".join(lines), r"n_processes")

            self.assertRegexpMatches(buf.getvalue(), r'Wrote configuration')
    def test_main_shows_test(self):
        """Test running main shows tests when needed
        """
        class ExampleTest(unittest.TestCase):
            """Test case for the test
            """
            pass

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "show_tests"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(tests=[ExampleTest])

        self.assertRegexpMatches(buf.getvalue(), r"ExampleTest")
Esempio n. 32
0
    def test_main_shows_default_state(self):
        """Test running main shows the default state
        """
        with tempfile.NamedTemporaryFile() as temp:
            state['bla'] = 12
            state.save(temp.name)

            spec = ('[pyexperiment]\n'
                    'state_filename = string(default=%s)' % temp.name)
            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "show_state"]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main(config_spec=spec)
            self.assertRegexpMatches(buf.getvalue(), r"bla")
            self.assertRegexpMatches(buf.getvalue(), r"12")
    def test_main_shows_default_state(self):
        """Test running main shows the default state
        """
        with tempfile.NamedTemporaryFile() as temp:
            state['bla'] = 12
            state.save(temp.name)

            spec = ('[pyexperiment]\n'
                    'state_filename = string(default=%s)' % temp.name)
            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "show_state"
            ]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main(config_spec=spec)
            self.assertRegexpMatches(buf.getvalue(), r"bla")
            self.assertRegexpMatches(buf.getvalue(), r"12")
    def test_main_prints_timings_simple(self):
        """Test running main logs timings as expected with --print_timings
        """
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--print-timings"
        ]

        def hello():
            """Logs a message
            """
            with log.timed("bla"):
                pass

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(default=hello)

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r'bla')
    def test_main_prints_result(self):
        """Test running main prints the result of a function
        """
        def custom_function():
            """User function
            """
            return "Foo"

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "custom_function"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r'Foo')
Esempio n. 36
0
    def test_main_complains_on_help(self):
        """Test running help complains on help for wrong command
        """
        def custom_function():
            """Foo function
            """
            pass

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help", "foo"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertRegexpMatches(buf.getvalue(), r"[cC]ommand")
        self.assertRegexpMatches(buf.getvalue(), r"not")
        self.assertRegexpMatches(buf.getvalue(), r"foo")
    def test_main_saves_config(self):
        """Test running main saves the configuration
        """
        with tempfile.NamedTemporaryFile() as temp:
            # Monkey patch arg parser
            argparse._sys.argv = [  # pylint: disable=W0212
                "test", "save_config", temp.name
            ]

            buf = io.StringIO()
            with stdout_redirector(buf):
                experiment.main()

            lines = open(temp.name).readlines()
            self.assertNotEqual(len(lines), 0)
            self.assertRegexpMatches("".join(lines), r"\[pyexperiment\]")
            self.assertRegexpMatches("".join(lines), r"n_processes")

            self.assertRegexpMatches(buf.getvalue(), r'Wrote configuration')
Esempio n. 38
0
    def test_main_no_processes_long(self):
        """Test running main called with --processes works as expected
        """
        called = [False]

        def foo_fun():
            """Foo function
            """
            called[0] = True
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--processes", "44", "foo_fun"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.n_processes'],
                         44)
    def test_main_complains_on_help(self):
        """Test running help complains on help for wrong command
        """
        def custom_function():
            """Foo function
            """
            pass

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help", "foo"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertRegexpMatches(buf.getvalue(), r"[cC]ommand")
        self.assertRegexpMatches(buf.getvalue(), r"not")
        self.assertRegexpMatches(buf.getvalue(), r"foo")
Esempio n. 40
0
    def test_main_does_not_run_function(self):
        """Test running main does not call unnecessary function but complains
        """
        run = [False]

        def custom_function():
            """User function
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertFalse(run[0])
        self.assertNotEqual(len(buf.getvalue()), 0)
Esempio n. 41
0
    def test_main_gives_help(self):
        """Test running help shows docstring
        """
        run = [False]

        def custom_function():
            """This should be printed!!
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help", "custom_function"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertFalse(run[0])
        self.assertIn("This should be printed!!", buf.getvalue())
Esempio n. 42
0
    def test_main_runs_function(self):
        """Test running main calls function
        """
        run = [False]

        def custom_function():
            """User function
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "custom_function"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertTrue(run[0])
        self.assertEqual(len(buf.getvalue()), 0)
Esempio n. 43
0
    def test_main_doesnt_test_on_help(self):
        """Test running main does not call tests when not needed
        """
        class ExampleTest(unittest.TestCase):
            """Test case for the test
            """
            pass

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "-h"]

        buf = io.StringIO()
        with stdout_redirector(buf):
            with mock.patch.object(unittest, 'TextTestRunner') as mock_method:
                try:
                    experiment.main(commands=[], tests=[ExampleTest])
                    self.assertEqual(mock_method.call_count, 0)
                except SystemExit:
                    pass
    def test_main_does_not_run_function(self):
        """Test running main does not call unnecessary function but complains
        """
        run = [False]

        def custom_function():
            """User function
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertFalse(run[0])
        self.assertNotEqual(len(buf.getvalue()), 0)
    def test_main_gives_help(self):
        """Test running help shows docstring
        """
        run = [False]

        def custom_function():
            """This should be printed!!
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "help", "custom_function"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertFalse(run[0])
        self.assertIn("This should be printed!!", buf.getvalue())
    def test_main_runs_function(self):
        """Test running main calls function
        """
        run = [False]

        def custom_function():
            """User function
            """
            run[0] = True

        # Monkey patch arg parser here
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "custom_function"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[custom_function])

        self.assertTrue(run[0])
        self.assertEqual(len(buf.getvalue()), 0)
    def test_main_doesnt_test_on_help(self):
        """Test running main does not call tests when not needed
        """
        class ExampleTest(unittest.TestCase):
            """Test case for the test
            """
            pass

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "-h"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            with mock.patch.object(unittest, 'TextTestRunner') as mock_method:
                try:
                    experiment.main(commands=[], tests=[ExampleTest])
                    self.assertEqual(mock_method.call_count, 0)
                except SystemExit:
                    pass
    def test_main_no_processes_long(self):
        """Test running main called with --processes works as expected
        """
        called = [False]

        def foo_fun():
            """Foo function
            """
            called[0] = True

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--processes", "44", "foo_fun"
        ]

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.n_processes'], 44)
Esempio n. 49
0
    def test_print_timings_complains(self):
        """Test timing code complains if there are no timings
        """
        buf = io.StringIO()

        # Nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        log.initialize()
        # Still, nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        with stdout_redirector(buf):
            log.print_timings()

        # Something should be printed
        self.assertNotEqual(len(buf.getvalue()), 0)
        self.assertRegexpMatches(buf.getvalue(), r'No timings stored')

        log.close()

        # Nothing should be logged
        self.assertEqual(len(self.log_stream.getvalue()), 0)
Esempio n. 50
0
    def test_print_timings_prints(self):
        """Test timing code and printing really prints a message
        """
        buf = io.StringIO()

        # Nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        log.initialize()
        # Still, nothing should be logged yet
        self.assertEqual(len(self.log_stream.getvalue()), 0)

        with log.timed(level=logging.FATAL):
            _ = 1 + 1

        with stdout_redirector(buf):
            log.print_timings()

        # Something should be printed
        self.assertNotEqual(len(buf.getvalue()), 0)

        log.close()
        # Something should be logged
        self.assertNotEqual(len(self.log_stream.getvalue()), 0)
Esempio n. 51
0
    def test_main_overrides_option(self):
        """Test running main called with -o works as expected
        """
        called = [False]

        def foo_fun():
            """Foo function
            """
            called[0] = True
            self.assertEqual(conf['bla'], 'foo')

        conf['bla'] = 'bla'
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "-o", "bla", "foo", "foo_fun"]

        self.assertFalse(called[0])

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['bla'], 'foo')
    def test_main_overrides_verbosity(self):
        """Test running main called with --verbosity works as expected
        """
        called = [False]

        def foo_fun():
            """Foo function
            """
            called[0] = True

        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--verbosity", "DEBUG", "foo_fun"
        ]

        self.assertFalse(called[0])

        buf = io.StringIO()
        with stdout_redirector(buf):
            experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.verbosity'], 'DEBUG')

        called[0] = False
        # Monkey patch arg parser
        argparse._sys.argv = [  # pylint: disable=W0212
            "test", "--verbosity", "WARNING", "foo_fun"
        ]

        self.assertFalse(called[0])

        experiment.main(commands=[foo_fun])

        self.assertTrue(called[0])
        self.assertEqual(conf['pyexperiment.verbosity'], 'WARNING')
Esempio n. 53
0
    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")