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)

[((323, 331), 'oscar.apps.basket.models.Basket', 'Basket', ([], {}), '()\n', (329, 331), False, 'from oscar.apps.basket.models import Basket\n'), ((363, 381), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ([], {}), '()\n', (379, 381), False, 'from oscar.apps.partner import strategy\n'), ((405, 431), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (429, 431), False, 'from oscar.test import factories\n'), ((598, 637), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', (['self.record'], {}), '(self.record)\n', (624, 637), False, 'from oscar.test import factories\n'), ((1083, 1109), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (1107, 1109), False, 'from oscar.test import factories\n'), ((1384, 1392), 'oscar.apps.basket.models.Basket', 'Basket', ([], {}), '()\n', (1390, 1392), False, 'from oscar.apps.basket.models import Basket\n'), ((1424, 1442), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ([], {}), '()\n', (1440, 1442), False, 'from oscar.apps.partner import strategy\n'), ((1466, 1492), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (1490, 1492), False, 'from oscar.test import factories\n'), ((1623, 1662), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', (['self.record'], {}), '(self.record)\n', (1649, 1662), False, 'from oscar.test import factories\n'), ((2511, 2537), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (2535, 2537), False, 'from oscar.test import factories\n'), ((2830, 2856), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (2854, 2856), False, 'from oscar.test import factories\n'), ((2968, 3005), 'oscar.apps.catalogue.models.Option.objects.create', 'Option.objects.create', ([], {'name': '"""Message"""'}), "(name='Message')\n", (2989, 3005), False, 'from oscar.apps.catalogue.models import Option\n'), ((3375, 3401), 'oscar.test.factories.create_product', 'factories.create_product', ([], {}), '()\n', (3399, 3401), False, 'from oscar.test import factories\n'), ((3532, 3571), 'oscar.test.factories.create_stockinfo', 'factories.create_stockinfo', (['self.record'], {}), '(self.record)\n', (3558, 3571), False, 'from oscar.test import factories\n'), ((3600, 3608), 'oscar.apps.basket.models.Basket', 'Basket', ([], {}), '()\n', (3606, 3608), False, 'from oscar.apps.basket.models import Basket\n'), ((3645, 3663), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ([], {}), '()\n', (3661, 3663), False, 'from oscar.apps.partner import strategy\n'), ((3748, 3756), 'oscar.apps.basket.models.Basket', 'Basket', ([], {}), '()\n', (3754, 3756), False, 'from oscar.apps.basket.models import Basket\n'), ((3794, 3812), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ([], {}), '()\n', (3810, 3812), False, 'from oscar.apps.partner import strategy\n'), ((4223, 4231), 'oscar.apps.basket.models.Basket', 'Basket', ([], {}), '()\n', (4229, 4231), False, 'from oscar.apps.basket.models import Basket\n'), ((4263, 4281), 'oscar.apps.partner.strategy.Default', 'strategy.Default', ([], {}), '()\n', (4279, 4281), False, 'from oscar.apps.partner import strategy\n'), ((561, 571), 'decimal.Decimal', 'D', (['"""10.00"""'], {}), "('10.00')\n", (562, 571), True, 'from decimal import Decimal as D\n'), ((1208, 1218), 'decimal.Decimal', 'D', (['"""20.00"""'], {}), "('20.00')\n", (1209, 1218), True, 'from decimal import Decimal as D\n'), ((1586, 1596), 'decimal.Decimal', 'D', (['"""10.00"""'], {}), "('10.00')\n", (1587, 1596), True, 'from decimal import Decimal as D\n'), ((2308, 2317), 'decimal.Decimal', 'D', (['"""5.00"""'], {}), "('5.00')\n", (2309, 2317), True, 'from decimal import Decimal as D\n'), ((2621, 2630), 'decimal.Decimal', 'D', (['"""5.00"""'], {}), "('5.00')\n", (2622, 2630), True, 'from decimal import Decimal as D\n'), ((2940, 2949), 'decimal.Decimal', 'D', (['"""5.00"""'], {}), "('5.00')\n", (2941, 2949), True, 'from decimal import Decimal as D\n'), ((3495, 3505), 'decimal.Decimal', 'D', (['"""10.00"""'], {}), "('10.00')\n", (3496, 3505), True, 'from decimal import Decimal as D\n')]