diff --git a/src/outputs/intram_by_civil_defence.py b/src/outputs/intram_by_civil_defence.py index 03c24d3ce..b893ccf39 100644 --- a/src/outputs/intram_by_civil_defence.py +++ b/src/outputs/intram_by_civil_defence.py @@ -27,8 +27,7 @@ def output_intram_by_civil_defence( """ df_for_output = generate_intram_by_civil_defence( - df - ) + df) # Outputting the CSV file with timestamp and run_id output_path = config["outputs_paths"]["outputs_master"] @@ -38,8 +37,6 @@ def output_intram_by_civil_defence( filename = f"{survey_year}_output_intram_by_civil_defence{tdate}_v{run_id}.csv" write_csv(f"{output_path}/output_intram_by_civil_defence/{filename}", df_for_output) - - return df_for_output diff --git a/tests/test_outputs/test_intram_by_civil_defence.py b/tests/test_outputs/test_intram_by_civil_defence.py index c0a430278..c05f0673a 100644 --- a/tests/test_outputs/test_intram_by_civil_defence.py +++ b/tests/test_outputs/test_intram_by_civil_defence.py @@ -1,41 +1,35 @@ """Tests for intram_by_civil_defence.py.""" -# Standard Library Imports -#Local Standard Library Imports -import pytest - -#Third Party Imports +# Local Standard Library Imports import pandas as pd -import numpy as np +import pytest # Local Imports -from src.outputs.intram_by_civil_defence import (output_intram_by_civil_defence) +from src.outputs.intram_by_civil_defence import (generate_intram_by_civil_defence) + class TestIntramByCivilDefence(object): + """Test for Civil and Defence Output.""" - @pytest.fixture(scope="function") def input_data(self): - """Input dataframes for civil_defence output""" - columns= ["reference", "period", "200", "211"] - - data = [1, 2020, "C", 1000, - 2, 2020, "D", 2000, - 3, 2020, "C", nan, - 4, 2020, "D", 500, - 5, 2020, "C", 3020, - 6, 2020, "D", 40, - 7 2020, "D", 6000, - 8, 2020, "C", 700, - 9, 2020, "D", 8180, - 10, 2020, "C", 960] - df=pd.DataFrame(data=data, columns=columns) - return - + """Create input dataframe""" + + data = {"200": ["C", "D", "C", "D", "C", "D", "D", "C", "D", "C"], + "211": [1000, 2000, 0, 500, 3020, 40, 6000, 700, 8180, 960], } + input_df = pd.DataFrame(data) + return input_df + @pytest.fixture(scope="function") - def exp_out(self: - """Expected output for civil_defence output""" - columns = ["200", "211"] - data = [["Civil", 4720], ["Defence", 10720]] - df = pd.DataFrame(data=data, columns=columns) - return df + def exp_out(self): + """Create expected output dataframe""" + columns = ["Catergory", "Total Intramural Expenditure"] + data = [["Civil", 5680], ["Defence", 16720]] + expected_df = pd.DataFrame(data=data, columns=columns) + return expected_df + + def test_generate_intram_by_civil_defence(self, input_data, exp_out): + """Test generate_intram_by_civil_defence function. + The test checks if Civil and Defense are catergorised and summed correctly.""" + output_df = generate_intram_by_civil_defence(input_data) + assert output_df.equals(exp_out) diff --git a/tests/test_outputs/test_short_form.py b/tests/test_outputs/test_short_form.py index 862a70179..87531e87a 100644 --- a/tests/test_outputs/test_short_form.py +++ b/tests/test_outputs/test_short_form.py @@ -1,6 +1,5 @@ import numpy as np import pandas as pd -import unittest from pandas._testing import assert_frame_equal from src.outputs.short_form import create_headcount_cols @@ -9,48 +8,36 @@ class TestCreateHeadcountCols: """Test for create_headcount_cols function.""" - def create_input_df(self): - """Create input dataframe.""" - data = { - "reference": [1, 2, 3, 4, 5, 6], - "701": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "702": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "703": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "704": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "705": [100.0, 0.0, 200.0, 300.0, np.nan, 400], - "706": [20.0, 0.0, 80.0, 0.0, 10.0, 20], - "707": [10.0, 0.0, 0.0, 0.0, np.nan, np.nan], - "709": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "710": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "711": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], - "formtype": ["0006", "0006", "0006", "0006", "0006", "0006"], - "status": ["Clear", "Clear", "Clear", "Clear", "Clear", "Clear"], - } - - input_data_df = pd.DataFrame(data) + def create_input_data(self): + """Create input data as list of columns and list of rows.""" + columns = [ + "reference", "701", "702", "703", "704", "705", "706", "707", "709", "710", "711", "formtype", "status" + ] + data = [ + [1, np.nan, np.nan, np.nan, np.nan, 100.0, 20.0, 10.0, np.nan, np.nan, np.nan, "0006", "Clear"], + [2, np.nan, np.nan, np.nan, np.nan, 0.0, 0.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear"], + [3, np.nan, np.nan, np.nan, np.nan, 200.0, 80.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear"], + [4, np.nan, np.nan, np.nan, np.nan, 300.0, 0.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear"], + [5, np.nan, np.nan, np.nan, np.nan, np.nan, 10.0, np.nan, np.nan, np.nan, np.nan, "0006", "Clear"], + [6, np.nan, np.nan, np.nan, np.nan, 400, 20, np.nan, np.nan, np.nan, np.nan, "0006", "Clear"] + ] + input_data_df = pd.DataFrame(data=data, columns=columns) return input_data_df def create_expected_df(self): - """Create expected output dataframe.""" - data = { - "reference": [1, 2, 3, 4, 5, 6], - "701": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "702": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "703": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "704": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "705": [100.0, 0.0, 200.0, 300.0, 0.0, 400], - "706": [20.0, 0.0, 80.0, 0.0, 10.0, 20], - "707": [10.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "709": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "710": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "711": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], - "formtype": ["0006", "0006", "0006", "0006", "0006", "0006"], - "status": ["Clear", "Clear", "Clear", "Clear", "Clear", "Clear"], - "headcount_civil": [66.6667, 0.0, 200.0, 0.0, 0.0, 400], - "headcount_defence": [33.3333, 0.0, 0.0, 0.0, 0.0, 0], - } - - expected_data_df = pd.DataFrame(data) + """Create expected data as list of columns and list of rows.""" + columns = [ + "reference", "701", "702", "703", "704", "705", "706", "707", "709", "710", "711", "formtype", "status", "headcount_civil", "headcount_defence" + ] + data = [ + [1, np.nan, np.nan, np.nan, np.nan, 100.0, 20.0, 10.0, np.nan, np.nan, np.nan, "0006", "Clear", 66.6667, 33.3333], + [2, np.nan, np.nan, np.nan, np.nan, 0.0, 0.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear", 0.0, 0.0], + [3, np.nan, np.nan, np.nan, np.nan, 200.0, 80.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear", 200.0, 0.0], + [4, np.nan, np.nan, np.nan, np.nan, 300.0, 0.0, 0.0, np.nan, np.nan, np.nan, "0006", "Clear", 0.0, 0.0], + [5, np.nan, np.nan, np.nan, np.nan, np.nan, 10.0, np.nan, np.nan, np.nan, np.nan, "0006", "Clear", 0.0, 0.0], + [6, np.nan, np.nan, np.nan, np.nan, 400, 20, np.nan, np.nan, np.nan, np.nan, "0006", "Clear", 400, 0] + ] + expected_data_df = pd.DataFrame(data=data, columns=columns) return expected_data_df def test_create_headcount_cols(self):