Changeset - 352d16868760
[Not reviewed]
0 2 0
Lance Edgar - 9 years ago 2016-02-27 11:05:34
ledgar@sacfoodcoop.com
Fix some tests.
2 files changed with 19 insertions and 24 deletions:
0 comments (0 inline, 0 general)
rattail/exceptions.py
Show inline comments
 
@@ -17,35 +17,32 @@
 
#  more details.
 
#
 
#  You should have received a copy of the GNU Affero General Public License
 
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
**Exceptions**
 

	
 
This module contains all core Rattail exception classes.
 
"""
 

	
 
from __future__ import unicode_literals
 
from __future__ import unicode_literals, absolute_import
 

	
 

	
 
class RattailError(Exception):
 
    """
 
    Base class for all Rattail exceptions.
 
    """
 

	
 
    def __str__(self):
 
        return unicode(self).encode('utf_8')
 

	
 

	
 
class ConfigurationError(RattailError):
 
    """
 
    Generic class for configuration errors.
 
    """
 

	
 

	
 
class SQLAlchemyNotInstalled(RattailError):
 
    """
 
    Error raised when an operation is requested which requires SQLAlchemy
 
    (and/or related libraries) to be installed, but they are not available.
 
    """
 
@@ -109,96 +106,93 @@ class FileOperationError(RattailError):
 

	
 
class PathNotFound(FileOperationError):
 
    """
 
    Raised when "path not found" errors are encountered within the
 
    :func:`rattail.files.locking_copy_test()` function.  The purpose of this is
 
    to normalize these errors to a single type, since the file monitor retry
 
    mechanism will fail if two distinct exceptions are encountered during its
 
    processing attempts.
 
    """
 
    def __init__(self, original_error):
 
        self.original_error = original_error
 

	
 
    def __str__(self):
 
        return unicode(self).encode('utf_8')
 

	
 
    def __unicode__(self):
 
        return '{0}: {1}'.format(
 
            self.original_error.__class__.__name__,
 
            self.original_error)
 

	
 

	
 
class BatchError(RattailError):
 
    """
 
    Base class for all batch-related errors.
 
    """
 

	
 

	
 
class BatchTypeNotFound(BatchError):
 

	
 
    def __init__(self, name):
 
        self.name = name
 

	
 
    def __str__(self):
 
        return "Batch type not found: %s" % self.name
 
    def __unicode__(self):
 
        return "Batch type not found: {}".format(self.name)
 

	
 
    
 
class BatchTypeNotSupported(BatchError):
 
    """
 
    Raised when a :class:`rattail.db.batches.BatchExecutor` instance is asked
 
    to execute a batch, but the batch is of an unsupported type.
 
    """
 

	
 
    def __init__(self, executor, batch_type):
 
        self.executor = executor
 
        self.batch_type = batch_type
 

	
 
    def __str__(self):
 
        return "Batch type '%s' is not supported by executor: %s" % (
 
    def __unicode__(self):
 
        return "Batch type '{}' is not supported by executor: {}".format(
 
            self.batch_type, repr(self.executor))
 

	
 

	
 
class BatchExecutorNotFound(BatchError):
 

	
 
    def __init__(self, name):
 
        self.name = name
 

	
 
    def __str__(self):
 
        return "Batch executor not found: %s" % self.name
 
    def __unicode__(self):
 
        return "Batch executor not found: {}".format(self.name)
 

	
 
    
 
class LabelPrintingError(Exception):
 

	
 
    pass
 

	
 

	
 
class PalmError(RattailError):
 
    """
 
    Base class for all errors relating to the Palm OS application interface.
 
    """
 

	
 

	
 
class PalmClassicDatabaseTypelibNotFound(PalmError):
 

	
 
    def __str__(self):
 
    def __unicode__(self):
 
        return ("The Python module for the Palm Classic Database type library "
 
                "could not be generated.  (Is the HotSync Manager software "
 
                "installed?)")
 

	
 

	
 
class PalmConduitManagerNotFound(PalmError):
 

	
 
    def __str__(self):
 
    def __unicode__(self):
 
        return ("The Palm Desktop Conduit Manager could not be instantiated.  "
 
                "(Is the HotSync Manager software installed?)")
 

	
 

	
 
class PalmConduitAlreadyRegistered(PalmError):
 

	
 
    def __str__(self):
 
    def __unicode__(self):
 
        return "The Rattail Palm conduit is already registered."
 

	
 

	
 
class PalmConduitNotRegistered(PalmError):
 

	
 
    def __str__(self):
 
    def __unicode__(self):
 
        return "The Rattail Palm conduit is not registered."
tests/test_commands.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from __future__ import unicode_literals
 
from __future__ import unicode_literals, absolute_import
 

	
 
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.commands.core import ArgumentParser, date_argument
 
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 = 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')
 
        date = 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')
 
        self.assertRaises(argparse.ArgumentTypeError, 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')
 
@@ -175,47 +176,47 @@ class TestAddUser(DataTestCase):
 

	
 
    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:
 
        with patch('rattail.commands.core.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:
 
        with patch('rattail.commands.core.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:
 
        with patch('rattail.commands.core.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')
 

	
 

	
 
# TODO: more broken tests..ugh.  these aren't very good or else i might bother
 
# fixing them...
 
# class TestDatabaseSync(TestCase):
 

	
 
#     @patch('rattail.db.sync.linux.start_daemon')
0 comments (0 inline, 0 general)