def setup_class(m_args): """Run the simulation up to module E0 and prepare bus_data for E1""" if os.path.exists(TEST_OUTPUT_PATH): shutil.rmtree(TEST_OUTPUT_PATH, ignore_errors=True) logging.debug("Accessing script: A0_initialization") user_input = A0.process_user_arguments() A1.create_input_json(input_directory=os.path.join( user_input[PATH_INPUT_FOLDER], CSV_ELEMENTS)) logging.debug("Accessing script: B0_data_input_json") dict_values = B0.load_json( user_input[PATH_INPUT_FILE], path_input_folder=user_input[PATH_INPUT_FOLDER], path_output_folder=user_input[PATH_OUTPUT_FOLDER], move_copy=True, set_default_values=True, ) logging.debug("Accessing script: C0_data_processing") C0.all(dict_values) logging.debug("Accessing script: D0_modelling_and_optimization") results_meta, results_main = D0.run_oemof(dict_values) bus_data = {} # Store all information related to busses in bus_data for bus in dict_values[ENERGY_BUSSES]: # Read all energy flows from busses bus_data.update({bus: solph.views.node(results_main, bus)}) # Pickle dump bus data with open(BUS_DATA_DUMP, "wb") as handle: pickle.dump(bus_data, handle, protocol=pickle.HIGHEST_PROTOCOL)
def test_create_json_from_csv_without_providing_parameters_raises_MissingParameterError( ): with pytest.raises(A1.MissingParameterError): A1.create_json_from_csv(DUMMY_CSV_PATH, "csv_comma", parameters=[], asset_is_a_storage=False)
def test_create_json_from_csv_storage_raises_WrongStorageColumn(): with pytest.raises(A1.WrongStorageColumn): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_column_name", parameters=[AGE_INSTALLED, DEVELOPMENT_COSTS], asset_is_a_storage=True, )
def test_create_json_from_csv_with_unknown_separator_for_csv_raises_CsvParsingError(): with pytest.raises(CsvParsingError): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_unknown_separator", CSV_PARAMETERS, asset_is_a_storage=False, )
def test_create_json_from_csv_storage_raises_WrongParameterWarning(): with pytest.warns(A1.WrongParameterWarning): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_parameter", parameters=[AGE_INSTALLED, DEVELOPMENT_COSTS], asset_is_a_storage=True, )
def test_create_json_from_csv_with_wrong_parameters_raises_WrongParameterWarning( ): with pytest.warns(A1.WrongParameterWarning): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_wrong_parameter", parameters=["param1", "param2"], asset_is_a_storage=False, )
def test_create_json_from_csv_with_uncomplete_parameters_raises_WrongParameterWarning( ): with pytest.raises(A1.MissingParameterError): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_comma", parameters=["param1", "param2", "param3"], asset_is_a_storage=False, )
def test_create_json_from_csv_storage_raises_logging_warning_for_wrong_values(caplog): with caplog.at_level(logging.WARNING): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_values", parameters=[AGE_INSTALLED, DEVELOPMENT_COSTS], asset_is_a_storage=True, ) assert ( "be set to NaN." in caplog.text ), f"There is parameter with a wrong value in provided in the storage asset, but it is not recognized as suchblack ."
def test_create_json_from_csv_storage_raises_logging_warning(caplog): with caplog.at_level(logging.WARNING): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_parameter", parameters=[AGE_INSTALLED, DEVELOPMENT_COSTS], asset_is_a_storage=True, ) assert ( "is not recognized." in caplog.text ), f"There is a unexpected/wrong parameter provided in the storage asset, but it is not recognized as such."
def test_create_json_from_csv_with_wrong_parameters_raises_loggin_warning(caplog): with caplog.at_level(logging.WARNING): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_wrong_parameter", parameters=["param1", "param2"], asset_is_a_storage=False, ) assert ( ".csv is not expected." in caplog.text ), f"There is an unexpected/wrong parameter in the inputs, but it is not recognized as such."
def test_load_json_copies_json_file_to_output_folder(self, m_args): A0.process_user_arguments() A1.create_input_json(input_directory=CSV_PATH, pass_back=True) dict_values = B0.load_json(JSON_CSV_PATH, path_output_folder=self.test_out_path, move_copy=True) assert (os.path.exists( os.path.join( dict_values[SIMULATION_SETTINGS][PATH_OUTPUT_FOLDER_INPUTS], CSV_FNAME, )) is True)
def test_create_json_from_csv_with_ampersand_separated_csv(): d = A1.create_json_from_csv(DUMMY_CSV_PATH, "csv_ampersand", CSV_PARAMETERS, asset_is_a_storage=False) assert d == {"csv_ampersand": CSV_EXAMPLE}
def test_default_values_storage_with_thermal_losses(): exp = { THERM_LOSSES_REL: {UNIT: "no_unit", VALUE: 0.001}, THERM_LOSSES_ABS: {UNIT: "kWh", VALUE: 0.00001}, } data_path = os.path.join( TEST_REPO_PATH, "benchmark_test_inputs", "Feature_stratified_thermal_storage", "csv_elements", ) json = A1.create_json_from_csv( input_directory=data_path, filename="storage_fix_with_fixed_thermal_losses_float", parameters=[ "age_installed", "development_costs", "specific_costs", "efficiency", "installedCap", "lifetime", "specific_costs_om", "unit", ], asset_is_a_storage=True, ) for param, result in exp.items(): assert ( json["storage capacity"][param][VALUE] == result[VALUE] ), f"{param} should match {result[VALUE]}"
def test_create_json_from_csv_float_int_parsing(): exp = { "param1": { UNIT: "years", VALUE: 50.0 }, "param2": { UNIT: "factor", VALUE: 0.2 }, "param3": { UNIT: "currency", VALUE: 65.5 }, } json = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_float_int", parameters=["param1", "param2", "param3"], asset_is_a_storage=False, ) for param in exp: assert json["csv_float_int"]["col1"][param][VALUE] == exp[param][VALUE] assert type(json["csv_float_int"]["col1"]["param1"][VALUE]) is int assert type(json["csv_float_int"]["col1"]["param2"][VALUE]) is float assert type(json["csv_float_int"]["col1"]["param3"][VALUE]) is float
def test_create_json_from_csv_without_providing_parameters_raises_MissingParameterError(): with pytest.raises(MissingParameterError): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_comma", parameters=[], asset_is_a_storage=False ) utils.compare_input_parameters_with_reference(d, flag_missing=True)
def test_if_check_for_official_extra_parameters_adds_no_parameter_when_not_necessary( ): parameters_updated, _ = A1.check_for_official_extra_parameters( filename_b, df_no_new_parameter, parameters, official_extra_parameters=list_of_new_parameter, ) assert parameters == parameters_updated
def test_if_check_for_official_extra_parameters_adds_to_parameter_list_when_new_parameter_exists( ): parameters_updated, _ = A1.check_for_official_extra_parameters( filename_a, df_with_new_parameter, parameters, official_extra_parameters=list_of_new_parameter, ) assert ["unit", "value", "max"] == parameters_updated
def test_if_check_for_official_extra_parameters_raises_warning_if_parameter_doesnt_exist( ): with pytest.warns(A1.MissingParameterWarning): parameters_updated, _ = A1.check_for_official_extra_parameters( filename_a, df_no_new_parameter, parameters, official_extra_parameters=list_of_new_parameter, )
def test_load_json_removes_json_file_from_inputs_folder(self, m_args): A0.process_user_arguments() A1.create_input_json(input_directory=CSV_PATH, pass_back=True) dict_values = B0.load_json(JSON_CSV_PATH, path_output_folder=self.test_out_path, move_copy=True) assert os.path.exists(os.path.join( CSV_PATH, CSV_ELEMENTS, CSV_FNAME, )) is False assert os.path.exists(os.path.join( CSV_PATH, CSV_FNAME, )) is False
def test_create_json_from_csv_ignore_extra_parameters_in_csv(): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_wrong_parameter", parameters=["param1", "param2"], asset_is_a_storage=False, ) assert d == {"csv_wrong_parameter": CSV_EXAMPLE}
def test_create_json_from_csv_storage_raises_MissingParameterError(): with pytest.raises(A1.MissingParameterError): A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_parameter", parameters=[ AGE_INSTALLED, DEVELOPMENT_COSTS, C_RATE, DISPATCH_PRICE, SOC_INITIAL, SOC_MAX, SOC_MIN, INSTALLED_CAP, ], asset_is_a_storage=True, )
def test_create_json_from_csv_for_list(): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_list", parameters=["param1", "param2", "param3", "param4", "param5"], asset_is_a_storage=False, ) for k, v in d["csv_list"]["col1"].items(): assert v == CSV_LIST[k]
def test_create_json_from_csv_for_time_series(): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_timeseries", parameters=["param1"], asset_is_a_storage=False, ) for k, v in d["csv_timeseries"]["col1"].items(): assert v == CSV_TIMESERIES[k]
def test_add_storage_components_label_correctly_added(): storage_label = "ESS Li-Ion" input_directory = os.path.join(TEST_REPO_PATH, INPUT_FOLDER, CSV_ELEMENTS) single_dict = A1.add_storage_components( storage_filename="storage_01", input_directory=input_directory, storage_label=storage_label, ) for column_name in [STORAGE_CAPACITY, INPUT_POWER, OUTPUT_POWER]: assert ( single_dict[column_name][LABEL] == f"{storage_label} {column_name}" ), f"Label of storage {storage_label} defined incorrectly, should be: '{storage_label} {column_name}'."
def test_create_json_from_csv_storage_raises_MissingParameterError(): with pytest.raises(MissingParameterError): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_storage_wrong_parameter", parameters=[ AGE_INSTALLED, DEVELOPMENT_COSTS, C_RATE, DISPATCH_PRICE, SOC_INITIAL, SOC_MAX, SOC_MIN, INSTALLED_CAP, ], asset_is_a_storage=True, ) utils.compare_input_parameters_with_reference(d, flag_missing=True)
def test_conversion(): d = A1.create_json_from_csv( DUMMY_CSV_PATH, "csv_type", parameters=[ "param_str", "param_factor", "param_cur", "param_bool1", "param_bool2", "param_bool3", "param_bool4", "param_bool5", "param_bool6", "param_year", ], asset_is_a_storage=False, ) for k, v in d["csv_type"]["col1"].items(): assert v == CONVERSION_TYPE[k]
def test_create_json_from_csv_file_not_exist_raises_filenotfound_error(): with pytest.raises(FileNotFoundError): A1.create_json_from_csv(input_directory=CSV_PATH, filename="not_existing", parameters=[])
def test_create_input_json_already_existing_json_file_raises_FileExistsError(): with open(os.path.join(CSV_PATH, CSV_FNAME), "w") as of: of.write("something") with pytest.raises(FileExistsError): A1.create_input_json(input_directory=CSV_PATH)
def test_create_input_json_raises_FileNotFoundError_if_missing_required_csv_files( ): with pytest.raises(FileNotFoundError): A1.create_input_json(input_directory=DUMMY_CSV_PATH)
def test_create_input_json_required_fields_are_filled(): js_file = A1.create_input_json(input_directory=CSV_PATH, pass_back=True) js = data_input.load_json(js_file) for k in js.keys(): assert k in REQUIRED_CSV_FILES + (PATHS_TO_PLOTS, )