Esempio n. 1
0
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone", "Almenno"
            ],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI", "AL"],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"],
            "veicolo__a___sesso_conducente": [1, 1, 2, 0, 1, 1, 0],
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                                   anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                                   support_dataframes = support_dataframes,
                                   data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":          ["Milano", "Como", "Como", "Cernusco Sul Naviglio","Roncolate", "Vimodrone"],
            "comune":          ["1001",   "1000", "1000", "1002",                 "1003",      "1004"],
            "Provincia":       ["MI",     "CO",   "CO",   "MI",                    "BG",       "MI"],

            "COL01":           [ 0,        1,      1,      1,                       1,          4], # Incolume: 4, Morto: 1
            "COL02":           [ 4,        3,      1,      2,                       2,          1], # Incolume: 2, Morto: 2, Ferito: 2
            "COL03":           [ 1,        1,      2,      2,                       4,          1], # Incolume: 3, Morto: 1, Ferito: 2

            "ODEA01":          [ 0,        0,      2,      1,                       0,          1], # 4

            "OINJ01":          [ 3,        0,      0,      0,                       5,          0], # 8
            "OINJ02":          [ 6,        0,      1,      1,                       0,          1], # 9
            "natura_incidente":[11,        1,      1,      2,                       2,          1]
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)
class TestIncidentsDataframeAggregator(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_number_of_incidents_by_comune

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], "convert_to_number": []}}'
        config = json.loads(s_config)

        incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                              anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                              support_dataframes = SupportDecodeDataframes(),
                              data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":         ["Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate"],
            "comune":         ["1001",   "1000", "1000", "1002",                  "1003" ],
            "provincia":      ["MI",     "CO",   "CO",   "MI",                    "BG"],
            "tipo_veicolo_a": [ 1,        1,      2,      1,                      2]
        }
        incidenti.df_incidenti = pd.DataFrame.from_records(test_df)
        self.aggregator = IncidentsDataframeAggregator(incidenti)

    def test_get_number_of_incidents_by_comune(self):
        """

        :return:
        """
        try:
            expected_result = 2
            result = self.aggregator.get_number_of_incidents_by_comune("Como")
            self.assertTrue(result == expected_result, "Check result - multiple rows returned.")

            expected_result = 1
            result = self.aggregator.get_number_of_incidents_by_comune("Roncolate")
            self.assertTrue(result == expected_result, "Check result - single row returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")


    def test_get_number_of_incidents_by_not_existing_comune(self):
        """

        :return:
        """
        result = 0
        expected_result = 0
        try:
            result = self.aggregator.get_number_of_incidents_by_comune("Pero")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

        self.assertTrue(result == expected_result)
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], "convert_to_number": []}}'
        config = json.loads(s_config)

        incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                              anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                              support_dataframes = SupportDecodeDataframes(),
                              data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":         ["Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate"],
            "comune":         ["1001",   "1000", "1000", "1002",                  "1003" ],
            "provincia":      ["MI",     "CO",   "CO",   "MI",                    "BG"],
            "tipo_veicolo_a": [ 1,        1,      2,      1,                      2]
        }
        incidenti.df_incidenti = pd.DataFrame.from_records(test_df)
        self.aggregator = IncidentsDataframeAggregator(incidenti)
Esempio n. 5
0
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone", "Peschiera Borromeo"
            ],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI", "MI"],
            "veicolo__a___esito_conducente": [1, 1, 3, 0, 1, 0, 0],
            "veicolo__a___passeggeri_an35": [1, 2, 1, 0, 1, 0, 0],
            "veicolo__a___esito_passegg38": [2, 1, 0, 0, 3, 0, 0],
            "veicolo__a___esito_passegg41": [2, 1, 2, 0, 4, 0, 0],
            "veicolo__a___esito_passegg44": [3, 3, 4, 0, 0, 0, 0]
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)
        self.outcome_labels = [
            "Incolume", "Ferito", "Morto nelle 24 ore",
            "Morto entro il trentesimo giorno", "Non dato"
        ]
Esempio n. 6
0
def main(args):
    log = logging.getLogger('RunIncidenti')
    log.info("************> INIZIO <************")
    rv = False
    try:
        if os.path.isfile(args.config_file) == False:
            log.error("Invalid config file parameter: {fi}".format(
                fi=args.config_file))
            return rv
        with open(args.config_file) as fconfig:
            config = json.load(fconfig)
            base_dir = config['data_files']['data_path']
            incidenti_fn = config['data_files']['file_incidenti']
            com_anagr_fn = config['data_files']['anagrafica_comuni']

            anagrafica_comuni = AnagraficaComuni(
                file_comuni_anagrafica=os.path.join(base_dir, com_anagr_fn))
            support_dataframes = SupportDecodeDataframes()

            incidenti = Incidenti(
                file_incidenti=os.path.join(base_dir, incidenti_fn),
                anagrafica_comuni=anagrafica_comuni,
                support_dataframes=support_dataframes,
                data_file_configurations=config["data_structure"])
            rv = incidenti.load_data_files()

            aggregator = IncidentsDataframeAggregator(incidenti)

            if rv == False:
                log.error("Fallita lettura dei datafile.")
            else:
                if args.operation == "article":
                    rv = article_artifacts_maker(aggregator, incidenti, config)
                elif args.operation == "debug":
                    aggregator.get_incidents_outcome_by_typology('nation_wide')
                    aggregator.calculate_total_incident_outcome()
                    chart_male_female(aggregator, config)
                    chart_incident_typology(aggregator, config)
                    chart_hourly_incidents(aggregator, config)
                    passengers_outcome("province", "Milano", aggregator,
                                       incidenti, config)

    except Exception as ex:
        log.error("The job returned an error - {ex}".format(ex=str(ex)))

    if rv == False:
        log.error("Operation failed - calculated data is not valid.")

    log.info("************> FINE <************")
    return rv
Esempio n. 7
0
class TestIncidentsDataframeAggregator_get_veicle_passengers_outcome(TestCase):
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone", "Peschiera Borromeo"
            ],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI", "MI"],
            "veicolo__a___esito_conducente": [1, 1, 3, 0, 1, 0, 0],
            "veicolo__a___passeggeri_an35": [1, 2, 1, 0, 1, 0, 0],
            "veicolo__a___esito_passegg38": [2, 1, 0, 0, 3, 0, 0],
            "veicolo__a___esito_passegg41": [2, 1, 2, 0, 4, 0, 0],
            "veicolo__a___esito_passegg44": [3, 3, 4, 0, 0, 0, 0]
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)
        self.outcome_labels = [
            "Incolume", "Ferito", "Morto nelle 24 ore",
            "Morto entro il trentesimo giorno", "Non dato"
        ]

    def test_get_passengers_outcome_unknown_area(self):
        try:
            result = self.aggregator.get_veicle_passengers_outcome(
                area="UNKNOWN", city=None)
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_city_none(self):
        try:
            result = self.aggregator.get_veicle_passengers_outcome(area="city",
                                                                   city=None)
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_veicle_passengers_outcome_city(self):
        try:
            conducente_expected = pd.Series([1, 0, 1, 0, 0],
                                            index=self.outcome_labels)
            conducente_expected.sort_index(inplace=True)

            pass_ant_expected = pd.Series([1, 1, 0, 0, 0],
                                          index=self.outcome_labels)
            pass_ant_expected.sort_index(inplace=True)

            pass_pos_1_expected = pd.Series([1, 0, 0, 0, 1],
                                            index=self.outcome_labels)
            pass_pos_1_expected.sort_index(inplace=True)

            pass_pos_2_expected = pd.Series([1, 1, 0, 0, 0],
                                            index=self.outcome_labels)
            pass_pos_2_expected.sort_index(inplace=True)

            pass_pos_3_expected = pd.Series([0, 0, 1, 1, 0],
                                            index=self.outcome_labels)
            pass_pos_3_expected.sort_index(inplace=True)

            df_result = self.aggregator.get_veicle_passengers_outcome(
                "city", "Como")
            self.assertTrue(df_result is not None,
                            "A valid result must be returned.")

            conducente_df = df_result.loc["Conducente"]
            conducente_df.sort_index(inplace=True)
            self.assertTrue(conducente_df.equals(conducente_expected),
                            "Check conducente.")

            pass_ant_df = df_result.loc["Passeggero anteriore"]
            pass_ant_df.sort_index(inplace=True)
            self.assertTrue(pass_ant_df.equals(pass_ant_expected),
                            "Check passeggero anteriore.")

            pass_pos_1_df = df_result.loc["Passeggero posteriore 1"]
            pass_pos_1_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_1_df.equals(pass_pos_1_expected),
                            "Check passeggero posteriore 1.")

            pass_pos_2_df = df_result.loc["Passeggero posteriore 2"]
            pass_pos_2_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_2_df.equals(pass_pos_2_expected),
                            "Check passeggero posteriore 2.")

            pass_pos_3_df = df_result.loc["Passeggero posteriore 3"]
            pass_pos_3_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_3_df.equals(pass_pos_3_expected),
                            "Check passeggero posteriore 3.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_veicle_passengers_outcome_province(self):
        try:
            conducente_expected = pd.Series([1, 0, 0, 0, 0],
                                            index=self.outcome_labels)
            conducente_expected.sort_index(inplace=True)

            pass_ant_expected = pd.Series([1, 0, 0, 0, 0],
                                          index=self.outcome_labels)
            pass_ant_expected.sort_index(inplace=True)

            pass_pos_1_expected = pd.Series([0, 0, 1, 0, 0],
                                            index=self.outcome_labels)
            pass_pos_1_expected.sort_index(inplace=True)

            pass_pos_2_expected = pd.Series([0, 0, 0, 1, 0],
                                            index=self.outcome_labels)
            pass_pos_2_expected.sort_index(inplace=True)

            pass_pos_3_expected = pd.Series([0, 0, 0, 0, 1],
                                            index=self.outcome_labels)
            pass_pos_3_expected.sort_index(inplace=True)

            df_result = self.aggregator.get_veicle_passengers_outcome(
                "province", "BG")
            self.assertTrue(df_result is not None,
                            "A valid result must be returned.")

            conducente_df = df_result.loc["Conducente"]
            conducente_df.sort_index(inplace=True)
            self.assertTrue(conducente_df.equals(conducente_expected),
                            "Check conducente.")

            pass_ant_df = df_result.loc["Passeggero anteriore"]
            pass_ant_df.sort_index(inplace=True)
            self.assertTrue(pass_ant_df.equals(pass_ant_expected),
                            "Check passeggero anteriore.")

            pass_pos_1_df = df_result.loc["Passeggero posteriore 1"]
            pass_pos_1_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_1_df.equals(pass_pos_1_expected),
                            "Check passeggero posteriore 1.")

            pass_pos_2_df = df_result.loc["Passeggero posteriore 2"]
            pass_pos_2_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_2_df.equals(pass_pos_2_expected),
                            "Check passeggero posteriore 2.")

            pass_pos_3_df = df_result.loc["Passeggero posteriore 3"]
            pass_pos_3_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_3_df.equals(pass_pos_3_expected),
                            "Check passeggero posteriore 3.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_veicle_passengers_outcome_nation_wide(self):
        try:
            conducente_expected = pd.Series([3, 0, 1, 0, 3],
                                            index=self.outcome_labels)
            conducente_expected.sort_index(inplace=True)

            pass_ant_expected = pd.Series([3, 1, 0, 0, 3],
                                          index=self.outcome_labels)
            pass_ant_expected.sort_index(inplace=True)

            pass_pos_1_expected = pd.Series([1, 1, 1, 0, 4],
                                            index=self.outcome_labels)
            pass_pos_1_expected.sort_index(inplace=True)

            pass_pos_2_expected = pd.Series([1, 2, 0, 1, 3],
                                            index=self.outcome_labels)
            pass_pos_2_expected.sort_index(inplace=True)

            pass_pos_3_expected = pd.Series([0, 0, 2, 1, 4],
                                            index=self.outcome_labels)
            pass_pos_3_expected.sort_index(inplace=True)

            df_result = self.aggregator.get_veicle_passengers_outcome(
                "nation_wide", city=None)
            self.assertTrue(df_result is not None,
                            "A valid result must be returned.")

            conducente_df = df_result.loc["Conducente"]
            conducente_df.sort_index(inplace=True)
            self.assertTrue(conducente_df.equals(conducente_expected),
                            "Check conducente.")

            pass_ant_df = df_result.loc["Passeggero anteriore"]
            pass_ant_df.sort_index(inplace=True)
            self.assertTrue(pass_ant_df.equals(pass_ant_expected),
                            "Check passeggero anteriore.")

            pass_pos_1_df = df_result.loc["Passeggero posteriore 1"]
            pass_pos_1_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_1_df.equals(pass_pos_1_expected),
                            "Check passeggero posteriore 1.")

            pass_pos_2_df = df_result.loc["Passeggero posteriore 2"]
            pass_pos_2_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_2_df.equals(pass_pos_2_expected),
                            "Check passeggero posteriore 2.")

            pass_pos_3_df = df_result.loc["Passeggero posteriore 3"]
            pass_pos_3_df.sort_index(inplace=True)
            self.assertTrue(pass_pos_3_df.equals(pass_pos_3_expected),
                            "Check passeggero posteriore 3.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
Esempio n. 8
0
class TestIncidentsDataframeAggregator_get_male_female_by_area(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_male_female_by_area

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone", "Almenno"
            ],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI", "AL"],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"],
            "veicolo__a___sesso_conducente": [1, 1, 2, 0, 1, 1, 0],
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_get_male_female_by_area_nation_wide(self):
        try:
            expected_male = 4
            expected_female = 1
            expected_unknown = 2

            result = self.aggregator.get_male_female_by_area("nation_wide")
            self.assertTrue(result is not None, "None return not allowed.")

            result.set_index('Sesso conducente', inplace=True)
            result_male = result.loc['maschio', 'Numero']
            self.assertTrue(result_male == expected_male,
                            "Check numeric result - male.")

            result_female = result.loc['femmina', 'Numero']
            self.assertTrue(result_female == expected_female,
                            "Check numeric result - female.")

            result_unknown = result.loc['non dato', 'Numero']
            self.assertTrue(result_unknown == expected_unknown,
                            "Check numeric result - unknown.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_male_female_by_area_province(self):
        try:
            expected_male = 2
            expected_unknown = 1

            result = self.aggregator.get_male_female_by_area("province", "MI")
            self.assertTrue(result is not None, "None return not allowed.")

            result.set_index('Sesso conducente', inplace=True)
            result_male = result.loc['maschio', 'Numero']
            self.assertTrue(result_male == expected_male,
                            "Check numeric result - male.")

            self.assertFalse('femmina' in result.index,
                             "Check numeric result - female.")

            result_unknown = result.loc['non dato', 'Numero']
            self.assertTrue(result_unknown == expected_unknown,
                            "Check numeric result - unknown.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_male_female_by_area_city(self):
        try:
            expected_male = 1
            expected_female = 1

            result = self.aggregator.get_male_female_by_area("city", "Como")
            self.assertTrue(result is not None, "None return not allowed.")

            result.set_index('Sesso conducente', inplace=True)
            result_male = result.loc['maschio', 'Numero']
            self.assertTrue(result_male == expected_male,
                            "Check numeric result - male.")

            result_female = result.loc['femmina', 'Numero']
            self.assertTrue(result_female == expected_female,
                            "Check numeric result - female.")

            self.assertFalse('non dato' in result.index,
                             "Check numeric result - unknown.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_male_female_by_area_unknown_area(self):
        try:
            result = self.aggregator.get_male_female_by_area(
                "province", "NOT KNOWN")
            self.assertTrue(result is None, "No result returned - province.")

            result = self.aggregator.get_male_female_by_area(
                "city", "NOT KNOWN")
            self.assertTrue(result is None, "No result returned - city.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
class TestIncidentsDataframeAggregator_get_number_of_incidents_by_hour(
        TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_number_of_incidents_by_hour

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone"
            ],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004"],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI"],
            "COL01": [0, 1, 1, 1, 1, 4],  # Incolume: 4, Morto: 1
            "COL02": [4, 3, 1, 2, 2, 1],  # Incolume: 2, Morto: 2, Ferito: 2
            "COL03": [1, 1, 2, 2, 4, 1],  # Incolume: 3, Morto: 1, Ferito: 2
            "ODEA01": [0, 0, 2, 1, 0, 1],  # 4
            "OINJ01": [3, 0, 0, 0, 5, 0],  # 8
            "OINJ02": [6, 0, 1, 1, 0, 1],  # 9
            "natura_incidente": [11, 1, 1, 2, 2, 1],
            "Ora": [15, 2, 10, 2, 2, 11],
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_get_number_of_incidents_by_hour_unknown_area(self):
        try:
            result = self.aggregator.get_number_of_incidents_by_hour("UNKNOWN")
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_number_of_incidents_by_hour_nation_wide_area(self):
        try:
            expected_results = {15: 1, 10: 1, 11: 1, 2: 3}
            result_df = self.aggregator.get_number_of_incidents_by_hour(
                "nation_wide")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results),
                            "Check the number of found results.")

            for key in expected_results.keys():
                rs = result_df.loc[key]['Numero']
                self.assertTrue(rs == expected_results[key],
                                "Check result for hour {hh}".format(hh=key))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_number_of_incidents_by_hour_province_area(self):
        try:
            expected_results = {15: 1, 11: 1, 2: 1}
            result_df = self.aggregator.get_number_of_incidents_by_hour(
                "province", "MI")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results),
                            "Check the number of found results.")

            for key in expected_results.keys():
                rs = result_df.loc[key]['Numero']
                self.assertTrue(rs == expected_results[key],
                                "Check result for hour {hh}".format(hh=key))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_number_of_incidents_by_hour_city_area(self):
        try:
            expected_results = {2: 1, 10: 1}
            result_df = self.aggregator.get_number_of_incidents_by_hour(
                "city", "Como")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results),
                            "Check the number of found results.")

            for key in expected_results.keys():
                rs = result_df.loc[key]['Numero']
                self.assertTrue(rs == expected_results[key],
                                "Check result for hour {hh}".format(hh=key))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
class TestIncidentsDataframeAggregator(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->__calculate_incident_outcome

    NOTE: this is a private method so this test could be removed.

    """

    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], "convert_to_number": [],"incident_outcome_columns":["COL01","COL02","COL03"]}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                              anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                              support_dataframes = support_dataframes,
                              data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":          ["Milano", "Como", "Como", "Cernusco Sul Naviglio","Roncolate"],
            "comune":          ["1001",   "1000", "1000", "1002",                 "1003"],
            "Provincia":       ["MI",     "CO",   "CO",   "MI",                    "BG"],
            "COL01":           [ 0,        1,      1,      1,                       1],
            "COL02":           [ 4,        3,      1,      2,                       2],
            "COL03":           [ 1,        1,      2,      2,                       4]
        }
        incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(incidenti)

    def test___calculate_incident_outcome(self):
        try:
            expected_incolumi = 7
            expected_feriti = 4
            expected_morti = 3

            result_df = self.aggregator._IncidentsDataframeAggregator__calculate_incident_outcome()
            self.assertTrue(result_df, "Must return the ok result.")
            df = self.aggregator._IncidentsDataframeAggregator__df()
            n_incolumi = df['Incolumi'].sum()
            n_feriti = df['Feriti'].sum()
            n_morti = df['Morti'].sum()

            self.assertTrue(n_incolumi == expected_incolumi, "Incolumi - check decode and count.")
            self.assertTrue(n_feriti == expected_feriti, "Morti - check decode and count.")
            self.assertTrue(n_morti == expected_morti, "Morti - check decode and count.")
            pass
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test___calculate_incident_outcome_call_more_than_once(self):
        try:
            expected_incolumi = 7
            expected_feriti = 4
            expected_morti = 3

            result_df = self.aggregator._IncidentsDataframeAggregator__calculate_incident_outcome()
            self.assertTrue(result_df, "Must return the ok result.")

            result_df = self.aggregator._IncidentsDataframeAggregator__calculate_incident_outcome()
            self.assertTrue(result_df, "Must return the ok result.")

            df = self.aggregator._IncidentsDataframeAggregator__df()
            n_incolumi = df['Incolumi'].sum()
            n_feriti = df['Feriti'].sum()
            n_morti = df['Morti'].sum()

            self.assertTrue(n_incolumi == expected_incolumi, "Incolumi - check decode and count.")
            self.assertTrue(n_feriti == expected_feriti, "Morti - check decode and count.")
            self.assertTrue(n_morti == expected_morti, "Morti - check decode and count.")
            pass
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
class TestIncidentsDataframeAggregator(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_count_of_incidents_by_typology

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], "convert_to_number": [],"incident_outcome_columns":["COL01","COL02","COL03"]}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Almenno", "Vimodrone"
            ],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "BG", "MI"],
            "natura_incidente": [1, 1, 4, 10, 1, 4, 4],
        }
        incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(incidenti)

    def test_get_count_of_incidents_by_typology_first_parameter(self):
        try:
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "WRONG_FIRST_PARAMETER")
            self.assertTrue(result_df is None,
                            "Wrong first parameter - return None.")
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_count_of_incidents_by_typology_nation_wide(self):
        try:
            expected_scontro_frontale = 3
            expected_tamponamento = 3
            expected_fuoriuscita = 1

            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "nation_wide")
            self.assertTrue(result_df is not None, "None return not allowed.")

            result_df.set_index('descrizione', inplace=True)
            scontro_frontale = result_df[['Numero']].loc['Scontro frontale'][0]
            scontro_tamponamento = result_df[['Numero']].loc['Tamponamento'][0]
            scontro_fuoriuscita = result_df[['Numero']].loc['Fuoriuscita'][0]

            self.assertTrue(scontro_frontale == expected_scontro_frontale,
                            "Check count - scontro frontale.")
            self.assertTrue(scontro_tamponamento == expected_tamponamento,
                            "Check count - tamponamento.")
            self.assertTrue(scontro_fuoriuscita == expected_fuoriuscita,
                            "Check count - fuoriuscita.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_count_of_incidents_by_typology_province(self):
        try:
            expected_scontro_frontale = 1
            expected_tamponamento = 1
            expected_fuoriuscita = 1

            provincia = "MI"
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "province", provincia)
            self.assertTrue(result_df is not None, "None return not allowed.")

            result_df.set_index('descrizione', inplace=True)
            scontro_frontale = result_df[['Numero']].loc['Scontro frontale'][0]
            scontro_tamponamento = result_df[['Numero']].loc['Tamponamento'][0]
            scontro_fuoriuscita = result_df[['Numero']].loc['Fuoriuscita'][0]

            self.assertTrue(
                scontro_frontale == expected_scontro_frontale,
                "Check count {prov} - scontro frontale.".format(
                    prov=provincia))
            self.assertTrue(
                scontro_tamponamento == expected_tamponamento,
                "Check count {prov} - tamponamento.".format(prov=provincia))
            self.assertTrue(
                scontro_fuoriuscita == expected_fuoriuscita,
                "Check count {prov} - fuoriuscita.".format(prov=provincia))

            expected_scontro_frontale = 1
            expected_tamponamento = 1

            provincia = "BG"
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "province", provincia)
            self.assertTrue(result_df is not None, "None return not allowed.")

            result_df.set_index('descrizione', inplace=True)
            scontro_frontale = result_df[['Numero']].loc['Scontro frontale'][0]
            scontro_tamponamento = result_df[['Numero']].loc['Tamponamento'][0]
            empty_fuoriuscita = 'Fuoriuscita' in result_df.index

            self.assertTrue(
                scontro_frontale == expected_scontro_frontale,
                "Check count {prov} - scontro frontale.".format(
                    prov=provincia))
            self.assertTrue(
                scontro_tamponamento == expected_tamponamento,
                "Check count {prov} - tamponamento.".format(prov=provincia))
            self.assertTrue(
                empty_fuoriuscita == False,
                "Check count {prov} - fuoriuscita empty.".format(
                    prov=provincia))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_count_of_incidents_by_typology_not_existent_province(self):
        try:
            provincia = "DOES_NOT_EXIST"
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "province", provincia)
            self.assertTrue(result_df.empty == True,
                            "An empty dataframe expected.")
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_count_of_incidents_by_typology_not_existent_comune(self):
        try:
            comune = "DOES_NOT_EXIST"
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "city", comune)
            self.assertTrue(result_df.empty == True,
                            "An empty dataframe expected.")
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_count_of_incidents_by_typology_city(self):
        try:
            expected_scontro_frontale = 1
            expected_tamponamento = 1

            comune = "Como"
            result_df = self.aggregator.get_count_of_incidents_by_typology(
                "city", comune)
            self.assertTrue(result_df is not None,
                            "A valid dataframe expected.")

            result_df.set_index('descrizione', inplace=True)
            scontro_frontale = result_df[['Numero']].loc['Scontro frontale'][0]
            scontro_tamponamento = result_df[['Numero']].loc['Tamponamento'][0]

            empty_fuoriuscita = 'Fuoriuscita' in result_df.index
            self.assertTrue(
                scontro_frontale == expected_scontro_frontale,
                "Check count {city} - scontro frontale.".format(city=comune))
            self.assertTrue(
                scontro_tamponamento == expected_tamponamento,
                "Check count {city} - tamponamento.".format(city=comune))
            self.assertTrue(
                empty_fuoriuscita == False,
                "Check count {prov} - fuoriuscita empty.".format(prov=comune))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
Esempio n. 12
0
class TestIncidentsDataframeAggregator(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->calculate_total_incident_outcome

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone"
            ],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004"],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI"],
            "COL01": [0, 1, 1, 1, 1, 4],  # Incolume: 4, Morto: 1
            "COL02": [4, 3, 1, 2, 2, 1],  # Incolume: 2, Morto: 2, Ferito: 2
            "COL03": [1, 1, 2, 2, 4, 1],  # Incolume: 3, Morto: 1, Ferito: 2
            "ODEA01": [0, 0, 2, 1, 0, 1],  # 4
            "OINJ01": [3, 0, 0, 0, 5, 0],  # 8
            "OINJ02": [6, 0, 1, 1, 0, 1]  # 9
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_calculate_total_incident_outcome(self):
        try:
            expected_o_feriti = 17
            expected_o_morti = 4

            expected_incolumi = 9
            expected_feriti = 4 + expected_o_feriti
            expected_morti = 4 + expected_o_morti

            result = self.aggregator.calculate_total_incident_outcome()
            self.assertTrue(result, "Must return the OK code.")

            injured_num = self.incidenti.df_incidenti["Feriti"].sum()
            dead_num = self.incidenti.df_incidenti["Morti"].sum()
            uninjured_num = self.incidenti.df_incidenti["Incolumi"].sum()

            self.assertTrue(uninjured_num == expected_incolumi,
                            "Check totals - incolumi.")
            self.assertTrue(dead_num == expected_morti,
                            "Check totals - morti.")
            self.assertTrue(injured_num == expected_feriti,
                            "Check totals - feriti.")

            other_injured = self.incidenti.df_incidenti["TotAltriFeriti"].sum()
            other_dead = self.incidenti.df_incidenti["TotAltriMorti"].sum()
            self.assertTrue(expected_o_feriti == other_injured,
                            "Chech partial - injured count.")
            self.assertTrue(expected_o_morti == other_dead,
                            "Chech partial - dead count.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_calculate_total_incident_outcome_call_more_than_once(self):
        try:
            expected_o_feriti = 17
            expected_o_morti = 4

            expected_incolumi = 9
            expected_feriti = 4 + expected_o_feriti
            expected_morti = 4 + expected_o_morti

            result = self.aggregator.calculate_total_incident_outcome()
            self.assertTrue(result, "Must return the OK (1) code.")

            result = self.aggregator.calculate_total_incident_outcome()
            self.assertTrue(result, "Must return the OK (2) code.")

            injured_num = self.incidenti.df_incidenti["Feriti"].sum()
            dead_num = self.incidenti.df_incidenti["Morti"].sum()
            uninjured_num = self.incidenti.df_incidenti["Incolumi"].sum()

            self.assertTrue(uninjured_num == expected_incolumi,
                            "Check totals - incolumi.")
            self.assertTrue(dead_num == expected_morti,
                            "Check totals - morti.")
            self.assertTrue(injured_num == expected_feriti,
                            "Check totals - feriti.")

            other_injured = self.incidenti.df_incidenti["TotAltriFeriti"].sum()
            other_dead = self.incidenti.df_incidenti["TotAltriMorti"].sum()
            self.assertTrue(expected_o_feriti == other_injured,
                            "Chech partial - injured count.")
            self.assertTrue(expected_o_morti == other_dead,
                            "Chech partial - dead count.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
Esempio n. 13
0
from src.ClassIncidenti import Incidenti
from src.ClassAnagraficaComuni import AnagraficaComuni
from src.ClassSupportDecodeDataframes import SupportDecodeDataframes
from src.ClassIncidentsDataframeAggregator import IncidentsDataframeAggregator

config = None
with open(config_file) as fconfig:
    config = json.load(fconfig)

init_logger('/tmp', log_level=logging.FATAL, std_out_log_level=logging.FATAL)

base_dir = config['data_files']['data_path']
print(base_dir)
incidenti_fn = config['data_files']['file_incidenti']
incidenti_cols = config['data_structure']['columns']
cols_to_be_converted = config["data_structure"]["convert_to_number"]

anagrafica_comuni = AnagraficaComuni(
    file_comuni_anagrafica=os.path.join(data_dir, istat_comuni_file))
support_dataframes = SupportDecodeDataframes()

incidenti = Incidenti(file_incidenti=os.path.join(data_dir, incidenti_fn),
                      anagrafica_comuni=anagrafica_comuni,
                      support_dataframes=support_dataframes,
                      data_file_configurations=config["data_structure"])
incidenti.load_data_files()
aggregator = IncidentsDataframeAggregator(incidenti)

("Vers. Incidenti:", Incidenti.get_version(), "Vers. Aggregator:",
 IncidentsDataframeAggregator.get_version(), "Numerosita' df:",
 incidenti.df_incidenti.shape[0])
class TestIncidentsDataframeAggregator_get_passengers_outcome(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_number_of_incidents_by_hour

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                                   anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                                   support_dataframes = support_dataframes,
                                   data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":          ["Milano", "Como", "Como", "Cernusco Sul Naviglio","Roncolate", "Vimodrone" ,"Peschiera Borromeo"],
            "comune":          ["1001",   "1000", "1000", "1002",                 "1003",      "1004"      ,"1005"],
            "Provincia":       ["MI",     "CO",   "CO",   "MI",                    "BG",       "MI"        ,"MI"],
            "COL01":           [ 4,        2,      2,      1,                       1,          3          ,2 ],
            # COL01 -> Incolume: 2, Feriti: 2 ,Morto: 1+1
            "COL02":           [ 4,        2,      2,      1,                       1,          3          ,1 ]
            # COL02[MI] -> Incolume: 2, Feriti: 2 ,Morto: 1+1
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_get_passengers_outcome_unknown_area(self):
        try:
            result = self.aggregator.get_passengers_outcome(area="UNKNOWN", passenger_column="XXX")
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_city_none(self):
        try:
            result = self.aggregator.get_passengers_outcome(area="city", name=None, passenger_column="XXX")
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_column_none(self):
        try:
            result = self.aggregator.get_passengers_outcome(area="city", name="Vimodrone", passenger_column=None)
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_nation_wide_area(self):
        try:
            expected_results = {
                "Incolume": 2,
                "Ferito": 3,
                "Morto nelle 24 ore": 1,
                "Morto entro il trentesimo giorno": 1
            }
            result_df = self.aggregator.get_passengers_outcome("nation_wide", passenger_column="COL01")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results), "Check the number of found results.")

            result_df = result_df[["descrizione","Numero"]]
            result_df.set_index("descrizione", inplace=True)
            for key in expected_results.keys():
                self.assertTrue(result_df.loc[key]['Numero'] == expected_results[key], "Check key value {k}".format(k=key))
            pass
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_nation_province(self):
        try:
            expected_results = {
                "Incolume": 2,
                "Morto nelle 24 ore": 1,
                "Morto entro il trentesimo giorno": 1
            }
            result_df = self.aggregator.get_passengers_outcome("province", passenger_column="COL02", name="MI")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results), "Check the number of found results.")

            result_df = result_df[["descrizione","Numero"]]
            result_df.set_index("descrizione", inplace=True)
            for key in expected_results.keys():
                self.assertTrue(result_df.loc[key]['Numero'] == expected_results[key], "Check key value {k}".format(k=key))
        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_passengers_outcome_nation_city(self):
        try:
            expected_results = {
                "Ferito": 2,
            }
            result_df = self.aggregator.get_passengers_outcome("city", passenger_column="COL02", name="Como")
            self.assertTrue(result_df is not None, "None return not allowed.")
            self.assertTrue(result_df.shape[0] == len(expected_results), "Check the number of found results.")

            result_df = result_df[["descrizione","Numero"]]
            result_df.set_index("descrizione", inplace=True)
            for key in expected_results.keys():
                self.assertTrue(result_df.loc[key]['Numero'] == expected_results[key], "Check key value {k}".format(k=key))

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
class TestIncidentsDataframeAggregator(TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_incidents_outcome_by_typology

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(file_incidenti = os.path.join("/fake_dir/", "fake_file_name.csv"),
                                   anagrafica_comuni = AnagraficaComuni("/fake_dir/fake_filename.csv"),
                                   support_dataframes = support_dataframes,
                                   data_file_configurations=config["data_structure"])
        test_df = {
            "Comune":          ["Milano", "Como", "Como", "Cernusco Sul Naviglio","Roncolate", "Vimodrone"],
            "comune":          ["1001",   "1000", "1000", "1002",                 "1003",      "1004"],
            "Provincia":       ["MI",     "CO",   "CO",   "MI",                    "BG",       "MI"],

            "COL01":           [ 0,        1,      1,      1,                       1,          4], # Incolume: 4, Morto: 1
            "COL02":           [ 4,        3,      1,      2,                       2,          1], # Incolume: 2, Morto: 2, Ferito: 2
            "COL03":           [ 1,        1,      2,      2,                       4,          1], # Incolume: 3, Morto: 1, Ferito: 2

            "ODEA01":          [ 0,        0,      2,      1,                       0,          1], # 4

            "OINJ01":          [ 3,        0,      0,      0,                       5,          0], # 8
            "OINJ02":          [ 6,        0,      1,      1,                       0,          1], # 9
            "natura_incidente":[11,        1,      1,      2,                       2,          1]
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_get_incidents_outcome_by_typology_unknown_area(self):
        try:
            result = self.aggregator.get_incidents_outcome_by_typology("UNKNOWN")
            self.assertTrue(result is None, "No result must be returned.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_incidents_outcome_by_typology_nation_wide(self):
        try:
            fi_expected_feriti = 9
            fi_expected_morti = 1
            fi_expected_incolumi = 1

            result_df = self.aggregator.get_incidents_outcome_by_typology("nation_wide")
            self.assertTrue(result_df is not None, "Must be a valid dataframe.")

            result_df.set_index('natura_incidente', inplace=True)
            frenata_improvvisa = result_df.loc[11]
            self.assertTrue(frenata_improvvisa['Morti'] == fi_expected_morti, "Check value for Morti.")
            self.assertTrue(frenata_improvvisa['Feriti'] == fi_expected_feriti, "Check value for Feriti.")
            self.assertTrue(frenata_improvvisa['Incolumi'] == fi_expected_incolumi, "Check value for Incolumi.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_incidents_outcome_by_typology_provincia(self):
        try:
            scf_expected_feriti = 3
            scf_expected_morti = 1
            scf_expected_incolumi = 1

            result_df = self.aggregator.get_incidents_outcome_by_typology("province", "MI")
            self.assertTrue(result_df is not None, "Must be a valid dataframe.")

            result_df.set_index('natura_incidente', inplace=True)
            scontro_front_lat = result_df.loc[2]
            self.assertTrue(scontro_front_lat['Morti']    == scf_expected_morti, "Check value for Morti.")
            self.assertTrue(scontro_front_lat['Feriti']   == scf_expected_feriti, "Check value for Feriti.")
            self.assertTrue(scontro_front_lat['Incolumi'] == scf_expected_incolumi, "Check value for Incolumi.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_incidents_outcome_by_typology_city(self):
        try:
            scf_expected_feriti = 3
            scf_expected_morti = 1
            scf_expected_incolumi = 1

            result_df = self.aggregator.get_incidents_outcome_by_typology("city", "Cernusco Sul Naviglio")
            self.assertTrue(result_df is not None, "Must be a valid dataframe.")

            result_df.set_index('natura_incidente', inplace=True)
            scontro_front_lat = result_df.loc[2]
            self.assertTrue(scontro_front_lat['Morti']    == scf_expected_morti, "Check value for Morti.")
            self.assertTrue(scontro_front_lat['Feriti']   == scf_expected_feriti, "Check value for Feriti.")
            self.assertTrue(scontro_front_lat['Incolumi'] == scf_expected_incolumi, "Check value for Incolumi.")

            scf_expected_feriti = 2
            scf_expected_morti = 3
            scf_expected_incolumi = 4

            result_df = self.aggregator.get_incidents_outcome_by_typology("city", "Como")
            self.assertTrue(result_df is not None, "Must be a valid dataframe.")

            result_df.set_index('natura_incidente', inplace=True)
            scontro_front_lat = result_df.loc[1]
            self.assertTrue(scontro_front_lat['Morti']    == scf_expected_morti, "Check value for Morti.")
            self.assertTrue(scontro_front_lat['Feriti']   == scf_expected_feriti, "Check value for Feriti.")
            self.assertTrue(scontro_front_lat['Incolumi'] == scf_expected_incolumi, "Check value for Incolumi.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")
class TestIncidentsDataframeAggregator_get_number_of_incidents_by_provincia(
        TestCase):
    """
    Unit tests for the method:
            - IncidentsDataframeAggregator->get_number_of_incidents_by_provincia

    """
    def setUp(self) -> None:
        self.__log = logging.getLogger('IncidentsDataframeAggregator')

        s_config = '{"data_structure":{"columns":[], ' \
                    '"convert_to_number": [],' \
                    '"incident_outcome_columns":["COL01","COL02","COL03"],' \
                    '"other_injured_columns": ["OINJ01", "OINJ02"],' \
                    '"other_deadh_columns": ["ODEA01"]' \
                    '}}'
        config = json.loads(s_config)
        support_dataframes = SupportDecodeDataframes()
        support_dataframes.load_dataframes()

        self.incidenti = Incidenti(
            file_incidenti=os.path.join("/fake_dir/", "fake_file_name.csv"),
            anagrafica_comuni=AnagraficaComuni("/fake_dir/fake_filename.csv"),
            support_dataframes=support_dataframes,
            data_file_configurations=config["data_structure"])
        test_df = {
            "Comune": [
                "Milano", "Como", "Como", "Cernusco Sul Naviglio", "Roncolate",
                "Vimodrone", "Almenno"
            ],
            "Provincia": ["MI", "CO", "CO", "MI", "BG", "MI", "AL"],
            "comune": ["1001", "1000", "1000", "1002", "1003", "1004", "1005"]
        }
        self.incidenti.df_incidenti = pd.DataFrame.from_records(test_df)

        self.aggregator = IncidentsDataframeAggregator(self.incidenti)

    def test_get_number_of_incidents_by_provincia(self):
        try:
            expected_result = 3
            result = self.aggregator.get_number_of_incidents_by_provincia("MI")
            self.assertTrue(expected_result == result,
                            "Check the result for MI.")

            expected_result = 1
            result = self.aggregator.get_number_of_incidents_by_provincia("AL")
            self.assertTrue(expected_result == result,
                            "Check the result for BG.")

            expected_result = 2
            result = self.aggregator.get_number_of_incidents_by_provincia("CO")
            self.assertTrue(expected_result == result,
                            "Check the result for CO.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")

    def test_get_number_of_incidents_by_provincia_not_existent(self):
        try:
            expected_result = 0
            result = self.aggregator.get_number_of_incidents_by_provincia(
                "DOES_NOT_EXIST")
            self.assertTrue(expected_result == result,
                            "Check the result for DOES_NOT_EXIST.")

        except Exception as ex:
            self.fail("Exception received - but not expected - test fails.")