class InterventieController(object):
    """Body controller.

    Provides the linking layer between the view (REST API interface) and repository (storage).
    """
    def __init__(self):
        self.repository = InterventieMapper()

    """Ophalen van alle interventies"""

    def get_interventies(self, project_id: int):
        return self.repository.get_interventies(project_id)

    """"Ophalen van een interventie"""

    def get_interventie(self, uuid: str,
                        project_id: int) -> Optional[InterventieResponseModel]:
        return self.repository.get_interventie(uuid, project_id)

    """"create_interventie geeft het meegegeven interventie mee aan de repository zodat het interventie kan worden 
    toegevoegd """

    def create_interventie(self, model: InterventieResponseModel,
                           project_id: int):
        return self.repository.add_interventie(model, project_id)

    """update_interventie geeft het meegegeven interventie mee aan de repository zodat het meegegeven interventie bijgewerkt 
        kan worden """

    def update_interventie(self, model: InterventieResponseModel,
                           project_id: int):
        return self.repository.update_interventie(model, project_id)

    def delete_interventie(self, interventie_id, project_id: int):
        return self.repository.delete_interventie(interventie_id, project_id)
    def test_get_interventies_returns_list(self, mock_data_handler):
        mapper = InterventieMapper()
        model = InterventieResponseModel()
        model.naam = 'Naam'
        project_id = 1
        scenarios = mapper._get_interventies(project_id)

        assert scenarios[0].naam == model.naam
    def test_get_interventies_calls_handler(self, mock__data_handler):
        mapper = InterventieMapper()
        project_id = 1
        result = mapper.get_interventies(project_id)

        mock__data_handler.assert_called_with(
            'SELECT * FROM interventie i LEFT JOIN project_interventie pi on pi.interventie_id = i.id WHERE pi.project_id = %s',
            (1, ), True)
    def test_add_interventie_calls_data_handler(self, mock__data_handler,
                                                mocked_stored_procedure):
        model = InterventieResponseModel()
        mapper = InterventieMapper()
        project_id = 1
        mapper.add_interventie(model, project_id)

        mocked_stored_procedure.assert_called_with(
            'maak_interventie_en_stop_in_project',
            ('CAPEX', 'c', '$', 1, 'toelichting', 1))
    def test_delete_interventie_calls_data_handler(self, mock__data_handler):
        mapper = InterventieMapper()
        project_id = 1
        mapper.delete_interventie(1, project_id)

        mock__data_handler.assert_has_calls([
            call('DELETE FROM interventie WHERE id = %s', (1, ), False),
            call(
                'SELECT * FROM interventie i LEFT JOIN project_interventie pi on pi.interventie_id = i.id WHERE pi.project_id = %s',
                (1, ), True)
        ])
    def test_update_scenario_calls_data_handler(self, mock__data_handler):
        model = InterventieResponseModel()
        mapper = InterventieMapper()
        project_id = 1
        mapper.update_interventie(model, project_id)

        mock__data_handler.assert_has_calls([
            call(
                'UPDATE interventie SET naam = %s, type = %s, eenheid = %s, waarde = %s, toelichting = %s WHERE id = %s',
                ('CAPEX', 'c', '$', 1, 'toelichting', 1), False),
            call(
                'SELECT * FROM interventie i LEFT JOIN project_interventie pi on pi.interventie_id = i.id WHERE pi.project_id = %s',
                (project_id, ), True)
        ])
 def test_data_handler_throws_exception(self):
     with self.assertRaises(Exception) as excep:
         mapper = InterventieMapper()
         mapper._data_handler("WRONGQUERY", "", False)
    def test_data_handler_returns_None(self, mock_db_connection):
        mapper = InterventieMapper()

        result = mapper._data_handler("", "", False)

        assert result == None
    def test_data_handler_calls_connection(self, mock_db_connection):
        mapper = InterventieMapper()

        mapper._data_handler("", "", True)

        mock_db_connection.assert_called_with()
    def test_db_connection(self, mock_connect):
        mapper = InterventieMapper()

        mapper._db_connection()

        mock_connect.assert_called()
    def test_get_interventie_returns_none(self, mock__data_handler):
        mapper = InterventieMapper()
        project_id = 1
        result = mapper.get_interventie(1, project_id)

        assert result is None
    def test_get_interventie_calls_data_handler(self, mock__data_handler):
        mapper = InterventieMapper()
        project_id = 1
        result = mapper.get_interventie(1, project_id)

        assert result.id == 1
 def __init__(self):
     self.repository = InterventieMapper()
Esempio n. 14
0
 def import_to_db_interventie(self, json_model_array, project_id):
     mapper = InterventieMapper()
     for i in range(len(json_model_array)):
         mapper.add_interventie(json_model_array[i], project_id)