"""Hull listing / section tests."""

from django.contrib.auth.models import User
from django.test import TestCase

from shop.models import HULL, HullListing, Shop


class EnabledSectionsTests(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="u1")

    def test_section_enabled(self):
        shop = Shop.objects.create(
            slug="s", name="S", created_by=self.user, enabled_sections=[HULL]
        )
        self.assertTrue(shop.section_enabled(HULL))

    def test_section_disabled_by_default(self):
        shop = Shop.objects.create(slug="s2", name="S2", created_by=self.user)
        self.assertFalse(shop.section_enabled(HULL))


class HullListingModelTests(TestCase):
    def setUp(self):
        self.shop = Shop.objects.create(
            slug="hl", name="HL", created_by=User.objects.create(username="o")
        )

    def _eve_type(self):
        from eveuniverse.models import EveCategory, EveGroup, EveType

        cat = EveCategory.objects.create(id=6, name="Ship", published=True)
        grp = EveGroup.objects.create(
            id=30, name="Titan", eve_category=cat, published=True
        )
        return EveType.objects.create(
            id=671, name="Erebus", eve_group=grp, published=True
        )

    def test_price_display_ask_when_zero(self):
        hl = HullListing.objects.create(shop=self.shop, eve_type=self._eve_type())
        self.assertEqual(hl.price_display, "Ask")

    def test_price_display_formats_isk(self):
        hl = HullListing.objects.create(
            shop=self.shop, eve_type=self._eve_type(), price=149000000000
        )
        self.assertEqual(hl.price_display, "149B ISK")

    def test_unique_per_shop_and_type(self):
        from django.db import IntegrityError, transaction

        t = self._eve_type()
        HullListing.objects.create(shop=self.shop, eve_type=t)
        with self.assertRaises(IntegrityError):
            with transaction.atomic():
                HullListing.objects.create(shop=self.shop, eve_type=t)
