<commit_before>from __future__ import absolute_import

from django.utils.unittest import TestCase
from django.test.client import RequestFactory
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.sessions.backends.db import SessionStore as DatabaseSession

from experiments import stats, counters
from experiments.utils import WebUser, PARTICIPANT_KEY, GOAL_KEY
from experiments.models import Experiment, ENABLED_STATE

request_factory = RequestFactory()
TEST_KEY = 'CounterTestCase'
TEST_ALTERNATIVE = 'blue'
TEST_GOAL = 'buy'

class StatsTestCase(TestCase):
    def test_flatten(self):
        self.assertEqual(
            list(stats.flatten([1,[2,[3]],4,5])),
            [1,2,3,4,5]
            )


class CounterTestCase(TestCase):
    def setUp(self):
        counters.reset(TEST_KEY)
        self.assertEqual(counters.get(TEST_KEY), 0)

    def tearDown(self):
        counters.reset(TEST_KEY)

    def test_add_item(self):
        counters.increment(TEST_KEY, 'fred')
        self.assertEqual(counters.get(TEST_KEY), 1)

    def test_add_multiple_items(self):
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'barney')
        counters.increment(TEST_KEY, 'george')
        self.assertEqual(counters.get(TEST_KEY), 3)

    def test_add_duplicate_item(self):
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'fred')
        self.assertEqual(counters.get(TEST_KEY), 1)

    def test_delete_key(self):
        counters.increment(TEST_KEY, 'fred')
        counters.reset(TEST_KEY)
        self.assertEqual(counters.get(TEST_KEY), 0)


class WebUserTests:
    def setUp(self):
        self.experiment = Experiment(name='backgroundcolor', state=ENABLED_STATE)
        self.experiment.save()
        self.request = request_factory.get('/')
        self.request.session = DatabaseSession()

    def tearDown(self):
        self.experiment.delete()

    def confirm_human(self, experiment_user):
        raise NotImplementedError

    def participants(self, alternative):
        return counters.get(PARTICIPANT_KEY % (self.experiment.name, alternative))

    def test_user_enrolls(self):
        experiment_user = WebUser(self.request)
        experiment_user.set_enrollment(self.experiment, TEST_ALTERNATIVE)
        self.assertEqual(experiment_user.get_enrollment(self.experiment), TEST_ALTERNATIVE)

    def test_record_goal_increments_counts(self):
        experiment_user = WebUser(self.request)
        self.confirm_human(experiment_user)
        experiment_user.set_enrollment(self.experiment, TEST_ALTERNATIVE)

        self.assertEqual(counters.get(GOAL_KEY % (self.experiment.name, TEST_ALTERNATIVE, TEST_GOAL)), 0)<commit_msg>Add test for unenrolled users<commit_after>from __future__ import absolute_import

from django.utils.unittest import TestCase
from django.test.client import RequestFactory
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.sessions.backends.db import SessionStore as DatabaseSession

from experiments import stats, counters
from experiments.utils import WebUser, PARTICIPANT_KEY, GOAL_KEY
from experiments.models import Experiment, ENABLED_STATE

request_factory = RequestFactory()
TEST_KEY = 'CounterTestCase'
TEST_ALTERNATIVE = 'blue'
TEST_GOAL = 'buy'

class StatsTestCase(TestCase):
    def test_flatten(self):
        self.assertEqual(
            list(stats.flatten([1,[2,[3]],4,5])),
            [1,2,3,4,5]
            )


class CounterTestCase(TestCase):
    def setUp(self):
        counters.reset(TEST_KEY)
        self.assertEqual(counters.get(TEST_KEY), 0)

    def tearDown(self):
        counters.reset(TEST_KEY)

    def test_add_item(self):
        counters.increment(TEST_KEY, 'fred')
        self.assertEqual(counters.get(TEST_KEY), 1)

    def test_add_multiple_items(self):
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'barney')
        counters.increment(TEST_KEY, 'george')
        self.assertEqual(counters.get(TEST_KEY), 3)

    def test_add_duplicate_item(self):
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'fred')
        counters.increment(TEST_KEY, 'fred')
        self.assertEqual(counters.get(TEST_KEY), 1)

    def test_delete_key(self):
        counters.increment(TEST_KEY, 'fred')
        counters.reset(TEST_KEY)
        self.assertEqual(counters.get(TEST_KEY), 0)


class WebUserTests:
    def setUp(self):
        self.experiment = Experiment(name='backgroundcolor', state=ENABLED_STATE)
        self.experiment.save()
        self.request = request_factory.get('/')
        self.request.session = DatabaseSession()

    def tearDown(self):
        self.experiment.delete()

    def confirm_human(self, experiment_user):
        raise NotImplementedError

    def participants(self, alternative):
        return counters.get(PARTICIPANT_KEY % (self.experiment.name, alternative))

    def enrollment_initially_none(self,):
        experiment_user = WebUser(self.request)
        self.assertEqual(experiment_user.get_enrollment(self.experiment), None)

    def test_user_enrolls(self):
        experiment_user = WebUser(self.request)
        experiment_user.set_enrollment(self.experiment, TEST_ALTERNATIVE)
        self.assertEqual(experiment_user.get_enrollment(self.experiment), TEST_ALTERNATIVE)

    def test_record_goal_increments_counts(self):
        experiment_user = WebUser(self.request)
        self.confirm_human(experiment_user)
        experiment_user.set_enrollment(self.experiment, TEST_ALTERNATIVE)

        self.assertEqual(counters.get(GOAL_KEY % (self.experiment.name, TEST_ALTERNATIVE, TEST_GOAL)), 0)
{'fraction_non_alphanumeric': 0.10040090639707164, 'fraction_numerical': 0.005577828133170646, 'mean_word_length': 3.8503803888419275, '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': '5859807', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1758, 'n_words': 297}