from decimal import Decimal as D

from django.test import TestCase

from oscar.apps.basket.models import Basket
from oscar.apps.partner import strategy
from oscar.test import factories
from oscar.apps.catalogue.models import Option


class TestAddingAProductToABasket(TestCase):

    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(
            currency='GBP',
            product=self.product, price_excl_tax=D('10.00'))
        self.stockinfo = factories.create_stockinfo(self.record)
        self.basket.add(self.product)

    def test_creates_a_line(self):
        self.assertEqual(1, self.basket.num_lines)

    def test_sets_line_prices(self):
        line = self.basket.lines.all()[0]
        self.assertEqual(line.price_incl_tax, self.stockinfo.price.incl_tax)
        self.assertEqual(line.price_excl_tax, self.stockinfo.price.excl_tax)

    def test_means_another_currency_product_cannot_be_added(self):
        product = factories.create_product()
        factories.create_stockrecord(
            currency='USD', product=product, price_excl_tax=D('20.00'))
        with self.assertRaises(ValueError):
            self.basket.add(product)


class TestANonEmptyBasket(TestCase):

    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(
            self.product, price_excl_tax=D('10.00'))
        self.stockinfo = factories.create_stockinfo(self.record)
        self.basket.add(self.product, 10)

    def test_can_be_flushed(self):
        self.basket.flush()
        self.assertEqual(self.basket.num_items, 0)

    def test_returns_correct_product_quantity(self):
        self.assertEqual(10, self.basket.product_quantity(
            self.product))

    def test_returns_correct_line_quantity_for_existing_product_and_stockrecord(self):
        self.assertEqual(10, self.basket.line_quantity(
            self.product, self.record))

    def test_returns_zero_line_quantity_for_alternative_stockrecord(self):
        record = factories.create_stockrecord(
            self.product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            self.product, record))

    def test_returns_zero_line_quantity_for_missing_product_and_stockrecord(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            product, record))

    def test_returns_correct_quantity_for_existing_product_and_stockrecord_and_options(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        option = Option.objects.create(name="Message")
        options = [{"option": option, "value": "2"}]

        self.basket.add(product, options=options)
        self.assertEqual(0, self.basket.line_quantity(
            product, record))
        self.assertEqual(1, self.basket.line_quantity(
            product, record, options))


class TestMergingTwoBaskets(TestCase):

    def setUp(self):
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(
            self.product, price_excl_tax=D('10.00'))
        self.stockinfo = factories.create_stockinfo(self.record)

        self.main_basket = Basket()
        self.main_basket.strategy = strategy.Default()
        self.main_basket.add(self.product, quantity=2)

        self.merge_basket = Basket()
        self.merge_basket.strategy = strategy.Default()
        self.merge_basket.add(self.product, quantity=1)

        self.main_basket.merge(self.merge_basket)

    def test_doesnt_sum_quantities(self):
        self.assertEquals(1, self.main_basket.num_lines)

    def test_changes_status_of_merge_basket(self):
        self.assertEquals(Basket.MERGED, self.merge_basket.status)


class TestASubmittedBasket(TestCase):

    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.basket.submit()

    def test_has_correct_status(self):
        self.assertTrue(self.basket.is_submitted)

    def test_can_be_edited(self):
        self.assertFalse(self.basket.can_be_edited)

[((14, 22, 14, 30), 'oscar.apps.basket.models.Basket', 'Basket', ({}, {}), '()', False, 'from oscar.apps.basket.models import Basket\n'), ((15, 31, 15, 49), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ({}, {}), '()', False, 'from oscar.apps.partner import strategy\n'), ((16, 23, 16, 49), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((20, 25, 20, 64), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', ({(20, 52, 20, 63): 'self.record'}, {}), '(self.record)', False, 'from oscar.test import factories\n'), ((32, 18, 32, 44), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((42, 22, 42, 30), 'oscar.apps.basket.models.Basket', 'Basket', ({}, {}), '()', False, 'from oscar.apps.basket.models import Basket\n'), ((43, 31, 43, 49), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ({}, {}), '()', False, 'from oscar.apps.partner import strategy\n'), ((44, 23, 44, 49), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((47, 25, 47, 64), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', ({(47, 52, 47, 63): 'self.record'}, {}), '(self.record)', False, 'from oscar.test import factories\n'), ((69, 18, 69, 44), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((76, 18, 76, 44), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((79, 17, 79, 54), 'oscar.apps.catalogue.models.Option.objects.create', 'Option.objects.create', (), '', False, 'from oscar.apps.catalogue.models import Option\n'), ((92, 23, 92, 49), 'oscar.test.factories.create_product', 'factories.create_product', ({}, {}), '()', False, 'from oscar.test import factories\n'), ((95, 25, 95, 64), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', ({(95, 52, 95, 63): 'self.record'}, {}), '(self.record)', False, 'from oscar.test import factories\n'), ((97, 27, 97, 35), 'oscar.apps.basket.models.Basket', 'Basket', ({}, {}), '()', False, 'from oscar.apps.basket.models import Basket\n'), ((98, 36, 98, 54), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ({}, {}), '()', False, 'from oscar.apps.partner import strategy\n'), ((101, 28, 101, 36), 'oscar.apps.basket.models.Basket', 'Basket', ({}, {}), '()', False, 'from oscar.apps.basket.models import Basket\n'), ((102, 37, 102, 55), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ({}, {}), '()', False, 'from oscar.apps.partner import strategy\n'), ((117, 22, 117, 30), 'oscar.apps.basket.models.Basket', 'Basket', ({}, {}), '()', False, 'from oscar.apps.basket.models import Basket\n'), ((118, 31, 118, 49), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ({}, {}), '()', False, 'from oscar.apps.partner import strategy\n'), ((19, 49, 19, 59), 'decimal.Decimal', 'D', ({(19, 51, 19, 58): '"""10.00"""'}, {}), "('10.00')", True, 'from decimal import Decimal as D\n'), ((34, 60, 34, 70), 'decimal.Decimal', 'D', ({(34, 62, 34, 69): '"""20.00"""'}, {}), "('20.00')", True, 'from decimal import Decimal as D\n'), ((46, 41, 46, 51), 'decimal.Decimal', 'D', ({(46, 43, 46, 50): '"""10.00"""'}, {}), "('10.00')", True, 'from decimal import Decimal as D\n'), ((64, 41, 64, 50), 'decimal.Decimal', 'D', ({(64, 43, 64, 49): '"""5.00"""'}, {}), "('5.00')", True, 'from decimal import Decimal as D\n'), ((71, 36, 71, 45), 'decimal.Decimal', 'D', ({(71, 38, 71, 44): '"""5.00"""'}, {}), "('5.00')", True, 'from decimal import Decimal as D\n'), ((78, 36, 78, 45), 'decimal.Decimal', 'D', ({(78, 38, 78, 44): '"""5.00"""'}, {}), "('5.00')", True, 'from decimal import Decimal as D\n'), ((94, 41, 94, 51), 'decimal.Decimal', 'D', ({(94, 43, 94, 50): '"""10.00"""'}, {}), "('10.00')", True, 'from decimal import Decimal as D\n')]