def test_get_qualifying_providers(self): category1 = make_recipe("legalaid.category") category2 = make_recipe("legalaid.category") cat1_provider1 = make_recipe("cla_provider.provider", active=True) make_recipe( "cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat1_provider1, category=category1 ) cat1_provider2 = make_recipe("cla_provider.provider", active=True) make_recipe( "cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat1_provider2, category=category1 ) cat2_provider1 = make_recipe("cla_provider.provider", active=True) make_recipe( "cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat2_provider1, category=category2 ) helper = ProviderAllocationHelper() cat1_providers = helper.get_qualifying_providers(category1) self.assertItemsEqual(cat1_providers, [cat1_provider1, cat1_provider2]) # Set cat1_provider2.active == False => only one prov returned cat1_provider2.active = False cat1_provider2.save() helper = ProviderAllocationHelper() cat1_providers = helper.get_qualifying_providers(category1) self.assertItemsEqual(cat1_providers, [cat1_provider1])
def test_get_qualifying_providers(self): category1 = make_recipe("legalaid.category") category2 = make_recipe("legalaid.category") cat1_provider1 = make_recipe("cla_provider.provider", active=True) make_recipe("cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat1_provider1, category=category1) cat1_provider2 = make_recipe("cla_provider.provider", active=True) make_recipe("cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat1_provider2, category=category1) cat2_provider1 = make_recipe("cla_provider.provider", active=True) make_recipe("cla_provider.provider_allocation", weighted_distribution=0.5, provider=cat2_provider1, category=category2) helper = ProviderAllocationHelper() cat1_providers = helper.get_qualifying_providers(category1) self.assertItemsEqual(cat1_providers, [cat1_provider1, cat1_provider2]) # Set cat1_provider2.active == False => only one prov returned cat1_provider2.active = False cat1_provider2.save() helper = ProviderAllocationHelper() cat1_providers = helper.get_qualifying_providers(category1) self.assertItemsEqual(cat1_providers, [cat1_provider1])
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)
def test_save_without_matter_type_only_mt1(self, timezone_mock, models_timezone_mock): _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 12, 59, 0), timezone_mock, models_timezone_mock) case = make_recipe("legalaid.case") category = case.eligibility_check.category provider = make_recipe("cla_provider.provider", active=True) make_recipe("cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category) case.matter_type1 = make_recipe("legalaid.matter_type1", category=category) case.save() helper = ProviderAllocationHelper() form = ProviderAllocationForm( case=case, data={"provider": helper.get_suggested_provider(category).pk}, providers=helper.get_qualifying_providers(category), ) self.assertFalse(form.is_valid()) self.assertDictEqual( form.errors, { "__all__": [ u"Can't assign to specialist provider without setting matter_type1 and matter_type2" ] }, )
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)
def test_save_without_matter_type_only_mt1(self, timezone_mock, models_timezone_mock): _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 12, 59, 0), timezone_mock, models_timezone_mock) case = make_recipe('legalaid.case') category = case.eligibility_check.category provider = make_recipe('cla_provider.provider', active=True) make_recipe('cla_provider.provider_allocation', weighted_distribution=0.5, provider=provider, category=category) case.matter_type1 = make_recipe('legalaid.matter_type1', category=category) case.save() helper = ProviderAllocationHelper() form = ProviderAllocationForm(case=case, data={ 'provider': helper.get_suggested_provider(category).pk}, providers=helper.get_qualifying_providers( category)) self.assertFalse(form.is_valid()) self.assertDictEqual(form.errors, {'__all__': [ u"Can't assign to specialist provider without setting matter_type1 and matter_type2"]})
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)
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())
def assign_suggest(self, request, reference=None, **kwargs): """ @return: dict - 'suggested_provider' (single item) ; 'suitable_providers' all possible providers for this category. """ as_of = None if 'as_of' in request.GET and (settings.DEBUG or settings.TEST_MODE): as_of = parser.parse(request.GET.get('as_of')) as_of = as_of.replace(tzinfo=timezone.get_current_timezone()) obj = self.get_object() helper = ProviderAllocationHelper(as_of=as_of) if hasattr(obj, 'eligibility_check') and obj.eligibility_check != None and obj.eligibility_check.category: category = obj.eligibility_check.category suggested = helper.get_suggested_provider(category) if suggested: suggested_provider = ProviderSerializer(suggested).data else: suggested_provider = None else: category = None suggested_provider = None suitable_providers = [ ProviderSerializer(p).data for p in helper.get_qualifying_providers(category)] suggestions = {'suggested_provider': suggested_provider, 'suitable_providers': suitable_providers, 'as_of': helper.as_of } return DRFResponse(suggestions)
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)
def test_save_without_matter_type_category_mismatch( self, timezone_mock, models_timezone_mock): _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 12, 59, 0), timezone_mock, models_timezone_mock) case = make_recipe("legalaid.case") category = case.eligibility_check.category 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).replace(tzinfo=timezone.get_current_timezone()), category=category, ) make_recipe("cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category) case.matter_type1 = make_recipe("legalaid.matter_type1", category=category) other_category = make_recipe("legalaid.category") case.matter_type2 = make_recipe("legalaid.matter_type2", category=other_category) case.save() helper = ProviderAllocationHelper() form = ProviderAllocationForm( case=case, data={"provider": helper.get_suggested_provider(category).pk}, providers=helper.get_qualifying_providers(category), ) self.assertFalse(form.is_valid()) self.assertDictEqual( form.errors, { "__all__": [ u"Category of matter type 1: {category1} must match category of matter type 2: {category2}" .format(category1=category.name, category2=other_category.name), u"Category of Matter Types: {category1}, {category2} must match category of case: {case_category}" .format(category1=category.name, category2=other_category.name, case_category=category.name), ] }, )
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)
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)
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).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)
def test_save_without_matter_type_category_mismatch(self, timezone_mock, models_timezone_mock): _mock_datetime_now_with(datetime.datetime(2014, 1, 2, 12, 59, 0), timezone_mock, models_timezone_mock) case = make_recipe("legalaid.case") category = case.eligibility_check.category 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).replace(tzinfo=timezone.get_current_timezone()), category=category, ) make_recipe( "cla_provider.provider_allocation", weighted_distribution=0.5, provider=provider, category=category ) case.matter_type1 = make_recipe("legalaid.matter_type1", category=category) other_category = make_recipe("legalaid.category") case.matter_type2 = make_recipe("legalaid.matter_type2", category=other_category) case.save() helper = ProviderAllocationHelper() form = ProviderAllocationForm( case=case, data={"provider": helper.get_suggested_provider(category).pk}, providers=helper.get_qualifying_providers(category), ) self.assertFalse(form.is_valid()) self.assertDictEqual( form.errors, { "__all__": [ u"Category of matter type 1: {category1} must match category of matter type 2: {category2}".format( category1=category.name, category2=other_category.name ), u"Category of Matter Types: {category1}, {category2} must match category of case: {case_category}".format( category1=category.name, category2=other_category.name, case_category=category.name ), ] }, )
def assign(self, request, reference=None, **kwargs): """ Assigns the case to a provider """ obj = self.get_object() helper = ProviderAllocationHelper() category = obj.eligibility_check.category if obj.eligibility_check else None suitable_providers = helper.get_qualifying_providers(category) # find given provider in suitable - avoid extra lookup and ensures # valid provider for sp in suitable_providers: if sp.id == int(request.DATA['provider_id']): p = sp break else: raise ValueError("Provider not found") # if we're inside office hours then: # Randomly assign to provider who offers this category of service # else it should be the on duty provider data = request.DATA.copy() data['provider'] = p.pk form = ProviderAllocationForm(case=obj, data=data, providers=suitable_providers) if form.is_valid(): provider = form.save(request.user) notify_case_assigned(provider, form.case) provider_serialised = ProviderSerializer(provider) return DRFResponse(data=provider_serialised.data) return DRFResponse( dict(form.errors), status=status.HTTP_400_BAD_REQUEST )