Files
@ 369451c7c4f8
Branch filter:
Location: rattail-project/rattail/rattail/tests/commands/test_core.py
369451c7c4f8
7.7 KiB
text/x-python
OMG a ridiculous commit to overhaul import handler config etc.
- add `AppHandler.get_designated_import_handlers()` and friends
- exit w/ code 1 when command line makes no sense and must print help
- register all core import/export handlers via setup.py and config
- use "handler key" lookup for all core import/export commands
- stop displaying model list in help for import/export command
- migrate datasync "change" permissions, per tailbone changes
- log warning w/ traceback if loading entry point fails
- add `AppHandler.get_designated_import_handlers()` and friends
- exit w/ code 1 when command line makes no sense and must print help
- register all core import/export handlers via setup.py and config
- use "handler key" lookup for all core import/export commands
- stop displaying model list in help for import/export command
- migrate datasync "change" permissions, per tailbone changes
- log warning w/ traceback if loading entry point fails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | # -*- coding: utf-8; -*-
from __future__ import unicode_literals, absolute_import
import os
import csv
import datetime
import argparse
import shutil
import tempfile
from unittest import TestCase
from six import StringIO
import six
# from sqlalchemy import func
from mock import patch, Mock
from rattail.commands import core
from rattail.db import Session, model
from rattail.db.auth import authenticate_user
from rattail.tests import DataTestCase
class TestArgumentParser(TestCase):
def test_parse_args_preserves_extra_argv(self):
parser = core.ArgumentParser()
parser.add_argument('--some-optional-arg')
parser.add_argument('some_required_arg')
args = parser.parse_args([
'--some-optional-arg', 'optional-value', 'required-value',
'some', 'extra', 'args'])
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()
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')
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)
class TestSubcommand(TestCase):
def test_add_parser_args_does_nothing(self):
command = core.Command()
subcommand = core.Subcommand(command)
# Not sure this is really the way to test this, but...
self.assertEqual(len(subcommand.parser._action_groups[0]._actions), 1)
subcommand.add_parser_args(subcommand.parser)
self.assertEqual(len(subcommand.parser._action_groups[0]._actions), 1)
def test_run_not_implemented(self):
command = core.Command()
subcommand = core.Subcommand(command)
args = subcommand.parser.parse_args([])
self.assertRaises(NotImplementedError, subcommand.run, args)
# TODO: more broken tests..ugh. these aren't very good or else i might bother
# fixing them...
# class TestFileMonitor(TestCase):
# @patch('rattail.filemon.linux.start_daemon')
# def test_start_daemon_with_default_args(self, start_daemon):
# commands.main('filemon', '--no-init', 'start')
# start_daemon.assert_called_once_with(None, None, True)
# @patch('rattail.filemon.linux.start_daemon')
# def test_start_daemon_with_explicit_args(self, start_daemon):
# tmp = TempIO()
# pid_path = tmp.putfile('test.pid', '')
# commands.main('filemon', '--no-init', '--pidfile', pid_path, '--do-not-daemonize', 'start')
# start_daemon.assert_called_once_with(None, pid_path, False)
# @patch('rattail.filemon.linux.stop_daemon')
# def test_stop_daemon_with_default_args(self, stop_daemon):
# commands.main('filemon', '--no-init', 'stop')
# stop_daemon.assert_called_once_with(None, None)
# @patch('rattail.filemon.linux.stop_daemon')
# def test_stop_daemon_with_explicit_args(self, stop_daemon):
# tmp = TempIO()
# pid_path = tmp.putfile('test.pid', '')
# commands.main('filemon', '--no-init', '--pidfile', pid_path, 'stop')
# stop_daemon.assert_called_once_with(None, pid_path)
# @patch('rattail.commands.sys')
# def test_unknown_platform_not_supported(self, sys):
# tmp = TempIO()
# stderr_path = tmp.putfile('stderr.txt', '')
# sys.platform = 'bogus'
# commands.main('--no-init', '--stderr', stderr_path, 'filemon', 'start')
# sys.exit.assert_called_once_with(1)
# with open(stderr_path) as f:
# self.assertEqual(f.read(), "File monitor is not supported on platform: bogus\n")
|