Files
@ cb0cb5abcb0e
Branch filter:
Location: rattail-project/rattail/tests/test_commands.py
cb0cb5abcb0e
16.1 KiB
text/x-python
Remove some more edbob references.
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
import csv
import datetime
import argparse
import logging
from unittest import TestCase
from cStringIO import StringIO
from mock import patch, Mock
from fixture import TempIO
from sqlalchemy import create_engine
from sqlalchemy import func
from . import DataTestCase
from rattail import commands
from rattail.db import Session
from rattail.db import model
from rattail.db.auth import authenticate_user
class TestArgumentParser(TestCase):
def test_parse_args_preserves_extra_argv(self):
parser = commands.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 = commands.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, commands.date_argument, 'invalid-date')
class TestCommand(TestCase):
def test_initial_subcommands_are_sane(self):
command = commands.Command()
self.assertTrue('filemon' in command.subcommands)
def test_unicode(self):
command = commands.Command()
command.name = 'some-app'
self.assertEqual(unicode(command), u'some-app')
def test_iter_subcommands_includes_expected_item(self):
command = commands.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 = commands.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 = commands.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 = commands.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 = commands.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 = commands.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 = commands.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(commands.Subcommand):
def run(self, args):
self.stdout.write("standard output stuff")
self.stdout.flush()
command = commands.Command()
fake = command.subcommands['fake'] = Fake
tmp = TempIO()
config_path = tmp.putfile('test.ini', '')
out_path = tmp.putfile('out.txt', '')
command.run('fake', '--config', config_path, '--stdout', out_path)
with open(out_path) as f:
self.assertEqual(f.read(), "standard output stuff")
def test_stderr_may_be_redirected(self):
class Fake(commands.Subcommand):
def run(self, args):
self.stderr.write("standard error stuff")
self.stderr.flush()
command = commands.Command()
fake = command.subcommands['fake'] = Fake
tmp = TempIO()
config_path = tmp.putfile('test.ini', '')
err_path = tmp.putfile('err.txt', '')
command.run('fake', '--config', config_path, '--stderr', err_path)
with open(err_path) as f:
self.assertEqual(f.read(), "standard error stuff")
def test_verbose_flag_sets_root_logging_level_to_info(self):
self.assertEqual(logging.getLogger().getEffectiveLevel(), logging.NOTSET)
tmp = TempIO()
config_path = tmp.putfile('test.ini', '')
command = commands.Command()
fake = command.subcommands['fake'] = Mock()
command.run('fake', '--config', config_path, '--verbose')
self.assertEqual(logging.getLogger().getEffectiveLevel(), logging.INFO)
def test_debug_flag_sets_root_logging_level_to_debug(self):
self.assertEqual(logging.getLogger().getEffectiveLevel(), logging.NOTSET)
tmp = TempIO()
config_path = tmp.putfile('test.ini', '')
command = commands.Command()
fake = command.subcommands['fake'] = Mock()
command.run('fake', '--config', config_path, '--debug')
self.assertEqual(logging.getLogger().getEffectiveLevel(), logging.DEBUG)
def test_noinit_flag_means_no_config(self):
command = commands.Command()
fake = command.subcommands['fake'] = Mock()
command.run('fake', '--no-init')
self.assertTrue(fake.return_value.config is None)
class TestSubcommand(TestCase):
def test_repr(self):
command = commands.Command()
subcommand = commands.Subcommand(command)
subcommand.name = 'fake-command'
self.assertEqual(repr(subcommand), "Subcommand(name='fake-command')")
def test_add_parser_args_does_nothing(self):
command = commands.Command()
subcommand = commands.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 = commands.Command()
subcommand = commands.Subcommand(command)
args = subcommand.parser.parse_args([])
self.assertRaises(NotImplementedError, subcommand.run, args)
class TestAddUser(DataTestCase):
def setUp(self):
super(TestAddUser, self).setUp()
self.tmp = TempIO()
self.stdout_path = self.tmp.putfile('stdout.txt', '')
self.stderr_path = self.tmp.putfile('stderr.txt', '')
def test_no_user_created_if_username_already_exists(self):
self.session.add(model.User(username='fred'))
self.session.commit()
self.assertEqual(self.session.query(model.User).count(), 1)
commands.main('adduser', '--no-init', '--stderr', self.stderr_path, 'fred')
with open(self.stderr_path) as f:
self.assertEqual(f.read(), "User 'fred' already exists.\n")
self.assertEqual(self.session.query(model.User).count(), 1)
def test_no_user_created_if_password_prompt_is_canceled(self):
self.assertEqual(self.session.query(model.User).count(), 0)
with patch('rattail.commands.getpass') as getpass:
getpass.side_effect = KeyboardInterrupt
commands.main('adduser', '--no-init', '--stderr', self.stderr_path, 'fred')
with open(self.stderr_path) as f:
self.assertEqual(f.read(), "\nOperation was canceled.\n")
self.assertEqual(self.session.query(model.User).count(), 0)
def test_normal_user_created_with_correct_password_but_no_admin_role(self):
self.assertEqual(self.session.query(model.User).count(), 0)
with patch('rattail.commands.getpass') as getpass:
getpass.return_value = 'fredpass'
commands.main('adduser', '--no-init', '--stdout', self.stdout_path, 'fred')
with open(self.stdout_path) as f:
self.assertEqual(f.read(), "Created user: fred\n")
fred = self.session.query(model.User).one()
self.assertEqual(fred.username, 'fred')
self.assertEqual(len(fred.roles), 0)
user = authenticate_user(self.session, 'fred', 'fredpass')
self.assertTrue(user is fred)
def test_admin_user_created_with_administrator_role(self):
self.assertEqual(self.session.query(model.User).count(), 0)
with patch('rattail.commands.getpass') as getpass:
getpass.return_value = 'fredpass'
commands.main('adduser', '--no-init', '--stdout', self.stdout_path, 'fred', '--administrator')
fred = self.session.query(model.User).one()
self.assertEqual(len(fred.roles), 1)
self.assertEqual(fred.roles[0].name, 'Administrator')
class TestDatabaseSync(TestCase):
@patch('rattail.db.sync.linux.start_daemon')
def test_start_daemon_with_default_args(self, start_daemon):
commands.main('dbsync', '--no-init', 'start')
start_daemon.assert_called_once_with(None, None, True)
@patch('rattail.db.sync.linux.start_daemon')
def test_start_daemon_with_explicit_args(self, start_daemon):
tmp = TempIO()
pid_path = tmp.putfile('test.pid', '')
commands.main('dbsync', '--no-init', '--pidfile', pid_path, '--do-not-daemonize', 'start')
start_daemon.assert_called_once_with(None, pid_path, False)
@patch('rattail.db.sync.linux.start_daemon')
def test_keyboard_interrupt_raises_error_when_daemonized(self, start_daemon):
start_daemon.side_effect = KeyboardInterrupt
self.assertRaises(KeyboardInterrupt, commands.main, 'dbsync', '--no-init', 'start')
@patch('rattail.db.sync.linux.start_daemon')
def test_keyboard_interrupt_handled_gracefully_when_not_daemonized(self, start_daemon):
tmp = TempIO()
stderr_path = tmp.putfile('stderr.txt', '')
start_daemon.side_effect = KeyboardInterrupt
commands.main('dbsync', '--no-init', '--stderr', stderr_path, '--do-not-daemonize', 'start')
with open(stderr_path) as f:
self.assertEqual(f.read(), "Interrupted.\n")
@patch('rattail.db.sync.linux.stop_daemon')
def test_stop_daemon_with_default_args(self, stop_daemon):
commands.main('dbsync', '--no-init', 'stop')
stop_daemon.assert_called_once_with(None, None)
@patch('rattail.db.sync.linux.stop_daemon')
def test_stop_daemon_with_explicit_args(self, stop_daemon):
tmp = TempIO()
pid_path = tmp.putfile('test.pid', '')
commands.main('dbsync', '--no-init', '--pidfile', pid_path, 'stop')
stop_daemon.assert_called_once_with(None, pid_path)
class TestDump(DataTestCase):
def setUp(self):
super(TestDump, self).setUp()
self.session.add(model.Product(upc='074305001321'))
self.session.add(model.Product(upc='074305001161'))
self.session.commit()
def test_unknown_model_cannot_be_dumped(self):
tmp = TempIO()
stderr_path = tmp.putfile('stderr.txt', '')
self.assertRaises(SystemExit, commands.main, '--no-init', '--stderr', stderr_path, 'dump', 'NoSuchModel')
with open(stderr_path) as f:
self.assertEqual(f.read(), "Unknown model: NoSuchModel\n")
def test_dump_goes_to_stdout_by_default(self):
tmp = TempIO()
stdout_path = tmp.putfile('stdout.txt', '')
commands.main('--no-init', '--stdout', stdout_path, 'dump', 'Product')
with open(stdout_path, 'rb') as csv_file:
reader = csv.DictReader(csv_file)
upcs = [row['upc'] for row in reader]
self.assertEqual(len(upcs), 2)
self.assertTrue('00074305001321' in upcs)
self.assertTrue('00074305001161' in upcs)
def test_dump_goes_to_file_if_so_invoked(self):
tmp = TempIO()
output_path = tmp.putfile('output.txt', '')
commands.main('--no-init', 'dump', 'Product', '--output', output_path)
with open(output_path, 'rb') as csv_file:
reader = csv.DictReader(csv_file)
upcs = [row['upc'] for row in reader]
self.assertEqual(len(upcs), 2)
self.assertTrue('00074305001321' in upcs)
self.assertTrue('00074305001161' in upcs)
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")
# # TODO: The purge-batches command tests don't work yet; the db.batches.util
# # tests need to be figured out first...
# class TestPurgeBatches(DataTestCase):
# def setUp(self):
# super(TestPurgeBatches, self).setUp()
# self.session.add(model.Batch(purge=datetime.date(2014, 1, 1)))
# self.session.add(model.Batch(purge=datetime.date(2014, 2, 1)))
# self.session.add(model.Batch(purge=datetime.date(2014, 3, 1)))
# self.session.commit()
# self.tmp = TempIO()
# self.stdout_path = self.tmp.putfile('stdout.txt', '')
# def test_purging_honors_batch_purge_dates(self):
# self.assertEqual(self.session.query(model.Batch).count(), 3)
# commands.main('--no-init', '--stdout', self.stdout_path, 'purge-batches', '--date', '2014-01-15')
# self.assertEqual(self.session.query(model.Batch).count(), 2)
# self.assertEqual(self.session.query(func.min(model.Batch.purge)).scalar(), datetime.date(2014, 2, 1))
# with open(self.stdout_path) as f:
# self.assertTrue(f.read().endswith("\nPurged 1 normal and 0 orphaned batches.\n"))
# def test_specifying_all_purges_everything(self):
# self.assertEqual(self.session.query(model.Batch).count(), 3)
# commands.main('--no-init', '--stdout', self.stdout_path, 'purge-batches', '--all')
# self.assertEqual(self.session.query(model.Batch).count(), 0)
# with open(self.stdout_path) as f:
# self.assertTrue(f.read().endswith("\nPurged 3 normal and 0 orphaned batches.\n"))
# def test_orphaned_tables_are_also_purged(self):
# self.session.delete(self.session.query(model.Batch).first())
# self.session.commit()
# self.assertEqual(self.session.query(model.Batch).count(), 2)
# commands.main('--no-init', '--stdout', self.stdout_path, 'purge-batches', '--date', '2013-12-31')
# self.assertEqual(self.session.query(model.Batch).count(), 2)
# with open(self.stdout_path) as f:
# self.assertTrue(f.read().endswith("\nPurged 0 normal and 1 orphaned batches.\n"))
|