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)

    def test_past_visit(self, send_message, start_feedback_survey):
        """
        We should not do anything for visits that have already had the
        welcome message sent.
        """
        welcome_sent = timezone.now()
        self.visit = factories.Visit(welcome_sent=welcome_sent)
        tasks.handle_new_visits()
        self.assertEqual(send_message.call_count, 0)
        self.assertEqual(start_feedback_survey.call_count, 0)
        self.visit = Visit.objects.get(pk=self.visit.pk)
        self.assertEqual(self.visit.welcome_sent, welcome_sent)
        self.assertIsNone(self.visit.survey_sent)

@@ -72,16 +72,64 @@
 timezone
+%0Afrom django.test.utils import override_settings
 %0A%0Afrom m
@@ -287,16 +287,72 @@
 ption%0A%0A%0A
+@override_settings(TEXTIT_API_TOKEN='dummy test token')%0A
 @mock.pa
@@ -768,16 +768,17 @@
 l_args, 
+(
 (self.su
@@ -791,16 +791,18 @@
 low_id,)
+,)
 )%0A%0A    d
@@ -1065,24 +1065,80 @@
 count, 0)%0A%0A%0A
+@override_settings(TEXTIT_API_TOKEN='dummy test token')%0A
 @mock.patch.
@@ -2322,32 +2322,33 @@
      expected = 
+(
 (self.survey.flo
@@ -2371,32 +2371,34 @@
 .patient.mobile)
+,)
 %0A        self.as
@@ -2804,15 +2804,14 @@
 flow
-, 
+.
 call
--
+_
 coun
@@ -2835,16 +2835,17 @@
 ected = 
+(
 (self.su
@@ -2884,16 +2884,18 @@
 .mobile)
+,)
 %0A       
@@ -3053,16 +3053,72 @@
 sent)%0A%0A%0A
+@override_settings(TEXTIT_API_TOKEN='dummy test token')%0A
 @mock.pa
@@ -3805,14 +3805,12 @@
 args
-%5B1%5D, %5B
+, ((
 self
@@ -3834,9 +3834,12 @@
 bile
-%5D
+,),)
 )%0A  

fix a few assertions