from django.test import TestCase

from oscar.core.loading import get_class, get_model

Product = get_model("catalogue", "Product")
ProductClass = get_model("catalogue", "ProductClass")
Basket = get_model("basket", "Basket")
Selector = get_class("partner.strategy", "Selector")


class SelectCorrectStockRecordTest(TestCase):
    fixtures = ["taxrounding"]

    def test_price_for_standalone(self):
        product = Product.objects.get(pk=1)
        strategy = Selector().strategy()
        purchase_info = strategy.fetch_for_product(product)
        # we only want to know that there is a price available to make sure
        # the stockrecord selection is not broken
        self.assertTrue(purchase_info.price.excl_tax > 0)
        self.assertTrue(purchase_info.price.incl_tax > 0)

    def test_price_for_parent(self):
        product = Product.objects.get(pk=2)
        strategy = Selector().strategy()
        purchase_info = strategy.fetch_for_product(product)
        # parents don't have a price with fetch_for_product
        # as they don't have stockrecords
        self.assertIsNone(purchase_info.price.excl_tax)
        self.assertIsNone(purchase_info.price.incl_tax)

        # but fetch_for_parent should return a price based on the child
        # stockrecords
        purchase_info = strategy.fetch_for_parent(product)
        self.assertTrue(purchase_info.price.excl_tax > 0)
        self.assertTrue(purchase_info.price.incl_tax > 0)

    def test_price_for_child(self):
        product = Product.objects.get(pk=3)
        strategy = Selector().strategy()
        purchase_info = strategy.fetch_for_product(product)
        # we only want to know that there is a price available to make sure
        # the stockrecord selection is not broken
        self.assertTrue(purchase_info.price.excl_tax > 0)
        self.assertTrue(purchase_info.price.incl_tax > 0)

        # when no stock is tracked, we still should get a price
        ProductClass.objects.filter(id=322).update(track_stock=False)
        purchase_info = strategy.fetch_for_product(product)
        self.assertTrue(purchase_info.price.excl_tax > 0)
        self.assertTrue(purchase_info.price.incl_tax > 0)
