<commit_before>import datetime

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

from core.tests.mommy_utils import make_recipe, make_user
from cla_eventlog.tests.test_forms import BaseCaseLogFormTestCaseMixin, EventSpecificLogFormTestCaseMixin
from cla_eventlog.models import Log
from legalaid.models import Case
from cla_provider.helpers import ProviderAllocationHelper
from call_centre.forms import (
    DeferAssignmentCaseForm,
    ProviderAllocationForm,
    DeclineHelpCaseForm,
    CallMeBackForm,
    StopCallMeBackForm,
)


def _mock_datetime_now_with(date, *mocks):
    dt = date.replace(tzinfo=timezone.get_current_timezone())
    for _mock in mocks:
        _mock.return_value = dt
    return dt


class ProviderAllocationFormTestCase(TestCase):
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_in_office_hours(self, timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 9, 1, 0), timezone_mock)
        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category
        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()
        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )

        helper = ProviderAllocationHelper()
        form = ProviderAllocationForm(
            case=case,
            data={"provider": helper.get_suggested_provider(category).pk},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertTrue(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)
        form.save(user)

        self.assertEqual(case.provider, provider)
        self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_bank_holiday(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 1, 9, 1, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        provider = make_recipe("cla_provider.provider", active=True)

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )

        helper = ProviderAllocationHelper()

        suggested = helper.get_suggested_provider(category)
        self.assertIsNone(suggested)

        form = ProviderAllocationForm(
            case=case,
            data={"provider": suggested.pk if suggested else None},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertFalse(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 8, 59, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.outofhoursrota",
            provider=provider,
            start_date=datetime.datetime(2013, 12, 30).replace(tzinfo=timezone.get_current_timezone()),
            end_date=datetime.datetime(2014, 1, 2, 9, 0).replace(tzinfo=timezone.get_current_timezone()),
            category=category,
        )

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )
        # TODO - create a ProviderAllocation for this provider with the
        # same category as the case and a positive weighted_distribution

        helper = ProviderAllocationHelper()

        form = ProviderAllocationForm(
            case=case,
            data={"provider": helper.get_suggested_provider(category).pk},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertTrue(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)
        form.save(user)

        self.assertEqual(case.provider, provider)
        self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_saturday(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 11, 1, 10, 30, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        in_hours_provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.outofhoursrota",
            provider=provider,
            start_date=datetime.datetime(2013, 12, 30).replace(tzinfo=timezone.get_current_timezone()),
            end_date=datetime.datetime(2014, 12, 2).replace(tzinfo=timezone.get_current_timezone()),
            category=category,
        )

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=1, provider=in_hours_provider, category=category
        )

        make_recipe("cla_provider.provider_allocation", weighted_distribution=0, provider=provider, category=category)

        with mock.patch.object(
            ProviderAllocationHelper, "_get_random_provider", return_value=in_hours_provider
        ) as mocked_get_random_provider:
            helper = ProviderAllocationHelper()

            form = ProviderAllocationForm(
                case=case,
                data={"provider": helper.get_suggested_provider(category).pk},
                providers=helper.get_qualifying_providers(category),
            )
            self.assertEqual(mocked_get_random_provider.call_count, 0)
            self.assertTrue(form.is_valid())

            self.assertEqual(Log.objects.count(), 0)
            form.save(user)

            self.assertEqual(case.provider, provider)
            self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_no_valid_provider(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 1, 8, 59, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        provider = make_recipe("cla_provider.provider", active=True)

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )
        # TODO - create a ProviderAllocation for this provider with the
        # same category as the case and a positive weighted_distribution

        helper = ProviderAllocationHelper()

        suggested = helper.get_suggested_provider(category)
        self.assertIsNone(suggested)

        form = ProviderAllocationForm(
            case=case,
            data={"provider": suggested.pk if suggested else None},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertFalse(form.is_valid())
<commit_msg>Update bank holiday days closer to todays date<commit_after>import datetime

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

from core.tests.mommy_utils import make_recipe, make_user
from cla_eventlog.tests.test_forms import BaseCaseLogFormTestCaseMixin, EventSpecificLogFormTestCaseMixin
from cla_eventlog.models import Log
from legalaid.models import Case
from cla_provider.helpers import ProviderAllocationHelper
from call_centre.forms import (
    DeferAssignmentCaseForm,
    ProviderAllocationForm,
    DeclineHelpCaseForm,
    CallMeBackForm,
    StopCallMeBackForm,
)


def _mock_datetime_now_with(date, *mocks):
    dt = date.replace(tzinfo=timezone.get_current_timezone())
    for _mock in mocks:
        _mock.return_value = dt
    return dt


class ProviderAllocationFormTestCase(TestCase):
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_in_office_hours(self, timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 9, 1, 0), timezone_mock)
        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category
        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()
        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )

        helper = ProviderAllocationHelper()
        form = ProviderAllocationForm(
            case=case,
            data={"provider": helper.get_suggested_provider(category).pk},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertTrue(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)
        form.save(user)

        self.assertEqual(case.provider, provider)
        self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_bank_holiday(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(
            datetime.datetime(datetime.date.today().year, 1, 1, 9, 1, 0), timezone_mock, models_timezone_mock
        )

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        provider = make_recipe("cla_provider.provider", active=True)

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )

        helper = ProviderAllocationHelper()

        suggested = helper.get_suggested_provider(category)
        self.assertIsNone(suggested)

        form = ProviderAllocationForm(
            case=case,
            data={"provider": suggested.pk if suggested else None},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertFalse(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 8, 59, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.outofhoursrota",
            provider=provider,
            start_date=datetime.datetime(2013, 12, 30).replace(tzinfo=timezone.get_current_timezone()),
            end_date=datetime.datetime(2014, 1, 2, 9, 0).replace(tzinfo=timezone.get_current_timezone()),
            category=category,
        )

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )
        # TODO - create a ProviderAllocation for this provider with the
        # same category as the case and a positive weighted_distribution

        helper = ProviderAllocationHelper()

        form = ProviderAllocationForm(
            case=case,
            data={"provider": helper.get_suggested_provider(category).pk},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertTrue(form.is_valid())

        self.assertEqual(Log.objects.count(), 0)
        form.save(user)

        self.assertEqual(case.provider, provider)
        self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_saturday(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(datetime.datetime(2014, 11, 1, 10, 30, 0), timezone_mock, models_timezone_mock)

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        user = make_user()
        provider = make_recipe("cla_provider.provider", active=True)
        in_hours_provider = make_recipe("cla_provider.provider", active=True)
        make_recipe(
            "cla_provider.outofhoursrota",
            provider=provider,
            start_date=datetime.datetime(2013, 12, 30).replace(tzinfo=timezone.get_current_timezone()),
            end_date=datetime.datetime(2014, 12, 2).replace(tzinfo=timezone.get_current_timezone()),
            category=category,
        )

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=1, provider=in_hours_provider, category=category
        )

        make_recipe("cla_provider.provider_allocation", weighted_distribution=0, provider=provider, category=category)

        with mock.patch.object(
            ProviderAllocationHelper, "_get_random_provider", return_value=in_hours_provider
        ) as mocked_get_random_provider:
            helper = ProviderAllocationHelper()

            form = ProviderAllocationForm(
                case=case,
                data={"provider": helper.get_suggested_provider(category).pk},
                providers=helper.get_qualifying_providers(category),
            )
            self.assertEqual(mocked_get_random_provider.call_count, 0)
            self.assertTrue(form.is_valid())

            self.assertEqual(Log.objects.count(), 0)
            form.save(user)

            self.assertEqual(case.provider, provider)
            self.assertEqual(Log.objects.count(), 1)

    @mock.patch("cla_provider.models.timezone.now")
    @mock.patch("cla_provider.helpers.timezone.now")
    def test_save_out_office_hours_no_valid_provider(self, timezone_mock, models_timezone_mock):
        _mock_datetime_now_with(
            datetime.datetime(datetime.date.today().year, 1, 1, 8, 59, 0), timezone_mock, models_timezone_mock
        )

        case = make_recipe("legalaid.case")
        category = case.eligibility_check.category

        case.matter_type1 = make_recipe("legalaid.matter_type1", category=category)
        case.matter_type2 = make_recipe("legalaid.matter_type2", category=category)
        case.save()

        provider = make_recipe("cla_provider.provider", active=True)

        make_recipe(
            "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category
        )
        # TODO - create a ProviderAllocation for this provider with the
        # same category as the case and a positive weighted_distribution

        helper = ProviderAllocationHelper()

        suggested = helper.get_suggested_provider(category)
        self.assertIsNone(suggested)

        form = ProviderAllocationForm(
            case=case,
            data={"provider": suggested.pk if suggested else None},
            providers=helper.get_qualifying_providers(category),
        )

        self.assertFalse(form.is_valid())

{'fraction_non_alphanumeric': 0.09616619011495595, 'fraction_numerical': 0.01342125226118924, 'mean_word_length': 3.26318407960199, 'pattern_counts': {'":': 10, '<': 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': '818413', 'n_tokens_mistral': 5299, 'n_tokens_neox': 4948, 'n_words': 771}