class ModelInvoiceTests(TestCase):

    fixtures = ['user', 'unit_test']

    def setUp(self):
        self.existing_invoice = Invoice.objects.get(pk=1)
        
        self.new_invoice = Invoice(profile=CustomerProfile.objects.get(pk=1))
        self.new_invoice.save()

        self.shirt_offer = Offer.objects.get(pk=1)
        self.hamster = Offer.objects.get(pk=3)
        self.mug_offer = Offer.objects.get(pk=4)
 
    def test_default_site_id_saved(self):
        invoice = Invoice()
        invoice.profile = CustomerProfile.objects.get(pk=1)
        invoice.save()

        self.assertEquals(Site.objects.get_current(), invoice.site)

    def test_add_offer(self):
        self.existing_invoice.add_offer(Offer.objects.get(pk=4))
        self.new_invoice.add_offer(self.mug_offer)

        self.assertIsNotNone(OrderItem.objects.get(invoice=self.new_invoice))
        self.assertEquals(OrderItem.objects.filter(invoice=self.existing_invoice).count(), 4)

    # def test_fail_add_unavailable_offer(self):
        # raise NotImplementedError()

    def test_remove_offer(self):
        self.existing_invoice.remove_offer(Offer.objects.get(pk=3))

        self.assertEquals(OrderItem.objects.filter(invoice=self.existing_invoice).count(), 2)

    def test_update_totals(self):
        self.existing_invoice.update_totals()
        start_total = self.existing_invoice.total

        self.existing_invoice.add_offer(Offer.objects.get(pk=4))
        self.existing_invoice.update_totals()
        add_mug_total = self.existing_invoice.total

        self.existing_invoice.remove_offer(Offer.objects.get(pk=1))
        self.existing_invoice.update_totals()
        remove_shirt_total = self.existing_invoice.total

        self.assertEquals(start_total, 345.18)
        self.assertEquals(add_mug_total, 355.18)
        self.assertEquals(remove_shirt_total, 345.19)

    def test_add_quantity(self):
        self.shirt_offer.allow_multiple = True
        self.shirt_offer.save()
        start_quantity = self.existing_invoice.order_items.filter(offer=self.shirt_offer).first().quantity
        self.existing_invoice.add_offer(self.shirt_offer)
        end_quantity = self.existing_invoice.order_items.filter(offer=self.shirt_offer).first().quantity
        
        self.assertNotEquals(start_quantity, end_quantity)

    def test_remove_quantity(self):
        start_quantity = self.existing_invoice.order_items.filter(offer=self.shirt_offer).first().quantity
        self.existing_invoice.remove_offer(self.shirt_offer)
        end_quantity = self.existing_invoice.order_items.filter(offer=self.shirt_offer).first().quantity

        self.assertNotEquals(start_quantity, end_quantity)

    def test_remove_quantity_zero(self):
        start_quantity = self.existing_invoice.order_items.filter(offer=self.hamster).first().quantity
        self.existing_invoice.remove_offer(self.hamster)
        end_quantity = self.existing_invoice.order_items.filter(offer=self.hamster).count()

        self.assertNotEquals(start_quantity, end_quantity)

    def test_get_recurring_total(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)
        
        self.assertEquals(self.existing_invoice.get_recurring_total(), recurring_offer.current_price())

    def test_get_recurring_total_only_recurring_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.new_invoice.add_offer(recurring_offer)
        
        self.assertEquals(self.new_invoice.get_recurring_total(), recurring_offer.current_price())
        self.assertEquals(self.new_invoice.get_one_time_transaction_total(), 0)

    def test_get_one_time_transaction_total_with_recurring_offer(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)
        
        self.assertEquals(self.existing_invoice.get_one_time_transaction_total(), self.existing_invoice.total - self.existing_invoice.get_recurring_total())

    def test_get_one_time_transaction_total_no_recurring_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)
        
        self.assertEquals(self.existing_invoice.get_one_time_transaction_total(), self.existing_invoice.total - self.existing_invoice.get_recurring_total())
        self.assertEquals(self.existing_invoice.get_recurring_total(), 0)

    def test_get_recurring_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)

        self.assertEquals(self.existing_invoice.get_recurring_order_items().count(), 1)
    
    def test_get_one_time_transaction_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)

        self.assertEquals(self.existing_invoice.get_one_time_transaction_order_items().count(), self.existing_invoice.order_items.all().count() - 1)

    def test_empty_cart(self):
        self.assertNotEqual(0, self.existing_invoice.order_items.count())
        self.existing_invoice.empty_cart()
        self.assertEqual(0, self.existing_invoice.order_items.count())

    def test_empty_cart_in_checkout_state(self):
        self.existing_invoice.status = Invoice.InvoiceStatus.CHECKOUT
        self.existing_invoice.save()
        self.assertNotEqual(0, self.existing_invoice.order_items.count())
        self.existing_invoice.empty_cart()
        self.assertEqual(0, self.existing_invoice.order_items.count())
示例#2
0
class ModelInvoiceTests(TestCase):

    fixtures = ['user', 'unit_test']

    def setUp(self):
        self.existing_invoice = Invoice.objects.get(pk=1)

        self.new_invoice = Invoice(profile=CustomerProfile.objects.get(pk=1))
        self.new_invoice.save()

        self.shirt_offer = Offer.objects.get(pk=1)
        self.hamster = Offer.objects.get(pk=3)
        self.mug_offer = Offer.objects.get(pk=4)

    def test_default_site_id_saved(self):
        invoice = Invoice()
        invoice.profile = CustomerProfile.objects.get(pk=1)
        invoice.save()

        self.assertEquals(Site.objects.get_current(), invoice.site)

    def test_add_offer(self):
        self.existing_invoice.add_offer(Offer.objects.get(pk=4))
        self.new_invoice.add_offer(self.mug_offer)

        self.assertIsNotNone(OrderItem.objects.get(invoice=self.new_invoice))
        self.assertEquals(
            OrderItem.objects.filter(invoice=self.existing_invoice).count(), 4)

    # def test_fail_add_unavailable_offer(self):
    # raise NotImplementedError()

    def test_remove_offer(self):
        before_remove_count = self.existing_invoice.order_items.count()
        self.existing_invoice.remove_offer(Offer.objects.get(pk=3))

        self.assertEquals(self.existing_invoice.order_items.count(),
                          before_remove_count - 1)

    def test_update_totals(self):
        self.existing_invoice.update_totals()
        start_total = self.existing_invoice.total

        self.existing_invoice.add_offer(Offer.objects.get(pk=2))
        self.existing_invoice.update_totals()
        add_mug_total = self.existing_invoice.total

        self.existing_invoice.remove_offer(Offer.objects.get(pk=2))
        self.existing_invoice.update_totals()
        remove_shirt_total = self.existing_invoice.total

        self.assertEquals(
            get_display_decimal(add_mug_total),
            get_display_decimal(start_total))  # Offer.pk =4 has a trial period
        self.assertEquals(
            get_display_decimal(remove_shirt_total),
            get_display_decimal(start_total -
                                Offer.objects.get(pk=2).current_price()))

    def test_add_quantity(self):
        self.shirt_offer.allow_multiple = True
        self.shirt_offer.save()
        start_quantity = self.existing_invoice.order_items.filter(
            offer=self.shirt_offer).first().quantity
        self.existing_invoice.add_offer(self.shirt_offer)
        end_quantity = self.existing_invoice.order_items.filter(
            offer=self.shirt_offer).first().quantity

        self.assertNotEquals(start_quantity, end_quantity)

    def test_remove_quantity(self):
        start_quantity = self.existing_invoice.order_items.filter(
            offer=self.shirt_offer).first().quantity
        self.existing_invoice.remove_offer(self.shirt_offer)
        end_quantity = self.existing_invoice.order_items.filter(
            offer=self.shirt_offer).first().quantity

        self.assertNotEquals(start_quantity, end_quantity)

    def test_remove_quantity_zero(self):
        start_quantity = self.existing_invoice.order_items.filter(
            offer=self.hamster).first().quantity
        self.existing_invoice.remove_offer(self.hamster)
        end_quantity = self.existing_invoice.order_items.filter(
            offer=self.hamster).count()

        self.assertNotEquals(start_quantity, end_quantity)

    def test_get_recurring_total(self):
        recurring_offer = Offer.objects.get(pk=4)

        self.assertEquals(self.existing_invoice.get_recurring_total(),
                          recurring_offer.current_price())

    def test_get_recurring_total_only_recurring_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)

        self.assertEquals(self.new_invoice.get_recurring_total(),
                          recurring_offer.current_price())
        self.assertEquals(self.new_invoice.get_one_time_transaction_total(), 0)

    def test_get_one_time_transaction_total_with_recurring_offer(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.update_totals()
        self.existing_invoice.save()
        before_total = self.existing_invoice.total
        before_one_time_total = get_display_decimal(
            self.existing_invoice.get_one_time_transaction_total())
        self.existing_invoice.add_offer(recurring_offer)
        after_total = self.existing_invoice.total
        after_one_time_total = get_display_decimal(
            self.existing_invoice.get_one_time_transaction_total())

        self.assertEquals(before_one_time_total, after_one_time_total)
        self.assertEquals(before_total, after_total)

    def test_get_one_time_transaction_total_no_recurring_order_items(self):
        self.existing_invoice.update_totals()

        self.assertEquals(
            self.existing_invoice.get_one_time_transaction_total(),
            (self.existing_invoice.total -
             self.existing_invoice.get_recurring_total()))
        self.assertEquals(
            get_display_decimal(self.existing_invoice.get_recurring_total()),
            get_display_decimal(
                self.existing_invoice.total -
                self.existing_invoice.get_one_time_transaction_total()))

    def test_get_recurring_order_items(self):
        before_count = self.existing_invoice.get_recurring_order_items().count(
        )
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)
        after_count = self.existing_invoice.get_recurring_order_items().count()

        self.assertNotEquals(before_count, after_count)

    def test_get_one_time_transaction_order_items(self):
        recurring_offer = Offer.objects.get(pk=5)
        self.existing_invoice.add_offer(recurring_offer)

        self.assertEquals(
            self.existing_invoice.get_one_time_transaction_order_items().count(
            ),
            self.existing_invoice.order_items.all().count() -
            len(self.existing_invoice.get_recurring_order_items()))

    def test_empty_cart(self):
        self.assertNotEqual(0, self.existing_invoice.order_items.count())
        self.existing_invoice.empty_cart()
        self.assertEqual(0, self.existing_invoice.order_items.count())

    def test_empty_cart_in_checkout_state(self):
        self.existing_invoice.status = Invoice.InvoiceStatus.CHECKOUT
        self.existing_invoice.save()
        self.assertNotEqual(0, self.existing_invoice.order_items.count())
        self.existing_invoice.empty_cart()
        self.assertEqual(0, self.existing_invoice.order_items.count())

    def test_swap_offers_success(self):
        hulk_offer = Offer.objects.get(pk=4)
        free_hulk_offer = Offer.objects.get(pk=5)
        self.new_invoice.add_offer(hulk_offer)
        self.new_invoice.swap_offer(hulk_offer, free_hulk_offer)
        self.assertFalse(
            self.new_invoice.order_items.filter(offer=hulk_offer).exists())
        self.assertTrue(
            self.new_invoice.order_items.filter(
                offer=free_hulk_offer).exists())

    def test_swap_offers_fail_no_matching_products(self):
        hulk_offer = Offer.objects.get(pk=4)
        cheese_offer = Offer.objects.get(pk=2)
        self.new_invoice.add_offer(hulk_offer)
        self.new_invoice.swap_offer(hulk_offer, cheese_offer)
        self.assertFalse(
            self.new_invoice.order_items.filter(offer=cheese_offer).exists())
        self.assertTrue(
            self.new_invoice.order_items.filter(offer=hulk_offer).exists())

    def test_invoice_no_discounts(self):
        self.new_invoice.add_offer(Offer.objects.get(pk=3))
        self.assertEqual(self.new_invoice.get_discounts(), 0)
        self.assertGreater(self.new_invoice.total, 0)

    def test_invoice_with_discounts_ten_off(self):
        discount = 10
        wheel_offer = Offer.objects.get(pk=3)
        wheel_price = Price.objects.get(pk=5)
        wheel_price.cost = wheel_offer.get_msrp() - discount
        wheel_price.save()
        self.new_invoice.add_offer(wheel_offer)
        self.assertGreater(wheel_offer.get_msrp(), self.new_invoice.total)
        self.assertEqual(self.new_invoice.get_discounts(), discount)

    def test_invoice_with_trial_discounts(self):
        free_month_offer = Offer.objects.get(pk=7)
        discount = free_month_offer.current_price()
        self.new_invoice.add_offer(free_month_offer)
        self.assertEqual(self.new_invoice.get_discounts(), discount)
        self.assertEqual(self.new_invoice.total, 0)

    def test_invoice_with_discount_and_trial_discounts(self):
        discount = 10
        wheel_offer = Offer.objects.get(pk=3)
        wheel_price = Price.objects.get(pk=5)
        wheel_price.cost = wheel_offer.get_msrp() - discount
        wheel_price.save()
        free_month_offer = Offer.objects.get(pk=7)
        trial_discount = free_month_offer.current_price()
        self.new_invoice.add_offer(free_month_offer)
        self.new_invoice.add_offer(wheel_offer)
        self.assertEqual(self.new_invoice.get_discounts(),
                         discount + trial_discount)

    def test_save_discounts_vendor_notes(self):
        free_month_offer = Offer.objects.get(pk=7)
        discount = free_month_offer.current_price()
        self.new_invoice.add_offer(free_month_offer)
        self.new_invoice.save_discounts_vendor_notes()
        self.assertEqual(self.new_invoice.vendor_notes['discounts'], discount)

    def test_order_item_price_msrp(self):
        wheel_offer = Offer.objects.get(pk=3)
        self.new_invoice.add_offer(wheel_offer)
        self.assertEqual(self.new_invoice.order_items.first().price,
                         wheel_offer.get_msrp())
        self.assertNotEqual(self.new_invoice.order_items.first().price,
                            wheel_offer.current_price())

    def test_order_item_price_current_price(self):
        free_month = Offer.objects.get(pk=7)
        self.new_invoice.add_offer(free_month)
        self.assertNotEqual(self.new_invoice.order_items.first().price,
                            free_month.get_msrp())
        self.assertEqual(self.new_invoice.order_items.first().price,
                         free_month.current_price())

    def test_order_item_with_discounts(self):
        discount = 10
        wheel_offer = Offer.objects.get(pk=3)
        wheel_price = Price.objects.get(pk=5)
        wheel_price.cost = wheel_offer.get_msrp() - discount
        wheel_price.save()
        self.new_invoice.add_offer(wheel_offer)
        self.assertEqual(self.new_invoice.order_items.first().discounts,
                         discount)

    def test_order_item_no_discounts(self):
        wheel_offer = Offer.objects.get(pk=3)
        self.new_invoice.add_offer(wheel_offer)
        self.assertEqual(self.new_invoice.order_items.first().discounts, 0)

    def test_order_item_with_trial_amount(self):
        free_month = Offer.objects.get(pk=7)
        discount = free_month.current_price()
        self.new_invoice.add_offer(free_month)
        self.assertEqual(self.new_invoice.order_items.first().trial_amount,
                         free_month.current_price() - discount)

    def test_order_item_no_trial_amount(self):
        month_offer = Offer.objects.get(pk=6)
        self.new_invoice.add_offer(month_offer)
        self.assertEqual(self.new_invoice.get_recurring_total(),
                         month_offer.current_price())
        self.assertEqual(self.new_invoice.get_discounts(), 0)
        self.assertEqual(self.new_invoice.order_items.first().trial_amount,
                         month_offer.current_price())

    def test_get_promos(self):
        invoice = Invoice.objects.get(pk=1)

        self.assertNotEquals(invoice.get_promos(), "")

    def test_get_promos_none(self):
        self.assertEquals(self.new_invoice.get_promos(), "")

    def test_get_promos_empty(self):
        self.new_invoice.vendor_notes['promos'] = {}
        self.new_invoice.save()
        self.assertEquals(self.new_invoice.get_promos(), "")

    def test_soft_delete(self):
        invoice = Invoice.objects.all().first()
        invoice_count_before_deletion = Invoice.objects.all().count()
        invoice.delete()

        deleted_invoice_difference = Invoice.objects.all().count(
        ) - Invoice.not_deleted.count()

        self.assertEqual(
            Invoice.objects.all().count() - deleted_invoice_difference,
            Invoice.not_deleted.count())
        self.assertEquals(invoice_count_before_deletion,
                          Invoice.objects.all().count())

    def test_get_next_billing_date_month(self):
        pass

    def test_get_next_billing_price(self):
        pass