def test_MTC_find_by_code(sess): market_role_R = MarketRole.insert(sess, "R", "Distributor") participant = Participant.insert(sess, "CALB", "AK Industries") dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") code = "034" meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, dno, code, "an mtc", False, False, True, meter_type, meter_payment_type, 1, utc_datetime(2000, 1, 1), None, ) sess.commit() mtc = Mtc.find_by_code(sess, dno, "34", utc_datetime(2000, 1, 1)) assert mtc.code == code
def test_MTC_find_by_code(mocker): q_mock = mocker.Mock() q_mock.filter_by = mocker.Mock(return_value=mocker.Mock()) sess = mocker.Mock() sess.query.return_value = q_mock dno = mocker.Mock() code = "34" Mtc.find_by_code(sess, dno, code) q_mock.filter_by.assert_called_with(dno=dno, code="034")
def _import_Meter_Timeswitch_Class(sess, csv_reader): for values in csv_reader: code = values[0].zfill(3) valid_from = parse_date(values[1]) valid_to = parse_to_date(values[2]) description = values[3] is_common = values[4] == "T" if is_common: has_related_metering = values[5] == "T" meter_type_code = values[6] meter_type = MeterType.get_by_code(sess, meter_type_code, valid_from) meter_payment_type_code = values[7] meter_payment_type = MeterPaymentType.get_by_code( sess, meter_payment_type_code, valid_from) has_comms = values[8] == "T" is_hh = values[9] == "H" tpr_count_str = values[10] tpr_count = 0 if tpr_count_str == "" else int(tpr_count_str) mtc = Mtc.find_by_code(sess, None, code, valid_from) if mtc is None: Mtc.insert( code, description, has_related_metering, has_comms, is_hh, meter_type, meter_payment_type, tpr_count, valid_from, valid_to, ) else: mtc.description = description mtc.has_related_metering = has_related_metering mtc.has_comms = has_comms mtc.is_hh = is_hh mtc.meter_type = meter_type mtc.meter_payment_type = meter_payment_type mtc.tpr_count = tpr_count mtc.valid_to = valid_to sess.flush()
def content(table, version, fin, user): sess = None try: sess = Session() running_name, finished_name = chellow.dloads.make_names( table + '_' + version + '_general_import.csv', user) f = open(running_name, mode='w', newline='') w = csv.writer(f, lineterminator='\n') reader = iter(csv.reader(fin)) next(reader) if table == 'Line_Loss_Factor_Class': VOLTAGE_LEVEL_CODES = set( [v.code for v in sess.query(VoltageLevel)]) DNO_MAP = dict( (dno.participant.code, dno) for dno in sess.query(Party). join(MarketRole).filter(MarketRole.code == 'R').options( joinedload(Party.participant))) for i, values in enumerate(reader): participant_code = values[0] # market_role_code = values[1] llfc_code = values[3].zfill(3) valid_from = parse_date(values[4]) description = values[5] is_import = values[6] in ('A', 'B') is_substation = any( p in description for p in ( '_SS', ' SS', ' S/S', '(S/S)', 'sub', 'Sub')) valid_to = parse_to_date(values[7]) voltage_level_code = 'LV' description_upper = description.upper() for vl_code in VOLTAGE_LEVEL_CODES: if vl_code in description_upper: voltage_level_code = vl_code break try: dno = DNO_MAP[participant_code] except KeyError: w.writerow( ( "# There is no DNO with participant code ", participant_code)) continue llfc = sess.query(Llfc).filter( Llfc.dno == dno, Llfc.code == llfc_code, Llfc.valid_from == valid_from).first() if llfc is None: w.writerow( ( 'insert', 'llfc', dno.dno_code, llfc_code, description, voltage_level_code, is_substation, is_import, hh_format(valid_from), hh_format(valid_to, ongoing_str=''))) elif any( ( description != llfc.description, voltage_level_code != llfc.voltage_level.code, is_substation != llfc.is_substation, is_import != llfc.is_import, valid_to != llfc.valid_to)): w.writerow( ( 'update', 'llfc', dno.dno_code, llfc.code, hh_format(llfc.valid_from), description, voltage_level_code, is_substation, is_import, hh_format(valid_to, ongoing_str=''))) elif table == 'Market_Participant': for i, values in enumerate(reader): participant_code = values[0] participant_name = values[1] participant = sess.query(Participant).filter( Participant.code == participant_code).first() if participant is None: w.writerow( ( 'insert', 'participant', participant_code, participant_name)) elif participant_name != participant.name: w.writerow( ( 'update', 'participant', participant_code, participant_name)) elif table == 'Market_Role': for i, values in enumerate(reader): role_code = values[0] role_description = values[1] role = sess.query(MarketRole).filter( MarketRole.code == role_code).first() if role is None: w.writerow( ( 'insert', 'market_role', role_code, role_description)) elif role_description != role.description: w.writerow( ( 'update', 'market_role', role_code, role_description)) elif table == 'Market_Participant_Role': for i, values in enumerate(reader): participant_code = values[0] market_role_code = values[1] valid_from = parse_date(values[2]) party = sess.query(Party).join(Participant). \ join(MarketRole).filter( Party.valid_from == valid_from, Participant.code == participant_code, MarketRole.code == market_role_code).first() valid_to = parse_to_date(values[3]) name = values[4] dno_code_str = values[14] dno_code = None if len(dno_code_str) == 0 else dno_code_str if dno_code == '99': continue if party is None: w.writerow( ( 'insert', 'party', market_role_code, participant_code, name, hh_format(valid_from), hh_format(valid_to, ongoing_str=''), dno_code_str)) elif any( ( name != party.name, dno_code != party.dno_code, valid_to != party.valid_to)): w.writerow( ( 'update', 'party', market_role_code, participant_code, name, hh_format(valid_from), hh_format(valid_to, ongoing_str=''), dno_code_str)) elif table == 'Meter_Timeswitch_Class': for i, values in enumerate(reader): code = values[0].zfill(3) valid_from = parse_date(values[1]) valid_to = parse_to_date(values[2]) description = values[3] is_common = values[4] == 'T' has_related_metering = values[5] == 'T' meter_type_code = values[6] meter_payment_type_code = values[7] has_comms = values[8] == 'T' is_hh = values[9] == 'H' tpr_count_str = values[10] tpr_count = 0 if tpr_count_str == '' else int(tpr_count_str) if is_common: mtc = sess.query(Mtc).filter( Mtc.dno == null(), Mtc.code == code, Mtc.valid_from == valid_from).first() if mtc is None: w.writerow( ( 'insert', 'mtc', '', code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(valid_from), hh_format(valid_to, ongoing_str=''))) elif any( ( description != mtc.description, has_related_metering != mtc.has_related_metering, has_comms != mtc.has_comms, is_hh != mtc.is_hh, meter_type_code != mtc.meter_type.code, meter_payment_type_code != mtc.meter_payment_type.code, tpr_count != mtc.tpr_count, valid_to != mtc.valid_to)): w.writerow( ( 'update', 'mtc', '', mtc.code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(mtc.valid_from), hh_format(valid_to, ongoing_str=''))) elif table == 'MTC_in_PES_Area': dnos = dict( (p.participant.code, (p.id, p.dno_code)) for p in sess.query( Party).join(Participant).join(MarketRole).filter( MarketRole.code == 'R').options( joinedload(Party.participant))) mtcs = dict( ((m.dno_id, m.code, m.valid_from), m) for m in sess.query(Mtc).options( joinedload(Mtc.meter_type), joinedload(Mtc.meter_payment_type)).all()) for i, values in enumerate(reader): code_str = values[0] if not Mtc.has_dno(code_str): continue code_int = int(code_str) code = code_str.zfill(3) participant_code = values[2] dno_id, dno_code = dnos[participant_code] valid_from = parse_date(values[3]) valid_to = parse_to_date(values[4]) description = values[5] meter_type_code = values[6] meter_payment_type_code = values[7] has_related_metering = code_int > 500 has_comms = values[8] == 'Y' is_hh = values[9] == 'H' tpr_count_str = values[10] tpr_count = 0 if tpr_count_str == '' else int(tpr_count_str) mtc = mtcs.get((dno_id, code, valid_from)) if mtc is None: w.writerow( ( 'insert', 'mtc', dno_code, code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(valid_from), hh_format(valid_to, ongoing_str=''))) elif any( ( description != mtc.description, has_related_metering != mtc.has_related_metering, has_comms != mtc.has_comms, is_hh != mtc.is_hh, meter_type_code != mtc.meter_type.code, meter_payment_type_code != mtc.meter_payment_type.code, tpr_count != mtc.tpr_count, valid_to != mtc.valid_to)): w.writerow( ( 'update', 'mtc', mtc.dno.dno_code, mtc.code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(mtc.valid_from), hh_format(valid_to, ongoing_str=''))) elif table == 'MTC_Meter_Type': for i, values in enumerate(reader): code = values[0] description = values[1] valid_from = parse_date(values[2]) valid_to = parse_to_date(values[3]) pt = sess.query(MeterType).filter( MeterType.code == code, MeterType.valid_from == valid_from).first() if pt is None: w.writerow( ( 'insert', 'meter_type', code, description, hh_format(valid_from), hh_format(valid_to, ongoing_str=''))) elif (description, valid_from, valid_to) != ( pt.description, pt.valid_from, pt.valid_to): w.writerow( ( 'update', 'meter_type', code, description, hh_format(valid_from), hh_format(valid_to))) else: raise Exception("The table " + table + " is not recognized.") except BaseException: w.writerow([traceback.format_exc()]) finally: if sess is not None: sess.close() if f is not None: f.close() os.rename(running_name, finished_name)
def test_get_era_bundles_bill_after_supply_end(sess, client): """Check that a bill starting after the end of a supply still gets shown. """ site = Site.insert(sess, "22488", "Water Works") insert_sources(sess) source = Source.get_by_code(sess, "net") gsp_group = GspGroup.insert(sess, "_L", "South Western") participant = Participant.insert(sess, "hhak", "AK Industries") market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_hhdc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2020, 1, 1), utc_datetime(2020, 1, 31), gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) batch = imp_supplier_contract.insert_batch(sess, "b1", "batch 1") insert_bill_types(sess) bill_type_N = BillType.get_by_code(sess, "N") batch.insert_bill( sess, "ytgeklf", "s77349", utc_datetime(2020, 2, 10), utc_datetime(2020, 2, 2), utc_datetime(2020, 3, 1), Decimal(0), Decimal("0.00"), Decimal("0.00"), Decimal("0.00"), bill_type_N, {}, supply, ) sess.commit() bundles = get_era_bundles(sess, supply) assert len(bundles[0]["imp_bills"]["bill_dicts"]) == 1
def test_Supply_insert_era_at(sess): """Where an era is inserted in the last HH of another era, check the template era is the one at the insertion date. """ site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_hhdc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) mtc_845 = Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") era1_msn = "e1msn" imp_mpan_core = "22 7867 6232 781" supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", era1_msn, pc, "845", cop, None, energisation_status, {}, imp_mpan_core, "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) era1 = supply.eras[0] era2_start_date = utc_datetime(2009, 7, 31, 23, 30) era2 = supply.insert_era_at(sess, era2_start_date) era2_msn = "e2msn" era2.update( sess, era2_start_date, None, mop_contract, "379540", dc_contract, "547yt", era2_msn, pc, mtc_845, cop, None, energisation_status, {}, imp_mpan_core, "510", imp_supplier_contract, "9745y6", 361, None, None, None, None, None, ) sess.commit() start_date = utc_datetime(2009, 7, 31, 23, 00) era3 = supply.insert_era_at(sess, start_date) assert era3.msn == era1.msn
def test_https_handler(mocker, sess): site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_hhdc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) era = supply.eras[0] era.insert_channel(sess, True, "ACTIVE") sess.commit() mock_requests = mocker.patch("chellow.hh_importer.requests") mock_response = mocker.Mock() mock_requests.get.return_value = mock_response mock_response.json.return_value = { "DataPoints": [{ "Flags": 0, "Time": 636188256000000000, "Value": 21 }] } log = [] def log_f(msg): log.append(msg) properties = { "enabled": True, "protocol": "https", "download_days": 8, "url_template": "https://example.com/?from=" "{{chunk_start.strftime('%d/%m/%Y')}}&to=" "{{chunk_finish.strftime('%d/%m/%Y')}}", "url_values": { "22 7907 4116 080": { "api_key": "768234ht" } }, } now = utc_datetime(2020, 12, 22) https_handler(sess, log_f, properties, dc_contract, now=now) expected_log = [ "Window start: 2020-12-14 00:00", "Window finish: 2020-12-21 23:30", "Looking at MPAN core 22 7867 6232 781.", "Retrieving data from " "https://example.com/?from=14/12/2020&to=21/12/2020.", "Finished loading.", ] assert log == expected_log
def test_general_import_era_insert(sess): site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_dc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") insert_comms(sess) comm = Comm.get_by_code(sess, "GSM") exp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, comm, None, energisation_status, {}, None, None, None, None, None, "22 7867 6232 781", "521", exp_supplier_contract, "7748", 361, ) sess.commit() action = "insert" vals = [ "22 7867 6232 781", "2020-10-01 00:00", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "{no change}", "Fusion Supplier 2000", "{no change}", "{no change}", "{no change}", "{no change}", ] args = [] chellow.general_import.general_import_era(sess, action, vals, args)
def content(table, version, f, sess): reader = iter(csv.reader(f)) next(reader) if table == 'Line_Loss_Factor_Class': LLFC_MAP = dict( ((llfc.dno.participant.code, llfc.code), llfc) for llfc in sess.query(Llfc).join(Party).options( joinedload(Llfc.dno).joinedload('participant'))) VOLTAGE_LEVEL_CODES = set( [v.code for v in sess.query(VoltageLevel)]) DNO_MAP = dict( (dno.participant.code, dno) for dno in sess.query(Party). join(MarketRole).filter(MarketRole.code == 'R').options( joinedload(Party.participant))) for i, values in enumerate(reader): participant_code = values[0] # market_role_code = values[1] from_date_mpr = values[2] llfc_code_raw = values[3] # from_date_settlement = values[4] llfc_description = values[5] class_indicator = values[6] to_date_settlement = values[7] llfc_code = llfc_code_raw.zfill(3) llfc = LLFC_MAP.get((participant_code, llfc_code)) if llfc is None: try: dno = DNO_MAP[participant_code] except KeyError: yield ''.join( "# There is no DNO with participant code ", participant_code, ".\n") continue voltage_level_code = 'LV' llfc_description_upper = llfc_description.upper() for vl_code in VOLTAGE_LEVEL_CODES: if vl_code in llfc_description_upper: voltage_level_code = vl_code break is_substation = any( p in llfc_description for p in [ '_SS', ' SS', ' S/S', '(S/S)', 'sub', 'Sub']) is_import = not any( p in class_indicator for p in ['C', 'D']) yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'llfc', dno.dno_code, llfc_code, llfc_description, voltage_level_code, is_substation, is_import, to_iso(from_date_mpr), to_iso(to_date_settlement)))) + "\n" elif table == 'Market_Participant': for i, values in enumerate(reader): participant_code = values[0] participant_name = values[1] participant = sess.query(Participant).filter( Participant.code == participant_code).first() if participant is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'participant', participant_code, participant_name))) + "\n" elif participant_name != participant.name: yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'participant', participant_code, participant_name))) + "\n" elif table == 'Market_Role': for i, values in enumerate(reader): role_code = values[0] role_description = values[1] role = sess.query(MarketRole).filter( MarketRole.code == role_code).first() if role is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'market_role', role_code, role_description))) + "\n" elif role_description != role.description: yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'market_role', role_code, role_description))) + "\n" elif table == 'Market_Participant_Role': for i, values in enumerate(reader): participant_code = values[0] market_role_code = values[1] party = sess.query(Party).join(Participant). \ join(MarketRole).filter( Participant.code == participant_code, MarketRole.code == market_role_code).first() valid_from_str = values[2] valid_from = Datetime.strptime(valid_from_str, "%d/%m/%Y") valid_to_str = values[3] if valid_to_str == '': valid_to = None else: valid_to = Datetime.strptime(valid_to_str, "%d/%m/%Y") name = values[4] dno_code_str = values[14] if len(dno_code_str) == 0: dno_code = None else: dno_code = dno_code_str if party is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'party', market_role_code, participant_code, name, hh_format(valid_from), '' if valid_to is None else hh_format(valid_to), dno_code_str))) + "\n" elif name != party.name or dno_code != party.dno_code: yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'party', market_role_code, participant_code, name, hh_format(valid_from), '' if valid_to is None else hh_format(valid_to), dno_code_str))) + "\n" elif table == 'Meter_Timeswitch_Class': for i, values in enumerate(reader): code_str = values[0] code_int = int(code_str) if is_common_mtc(code_int): code = code_str.zfill(3) valid_from_str = values[1] valid_from = Datetime.strptime( valid_from_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_from_out = hh_format(valid_from) valid_to_str = values[2] if valid_to_str == '': valid_to = None valid_to_out = '' else: valid_to = Datetime.strptime( valid_to_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_to_out = hh_format(valid_to) description = values[3] # common_code_indicator = values[4] has_related_metering_str = values[5] has_related_metering = has_related_metering_str == 'T' meter_type_code = values[6] meter_payment_type_code = values[7] has_comms_str = values[8] has_comms = has_comms_str == 'T' is_hh_str = values[9] is_hh = is_hh_str == 'H' tpr_count_str = values[10] if tpr_count_str == '': tpr_count = 0 else: tpr_count = int(tpr_count_str) mtc = Mtc.find_by_code(sess, None, code) if mtc is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'mtc', '', code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from_out, valid_to_out))) + "\n" elif ( description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from, valid_to) != ( mtc.description, mtc.has_related_metering, mtc.has_comms, mtc.is_hh, mtc.meter_type.code, mtc.meter_payment_type.code, mtc.tpr_count, mtc.valid_from, mtc.valid_to): yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'mtc', '', code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from_out, valid_to_out))) + "\n" elif table == 'MTC_in_PES_Area': dnos = dict( (p.participant.code, (p.id, p.dno_code)) for p in sess.query( Party).join(Participant).join(MarketRole).filter( MarketRole.code == 'R').options( joinedload(Party.participant))) mtcs = dict( ((m.dno_id, m.code), m) for m in sess.query(Mtc).options( joinedload(Mtc.meter_type), joinedload(Mtc.meter_payment_type)).all()) for i, values in enumerate(reader): code_str = values[0] code_int = int(code_str) if not is_common_mtc(code_int): code = code_str.zfill(3) participant_code = values[2] dno_id, dno_code = dnos[participant_code] valid_from_str = values[3] valid_from = Datetime.strptime( valid_from_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_from_out = hh_format(valid_from) valid_to_str = values[4] if valid_to_str == '': valid_to = None valid_to_out = '' else: valid_to = Datetime.strptime( valid_to_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_to_out = hh_format(valid_to) description = values[5] meter_type_code = values[6] meter_payment_type_code = values[7] has_related_metering = code_int > 500 has_comms = values[8] == 'Y' is_hh = values[9] == 'H' tpr_count_str = values[10] if tpr_count_str == '': tpr_count = 0 else: tpr_count = int(tpr_count_str) mtc_dno_id = dno_id if Mtc.has_dno(code) else None mtc = mtcs.get((mtc_dno_id, code)) if mtc is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'mtc', dno_code, code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from_out, valid_to_out))) + "\n" elif ( description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from, valid_to) != ( mtc.description, mtc.has_related_metering, mtc.has_comms, mtc.is_hh, mtc.meter_type.code, mtc.meter_payment_type.code, mtc.tpr_count, mtc.valid_from, mtc.valid_to): yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'mtc', dno_code, code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, valid_from_out, valid_to_out))) + "\n" elif table == 'MTC_Meter_Type': for i, values in enumerate(reader): code = values[0] description = values[1] valid_from_str = values[2] valid_from = Datetime.strptime( valid_from_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_from_out = hh_format(valid_from) valid_to_str = values[3] if valid_to_str == '': valid_to = None valid_to_out = '' else: valid_to = Datetime.strptime( valid_to_str, "%d/%m/%Y").replace(tzinfo=pytz.utc) valid_to_out = hh_format(valid_to) pt = sess.query(MeterType).filter( MeterType.code == code).first() if pt is None: yield ','.join( ( '"' + str(v) + '"' for v in ( 'insert', 'meter_type', code, description, valid_from_out, valid_to_out))) + "\n" elif (description, valid_from, valid_to) != ( pt.description, pt.valid_from, pt.valid_to): yield ','.join( ( '"' + str(v) + '"' for v in ( 'update', 'meter_type', code, description, valid_from_out, valid_to_out))) + "\n" else: raise Exception("The table " + table + " is not recognized.")
def test_process(mocker, sess): site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party( sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None ) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party( sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None ) participant.insert_party( sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None ) participant.insert_party( sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None ) mop_contract = Contract.insert_mop( sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {} ) dc_contract = Contract.insert_dc( sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {} ) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") insert_comms(sess) comm = Comm.get_by_code(sess, "GSM") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party( sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22" ) meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert( sess, "CR", "Credit", utc_datetime(1996, 1, 1), None ) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, comm, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) site.insert_e_supply( sess, source, None, "Dave", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, comm, None, energisation_status, {}, "22 7868 6232 789", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) report_run = ReportRun.insert( sess, "ecoes_comparison", None, "ecoes_comparison", {}, ) sess.commit() f = StringIO() ecoes_lines = [ "titles", ",".join( ( "2278676232781", "address-line-1", "address-line-2", "address-line-3", "address-line-4", "address-line-5", "address-line-6", "address-line-7", "address-line-8", "address-line-9", "post-code", "supplier", "registration-from", "mtc", "mtc-date", "llfc", "llfc-from", "pc", "", "measurement-class", "energisation-status", "da", "dc", "mop", "mop-appoint-date", "gsp-group", "gsp-effective-from", "dno", "msn", "meter-install-date", "meter-type", "map-id", ), ), ",".join( ( "2278686232789", "address-line-1", "address-line-2", "address-line-3", "address-line-4", "address-line-5", "address-line-6", "address-line-7", "address-line-8", "address-line-9", "post-code", "supplier", "registration-from", "mtc", "mtc-date", "llfc", "llfc-from", "pc", "", "measurement-class", "energisation-status", "da", "dc", "mop", "mop-appoint-date", "gsp-group", "gsp-effective-from", "dno", "msn", "meter-install-date", "meter-type", "map-id", ), ), ] exclude_mpan_cores = [] ignore_mpan_cores_msn = [] show_ignored = True _process( sess, ecoes_lines, exclude_mpan_cores, ignore_mpan_cores_msn, f, show_ignored, report_run, ) expected = [ [ "mpan_core", "mpan_core_no_spaces", "ecoes_pc", "chellow_pc", "ecoes_mtc", "chellow_mtc", "ecoes_llfc", "chellow_llfc", "ecoes_ssc", "chellow_ssc", "ecoes_es", "chellow_es", "ecoes_supplier", "chellow_supplier", "chellow_supplier_contract_name", "ecoes_dc", "chellow_dc", "ecoes_mop", "chellow_mop", "ecoes_gsp_group", "chellow_gsp_group", "ecoes_msn", "chellow_msn", "ecoes_msn_install_date", "ecoes_meter_type", "chellow_meter_type", "ignored", "problem", ], [ "22 7867 6232 781", "2278676232781", "pc", "00", "mtc", "845", "llfc", "510", "", "", "energisation-status", "E", "supplier", "CALB", "Fusion Supplier 2000", "dc", "CALB", "mop", "CALB", "gsp-group", "_L", "msn", "hgjeyhuw", "meter-install-date", "meter-type", "H", "False", "The energisation statuses don't match. Can't parse the PC. Can't parse " "the MTC. The LLFCs don't match. The supplier codes don't match. The DC " "codes don't match. The MOP codes don't match. The GSP group codes don't " "match. The meter serial numbers don't match. The meter types don't match. " "See https://dtc.mrasco.com/DataItem.aspx?ItemCounter=0483 ", ], [ "22 7868 6232 789", "2278686232789", "pc", "00", "mtc", "845", "llfc", "510", "", "", "energisation-status", "E", "supplier", "CALB", "Fusion Supplier 2000", "dc", "CALB", "mop", "CALB", "gsp-group", "_L", "msn", "hgjeyhuw", "meter-install-date", "meter-type", "H", "False", "The energisation statuses don't match. Can't parse the PC. Can't parse " "the MTC. The LLFCs don't match. The supplier codes don't match. The DC " "codes don't match. The MOP codes don't match. The GSP group codes don't " "match. The meter serial numbers don't match. The meter types don't match. " "See https://dtc.mrasco.com/DataItem.aspx?ItemCounter=0483 ", ], ] assert f.getvalue() == "\n".join(",".join(line) for line in expected) + "\n"
def test_bill_after_end_supply(mocker, sess): site = Site.insert(sess, "CI017", "Water Works") start_date = utc_datetime(2009, 7, 31, 23, 00) months = 1 supply_id = None market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_charge_script = """ from chellow.utils import reduce_bill_hhs def virtual_bill_titles(): return ['net-gbp', 'problem'] def virtual_bill(ds): for hh in ds.hh_data: hh_start = hh['start-date'] bill_hh = ds.supplier_bill_hhs[hh_start] bill_hh['net-gbp'] = sum( v for k, v in bill_hh.items() if k.endswith('gbp')) ds.mop_bill = reduce_bill_hhs(ds.supplier_bill_hhs) """ mop_contract = Contract.insert_mop( sess, "Fusion Mop Contract", participant, mop_charge_script, {}, utc_datetime(2000, 1, 1), None, {}, ) dc_charge_script = """ from chellow.utils import reduce_bill_hhs def virtual_bill_titles(): return ['net-gbp', 'problem'] def virtual_bill(ds): for hh in ds.hh_data: hh_start = hh['start-date'] bill_hh = ds.supplier_bill_hhs[hh_start] bill_hh['net-gbp'] = sum( v for k, v in bill_hh.items() if k.endswith('gbp')) ds.dc_bill = reduce_bill_hhs(ds.supplier_bill_hhs) """ dc_contract = Contract.insert_dc( sess, "Fusion DC 2000", participant, dc_charge_script, {}, utc_datetime(2000, 1, 1), None, {}, ) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") insert_comms(sess) comm = Comm.get_by_code(sess, "GSM") supplier_charge_script = """ import chellow.ccl from chellow.utils import HH, reduce_bill_hhs, utc_datetime def virtual_bill_titles(): return [ 'ccl-kwh', 'ccl-rate', 'ccl-gbp', 'net-gbp', 'vat-gbp', 'gross-gbp', 'sum-msp-kwh', 'sum-msp-gbp', 'problem'] def virtual_bill(ds): for hh in ds.hh_data: hh_start = hh['start-date'] bill_hh = ds.supplier_bill_hhs[hh_start] bill_hh['sum-msp-kwh'] = hh['msp-kwh'] bill_hh['sum-msp-gbp'] = hh['msp-kwh'] * 0.1 bill_hh['net-gbp'] = sum( v for k, v in bill_hh.items() if k.endswith('gbp')) bill_hh['vat-gbp'] = 0 bill_hh['gross-gbp'] = bill_hh['net-gbp'] + bill_hh['vat-gbp'] ds.supplier_bill = reduce_bill_hhs(ds.supplier_bill_hhs) """ imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, supplier_charge_script, {}, utc_datetime(2000, 1, 1), None, {}, ) batch = imp_supplier_contract.insert_batch(sess, "a b", "") dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") gsp_group = GspGroup.insert(sess, "_L", "South Western") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), utc_datetime(2000, 1, 31, 23, 30), gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, comm, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) insert_bill_types(sess) bill_type = sess.execute( select(BillType).where(BillType.code == "N")).scalar_one() batch.insert_bill( sess, "dd", "hjk", start_date, utc_datetime(2009, 7, 10), utc_datetime(2009, 7, 10), Decimal("10.00"), Decimal("10.00"), Decimal("10.00"), Decimal("10.00"), bill_type, {}, supply, ) sess.commit() scenario_props = { "scenario_start_year": start_date.year, "scenario_start_month": start_date.month, "scenario_duration": months, "by_hh": False, } base_name = ["monthly_duration"] site_id = site.id user = mocker.Mock() compression = False site_codes = [] now = utc_datetime(2020, 1, 1) mock_file = BytesIO() mock_file.close = mocker.Mock() mocker.patch("chellow.reports.report_247.open", return_value=mock_file) mocker.patch("chellow.reports.report_247.chellow.dloads.make_names", return_value=("a", "b")) mocker.patch("chellow.reports.report_247.os.rename") content( scenario_props, base_name, site_id, supply_id, user, compression, site_codes, now, ) sheet = odio.parse_spreadsheet(mock_file) table = list(sheet.tables[0].rows) expected = [ [ "creation-date", "site-id", "site-name", "associated-site-ids", "month", "metering-type", "sources", "generator-types", "import-net-kwh", "export-net-kwh", "import-gen-kwh", "export-gen-kwh", "import-3rd-party-kwh", "export-3rd-party-kwh", "displaced-kwh", "used-kwh", "used-3rd-party-kwh", "import-net-gbp", "export-net-gbp", "import-gen-gbp", "export-gen-gbp", "import-3rd-party-gbp", "export-3rd-party-gbp", "displaced-gbp", "used-gbp", "used-3rd-party-gbp", "billed-import-net-kwh", "billed-import-net-gbp", "billed-supplier-import-net-gbp", "billed-dc-import-net-gbp", "billed-mop-import-net-gbp", ], [ Datetime(2020, 1, 1, 0, 0), "CI017", "Water Works", "", Datetime(2009, 7, 31, 23, 30), None, "", "", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 10.0, 0.0, 0.0, ], ] assert expected == table
def test_scenario_new_generation(mocker, sess): site = Site.insert(sess, "CI017", "Water Works") start_date = utc_datetime(2009, 7, 31, 23, 00) finish_date = utc_datetime(2009, 8, 31, 22, 30) supply_id = None report_context = {} forecast_from = utc_datetime(2020, 1, 1) market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_dc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") insert_comms(sess) comm = Comm.get_by_code(sess, "GSM") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, comm, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) sess.commit() scenario_hh = { "CI017": { "generated": """ 2009-08-01 00:00, 40 2009-08-15 00:00, 40""" } } era_maps = { utc_datetime(2000, 8, 1): { "llfcs": { "22": { "new_export": "521" } }, "supplier_contracts": { "new_export": 4 }, } } site_deltas = _make_site_deltas(sess, report_context, site, scenario_hh, forecast_from, supply_id) calcs, _, _ = _make_calcs( sess, site, start_date, finish_date, supply_id, site_deltas, forecast_from, report_context, era_maps, ) assert calcs[1][1] == "CI017_extra_gen_TRUE" assert calcs[2][2] == "CI017_extra_net_export"
def _import_Valid_MTC_LLFC_SSC_PC_Combination(sess, csv_reader): dnos = {} mtcs = {} llfcs = {} sscs = {} pcs = dict((v.code, v) for v in sess.execute(select(Pc)).scalars()) combos = dict(((v.mtc.id, v.llfc.id, v.ssc.id, v.pc.id, v.valid_from), v) for v in sess.execute(select(ValidMtcLlfcSscPc)).scalars()) for values in csv_reader: mtc_code = values[0].zfill(3) # Meter Timeswitch Class ID # Effective From Settlement Date (MTC) participant_code = values[2] # Market Participant ID mtc_from_str = values[3] # Effective From Settlement Date (MTCPA) mtc_from = parse_date(mtc_from_str) ssc_code = values[4] # Standard Settlement Configuration ID ssc_from_str = values[5] # Effective From Settlement date (VMTCSC) ssc_from = parse_date(ssc_from_str) llfc_code = values[6] # Line Loss Factor Class ID llfc_from_str = values[7] # Effective From Settlement Date (VMTCLSC) llfc_from = parse_date(llfc_from_str) pc_code = values[8].zfill(2) # Profile Class ID valid_from_str = values[9] # Effective From Settlement Date (VMTCLSPC) valid_from = parse_date(valid_from_str) valid_to_str = values[10] # Effective To Settlement Date (VMTCLSPC) valid_to = parse_date(valid_to_str) # Preserved Tariff Indicator try: dno = dnos[(participant_code, valid_from)] except KeyError: dno = dnos[(participant_code, valid_from)] = sess.execute( select(Party).join(Participant).join(MarketRole).where( Participant.code == participant_code, MarketRole.code == "R", Party.valid_from <= valid_from, or_(Party.valid_to == null(), Party.valid_to >= valid_from), )).scalar_one() mtc_dno_id = dno.id if Mtc.has_dno(mtc_code) else None try: mtc = mtcs[(mtc_dno_id, mtc_code, mtc_from)] except KeyError: mtc = mtcs[(mtc_dno_id, mtc_code, mtc_from)] = Mtc.get_by_code(sess, dno, mtc_code, mtc_from) try: llfc = llfcs[(dno.id, llfc_code, llfc_from)] except KeyError: llfc = llfcs[(dno.id, llfc_code, llfc_from)] = dno.get_llfc_by_code( sess, llfc_code, llfc_from) try: ssc = sscs[(ssc_code, ssc_from)] except KeyError: ssc = sscs[(ssc_code, ssc_from)] = Ssc.get_by_code(sess, ssc_code, ssc_from) pc = pcs[pc_code] combo = combos.get((mtc.id, llfc.id, ssc.id, pc.id, valid_from)) if combo is None: ValidMtcLlfcSscPc.insert( sess, mtc, llfc, ssc, pc, valid_from, valid_to, ) else: combo.valid_to = valid_to sess.flush()
def _import_MTC_in_PES_Area(sess, csv_reader): dnos = dict( (p.participant.code, p) for p in sess.query(Party).join(Participant).join(MarketRole).filter( MarketRole.code == "R").options(joinedload(Party.participant))) mtcs = dict( ((m.dno_id, m.code, m.valid_from), m) for m in sess.query(Mtc).options(joinedload( Mtc.meter_type), joinedload(Mtc.meter_payment_type)).all()) meter_types = dict( (m.code, m) for m in sess.execute(select(MeterType)).scalars()) meter_payment_types = dict( (m.code, m) for m in sess.execute(select(MeterPaymentType)).scalars()) for values in csv_reader: code_str = values[0] if not Mtc.has_dno(code_str): continue code_int = int(code_str) code = code_str.zfill(3) participant_code = values[2] dno = dnos[participant_code] valid_from = parse_date(values[3]) valid_to = parse_to_date(values[4]) description = values[5] meter_type_code = values[6] meter_type = meter_types[meter_type_code] meter_payment_type_code = values[7] meter_payment_type = meter_payment_types[meter_payment_type_code] has_related_metering = code_int > 500 has_comms = values[8] == "Y" is_hh = values[9] == "H" tpr_count_str = values[10] tpr_count = 0 if tpr_count_str == "" else int(tpr_count_str) mtc = mtcs.get((dno.id, code, valid_from)) if mtc is None: Mtc.insert( sess, dno.dno_code, code, description, has_related_metering, has_comms, is_hh, meter_type, meter_payment_type, tpr_count, valid_from, valid_to, ) else: mtc.description = description mtc.has_related_metering = has_related_metering mtc.has_comms = has_comms mtc.is_hh = is_hh mtc.meter_type = meter_type mtc.meter_payment_type = meter_payment_type mtc.tpr_count = tpr_count mtc.valid_to = valid_to sess.flush()
def test_init_hh_data(sess, mocker): """New style channels""" site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_hhdc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "00", "hh", utc_datetime(2000, 1, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") imp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "E") gsp_group = GspGroup.insert(sess, "_L", "South Western") supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, None, energisation_status, {}, "22 7867 6232 781", "510", imp_supplier_contract, "7748", 361, None, None, None, None, None, ) era = supply.eras[0] channel = era.insert_channel(sess, True, "ACTIVE") data_raw = [{ "start_date": utc_datetime(2009, 8, 10), "value": 10, "status": "A", }] channel.add_hh_data(sess, data_raw) sess.commit() caches = {} chunk_start = utc_datetime(2009, 7, 31, 23, 00) chunk_finish = utc_datetime(2009, 8, 31, 22, 30) is_import = True full_channels, hhd = chellow.computer._init_hh_data( sess, caches, era, chunk_start, chunk_finish, is_import) assert full_channels expected_hhd = { utc_datetime(2009, 8, 10): { "imp-msp-kvarh": 0.0, "imp-msp-kvar": 0.0, "exp-msp-kvarh": 0.0, "exp-msp-kvar": 0.0, "status": "A", "hist-kwh": 10.0, "msp-kwh": 10.0, "msp-kw": 20.0, } } assert hhd == expected_hhd
def _parse_MTC_in_PES_Area(sess, csv_reader): rows = [] dnos = dict( (p.participant.code, (p.id, p.dno_code)) for p in sess.query(Party).join(Participant).join(MarketRole).filter( MarketRole.code == "R").options(joinedload(Party.participant))) mtcs = dict( ((m.dno_id, m.code, m.valid_from), m) for m in sess.query(Mtc).options(joinedload( Mtc.meter_type), joinedload(Mtc.meter_payment_type)).all()) for values in csv_reader: code_str = values[0] if not Mtc.has_dno(code_str): continue code_int = int(code_str) code = code_str.zfill(3) participant_code = values[2] dno_id, dno_code = dnos[participant_code] valid_from = parse_date(values[3]) valid_to = parse_to_date(values[4]) description = values[5] meter_type_code = values[6] meter_payment_type_code = values[7] has_related_metering = code_int > 500 has_comms = values[8] == "Y" is_hh = values[9] == "H" tpr_count_str = values[10] tpr_count = 0 if tpr_count_str == "" else int(tpr_count_str) mtc = mtcs.get((dno_id, code, valid_from)) if mtc is None: row = ( "insert", "mtc", dno_code, code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(valid_from), hh_format(valid_to, ongoing_str=""), ) rows.append(row) elif any(( description != mtc.description, has_related_metering != mtc.has_related_metering, has_comms != mtc.has_comms, is_hh != mtc.is_hh, meter_type_code != mtc.meter_type.code, meter_payment_type_code != mtc.meter_payment_type.code, tpr_count != mtc.tpr_count, valid_to != mtc.valid_to, )): row = ( "update", "mtc", mtc.dno.dno_code, mtc.code, description, has_related_metering, has_comms, is_hh, meter_type_code, meter_payment_type_code, tpr_count, hh_format(mtc.valid_from), hh_format(valid_to, ongoing_str=""), ) rows.append(row) return rows
def test_SupplySource_init_nhh(sess, mocker): """Old style channels""" site = Site.insert(sess, "CI017", "Water Works") market_role_Z = MarketRole.insert(sess, "Z", "Non-core") participant = Participant.insert(sess, "CALB", "AK Industries") participant.insert_party(sess, market_role_Z, "None core", utc_datetime(2000, 1, 1), None, None) bank_holiday_rate_script = {"bank_holidays": []} Contract.insert_non_core( sess, "bank_holidays", "", {}, utc_datetime(2000, 1, 1), None, bank_holiday_rate_script, ) market_role_X = MarketRole.insert(sess, "X", "Supplier") market_role_M = MarketRole.insert(sess, "M", "Mop") market_role_C = MarketRole.insert(sess, "C", "HH Dc") market_role_R = MarketRole.insert(sess, "R", "Distributor") participant.insert_party(sess, market_role_M, "Fusion Mop Ltd", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_X, "Fusion Ltc", utc_datetime(2000, 1, 1), None, None) participant.insert_party(sess, market_role_C, "Fusion DC", utc_datetime(2000, 1, 1), None, None) mop_contract = Contract.insert_mop(sess, "Fusion", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) dc_contract = Contract.insert_hhdc(sess, "Fusion DC 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}) pc = Pc.insert(sess, "03", "nhh", utc_datetime(2000, 1, 1), None) ssc = Ssc.insert(sess, "0393", "unrestricted", True, utc_datetime(2000, 1), None) insert_cops(sess) cop = Cop.get_by_code(sess, "5") exp_supplier_contract = Contract.insert_supplier( sess, "Fusion Supplier 2000", participant, "", {}, utc_datetime(2000, 1, 1), None, {}, ) dno = participant.insert_party(sess, market_role_R, "WPD", utc_datetime(2000, 1, 1), None, "22") meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) Mtc.insert( sess, None, "845", "HH COP5 And Above With Comms", False, False, True, meter_type, meter_payment_type, 0, utc_datetime(1996, 1, 1), None, ) insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") dno.insert_llfc( sess, "510", "PC 5-8 & HH HV", voltage_level, False, True, utc_datetime(1996, 1, 1), None, ) dno.insert_llfc( sess, "521", "Export (HV)", voltage_level, False, False, utc_datetime(1996, 1, 1), None, ) insert_sources(sess) source = Source.get_by_code(sess, "net") insert_energisation_statuses(sess) energisation_status = EnergisationStatus.get_by_code(sess, "D") gsp_group = GspGroup.insert(sess, "_L", "South Western") supply = site.insert_e_supply( sess, source, None, "Bob", utc_datetime(2000, 1, 1), None, gsp_group, mop_contract, "773", dc_contract, "ghyy3", "hgjeyhuw", pc, "845", cop, ssc, energisation_status, {}, None, None, None, None, None, "22 7867 6232 781", "521", exp_supplier_contract, "7748", 361, ) era = supply.eras[0] active_channel = era.insert_channel(sess, False, "ACTIVE") active_data_raw = [{ "start_date": utc_datetime(2009, 8, 10), "value": 10, "status": "A", }] active_channel.add_hh_data(sess, active_data_raw) era.insert_channel(sess, True, "REACTIVE_IMP") sess.commit() caches = {} start_date = utc_datetime(2009, 7, 31, 23, 00) finish_date = utc_datetime(2009, 8, 31, 22, 30) forecast_date = utc_datetime(2019, 8, 31, 22, 30) is_import = False chellow.computer.SupplySource(sess, start_date, finish_date, forecast_date, era, is_import, caches)
def test_parse_Valid_MTC_LLFC_SSC_PC_Combination(sess): participant_code = "EDFI" participant = Participant.insert(sess, participant_code, "AK Industries") market_role_R = MarketRole.insert(sess, "R", "Distributor") dno = participant.insert_party(sess, market_role_R, "WPD", to_utc(ct_datetime(2000, 1, 1)), None, "22") insert_voltage_levels(sess) voltage_level = VoltageLevel.get_by_code(sess, "HV") llfc_code = "906" llfc_description = "PC 5-8 & HH HV" llfc = dno.insert_llfc( sess, llfc_code, llfc_description, voltage_level, False, True, to_utc(ct_datetime(2009, 4, 16)), None, ) meter_type = MeterType.insert(sess, "C5", "COP 1-5", utc_datetime(2000, 1, 1), None) meter_payment_type = MeterPaymentType.insert(sess, "CR", "Credit", utc_datetime(1996, 1, 1), None) mtc = Mtc.insert( sess, dno, "001", "an mtc", False, False, True, meter_type, meter_payment_type, 1, to_utc(ct_datetime(2009, 4, 16)), None, ) ssc = Ssc.insert(sess, "0349", "an ssc", True, to_utc(ct_datetime(2009, 4, 16)), None) pc = Pc.insert(sess, "02", "dom", to_utc(ct_datetime(1996, 1, 1)), None) sess.commit() row = [ "1", "01/04/1996", participant_code, "16/04/2009", "0349", "16/04/2009", "906", "16/04/2009", "2", "17/03/2010", "20/08/2014", "F", ] csv_reader = iter([row]) chellow.mdd_importer._import_Valid_MTC_LLFC_SSC_PC_Combination( sess, csv_reader) sess.commit() ValidMtcLlfcSscPc.get_by_values(sess, mtc, llfc, ssc, pc, utc_datetime(2012, 1, 1))