def test_write_only_activity_parameters_no_activate_others(no_init): Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = deepcopy(DATA) obj.write_database(activate_parameters=False) NEW = [{ 'code': '45cb34db4147e510a2561cceec541f6b', 'database': 'PCB', 'exchanges': [], 'name': 'unmounted printed circuit board', 'type': 'process', 'unit': 'square meter', 'parameters': [{ 'name': 'something_test', 'amount': 2, 'formula': '3 + 2' }], }] obj = ExcelImporter() obj.db_name = "PCB" obj.write_activity_parameters(NEW) assert ActivityParameter.select().count() == 1 assert ActivityParameter.get().formula == '3 + 2' assert 'parameters' not in get_activity( ('PCB', '45cb34db4147e510a2561cceec541f6b')) assert 'parameters' in get_activity( ('PCB', '32aa5ab78beda5b8c8efbc89587de7a5'))
def test_write_only_activity_parameters_no_activate_others(no_init): Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = deepcopy(DATA) obj.write_database(activate_parameters=False) NEW = [ { "code": "45cb34db4147e510a2561cceec541f6b", "database": "PCB", "exchanges": [], "name": "unmounted printed circuit board", "type": "process", "unit": "square meter", "parameters": [{"name": "something_test", "amount": 2, "formula": "3 + 2"}], } ] obj = ExcelImporter() obj.db_name = "PCB" obj.write_activity_parameters(NEW) assert ActivityParameter.select().count() == 1 assert ActivityParameter.get().formula == "3 + 2" assert "parameters" not in get_activity(("PCB", "45cb34db4147e510a2561cceec541f6b")) assert "parameters" in get_activity(("PCB", "32aa5ab78beda5b8c8efbc89587de7a5"))
def test_write_only_activity_parameters(no_init): assert not ActivityParameter.select().count() Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = deepcopy(DATA) obj.write_database() assert ActivityParameter.select().count() == 1 assert ActivityParameter.get().amount != 7 NEW = [{ 'code': '32aa5ab78beda5b8c8efbc89587de7a5', 'database': 'PCB', 'parameters': [{ 'name': 'PCB_mass_total', 'amount': 11, 'formula': '7' }], }] obj.write_activity_parameters(NEW) assert ActivityParameter.select().count() == 1 a = ActivityParameter.get() assert a.formula == '7' assert a.amount == 7
def test_excel_products_lca(): ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "with_products.xlsx")) ei.strategies = [ csv_restore_tuples, csv_restore_booleans, csv_numerize, csv_drop_unknown, csv_add_missing_exchanges_section, normalize_units, set_code_by_activity_hash, assign_only_product_as_production, link_technosphere_by_activity_hash, drop_falsey_uncertainty_fields_but_keep_zeros, convert_uncertainty_types_to_integers, ] ei.apply_strategies() ei.match_database() ei.write_database() lca = LCA({('Product example', 'B'): 1}) lca.lci() keys = {('Product example', 'B'), ('Product example', 'C'), ('Product example', 'E')} for key in lca.product_dict: assert key in keys keys = {('Product example', 'A'), ('Product example', 'C'), ('Product example', 'D')} for key in lca.activity_dict: assert key in keys for value in lca.supply_array: assert np.allclose(value, 1) or np.allclose( value, 0.539) or np.allclose(value, 0.539 * 0.00805)
def test_example_notebook(): ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "sample_activities_with_variables.xlsx")) ei.strategies = [ csv_restore_tuples, csv_restore_booleans, csv_numerize, csv_drop_unknown, csv_add_missing_exchanges_section, normalize_units, set_code_by_activity_hash, assign_only_product_as_production, link_technosphere_by_activity_hash, drop_falsey_uncertainty_fields_but_keep_zeros, convert_uncertainty_types_to_integers, convert_activity_parameters_to_list, ] ei.apply_strategies() ei.match_database(fields=['name']) ei.write_database()
def test_empty_activity_parameters_dont_delete(no_init): # Empty activity parameters aren't considered at all # so they shouldn't be deleted if empty section provided Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = deepcopy(DATA) obj.write_database() assert len(parameters) NEW = deepcopy(DATA) for ds in NEW: if "parameters" in ds: del ds["parameters"] Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = NEW obj.write_database(delete_existing=False) assert len(parameters)
def test_write_only_activity_parameters(no_init): assert not ActivityParameter.select().count() Database("PCB").register() obj = ExcelImporter() obj.db_name = "PCB" obj.data = deepcopy(DATA) obj.write_database() assert ActivityParameter.select().count() == 1 assert ActivityParameter.get().amount != 7 NEW = [ { "code": "32aa5ab78beda5b8c8efbc89587de7a5", "database": "PCB", "parameters": [{"name": "PCB_mass_total", "amount": 11, "formula": "7"}], } ] obj.write_activity_parameters(NEW) assert ActivityParameter.select().count() == 1 a = ActivityParameter.get() assert a.formula == "7" assert a.amount == 7
def test_excel_products_lca(): ei = ExcelImporter(os.path.join(EXCEL_FIXTURES_DIR, "with_products.xlsx")) ei.strategies = [ csv_restore_tuples, csv_restore_booleans, csv_numerize, csv_drop_unknown, csv_add_missing_exchanges_section, normalize_units, set_code_by_activity_hash, assign_only_product_as_production, link_technosphere_by_activity_hash, drop_falsey_uncertainty_fields_but_keep_zeros, convert_uncertainty_types_to_integers, ] print(ei.data) ei.apply_strategies() ei.match_database() ei.write_database() fu, data_objs, _ = prepare_lca_inputs({("Product example", "B"): 1}) lca = LCA(fu, data_objs=data_objs) lca.lci() keys = { get_id(("Product example", "B")), get_id(("Product example", "C")), get_id(("Product example", "E")), } for key in lca.dicts.product: assert key in keys keys = { get_id(("Product example", "A")), get_id(("Product example", "C")), get_id(("Product example", "D")), } for key in lca.dicts.activity: assert key in keys for value in lca.supply_array: assert (np.allclose(value, 1) or np.allclose(value, 0.539) or np.allclose(value, 0.539 * 0.00805))