<commit_before>import mock

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

from myvoice.clinics.models import Visit
from myvoice.core.tests import factories

from .. import tasks
from .. import models
from ..textit import TextItException


@mock.patch('myvoice.survey.tasks.importer.import_responses')
class TestImportResponses(TestCase):

    def test_active_survey(self, import_responses):
        """We should call the import_responses utility for active surveys."""
        self.survey = factories.Survey(active=True)
        tasks.import_responses()
        self.assertEqual(import_responses.call_count, 1)
        self.assertEqual(import_responses.call_args, (self.survey.flow_id,))

    def test_inactive_survey(self, import_responses):
        """We should not try to import responses for inactive surveys."""
        self.survey = factories.Survey(active=False)
        tasks.import_responses()
        self.assertEqual(import_responses.call_count, 0)


@mock.patch.object(tasks.TextItApi, 'start_flow')
class TestStartFeedbackSurvey(TestCase):

    def setUp(self):
        super(TestStartFeedbackSurvey, self).setUp()
        self.survey = factories.Survey(role=models.Survey.PATIENT_FEEDBACK)
        self.visit = factories.Visit()

    def test_no_such_survey(self, start_flow):
        """No flow should be started if there is no patient feedback survey."""
        self.survey.delete()
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow.call_count, 0)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)

    def test_no_such_visit(self, start_flow):
        """No flow should be started if there is no associated visit."""
        tasks.start_feedback_survey(12345)
        self.assertEqual(start_flow.call_count, 0)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)

    def test_start_flow(self, start_flow):
        """When survey is sent, survey_sent field should be updated."""
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow.call_count, 1)
        expected = (self.survey.flow_id, self.visit.patient.mobile)
        self.assertEqual(start_flow.call_args, expected)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNotNone(self.visit.survey_sent)

    def test_error(self, start_flow):
        """If error occurs during start_flow, survey_sent should be null."""
        start_flow.side_effect = TextItException
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow, call-count, 1)
        expected = (self.survey.flow_id, self.visit.patient.mobile)
        self.assertEqual(start_flow.call_args, expected)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)


@mock.patch.object(tasks.TextItApi, 'send_message')
@mock.patch('myvoice.survey.tasks.start_feedback_survey.apply_async')
class TestHandleNewVisits(TestCase):

    def setUp(self):
        super(TestHandleNewVisits, self).setUp()
        self.survey = factories.Survey(role=models.Survey.PATIENT_FEEDBACK)

    def test_new_visit(self, send_message, start_feedback_survey):
        """
        We should send a welcome message and schedule the survey to be started
        for a new visit.
        """
        self.visit = factories.Visit(welcome_sent=None)
        tasks.handle_new_visits()
        self.assertEqual(send_message.call_count, 1)
        self.assertEqual(send_message.call_args[1], [self.visit.patient.mobile])
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNotNone(self.visit.welcome_sent)
        self.assertEqual(start_feedback_survey.call_count, 1)<commit_msg>fix a few assertions<commit_after>import mock

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

from myvoice.clinics.models import Visit
from myvoice.core.tests import factories

from .. import tasks
from .. import models
from ..textit import TextItException


@override_settings(TEXTIT_API_TOKEN='<PASSWORD>')
@mock.patch('myvoice.survey.tasks.importer.import_responses')
class TestImportResponses(TestCase):

    def test_active_survey(self, import_responses):
        """We should call the import_responses utility for active surveys."""
        self.survey = factories.Survey(active=True)
        tasks.import_responses()
        self.assertEqual(import_responses.call_count, 1)
        self.assertEqual(import_responses.call_args, ((self.survey.flow_id,),))

    def test_inactive_survey(self, import_responses):
        """We should not try to import responses for inactive surveys."""
        self.survey = factories.Survey(active=False)
        tasks.import_responses()
        self.assertEqual(import_responses.call_count, 0)


@override_settings(TEXTIT_API_TOKEN='dummy test token')
@mock.patch.object(tasks.TextItApi, 'start_flow')
class TestStartFeedbackSurvey(TestCase):

    def setUp(self):
        super(TestStartFeedbackSurvey, self).setUp()
        self.survey = factories.Survey(role=models.Survey.PATIENT_FEEDBACK)
        self.visit = factories.Visit()

    def test_no_such_survey(self, start_flow):
        """No flow should be started if there is no patient feedback survey."""
        self.survey.delete()
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow.call_count, 0)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)

    def test_no_such_visit(self, start_flow):
        """No flow should be started if there is no associated visit."""
        tasks.start_feedback_survey(12345)
        self.assertEqual(start_flow.call_count, 0)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)

    def test_start_flow(self, start_flow):
        """When survey is sent, survey_sent field should be updated."""
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow.call_count, 1)
        expected = ((self.survey.flow_id, self.visit.patient.mobile),)
        self.assertEqual(start_flow.call_args, expected)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNotNone(self.visit.survey_sent)

    def test_error(self, start_flow):
        """If error occurs during start_flow, survey_sent should be null."""
        start_flow.side_effect = TextItException
        tasks.start_feedback_survey(self.visit.pk)
        self.assertEqual(start_flow.call_count, 1)
        expected = ((self.survey.flow_id, self.visit.patient.mobile),)
        self.assertEqual(start_flow.call_args, expected)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNone(self.visit.survey_sent)


@override_settings(TEXTIT_API_TOKEN='<PASSWORD>')
@mock.patch.object(tasks.TextItApi, 'send_message')
@mock.patch('myvoice.survey.tasks.start_feedback_survey.apply_async')
class TestHandleNewVisits(TestCase):

    def setUp(self):
        super(TestHandleNewVisits, self).setUp()
        self.survey = factories.Survey(role=models.Survey.PATIENT_FEEDBACK)

    def test_new_visit(self, send_message, start_feedback_survey):
        """
        We should send a welcome message and schedule the survey to be started
        for a new visit.
        """
        self.visit = factories.Visit(welcome_sent=None)
        tasks.handle_new_visits()
        self.assertEqual(send_message.call_count, 1)
        self.assertEqual(send_message.call_args, ((self.visit.patient.mobile,),))
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertIsNotNone(self.visit.welcome_sent)
        self.assertEqual(start_feedback_survey.call_count, 1)
{'fraction_non_alphanumeric': 0.10440539852304558, 'fraction_numerical': 0.003437738731856379, 'mean_word_length': 3.62603062426384, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1538161', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2372, 'n_words': 440}