def mutate(self, info, operator,
               road_segment_id=None,
               road_condition_id=None,
               scenario_id=None,
               parent_id=None):
        try:
            response_plan = methods.create_response_plan(
                road_segment_id,
                operator,
                road_condition_id,
                scenario_id,
                parent_id
            )

            road_condition_id = response_plan.road_condition.id if road_condition_id else None
            road_segment_id = response_plan.road_segment.id if road_segment_id else None
            scenario_id = response_plan.scenario.id if scenario_id else None

            return CreateResponsePlan(
                id=response_plan.id,
                road_segment_id=road_segment_id,
                operator=response_plan.operator,
                road_condition_id=road_condition_id,
                scenario_id=scenario_id,
                parent_id=response_plan.parent_id
            )
        except ApiException as exc:
            raise GraphQLError(str(exc))
Beispiel #2
0
def import_response_plan(obj, parent_id=None):
    id = -1
    try:
        if 'operator' not in obj or 'children' not in obj:
            raise InvalidInputException()

        scenario_id = None
        road_segment_id = None
        road_condition_id = None

        if 'scenario_id' in obj:
            scenario_id = obj['scenario_id']
        if 'road_segment_id' in obj:
            road_segment_id = obj['road_segment_id']

        if scenario_id is None and road_segment_id is None:
            raise InvalidInputException()

        if 'road_condition' in obj:
            road_condition = obj['road_condition']
            if 'name' not in road_condition or 'value' not in road_condition:
                raise InvalidInputException()

            type_id = -1
            if 'road_condition_type_id' in road_condition:
                type_id = road_condition['road_condition_type_id']
            elif 'roadConditionType' in road_condition and 'id' in road_condition['roadConditionType']:
                type_id = road_condition['roadConditionType']['id']
            else:
                raise InvalidInputException()

            condition = create_road_condition(
                road_condition['name'],
                None,
                road_condition['value'],
                type_id,
                [], None, road_segment_id)
            road_condition_id = condition.id
        elif 'road_condition_id' in obj:
            road_condition_id = obj['road_condition_id']

        response_plan = create_response_plan(road_segment_id,
                                             obj['operator'],
                                             road_condition_id,
                                             scenario_id,
                                             parent_id)
        id = response_plan.id
        for child in obj['children']:
            import_response_plan(child, id)
    except ApiException as exc:
        if id != -1:
            delete_response_plan_cascade(id)
        if parent_id is None:
            return {"msg": str(exc)}
        else:
            raise InvalidInputException()
    return {"id": id}
Beispiel #3
0
    def test_create_response_plan(self):
        response_plan = create_response_plan(
            self.segments[0].id,
            'OR',
            self.conditions[1].id,
            self.scenarios[0].id,
            self.response_plans[2].id,
        )
        created = get_response_plan_with_id(response_plan.id)

        self.assertTrue(len(ResponsePlan.objects.all()),
                        len(get_all_response_plans()))
        self.assertEqual(response_plan, created)
Beispiel #4
0
 def test_create_response_plan_exception(self):
     with self.assertRaises(InvalidInputException):
         create_response_plan(None, None, None, None, None)
Beispiel #5
0
 def test_delete(self):
     create_response_plan(self.segments[0].id, 'OR', self.conditions[1].id,
                          self.scenarios[0].id, self.response_plans[1].id)
     current_len = len(get_all_response_plans())
     delete_response_plan(self.response_plans[1].id)
     self.assertEqual(len(get_all_response_plans()), current_len - 1)