class Items(TestCase): def setUp(self): self.cart = Cart.objects.create() self.product_1 = Product.objects.create(price=Decimal("0.01"), name="ABC") self.product_2 = Product.objects.create(price=Decimal("11.22"), name="CDE") self.item_1 = CartItem.objects.create(cart=self.cart, product=self.product_1) self.item_2 = CartItem.objects.create(cart=self.cart, product=self.product_2) self.cart_summary = CartSummary(self.cart) def test_items_creation(self): assert hasattr(self.cart_summary, "items"), "items attribute missing from instance_summary" def test_items_simple(self): self.assertEqual(len(self.cart_summary.items), 2) self.assertEqual(self.cart_summary.items[0].pk, self.item_1.pk) self.assertEqual(self.cart_summary.items[1].pk, self.item_2.pk) def test_items_total(self): self.assertEqual(self.cart_summary.items_total, Decimal("11.23")) def test_items_amount(self): voucher = Voucher.objects.create(percent=10) self.cart.vouchers.add(voucher) self.assertEqual(self.cart_summary.vouchers_total, Decimal("-1.12")) def test_cache_item_as(self): voucher = Voucher.objects.create(percent=10) self.cart.vouchers.add(voucher) total_one = self.cart_summary.vouchers_total self.assertEqual(self.cart_summary.vouchers[0].VOUCH_AMOUNT_XYZ, Decimal("-1.12")) # Change the total, and check that the old one was cached total_one = self.cart_summary.vouchers_total Voucher.objects.all().update(percent=50) total_two = self.cart_summary.vouchers_total self.assertEqual(total_one, total_two) def test_editable_table(self): form = self.cart_summary.form() self.assertEqual(form.as_table(), "") def test_editable_table_data(self): form = self.cart_summary.form() data = form.table_data() assert data self.assertEqual(len(data), len(self.cart_summary._meta.elements)) self.assertEqual(len(data[0]), 3) element_names = self.cart_summary._meta.elements.keys() self.assertEqual([i[0] for i in data], element_names) self.assertEqual([i[2] for i in data], [getattr(self.cart_summary, n) for n in element_names])
class Extras(TestCase): def setUp(self): self.cart = Cart.objects.create() self.order = Order.objects.create() self.cart_summary = CartSummary(instance=self.cart) self.order_summary = OrderSummary(instance=self.order) def test_extras_creation(self): """ Tests that the extras in the summary are created properly """ assert hasattr( self.cart_summary, "my_commission" ), "my_commission attribute missing from instance_summary" assert hasattr(self.cart_summary, "tax"), "tax attribute missing from instance_summary" assert hasattr( self.cart_summary, "discount"), "discount attribute missing from instance_summary" assert hasattr( self.cart_summary, "delivery"), "delivery attribute missing from instance_summary" assert hasattr( self.order_summary, "delivery"), "delivery attribute missing from instance_summary" def test_name_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.verbose_name, u"Rabatt") self.assertEqual(self.cart_summary.tax.verbose_name, u"GST") def test_name_default(self): """ Tests that the name attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.verbose_name, u"My commission") def test_name_callable(self): """ Tests that the name attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.verbose_name, u"Lieferung") def test_description_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.description, u"Mates Rates") self.assertEqual(self.cart_summary.tax.description, u"15%") def test_description_default(self): """ Tests that the description attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.description, None) def test_description_callable(self): """ Tests that the description attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.description, u"Interstate") def test_amount_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.amount, Decimal("-12.23")) def test_amount_default(self): """ Tests that the description attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.amount, Decimal("10.02")) self.assertEqual(self.cart_summary.tax.amount, Decimal("10.03")) def test_amount_callable(self): """ Tests that the description attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.amount, Decimal("10.01")) def test_included_explicit(self): """ Tests that the included attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.included, False) self.assertEqual(self.cart_summary.tax.included, True) def test_included_default(self): """ Tests that the included attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.included, False) def test_included_callable(self): """ Tests that the included attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.included, False) def test_editable(self): """ Tests that a form is provided. """ form = self.cart_summary.form() data = { 'items-TOTAL_FORMS': 0, 'items-INITIAL_FORMS': 0, 'items-MAX_NUM_FORMS': '', 'delivery-address': '123 Main St', 'discount_code': 'CDE', } form = self.cart_summary.form(data) assert form.is_valid(), str(form.errors) form.save() self.assertEqual(self.cart.discount_code, 'CDE') self.assertEqual(self.cart.address, '123 Main St') def test_editable_errors(self): form = self.cart_summary.form() data = { 'items-TOTAL_FORMS': 0, 'items-INITIAL_FORMS': 0, 'items-MAX_NUM_FORMS': '', # 'delivery-address' missing 'discount_code': 'CDE', } form = self.cart_summary.form(data) assert form.errors, "No error raised for form"
class Items(TestCase): def setUp(self): self.cart = Cart.objects.create() self.product_1 = Product.objects.create(price=Decimal("0.01"), name="ABC") self.product_2 = Product.objects.create(price=Decimal("11.22"), name="CDE") self.item_1 = CartItem.objects.create(cart=self.cart, product=self.product_1) self.item_2 = CartItem.objects.create(cart=self.cart, product=self.product_2) self.cart_summary = CartSummary(self.cart) def test_items_creation(self): assert hasattr( self.cart_summary, "items"), "items attribute missing from instance_summary" def test_items_simple(self): self.assertEqual(len(self.cart_summary.items), 2) self.assertEqual(self.cart_summary.items[0].pk, self.item_1.pk) self.assertEqual(self.cart_summary.items[1].pk, self.item_2.pk) def test_items_total(self): self.assertEqual(self.cart_summary.items_total, Decimal("11.23")) def test_items_amount(self): voucher = Voucher.objects.create(percent=10) self.cart.vouchers.add(voucher) self.assertEqual(self.cart_summary.vouchers_total, Decimal("-1.12")) def test_cache_item_as(self): voucher = Voucher.objects.create(percent=10) self.cart.vouchers.add(voucher) total_one = self.cart_summary.vouchers_total self.assertEqual(self.cart_summary.vouchers[0].VOUCH_AMOUNT_XYZ, Decimal("-1.12")) # Change the total, and check that the old one was cached total_one = self.cart_summary.vouchers_total Voucher.objects.all().update(percent=50) total_two = self.cart_summary.vouchers_total self.assertEqual(total_one, total_two) def test_editable_table(self): form = self.cart_summary.form() self.assertEqual(form.as_table(), "") def test_editable_table_data(self): form = self.cart_summary.form() data = form.table_data() assert data self.assertEqual(len(data), len(self.cart_summary._meta.elements)) self.assertEqual(len(data[0]), 3) element_names = self.cart_summary._meta.elements.keys() self.assertEqual([i[0] for i in data], element_names) self.assertEqual( [i[2] for i in data], [getattr(self.cart_summary, n) for n in element_names])
class Extras(TestCase): def setUp(self): self.cart = Cart.objects.create() self.order = Order.objects.create() self.cart_summary = CartSummary(instance=self.cart) self.order_summary = OrderSummary(instance=self.order) def test_extras_creation(self): """ Tests that the extras in the summary are created properly """ assert hasattr(self.cart_summary, "my_commission"), "my_commission attribute missing from instance_summary" assert hasattr(self.cart_summary, "tax"), "tax attribute missing from instance_summary" assert hasattr(self.cart_summary, "discount"), "discount attribute missing from instance_summary" assert hasattr(self.cart_summary, "delivery"), "delivery attribute missing from instance_summary" assert hasattr(self.order_summary, "delivery"), "delivery attribute missing from instance_summary" def test_name_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.verbose_name, u"Rabatt") self.assertEqual(self.cart_summary.tax.verbose_name, u"GST") def test_name_default(self): """ Tests that the name attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.verbose_name, u"My commission") def test_name_callable(self): """ Tests that the name attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.verbose_name, u"Lieferung") def test_description_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.description, u"Mates Rates") self.assertEqual(self.cart_summary.tax.description, u"15%") def test_description_default(self): """ Tests that the description attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.description, None) def test_description_callable(self): """ Tests that the description attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.description, u"Interstate") def test_amount_explicit(self): """ Tests that the name attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.amount, Decimal("-12.23")) def test_amount_default(self): """ Tests that the description attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.amount, Decimal("10.02")) self.assertEqual(self.cart_summary.tax.amount, Decimal("10.03")) def test_amount_callable(self): """ Tests that the description attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.amount, Decimal("10.01")) def test_included_explicit(self): """ Tests that the included attribute can be explicitly set.""" self.assertEqual(self.cart_summary.discount.included, False) self.assertEqual(self.cart_summary.tax.included, True) def test_included_default(self): """ Tests that the included attribute is set to a sensible default.""" self.assertEqual(self.cart_summary.my_commission.included, False) def test_included_callable(self): """ Tests that the included attribute can be set by a callable.""" self.assertEqual(self.cart_summary.delivery.included, False) def test_editable(self): """ Tests that a form is provided. """ form = self.cart_summary.form() data = {'items-TOTAL_FORMS': 0, 'items-INITIAL_FORMS': 0, 'items-MAX_NUM_FORMS': '', 'delivery-address': '123 Main St', 'discount_code': 'CDE', } form = self.cart_summary.form(data) assert form.is_valid(), str(form.errors) form.save() self.assertEqual(self.cart.discount_code, 'CDE') self.assertEqual(self.cart.address, '123 Main St') def test_editable_errors(self): form = self.cart_summary.form() data = {'items-TOTAL_FORMS': 0, 'items-INITIAL_FORMS': 0, 'items-MAX_NUM_FORMS': '', # 'delivery-address' missing 'discount_code': 'CDE', } form = self.cart_summary.form(data) assert form.errors, "No error raised for form"