from datetime import datetime

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

from coupons.forms import CouponForm
from coupons.models import Coupon


class DefaultCouponTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="user1")
        self.coupon = Coupon.objects.create_coupon('monetary', 100)
        self.form = CouponForm(data={'code': self.coupon.code}, user=self.user)

    def test_redeem(self):
        self.coupon.redeem(self.user)
        self.assertTrue(self.coupon.is_redeemed)
        self.assertEquals(self.coupon.users.count(), 1)
        self.assertIsInstance(self.coupon.users.first().redeemed_at, datetime)
        self.assertEquals(self.coupon.users.first().user, self.user)

    def test_redeem_via_form(self):
        # form should be valid
        self.assertTrue(self.form.is_valid())
        # perform redeem
        self.coupon.redeem(self.user)
        # coupon should be redeemed properly now
        self.assertTrue(self.coupon.is_redeemed)
        self.assertEquals(self.coupon.users.count(), 1)
        self.assertIsInstance(self.coupon.users.first().redeemed_at, datetime)
        self.assertEquals(self.coupon.users.first().user, self.user)
        # form should be invalid after redeem
        self.assertTrue(self.form.is_valid())

@@ -364,88 +364,8 @@
 100)
-%0A        self.form = CouponForm(data=%7B'code': self.coupon.code%7D, user=self.user)
 %0A%0A  
@@ -709,32 +709,107 @@
 via_form(self):%0A
+        form = CouponForm(data=%7B'code': self.coupon.code%7D, user=self.user)%0A
         # form s
@@ -839,37 +839,32 @@
 self.assertTrue(
-self.
 form.is_valid())
@@ -1299,21 +1299,629 @@
 ertTrue(
-self.
+form.is_valid())%0A%0A    def test_redeem_via_form_without_user(self):%0A        form = CouponForm(data=%7B'code': self.coupon.code%7D)%0A        # form should be valid%0A        self.assertTrue(form.is_valid())%0A        # perform redeem%0A        self.coupon.redeem()%0A        # coupon should be redeemed properly now%0A        self.assertTrue(self.coupon.is_redeemed)%0A        self.assertEquals(self.coupon.users.count(), 1)%0A        self.assertIsInstance(self.coupon.users.first().redeemed_at, datetime)%0A        self.assertIsNone(self.coupon.users.first().user)%0A        # form should be invalid after redeem%0A        self.assertTrue(
 form.is_

test redeem without user