"""Shop model tests."""

from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction
from django.test import TestCase

from shop.models import Shop


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

    def test_str_is_name(self):
        shop = Shop.objects.create(slug="my-shop", name="My Shop", created_by=self.user)
        self.assertEqual(str(shop), "My Shop")

    def test_slug_must_be_unique(self):
        Shop.objects.create(slug="dup", name="A", created_by=self.user)
        with self.assertRaises(IntegrityError):
            with transaction.atomic():
                Shop.objects.create(slug="dup", name="B", created_by=self.user)

    def test_slug_validator_rejects_invalid(self):
        shop = Shop(slug="Bad Slug!", name="X", created_by=self.user)
        with self.assertRaises(ValidationError):
            shop.full_clean()

    def test_slug_validator_accepts_valid(self):
        shop = Shop(slug="good-slug-1", name="X", created_by=self.user)
        shop.full_clean()  # must not raise

    def test_defaults(self):
        shop = Shop.objects.create(slug="d", name="D", created_by=self.user)
        self.assertEqual(shop.shop_type, Shop.ShopType.PERSONAL)
        self.assertFalse(shop.is_published)
        self.assertIsNone(shop.corporation)
