Changeset - ba7cb6c7120b
[Not reviewed]
0 1 2
Lance Edgar (lance) - 11 years ago 2013-08-23 01:31:39
lance@edbob.org
Fixed bugs with `CustomerGroupAssignment`.

Now orphaned records should no longer be allowed.
3 files changed with 106 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rattail/db/extension/model.py
Show inline comments
 
@@ -942,8 +942,7 @@ Customer.person = association_proxy(
 

	
 
class CustomerGroup(Base):
 
    """
 
    Represents an arbitrary group to which :class:`Customer` instances may (or
 
    may not) belong.
 
    Represents an arbitrary group to which customers may belong.
 
    """
 

	
 
    __tablename__ = 'customer_groups'
 
@@ -961,15 +960,14 @@ class CustomerGroup(Base):
 

	
 
class CustomerGroupAssignment(Base):
 
    """
 
    Represents the assignment of a :class:`Customer` to a
 
    :class:`CustomerGroup`.
 
    Represents the assignment of a customer to a group.
 
    """
 

	
 
    __tablename__ = 'customers_groups'
 

	
 
    uuid = uuid_column()
 
    customer_uuid = Column(String(32), ForeignKey('customers.uuid'))
 
    group_uuid = Column(String(32), ForeignKey('customer_groups.uuid'))
 
    customer_uuid = Column(String(32), ForeignKey('customers.uuid'), nullable=False)
 
    group_uuid = Column(String(32), ForeignKey('customer_groups.uuid'), nullable=False)
 
    ordinal = Column(Integer, nullable=False)
 

	
 
    group = relationship(CustomerGroup)
 
@@ -980,7 +978,8 @@ class CustomerGroupAssignment(Base):
 
Customer._groups = relationship(
 
    CustomerGroupAssignment, backref='customer',
 
    collection_class=ordering_list('ordinal', count_from=1),
 
    order_by=CustomerGroupAssignment.ordinal)
 
    order_by=CustomerGroupAssignment.ordinal,
 
    cascade='save-update, merge, delete, delete-orphan')
 

	
 
Customer.groups = association_proxy(
 
    '_groups', 'group',
tests/db/__init__.py
Show inline comments
 
new file 100644
 

	
 
import unittest
 

	
 
from sqlalchemy import create_engine
 
from edbob.db import Session, Base
 
from edbob.db.util import install_core_schema
 
from edbob.db.extensions import activate_extension
 

	
 

	
 
__all__ = ['DataTestCase']
 

	
 

	
 
# TODO: This is just awful...
 
engine = create_engine('sqlite://')
 
Session.configure(bind=engine)
 
install_core_schema(engine)
 
activate_extension('rattail', engine)
 

	
 

	
 
class DataTestCase(unittest.TestCase):
 

	
 
    def setUp(self):
 
        self.session = Session()
 
        for table in reversed(Base.metadata.sorted_tables):
 
            self.session.execute(table.delete())
 
        self.session.commit()
 

	
 
    def tearDown(self):
 
        self.session.close()
tests/db/test_model.py
Show inline comments
 
new file 100644
 

	
 
from unittest import TestCase
 
from . import DataTestCase
 

	
 
from rattail.db.extension import model
 
from sqlalchemy.exc import IntegrityError
 

	
 

	
 
class TestCustomer(DataTestCase):
 

	
 
    def test_repr(self):
 
        customer = model.Customer(uuid='whatever')
 
        self.assertEqual(repr(customer), "Customer(uuid='whatever')")
 

	
 
    def test_unicode(self):
 
        customer = model.Customer()
 
        self.assertEqual(unicode(customer), u'None')
 
        customer = model.Customer(name='Fred')
 
        self.assertEqual(unicode(customer), u'Fred')
 

	
 
    def test_cascade_delete_assignment(self):
 
        customer = model.Customer()
 
        assignment = model.CustomerGroupAssignment(
 
            customer=customer, group=model.CustomerGroup(), ordinal=1)
 
        self.session.add_all([customer, assignment])
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 1)
 
        self.session.delete(customer)
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 0)
 

	
 

	
 
class TestCustomerGroupAssignment(DataTestCase):
 

	
 
    def test_repr(self):
 
        assignment = model.CustomerGroupAssignment(uuid='whatever')
 
        self.assertEqual(repr(assignment), "CustomerGroupAssignment(uuid='whatever')")
 

	
 
    def test_customer_required(self):
 
        assignment = model.CustomerGroupAssignment(group=model.CustomerGroup())
 
        self.session.add(assignment)
 
        self.assertRaises(IntegrityError, self.session.commit)
 
        self.session.rollback()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 0)
 
        assignment.customer = model.Customer()
 
        self.session.add(assignment)
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 1)
 

	
 
    def test_group_required(self):
 
        assignment = model.CustomerGroupAssignment(customer=model.Customer())
 
        self.session.add(assignment)
 
        self.assertRaises(IntegrityError, self.session.commit)
 
        self.session.rollback()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 0)
 
        assignment.group = model.CustomerGroup()
 
        self.session.add(assignment)
 
        self.session.commit()
 
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 1)
 

	
 
    def test_ordinal_autoincrement(self):
 
        customer = model.Customer()
 
        self.session.add(customer)
 
        assignment = model.CustomerGroupAssignment(group=model.CustomerGroup())
 
        customer._groups.append(assignment)
 
        self.session.commit()
 
        self.assertEqual(assignment.ordinal, 1)
 
        assignment = model.CustomerGroupAssignment(group=model.CustomerGroup())
 
        customer._groups.append(assignment)
 
        self.session.commit()
 
        self.assertEqual(assignment.ordinal, 2)
0 comments (0 inline, 0 general)