def handle_response_plan(request):
    if request.method == 'POST':
        response = import_response_plan(
            json.loads(request.body.decode('utf-8')))
        return JsonResponse(response)
    else:
        return HttpResponseNotAllowed(permitted_methods='POST')
 def test_import_children_exception(self):
     operator = 'OR'
     response = import_response_plan({
         'operator': operator,
         'road_segment_id': self.segments[0].id,
         'children': [{}]
     })
     self.assertEqual(response['msg'], "Invalid input!")
 def test_import_missing_name_condition(self):
     operator = 'OR'
     response = import_response_plan({
         'operator': operator,
         'road_segment_id': self.segments[0].id,
         'road_condition': {},
         'children': []
     })
     self.assertEqual(response['msg'], "Invalid input!")
 def test_import_simple(self):
     operator = 'OR'
     response = import_response_plan({
         'operator': operator,
         'road_segment_id': self.segments[0].id,
         'children': []
     })
     self.assertTrue(response['id'] > 0)
     result = ResponsePlan.objects.filter(id=response['id']).first()
     self.assertEqual(result.operator, operator)
     self.assertEqual(result.road_segment, self.segments[0])
    def test_import_multi_children(self):
        operator = 'OR'
        segment_id = self.segments[0].id
        condition_id = self.conditions[0].id
        scenario_id = self.conditions[0].id
        response = import_response_plan({
            'operator':
            operator,
            'road_segment_id':
            segment_id,
            'scenario_id':
            scenario_id,
            'road_condition_id':
            condition_id,
            'children': [{
                'operator':
                operator,
                'road_segment_id':
                segment_id,
                'scenario_id':
                scenario_id,
                'road_condition_id':
                condition_id,
                'children': [{
                    'operator': operator,
                    'road_segment_id': segment_id,
                    'scenario_id': scenario_id,
                    'road_condition_id': condition_id,
                    'children': []
                }]
            }]
        })
        self.assertTrue(response['id'] > 0)
        result = ResponsePlan.objects.filter(id=response['id']).first()
        self.assertEqual(result.operator, operator)
        self.assertEqual(result.road_segment, self.segments[0])
        self.assertEqual(result.road_condition, self.conditions[0])
        self.assertEqual(result.scenario, self.scenarios[0])

        for child in ResponsePlan.objects.filter(
                parent_id=response['id']).all():
            self.assertEqual(child.operator, operator)
            for leaf in ResponsePlan.objects.filter(parent_id=child.id).all():
                self.assertEqual(leaf.operator, operator)
                self.assertEqual(leaf.road_condition, self.conditions[0])
 def test_import_custom_road_condition(self):
     operator = 'OR'
     condition_name = 'condition 1'
     condition_value = 'i | > | 2800'
     response = import_response_plan({
         'operator': operator,
         'road_segment_id': self.segments[0].id,
         'road_condition': {
             'name': condition_name,
             'value': condition_value,
             'road_condition_type_id': self.condition_types[0].id
         },
         'children': []
     })
     self.assertTrue(response['id'] > 0)
     result = ResponsePlan.objects.filter(id=response['id']).first()
     self.assertEqual(result.operator, operator)
     self.assertEqual(result.road_segment, self.segments[0])
     self.assertEqual(result.road_condition.name, condition_name)
     self.assertEqual(result.road_condition.value, condition_value)
     self.assertEqual(result.road_condition.road_condition_type.id,
                      self.condition_types[0].id)
 def test_import_no_road_segment_or_scenario_exception(self):
     response = import_response_plan({'operator': 'OR', 'children': []})
     self.assertEqual(response['msg'], "Invalid input!")
 def test_import_no_children_exception(self):
     response = import_response_plan({
         'operator': 'OR',
         'road_segment_id': 3
     })
     self.assertEqual(response['msg'], "Invalid input!")
 def test_import_no_operator_exception(self):
     response = import_response_plan({})
     self.assertEqual(response['msg'], "Invalid input!")