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()
 
@@ -32,33 +30,33 @@ class TestPurgeBatches(DataTestCase):
 

	
 
        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.
 

	
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)
 

	
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()
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)
 

	
 
@@ -82,34 +82,34 @@ class TestBatch(TestCase):
 
    #     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')
rattail/tests/test_commands.py
Show inline comments
 
@@ -6,30 +6,29 @@ 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')
0 comments (0 inline, 0 general)