Commit aa0291c2 by Vedran Karacic

Move invoices to point to order instead of basket.

parent e3905cd4
import logging
from django.core.management import BaseCommand
from oscar.core.loading import get_model
from ecommerce.core.models import BusinessClient
from ecommerce.invoice.models import Invoice
logger = logging.getLogger(__name__)
Order = get_model('order', 'Order')
class Command(BaseCommand):
"""
Populate the order field for all invoices from the basket order,
and set the basket values to None. Creates a new business client object
from the basket owner username value and assigns it to the invoice if there
is not one assigned already.
"""
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def handle(self, *args, **options):
for invoice in Invoice.objects.all():
if invoice.basket:
try:
order = Order.objects.get(basket=invoice.basket)
invoice.order = order
if not invoice.business_client:
invoice.business_client, __ = BusinessClient.objects.get_or_create(
name=invoice.basket.owner.username
)
invoice.basket = None
invoice.save()
logger.info('Order [%d] saved to invoice [%d].', order.id, invoice.id)
except Order.DoesNotExist:
logger.info('Order for basket [%s] does not exist.', invoice.basket)
from django.core.management import call_command
from oscar.core.loading import get_model
from oscar.test import factories from oscar.test import factories
from ecommerce.core.models import BusinessClient
from ecommerce.invoice.models import Invoice from ecommerce.invoice.models import Invoice
from ecommerce.tests.testcases import TestCase from ecommerce.tests.testcases import TestCase
Order = get_model('order', 'Order')
class InvoiceTests(TestCase): class InvoiceTests(TestCase):
"""Test to ensure Invoice objects are created correctly""" """Test to ensure Invoice objects are created correctly"""
...@@ -25,3 +30,70 @@ class InvoiceTests(TestCase): ...@@ -25,3 +30,70 @@ class InvoiceTests(TestCase):
def test_total(self): def test_total(self):
"""Test to check invoice total""" """Test to check invoice total"""
self.assertEqual(self.basket.order.total_incl_tax, self.invoice.total) self.assertEqual(self.basket.order.total_incl_tax, self.invoice.total)
class InvoiceCommandTests(TestCase):
"""Tests for the populate_invoice_orders command."""
def setUp(self):
super(InvoiceCommandTests, self).setUp()
self.order = factories.OrderFactory()
self.basket = factories.BasketFactory()
self.basket.owner = factories.UserFactory()
self.basket.save()
def test_order_populated(self):
"""Verify the order field is populated and basket set to None."""
self.order.basket = self.basket
self.order.save()
invoice_before = Invoice.objects.create(basket=self.basket)
self.assertIsNone(invoice_before.order)
call_command('populate_invoice_orders')
invoice_after = Invoice.objects.first()
self.assertIsNone(invoice_after.basket)
self.assertEqual(invoice_after.order, self.order)
def test_non_existing_order(self):
"""Verify the invoice is not altered if no order exists."""
self.assertIsNone(self.order.basket)
invoice_before = Invoice.objects.create(basket=self.basket)
invoice_before_no_basket = Invoice.objects.create()
self.assertIsNone(invoice_before.order)
self.assertIsNone(invoice_before_no_basket.order)
call_command('populate_invoice_orders')
invoices_after = Invoice.objects.all()
self.assertEqual(invoices_after.count(), 2)
self.assertIsNone(invoices_after.first().order)
self.assertIsNone(invoices_after.last().order)
self.assertEqual(invoices_after.get(id=invoice_before.id).basket, self.basket)
def test_client_changed(self):
"""Verify the business client value is added if it doesn't exist."""
self.assertIsNotNone(self.basket.owner)
self.order.basket = self.basket
self.order.save()
invoice_before = Invoice.objects.create(basket=self.basket)
self.assertIsNone(invoice_before.business_client)
self.assertEqual(BusinessClient.objects.count(), 0)
call_command('populate_invoice_orders')
invoice_after = Invoice.objects.first()
self.assertEqual(BusinessClient.objects.count(), 1)
self.assertEqual(invoice_after.business_client, BusinessClient.objects.first())
def test_client_unchanged(self):
"""Verify the business client value is unchanged if it exist."""
self.order.basket = self.basket
self.order.save()
business_client = BusinessClient.objects.create(name='Tester')
Invoice.objects.create(
basket=self.basket,
business_client=business_client
)
call_command('populate_invoice_orders')
invoice_after = Invoice.objects.first()
self.assertEqual(invoice_after.business_client, business_client)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment