<commit_before>from datetime import timedelta
import re

from django.utils import timezone
from django.test import TestCase

from coupons.models import Coupon
from coupons.settings import CODE_LENGTH, CODE_CHARS


class CouponTestCase(TestCase):
    def test_generate_code(self):
        self.assertIsNotNone(re.match("^[%s]{%d}" % (CODE_CHARS, CODE_LENGTH,), Coupon.generate_code()))

    def test_save(self):
        coupon = Coupon(type='monetary', value=100)
        coupon.save()
        self.assertTrue(coupon.pk)

    def test_create_coupon(self):
        coupon = Coupon.objects.create_coupon('monetary', 100)
        self.assertTrue(coupon.pk)

    def test_create_coupons(self):
        coupons = Coupon.objects.create_coupons(50, 'monetary', 100)
        for coupon in coupons:
            self.assertTrue(coupon.pk)

    def test_redeem(self):<commit_msg>add test case for segmented code generation<commit_after>from datetime import timedelta
import re

from django.utils import timezone
from django.test import TestCase

from coupons.models import Coupon
from coupons.settings import (
    CODE_LENGTH,
    CODE_CHARS,
    SEGMENT_LENGTH,
    SEGMENT_SEPARATOR,
)


class CouponTestCase(TestCase):
    def test_generate_code(self):
        self.assertIsNotNone(re.match("^[%s]{%d}" % (CODE_CHARS, CODE_LENGTH,), Coupon.generate_code()))

    def test_generate_code_segmented(self):
        num_segments = CODE_LENGTH // SEGMENT_LENGTH  # full ones
        num_rest = CODE_LENGTH - num_segments * SEGMENT_LENGTH
        self.assertIsNotNone(
            re.match(
                "^([{chars}]{{{sl}}}{sep}){{{ns}}}[{chars}]{{{nr}}}$".format(
                    chars=CODE_CHARS,
                    sep=SEGMENT_SEPARATOR,
                    sl=SEGMENT_LENGTH,
                    ns=num_segments,
                    nr=num_rest),
                Coupon.generate_code(True)
            )
        )

    def test_save(self):
        coupon = Coupon(type='monetary', value=100)
        coupon.save()
        self.assertTrue(coupon.pk)

    def test_create_coupon(self):
        coupon = Coupon.objects.create_coupon('monetary', 100)
        self.assertTrue(coupon.pk)

    def test_create_coupons(self):
        coupons = Coupon.objects.create_coupons(50, 'monetary', 100)
        for coupon in coupons:
            self.assertTrue(coupon.pk)

    def test_redeem(self):
{'fraction_non_alphanumeric': 0.10621326616288833, 'fraction_numerical': 0.009235936188077247, 'mean_word_length': 2.8435483870967744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '94650', 'n_tokens_mistral': 788, 'n_tokens_neox': 765, 'n_words': 141}