<commit_before>from .tasks import send_survey, generate_survey, send_summary, issue_summaries, issue_surveys
from unittest.mock import MagicMock
from django.test.utils import override_settings
from django.test import Client, TestCase
from .models import Report, Team, Membership, User, Survey, Role
import recurrence
from datetime import time

class TestTasks(TestCase):
    fixtures = ["teamreporter/fixtures/roles.json"]

    @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                       CELERY_ALWAYS_EAGER=True,
                       BROKER_BACKEND='memory')

    def setUp(self):
        rule = recurrence.Rule(recurrence.WEEKLY, byday = list(range(7)))
        rec = recurrence.Recurrence(rrules = [rule])
        self.admin = User.objects.create(email="<EMAIL>", username="celery_admin")
        self.user = User.objects.create(email="<EMAIL>", username="celery_user")
        self.team = Team.objects.create(name = "celery_team_test", admin=self.admin)
        roles = Role.objects.all()
        self.membership = Membership.objects.create(user=self.user, team = self.team)
        for r in roles:
            self.membership.roles.add(r)

        self.team.membership_set.add(self.membership)
        self.report = Report.objects.create(team = self.team, recurrences = rec, survey_send_time=time(0, 0))
        self.report.can_issue_daily = MagicMock(return_value = True)

    def test_issue_survey(self):
        """test survey issuing"""
        result = issue_surveys.apply().get()
        self.assertTrue(result == 1)<commit_msg>continue fleshing out celery tasks unit tests<commit_after>from .tasks import send_survey, generate_survey, send_summary, issue_summaries, issue_surveys
from unittest.mock import MagicMock
from django.test.utils import override_settings
from django.test import Client, TestCase
from .models import Report, Team, Membership, User, Survey, Role
import recurrence
from datetime import time

class TestTasks(TestCase):
    fixtures = ["teamreporter/fixtures/roles.json"]

    @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                       CELERY_ALWAYS_EAGER=True,
                       BROKER_BACKEND='memory')

    def setUp(self):
        rule = recurrence.Rule(recurrence.WEEKLY, byday = list(range(7)))
        rec = recurrence.Recurrence(rrules = [rule])
        self.admin = User.objects.create(email="<EMAIL>", username="celery_admin")
        self.user = User.objects.create(email="<EMAIL>", username="celery_user")
        self.team = Team.objects.create(name = "celery_team_test", admin=self.admin)
        roles = Role.objects.all()
        self.membership = Membership.objects.create(user=self.user, team = self.team)
        for r in roles:
            self.membership.roles.add(r)

        self.team.membership_set.add(self.membership)
        self.report = Report.objects.create(team = self.team, recurrences = rec, survey_send_time=time(0, 0))
        self.report.can_issue_daily = MagicMock(return_value = True)

    def test_generate_survey(self):
        """
        tests survey generation
        Survey should be created if daily report exists/is valid and the user the survey is going to is actually on the team
        """

        result = generate_survey(self.user.id, self.report.get_daily()).get()  # should create a survey given a valid daily report and user on the team
        self.assertTrue(result)

        result = generate_survey(3, self.report.get_daily()).get()  # User is not on this team therefore survey shouldn't be created
        self.assertFalse(result)

    def test_issue_survey(self):
        """
        test survey issuing
        """
        result = issue_surveys.apply().get()
        self.assertTrue(result == 1)
        self.membership.roles.filter(name="contributor").delete()
        self.report.get_daily().delete()  # 'reset' daily report
        result = issue_surveys.apply().get()
        self.assertTrue(result == 0) #should be sent to contributors only only!!

    def test_send_survey(self):
        pass
{'fraction_non_alphanumeric': 0.10565292520365342, 'fraction_numerical': 0.0024685262898049864, 'mean_word_length': 3.2787750791974655, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5294603', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1184, 'n_words': 270}