Skip to content

Commit c693ca6

Browse files
refactor: check hocId and tocId in test and improve loop
1 parent c08d675 commit c693ca6

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

gen/src/lib.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,8 @@ pub struct Tad {
244244
pub packaging_or_tr_eq_type: Option<PackagingOrTrEqType>,
245245
#[serde(skip_serializing_if = "Option::is_none")]
246246
pub packaging_or_tr_eq_amount: Option<usize>,
247-
// TODO: verify whether the absence of this property is intended.
248-
// #[serde(skip_serializing_if = "Option::is_none")]
249-
// pub energy_carrier: EnergyCarrier,
247+
// TODO: verify whether the absence of this property is intended. #[serde(skip_serializing_if =
248+
// "Option::is_none")] pub energy_carrier: EnergyCarrier,
250249
#[serde(skip_serializing_if = "Option::is_none")]
251250
pub feedstocks: Option<Vec<Feedstock>>,
252251
}
@@ -656,50 +655,48 @@ pub fn to_pcf(
656655
}
657656
}
658657

659-
pub fn gen_rnd_demo_data(size: usize) -> Vec<ProductFootprint> {
660-
let mut og = Gen::new(size);
658+
// TODO: invert logic to generate a list of HOCs and TOCs and only then generate TCEs, improving
659+
// readability and demo data quality, as suggested by Martin.
660+
pub fn gen_rnd_demo_data(size: u8) -> Vec<ProductFootprint> {
661+
let mut og = Gen::new(size as usize);
661662

662663
let mut shipment_footprints = vec![];
663664
let mut tocs = vec![];
664665
let mut hocs = vec![];
665666

666-
let num_of_shipments = u8::arbitrary(&mut og) % 5 + 1;
667+
let num_of_shipments = u8::arbitrary(&mut og) % size + 1;
667668
for _ in 0..num_of_shipments {
668669
let mut ship_foot = ShipmentFootprint::arbitrary(&mut og);
669670

670671
let mut tces: Vec<Tce> = vec![];
671672
let mut prev_tces: Vec<String> = vec![];
672673

673674
let mut i = 0;
674-
let limit = u8::arbitrary(&mut og) % 5 + 1;
675+
let limit = u8::arbitrary(&mut og) % size + 1;
675676
// TODO: improve code through pair programming with Martin.
676677
loop {
677678
let mut tce = Tce::arbitrary(&mut og);
678679

679-
if (tce.toc_id.is_none() && tce.hoc_id.is_none())
680-
|| tce.toc_id.is_some() && tce.hoc_id.is_some()
681-
{
682-
panic!("Either Toc or Hoc, but not both, must be provided");
683-
}
684-
685680
if let Some(prev_tce) = tces.last() {
686681
// Updates prevTceIds for the current TCE
687682
prev_tces.push(prev_tce.tce_id.clone());
688683
tce.prev_tce_ids = Some(prev_tces.clone());
689684

690685
// Avoids having two HOCs follow one another
691686
if prev_tce.hoc_id.is_some() && tce.hoc_id.is_some() {
692-
continue;
687+
tce = Tce::arbitrary(&mut og);
693688
}
694689
};
695690

691+
if i == 0 || i == limit - 1 && tce.hoc_id.is_some() {
692+
tce = Tce::arbitrary(&mut og);
693+
}
694+
696695
if tce.hoc_id.is_some() {
697696
// Avoids having an HOC as the first or the last TCE
698-
if i == 0 || i == limit - 1 {
699-
continue;
700-
}
701697

702698
let mut hoc = Hoc::arbitrary(&mut og);
699+
703700
hoc.hoc_id = tce.hoc_id.clone().unwrap();
704701

705702
tce.hoc_id = Some(hoc.hoc_id.clone());
@@ -732,6 +729,7 @@ pub fn gen_rnd_demo_data(size: usize) -> Vec<ProductFootprint> {
732729
.into();
733730

734731
tce.toc_id = Some(toc.toc_id.clone());
732+
735733
tce.co2e_wtw = WrappedDecimal::from(
736734
(toc.co2e_intensity_wtw.0 * tce.transport_activity.0).round_dp(2),
737735
);
@@ -784,6 +782,24 @@ mod tests {
784782
fn test_gen_rnd_demo_data() {
785783
let footprints = gen_rnd_demo_data(10);
786784

785+
for footprint in footprints.iter() {
786+
if let Some(extensions) = &footprint.extensions {
787+
for extension in extensions.iter() {
788+
if let Some(ship_foot) = extension.data.get("ShipmentFootprint") {
789+
let ship_foot =
790+
serde_json::from_value::<ShipmentFootprint>(ship_foot.to_owned())
791+
.unwrap();
792+
for tce in ship_foot.tces.0.iter() {
793+
assert!(
794+
tce.toc_id.is_some() ^ tce.hoc_id.is_some(),
795+
"Either tocId or hocId, but not both, must be provided."
796+
);
797+
}
798+
}
799+
}
800+
}
801+
}
802+
787803
println!("{footprints:#?}");
788804
}
789805
}

0 commit comments

Comments
 (0)