Fixes tests now that $0 invoices pay themselves

This commit is contained in:
Christopher Neugebauer 2016-04-06 17:19:09 +10:00
parent c9a62db774
commit 8ad265a65a
4 changed files with 46 additions and 16 deletions

View file

@ -57,6 +57,7 @@ class InvoiceController(object):
return value return value
@classmethod @classmethod
@transaction.atomic
def _generate(cls, cart): def _generate(cls, cart):
''' Generates an invoice for the given cart. ''' ''' Generates an invoice for the given cart. '''
invoice = rego.Invoice.objects.create( invoice = rego.Invoice.objects.create(
@ -65,7 +66,6 @@ class InvoiceController(object):
cart_revision=cart.revision, cart_revision=cart.revision,
value=Decimal() value=Decimal()
) )
invoice.save()
product_items = rego.ProductItem.objects.filter(cart=cart) product_items = rego.ProductItem.objects.filter(cart=cart)
@ -85,7 +85,6 @@ class InvoiceController(object):
quantity=item.quantity, quantity=item.quantity,
price=product.price, price=product.price,
) )
line_item.save()
invoice_value += line_item.quantity * line_item.price invoice_value += line_item.quantity * line_item.price
for item in discount_items: for item in discount_items:
@ -95,11 +94,13 @@ class InvoiceController(object):
quantity=item.quantity, quantity=item.quantity,
price=cls.resolve_discount_value(item) * -1, price=cls.resolve_discount_value(item) * -1,
) )
line_item.save()
invoice_value += line_item.quantity * line_item.price invoice_value += line_item.quantity * line_item.price
# TODO: calculate line items from discounts
invoice.value = invoice_value invoice.value = invoice_value
if invoice.value == 0:
invoice.paid = True
invoice.save() invoice.save()
return invoice return invoice

View file

@ -346,9 +346,8 @@ class DiscountTestCase(RegistrationCartTestCase):
discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) discounts = discount.available_discounts(self.USER_1, [self.CAT_2], [])
self.assertEqual(2, discounts[0].quantity) self.assertEqual(2, discounts[0].quantity)
inv = InvoiceController.for_cart(cart.cart) cart.cart.active = False
inv.pay("Dummy reference", inv.invoice.value) cart.cart.save()
self.assertTrue(inv.invoice.paid)
def test_discount_quantity_is_correct_after_first_purchase(self): def test_discount_quantity_is_correct_after_first_purchase(self):
self.test_discount_quantity_is_correct_before_first_purchase() self.test_discount_quantity_is_correct_before_first_purchase()
@ -358,9 +357,8 @@ class DiscountTestCase(RegistrationCartTestCase):
discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) discounts = discount.available_discounts(self.USER_1, [self.CAT_2], [])
self.assertEqual(1, discounts[0].quantity) self.assertEqual(1, discounts[0].quantity)
inv = InvoiceController.for_cart(cart.cart) cart.cart.active = False
inv.pay("Dummy reference", inv.invoice.value) cart.cart.save()
self.assertTrue(inv.invoice.paid)
def test_discount_is_gone_after_quantity_exhausted(self): def test_discount_is_gone_after_quantity_exhausted(self):
self.test_discount_quantity_is_correct_after_first_purchase() self.test_discount_quantity_is_correct_after_first_purchase()

View file

@ -83,18 +83,16 @@ class InvoiceTestCase(RegistrationCartTestCase):
code="VOUCHER", code="VOUCHER",
limit=1 limit=1
) )
voucher.save()
discount = rego.VoucherDiscount.objects.create( discount = rego.VoucherDiscount.objects.create(
description="VOUCHER RECIPIENT", description="VOUCHER RECIPIENT",
voucher=voucher, voucher=voucher,
) )
discount.save()
rego.DiscountForProduct.objects.create( rego.DiscountForProduct.objects.create(
discount=discount, discount=discount,
product=self.PROD_1, product=self.PROD_1,
percentage=Decimal(50), percentage=Decimal(50),
quantity=1 quantity=1
).save() )
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code) current_cart.apply_voucher(voucher.code)
@ -111,6 +109,32 @@ class InvoiceTestCase(RegistrationCartTestCase):
self.PROD_1.price * Decimal("0.5"), self.PROD_1.price * Decimal("0.5"),
invoice_1.invoice.value) invoice_1.invoice.value)
def test_zero_value_invoice_is_automatically_paid(self):
voucher = rego.Voucher.objects.create(
recipient="Voucher recipient",
code="VOUCHER",
limit=1
)
discount = rego.VoucherDiscount.objects.create(
description="VOUCHER RECIPIENT",
voucher=voucher,
)
rego.DiscountForProduct.objects.create(
discount=discount,
product=self.PROD_1,
percentage=Decimal(100),
quantity=1
)
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code)
# Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = InvoiceController.for_cart(current_cart.cart)
self.assertTrue(invoice_1.invoice.paid)
def test_invoice_voids_self_if_cart_is_invalid(self): def test_invoice_voids_self_if_cart_is_invalid(self):
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
@ -169,3 +193,8 @@ class InvoiceTestCase(RegistrationCartTestCase):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
invoice_1.void() invoice_1.void()
def test_cannot_generate_blank_invoice(self):
current_cart = TestingCartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
invoice_1 = InvoiceController.for_cart(current_cart.cart)

View file

@ -125,8 +125,8 @@ class VoucherTestCases(RegistrationCartTestCase):
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code) current_cart.apply_voucher(voucher.code)
inv = InvoiceController.for_cart(current_cart.cart) current_cart.cart.active = False
inv.pay("Hello!", inv.invoice.value) current_cart.cart.save()
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
@ -139,9 +139,11 @@ class VoucherTestCases(RegistrationCartTestCase):
voucher = self.new_voucher(limit=2) voucher = self.new_voucher(limit=2)
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code) current_cart.apply_voucher(voucher.code)
current_cart.add_to_cart(self.PROD_1, 1)
inv = InvoiceController.for_cart(current_cart.cart) inv = InvoiceController.for_cart(current_cart.cart)
inv.pay("Hello!", inv.invoice.value) if not inv.invoice.paid:
inv.pay("Hello!", inv.invoice.value)
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):