<commit_before>from django.http import HttpRequest
from django.test import TestCase

from referral.models import UserReferrer
from referral import settings
from referral.tests.factories import CampaignFactory, ReferrerFactory, UserReferrerFactory, UserFactory


class CampaignTestCase(TestCase):
    def test_model(self):
        obj = CampaignFactory()
        self.assertTrue(obj.pk)

    def test_count_users(self):
        obj = CampaignFactory()
        self.assertEqual(obj.count_users(), 0)
        ReferrerFactory(campaign=obj)
        self.assertEqual(obj.count_users(), 0)
        ref = ReferrerFactory(campaign=obj)
        UserReferrerFactory(referrer=ref)
        self.assertEqual(obj.count_users(), 1)


class ReferrerTestCase(TestCase):
    def test_model(self):
        obj = ReferrerFactory()
        self.assertTrue(obj.pk)

    def test_match_campaign(self):
        obj = ReferrerFactory()
        obj.match_campaign()
        CampaignFactory()
        self.assertIsNone(obj.campaign)
        campaign = CampaignFactory(pattern="Test Referrer")
        obj.match_campaign()
        self.assertEqual(obj.campaign, campaign)


class UserReferrerTestCase(TestCase):
    def test_model(self):
        obj = UserReferrerFactory()
        self.assertTrue(obj.pk)

    def test_repr(self):
        obj = UserReferrerFactory()
        self.assertEqual(unicode(obj), "{} -> {}".format(obj.user.username, obj.referrer.name))

    def test_manager_apply_referrer_no_ref(self):
        user = UserFactory()
        request = HttpRequest()
        request.session = {}
        UserReferrer.objects.apply_referrer(user, request)
        with self.assertRaises(UserReferrer.DoesNotExist):<commit_msg>use str instead of unicode in test_repr<commit_after>from django.http import HttpRequest
from django.test import TestCase

from referral.models import UserReferrer
from referral import settings
from referral.tests.factories import CampaignFactory, ReferrerFactory, UserReferrerFactory, UserFactory


class CampaignTestCase(TestCase):
    def test_model(self):
        obj = CampaignFactory()
        self.assertTrue(obj.pk)

    def test_count_users(self):
        obj = CampaignFactory()
        self.assertEqual(obj.count_users(), 0)
        ReferrerFactory(campaign=obj)
        self.assertEqual(obj.count_users(), 0)
        ref = ReferrerFactory(campaign=obj)
        UserReferrerFactory(referrer=ref)
        self.assertEqual(obj.count_users(), 1)


class ReferrerTestCase(TestCase):
    def test_model(self):
        obj = ReferrerFactory()
        self.assertTrue(obj.pk)

    def test_match_campaign(self):
        obj = ReferrerFactory()
        obj.match_campaign()
        CampaignFactory()
        self.assertIsNone(obj.campaign)
        campaign = CampaignFactory(pattern="Test Referrer")
        obj.match_campaign()
        self.assertEqual(obj.campaign, campaign)


class UserReferrerTestCase(TestCase):
    def test_model(self):
        obj = UserReferrerFactory()
        self.assertTrue(obj.pk)

    def test_repr(self):
        obj = UserReferrerFactory()
        self.assertEqual(str(obj), "{} -> {}".format(obj.user.username, obj.referrer.name))

    def test_manager_apply_referrer_no_ref(self):
        user = UserFactory()
        request = HttpRequest()
        request.session = {}
        UserReferrer.objects.apply_referrer(user, request)
        with self.assertRaises(UserReferrer.DoesNotExist):
{'fraction_non_alphanumeric': 0.09731934731934731, 'fraction_numerical': 0.0017482517482517483, 'mean_word_length': 3.3400758533501897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4985513', 'n_tokens_mistral': 994, 'n_tokens_neox': 981, 'n_words': 183}