def add_case(self, case_id, position=None): """ Adds a case to the itinerary """ is_top_bwv_case = True try: is_top_bwv_case = bool( not self.settings.day_settings.team_settings.use_zaken_backend) except Exception: pass if is_top_bwv_case is not (str(case_id).find("_") >= 0): raise ValueError("This case can not be used for this itinerary") case = Case.get(case_id=case_id, is_top_bwv_case=is_top_bwv_case) used_cases = Itinerary.get_cases_for_date(self.created_at) if case in used_cases: raise ValueError( "This case is already used in an itinerary for this date") itinerary_item = ItineraryItem.objects.create(case=case, itinerary=self, position=position) return itinerary_item
def get_itinerary_item(self, mock=None): """ Helper function for tests. Created and returns an ItineraryItem """ itinerary = Itinerary.objects.create() case = Case.get(FOO_CASE_ID_A) itinerary_item = ItineraryItem.objects.create(itinerary=itinerary, case=case) return itinerary_item
def test_create_with_start_case(self): """ Test creating with start_case """ self.assertEquals(ItinerarySettings.objects.count(), 0) itinerary = Itinerary.objects.create() case = Case.get("FOO_CASE_ID") ItinerarySettings.objects.create(opening_date="2020-04-04", itinerary=itinerary, start_case=case) self.assertEquals(ItinerarySettings.objects.count(), 1)
def test_case_get_function(self): """ The Case get function is a wrapper for get_or_create, and simplifies Case creation """ FOO_ID = "FOO_ID" self.assertEqual(Case.objects.count(), 0) Case.get(FOO_ID) self.assertEqual(Case.objects.count(), 1) # Another get will nog create another object Case.get(FOO_ID) self.assertEqual(Case.objects.count(), 1)
def test_get_location(self): """ Should return the case geolocation data """ case = Case.get("FOO") # This patches the objects __get_case__ function MOCK_BWV_DATA = {"address": {"lat": 0, "lng": 1, "foo": "OTHER DATA"}} case.__get_case__ = Mock() case.__get_case__.return_value = MOCK_BWV_DATA location = case.get_location() self.assertEqual(location, {"lat": 0, "lng": 1})
def test_fraud_prediction_property(self): """ Fraud prediction can be accessed through a case property """ CASE_ID = "FOO" case = Case.get(CASE_ID) fraud_prediction = FraudPrediction.objects.create( case_id=CASE_ID, fraud_probability=0.6, fraud_prediction=True, business_rules={}, shap_values={}, ) self.assertEqual(case.fraud_prediction, fraud_prediction)
def get_itinerary_items(self, mock=None): """ Helper function for tests. Created and returns multiple ItineraryItems """ itinerary = Itinerary.objects.create() case_a = Case.get(FOO_CASE_ID_A) itinerary_item_a = ItineraryItem.objects.create( itinerary=itinerary, case=case_a ) case_b = Case.get(FOO_CASE_ID_B) itinerary_item_b = ItineraryItem.objects.create( itinerary=itinerary, case=case_b ) return [itinerary_item_a, itinerary_item_b]
def generate(self): fraud_predictions = get_fraud_predictions() if self.start_case_id: case = Case.get( case_id=self.start_case_id, is_top_bwv_case=not self.settings.day_settings.team_settings. use_zaken_backend, ).__get_case__(self.start_case_id) case["fraud_prediction"] = fraud_predictions.get( self.start_case_id, None) suggestions = super().generate(case) suggestions = remove_cases_from_list(suggestions, [case]) suggestions = suggestions[:self.target_length - 1] suggestions = [case] + suggestions return suggestions
def __get_start_case__(self, case_id, team_settings): """ Returns a Case object """ if case_id: return Case.get( case_id, team_settings.use_zaken_backend, ) return None
def test_get_cases_for_date(self, mock): """ Should return cases which are in itineraries for the given date """ itinerary = Itinerary.objects.create() itinerary.add_case("FOO_CASE_ID_A") itinerary.add_case("FOO_CASE_ID_B") itinerary = Itinerary.objects.create() itinerary.add_case("FOO_CASE_ID_C") cases = Itinerary.get_cases_for_date("2019-12-25") cases_expected = [ Case.get("FOO_CASE_ID_A"), Case.get("FOO_CASE_ID_B"), Case.get("FOO_CASE_ID_C"), ] self.assertEquals(cases, cases_expected)
def test_authenticated_delete(self): """ An authenticated post should delete an Itinerary Item """ itinerary = Itinerary.objects.create() case = Case.get("FOO Case ID") itinerary_item = ItineraryItem.objects.create(itinerary=itinerary, case=case) self.assertEqual(1, len(itinerary.items.all())) url = reverse("v1:itinerary-item-detail", kwargs={"pk": itinerary_item.id}) client = get_authenticated_client() response = client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(0, len(itinerary.items.all()))
def get_mock_visit(self, case): """ Utility function to generate mock Visit object to test with """ # First create a mock Visit object itinerary = Itinerary.objects.create() case = Case.get("FOO Case ID") itinerary_item = ItineraryItem.objects.create(itinerary=itinerary, case=case) user = User.objects.create(email="*****@*****.**") visit = Visit.objects.create( author=user, itinerary_item=itinerary_item, start_time=datetime(2020, 10, 6, tzinfo=UTC), case_id=case, ) return visit
def test_save_no_position(self, mock): """ saving with no position sets the item to the last position """ items = self.get_itinerary_items() itinerary = items[0].itinerary case_c = Case.get(FOO_CASE_C_ID) item = ItineraryItem.objects.create(itinerary=itinerary, case=case_c) self.assertEquals(item.position, items[-1].position + 1)
def test_save_same_position_error(self, mock): """ Saving throws an error if another item has the same position """ items = self.get_itinerary_items() itinerary = items[0].itinerary case_c = Case.get(FOO_CASE_C_ID) same_position = items[0].position with self.assertRaises(Exception): ItineraryItem.objects.create( itinerary=itinerary, case=case_c, position=same_position )
def test_authenticated_update(self): """ Update the item's position """ itinerary = Itinerary.objects.create() case = Case.get("FOO Case ID") itinerary_item = ItineraryItem.objects.create(itinerary=itinerary, case=case, position=0) url = reverse("v1:itinerary-item-detail", kwargs={"pk": itinerary_item.id}) client = get_authenticated_client() NEW_POSITION = 1 data = {"position": NEW_POSITION} response = client.put(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) itinerary_item = ItineraryItem.objects.get(id=itinerary_item.id) self.assertEqual(itinerary_item.position, NEW_POSITION)
class VisitsSignalsTests(TestCase): def get_mock_case(self): case = Case.get("FOO Case ID") return case