Changeset - b9a4def2ca08
[Not reviewed]
0 6 0
Lance Edgar - 8 years ago 2016-05-11 17:31:57
ledgar@sacfoodcoop.com
Clean up some imports within tests
6 files changed with 19 insertions and 29 deletions:
0 comments (0 inline, 0 general)
rattail/tests/db/__init__.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
# TODO: Update references to this; should be importing from tests root.
 
from .. import DataTestCase
rattail/tests/db/batches/test_util.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from __future__ import unicode_literals
 
from __future__ import unicode_literals, absolute_import
 

	
 
import datetime
 

	
 
from sqlalchemy import func
 
from sqlalchemy import MetaData
 
import sqlalchemy as sa
 

	
 
from ... import DataTestCase
 

	
 
from rattail.db.batches import util
 
from rattail.db import model
 
from rattail.db.batches import util
 
from rattail.tests import DataTestCase
 

	
 

	
 
class TestPurgeBatches(DataTestCase):
 

	
 
    def setUp(self):
 
        super(TestPurgeBatches, self).setUp()
 

	
 
        batch = model.Batch(purge=datetime.date(2014, 1, 1))
 
        batch.add_column('F01')
 
        batch.add_column('F02')
 
        self.session.add(batch)
 
        batch.create_table()
 

	
 
        batch = model.Batch(purge=datetime.date(2014, 2, 1))
 
        batch.add_column('F01')
 
        batch.add_column('F02')
 
        self.session.add(batch)
 
        batch.create_table()
 

	
 
        batch = model.Batch(purge=datetime.date(2014, 3, 1))
 
        batch.add_column('F01')
 
        batch.add_column('F02')
 
        self.session.add(batch)
 
        batch.create_table()
 

	
 
        self.session.commit()
 

	
 
    def get_batch_tables_metadata(self):
 
        def batch_tables(name, metadata):
 
            return util.batch_pattern.match(name)
 
        metadata = MetaData(bind=self.engine)
 
        metadata = sa.MetaData(bind=self.engine)
 
        metadata.reflect(only=batch_tables)
 
        return metadata
 

	
 
    def test_purging_honors_batch_purge_dates(self):
 
        self.assertEqual(self.session.query(model.Batch).count(), 3)
 
        self.assertEqual(util.purge_batches(effective_date=datetime.date(2014, 1, 15)), 1)
 
        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))
 
        self.assertEqual(self.session.query(sa.func.min(model.Batch.purge)).scalar(), datetime.date(2014, 2, 1))
 

	
 
    def test_purging_everything_does_just_that(self):
 
        self.assertEqual(self.session.query(model.Batch).count(), 3)
 
        self.assertEqual(util.purge_batches(purge_everything=True), 3)
 
        self.assertEqual(self.session.query(model.Batch).count(), 0)
 

	
 
    # TODO: The next two tests each work if only one is enabled...but if both
 
    # are enabled, one will fail.  This needs more investigation, but one
 
    # possible cause is the "corruption" of Base.metadata when Batch.rowclass
 
    # is accessed?  In particular it seems *not* to be a SQLite problem, as it
 
    # occurred when using a PostgreSQL engine as well.
 

	
 
    # def test_purging_does_not_leave_orphaned_tables(self):
 
    #     self.assertEqual(self.session.query(model.Batch).count(), 3)
 
    #     self.assertEqual(util.purge_batches(purge_everything=True), 3)
 
    #     self.assertEqual(self.session.query(model.Batch).count(), 0)
 
    #     metadata = self.get_batch_tables_metadata()
 
    #     self.assertEqual(len(metadata.tables), 0)
 

	
 
    # def test_purging_does_not_delete_previously_orphaned_tables(self):
 
    #     metadata = self.get_batch_tables_metadata()
 
    #     self.assertEqual(len(metadata.tables), 3)
 
    #     batch = self.session.query(model.Batch).first()
 
    #     batch.drop_table()
 
    #     self.assertEqual(self.session.query(model.Batch).count(), 3)
 
    #     metadata = self.get_batch_tables_metadata()
 
    #     self.assertEqual(len(metadata.tables), 2)
rattail/tests/db/model/test_users.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from __future__ import unicode_literals
 

	
 
from ... import DataTestCase
 
from __future__ import unicode_literals, absolute_import
 

	
 
from rattail.db import model
 
from rattail.tests import DataTestCase
 

	
 

	
 
class TestUserEmailAddress(DataTestCase):
 

	
 
    def extra_setup(self):
 
        self.user = model.User(username='fred')
 
        self.session.add(self.user)
 
        self.session.flush()
 

	
 
    def test_email_defaults_to_none(self):
 
        self.assertTrue(self.user.get_email_address() is None)
 

	
 
    def test_email_comes_from_person_then_customer(self):
 
        # only customer has email at this point
 
        person = model.Person(first_name='Fred')
 
        customer = model.Customer(name='Fred')
 
        customer.add_email_address('customer@mailinator.com')
 
        customer.people.append(person)
 
        self.user.person = person
 
        self.session.flush()
 
        self.assertEqual(self.user.get_email_address(), 'customer@mailinator.com')
 

	
 
        # now person email will take preference
 
        person.add_email_address('person@mailinator.com')
 
        self.session.refresh(person)
 
        self.assertEqual(self.user.get_email_address(), 'person@mailinator.com')
 

	
 
    def test_email_address_property_works_too(self):
 
        # even though this may go away some day, cover it for now
 
        person = model.Person(first_name='Fred')
 
        person.add_email_address('person@mailinator.com')
 
        self.user.person = person
 
        self.session.flush()
 
        self.assertEqual(self.user.email_address, 'person@mailinator.com')
rattail/tests/db/test_auth.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from __future__ import unicode_literals
 
from __future__ import unicode_literals, absolute_import
 

	
 
from .. import DataTestCase
 

	
 
from rattail.db import auth
 
from rattail.db import model
 
from rattail.db import auth, model
 
from rattail.tests import DataTestCase
 

	
 

	
 
class TestAuthenticateUser(DataTestCase):
 

	
 
    def test_nonexistent_user_returns_none(self):
 
        self.assertTrue(auth.authenticate_user(self.session, u'fred', u'fredpass') is None)
 

	
 
    def test_correct_credentials_returns_user(self):
 
        fred = model.User(username=u'fred')
 
        auth.set_user_password(fred, u'fredpass')
 
        self.session.add(fred)
 
        self.session.commit()
 
        user = auth.authenticate_user(self.session, u'fred', u'fredpass')
 
        self.assertTrue(user is fred)
 

	
 
    def test_wrong_password_user_returns_none(self):
 
        fred = model.User(username=u'fred', active=False)
 
        auth.set_user_password(fred, u'fredpass')
 
        self.session.add(fred)
 
        self.session.commit()
 
        self.assertTrue(auth.authenticate_user(self.session, u'fred', u'BADPASS') is None)
 

	
 
    def test_inactive_user_returns_none(self):
 
        fred = model.User(username=u'fred', active=False)
 
        auth.set_user_password(fred, u'fredpass')
 
        self.session.add(fred)
 
        self.session.commit()
 
        self.assertTrue(auth.authenticate_user(self.session, u'fred', u'fredpass') is None)
rattail/tests/db/test_model.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from __future__ import unicode_literals
 
from __future__ import unicode_literals, absolute_import
 

	
 
from unittest import TestCase
 
from . import DataTestCase
 
from mock import patch, DEFAULT, Mock, MagicMock
 

	
 
from sqlalchemy import String, Boolean, Numeric
 
import sqlalchemy as sa
 
from sqlalchemy.exc import IntegrityError
 

	
 
from rattail.db import model
 
from rattail.db.types import GPCType
 
from rattail.db.changes import record_changes
 
from rattail.tests import DataTestCase
 

	
 

	
 
class SAErrorHelper(object):
 

	
 
    def integrity_or_flush_error(self):
 
        try:
 
            from sqlalchemy.exc import FlushError
 
        except ImportError:
 
            return IntegrityError
 
        else:
 
            return (IntegrityError, FlushError)
 

	
 

	
 
class TestPerson(DataTestCase):
 

	
 
    def test_default_display_name_is_generated_from_first_and_last_name_if_both_provided(self):
 
        person = model.Person(first_name='Fred', last_name='Flintstone')
 
        self.assertTrue(person.display_name is None)
 
        self.session.add(person)
 
        self.session.flush()
 
        self.assertEqual(person.display_name, 'Fred Flintstone')
 

	
 
    def test_default_display_name_is_generated_from_first_name_if_no_last_name_provided(self):
 
        person = model.Person(first_name='Fred')
 
        self.assertTrue(person.display_name is None)
 
        self.session.add(person)
 
        self.session.flush()
 
        self.assertEqual(person.display_name, 'Fred')
 

	
 
    def test_default_display_name_is_generated_from_last_name_if_no_first_name_provided(self):
 
        person = model.Person(last_name='Flintstone')
 
        self.assertTrue(person.display_name is None)
 
        self.session.add(person)
 
        self.session.flush()
 
        self.assertEqual(person.display_name, 'Flintstone')
 

	
 

	
 
class TestBatch(TestCase):
 

	
 
    # @patch('rattail.db.model.object_session')
 
    # def test_rowclass(self, object_session):
 
    #     object_session.return_value = object_session
 

	
 
    #     # no row classes to start with
 
    #     self.assertEqual(model.Batch._rowclasses, {})
 

	
 
    #     # basic, empty row class
 
    #     batch = model.Batch(uuid='some_uuid')
 
    #     batch.get_sqlalchemy_type = Mock(return_value='some_type')
 
    #     batch.columns = MagicMock()
 
    #     rowclass = batch.rowclass
 
    #     self.assertTrue(issubclass(rowclass, model.BatchRow))
 
    #     self.assertEqual(model.Batch._rowclasses.keys(), ['some_uuid'])
 
    #     self.assertTrue(model.Batch._rowclasses['some_uuid'] is rowclass)
 
    #     self.assertFalse(object_session.flush.called)
 

	
 
    #     # make sure rowclass.batch works
 
    #     object_session.query.return_value.get.return_value = batch
 
    #     self.assertTrue(rowclass().batch is batch)
 
    #     object_session.query.return_value.get.assert_called_once_with('some_uuid')
 

	
 
    #     # row class with generated uuid and some columns
 
    #     batch = model.Batch(uuid=None)
 
    #     batch.columns = [model.BatchColumn(name='F01'), model.BatchColumn(name='F02')]
 
    #     model.Batch.get_sqlalchemy_type = Mock(return_value=String(length=20))
 
    #     def set_uuid():
 
    #         batch.uuid = 'fresh_uuid'
 
    #     object_session.flush.side_effect = set_uuid
 
    #     rowclass = batch.rowclass
 
    #     object_session.flush.assert_called_once_with()
 
    #     self.assertEqual(sorted(model.Batch._rowclasses.keys()), sorted(['some_uuid', 'fresh_uuid']))
 
    #     self.assertTrue(model.Batch._rowclasses['fresh_uuid'] is rowclass)
 

	
 
    def test_get_sqlalchemy_type(self):
 

	
 
        # gpc
 
        self.assertTrue(isinstance(model.Batch.get_sqlalchemy_type('GPC(14)'), GPCType))
 

	
 
        # boolean
 
        self.assertTrue(isinstance(model.Batch.get_sqlalchemy_type('FLAG(1)'), Boolean))
 
        self.assertTrue(isinstance(model.Batch.get_sqlalchemy_type('FLAG(1)'), sa.Boolean))
 

	
 
        # string
 
        type_ = model.Batch.get_sqlalchemy_type('CHAR(20)')
 
        self.assertTrue(isinstance(type_, String))
 
        self.assertTrue(isinstance(type_, sa.String))
 
        self.assertEqual(type_.length, 20)
 

	
 
        # numeric
 
        type_ = model.Batch.get_sqlalchemy_type('NUMBER(9,3)')
 
        self.assertTrue(isinstance(type_, Numeric))
 
        self.assertTrue(isinstance(type_, sa.Numeric))
 
        self.assertEqual(type_.precision, 9)
 
        self.assertEqual(type_.scale, 3)
 

	
 
        # invalid
 
        self.assertRaises(AssertionError, model.Batch.get_sqlalchemy_type, 'CHAR(9,3)')
 
        self.assertRaises(AssertionError, model.Batch.get_sqlalchemy_type, 'OMGWTFBBQ')
 

	
 

	
 
class TestCustomerPerson(DataTestCase, SAErrorHelper):
 

	
 
    def test_repr(self):
 
        assoc = model.CustomerPerson(uuid='whatever')
 
        self.assertEqual(repr(assoc), "CustomerPerson(uuid=u'whatever')")
 

	
 
    def test_customer_required(self):
 
        assoc = model.CustomerPerson(person=model.Person())
 
        self.session.add(assoc)
 
        self.assertRaises(self.integrity_or_flush_error(), self.session.commit)
 
        self.session.rollback()
 
        self.assertEqual(self.session.query(model.CustomerPerson).count(), 0)
 
        assoc.customer = model.Customer()
 
        self.session.add(assoc)
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerPerson).count(), 1)
 

	
 
    def test_person_required(self):
 
        assoc = model.CustomerPerson(customer=model.Customer())
 
        self.session.add(assoc)
 
        self.assertRaises(IntegrityError, self.session.commit)
 
        self.session.rollback()
 
        self.assertEqual(self.session.query(model.CustomerPerson).count(), 0)
 
        assoc.person = model.Person()
 
        self.session.add(assoc)
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerPerson).count(), 1)
 

	
 
    def test_ordinal_autoincrement(self):
 
        customer = model.Customer()
 
        self.session.add(customer)
 
        assoc = model.CustomerPerson(person=model.Person())
 
        customer._people.append(assoc)
 
        self.session.commit()
 
        self.assertEqual(assoc.ordinal, 1)
 
        assoc = model.CustomerPerson(person=model.Person())
 
        customer._people.append(assoc)
 
        self.session.commit()
 
        self.assertEqual(assoc.ordinal, 2)
 

	
rattail/tests/test_commands.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
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 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 = 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 = 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, 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()
0 comments (0 inline, 0 general)