from unittest import TestCase
from django.http import QueryDict
from catalog.constants import INSTATE, REGULAR

from catalog.models import Subscription
from catalog.tests import SubscriptionFixture
from storefront.forms import SubscriptionForm

import pytest


# @pytest.mark.django_db
class SubscriptionFormFixture(SubscriptionFixture, TestCase):

    expected_is_valid = True

    @property
    def expected_subscription(self):
        return Subscription.objects.get_or_create(defaults=self.subscription_data)[0]

    def setUp(self):
        self.expected_subscription

        self.form = SubscriptionForm(
            QueryDict("shipping_method=0&title=0&duration=0"), shipping_type=INSTATE
        )

    def test_init(self):
        self.assertIsInstance(self.form, SubscriptionForm)

        self.assertEqual(
            [c for c in self.form.fields["title"].choices],
            [(0, self.expected_subscription.title)],
        )

        self.assertEqual(
            [c for c in self.form.fields["duration"].choices],
            [(0, self.expected_subscription.duration)],
        )

        # not dynamic
        self.assertEqual(
            [(0, REGULAR)], [(0, self.expected_subscription.shipping_method)]
        )

    def test_is_valid(self):
        self.assertTrue(self.form.is_valid())
