Changeset - b639ce7e4309
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 years ago 2021-12-06 21:15:45
lance@edbob.org
Fix some tests
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rattail/tests/commands/test_core.py
Show inline comments
 
@@ -33,121 +33,121 @@ class TestArgumentParser(TestCase):
 
        self.assertEqual(args.some_required_arg, 'required-value')
 
        self.assertEqual(args.some_optional_arg, 'optional-value')
 
        self.assertEqual(args.argv, ['some', 'extra', 'args'])
 

	
 

	
 
class TestDateArgument(TestCase):
 

	
 
    def test_valid_date_string_returns_date_object(self):
 
        date = core.date_argument('2014-01-01')
 
        self.assertEqual(date, datetime.date(2014, 1, 1))
 

	
 
    def test_invalid_date_string_raises_error(self):
 
        self.assertRaises(argparse.ArgumentTypeError, core.date_argument, 'invalid-date')
 

	
 

	
 
class TestCommand(TestCase):
 

	
 
    def test_initial_subcommands_are_sane(self):
 
        command = core.Command()
 
        self.assertTrue('filemon' in command.subcommands)
 

	
 
    def test_unicode(self):
 
        command = core.Command()
 
        command.name = 'some-app'
 
        self.assertEqual(six.text_type(command), 'some-app')
 
        
 
    def test_iter_subcommands_includes_expected_item(self):
 
        command = core.Command()
 
        found = False
 
        for subcommand in command.iter_subcommands():
 
            if subcommand.name == 'filemon':
 
                found = True
 
                break
 
        self.assertTrue(found)
 

	
 
    def test_print_help(self):
 
        command = core.Command()
 
        stdout = StringIO()
 
        command.stdout = stdout
 
        command.print_help()
 
        output = stdout.getvalue()
 
        stdout.close()
 
        self.assertTrue('Usage:' in output)
 
        self.assertTrue('Options:' in output)
 

	
 
    def test_run_with_no_args_prints_help(self):
 
        command = core.Command()
 
        with patch.object(command, 'print_help') as print_help:
 
            command.run()
 
            self.assertRaises(SystemExit, command.run)
 
            print_help.assert_called_once_with()
 

	
 
    def test_run_with_single_help_arg_prints_help(self):
 
        command = core.Command()
 
        with patch.object(command, 'print_help') as print_help:
 
            command.run('help')
 
            print_help.assert_called_once_with()
 

	
 
    def test_run_with_help_and_unknown_subcommand_args_prints_help(self):
 
        command = core.Command()
 
        with patch.object(command, 'print_help') as print_help:
 
            command.run('help', 'invalid-subcommand-name')
 
            print_help.assert_called_once_with()
 

	
 
    def test_run_with_help_and_subcommand_args_prints_subcommand_help(self):
 
        command = core.Command()
 
        fake = command.subcommands['fake'] = Mock()
 
        command.run('help', 'fake')
 
        fake.return_value.parser.print_help.assert_called_once_with()
 

	
 
    def test_run_with_unknown_subcommand_arg_prints_help(self):
 
        command = core.Command()
 
        with patch.object(command, 'print_help') as print_help:
 
            command.run('invalid-command-name')
 
            self.assertRaises(SystemExit, command.run, 'invalid-command-name')
 
            print_help.assert_called_once_with()
 

	
 
    def test_stdout_may_be_redirected(self):
 
        class Fake(core.Subcommand):
 
            def run(self, args):
 
                self.stdout.write("standard output stuff")
 
                self.stdout.flush()
 
        command = core.Command()
 
        fake = command.subcommands['fake'] = Fake
 
        tempdir = tempfile.mkdtemp()
 
        config_path = os.path.join(tempdir, 'test.ini')
 
        with open(config_path, 'wt') as f:
 
            f.write('')
 
        out_path = os.path.join(tempdir, 'out.txt')
 
        with open(out_path, 'wt') as f:
 
            f.write('')
 
        command.run('fake', '--config', config_path, '--stdout', out_path)
 
        with open(out_path) as f:
 
            self.assertEqual(f.read(), "standard output stuff")
 
        shutil.rmtree(tempdir)
 

	
 
    def test_stderr_may_be_redirected(self):
 
        class Fake(core.Subcommand):
 
            def run(self, args):
 
                self.stderr.write("standard error stuff")
 
                self.stderr.flush()
 
        command = core.Command()
 
        fake = command.subcommands['fake'] = Fake
 
        tempdir = tempfile.mkdtemp()
 
        config_path = os.path.join(tempdir, 'test.ini')
 
        with open(config_path, 'wt') as f:
 
            f.write('')
 
        err_path = os.path.join(tempdir, 'err.txt')
 
        with open(err_path, 'wt') as f:
 
            f.write('')
 
        command.run('fake', '--config', config_path, '--stderr', err_path)
 
        with open(err_path) as f:
 
            self.assertEqual(f.read(), "standard error stuff")
 
        shutil.rmtree(tempdir)
 

	
 
    # # TODO: Figure out a better way to test this, or don't bother.
 
    # def test_noinit_flag_means_no_config(self):
 
    #     command = commands.Command()
 
    #     fake = command.subcommands['fake'] = Mock()
 
    #     command.run('fake', '--no-init')
 
    #     self.assertEqual(len(fake.return_value.config.files_requested), 0)
 

	
 

	
0 comments (0 inline, 0 general)