Skip to content

Commit 230f394

Browse files
added potential fix to api error issue within the test cases
1 parent 6b940d8 commit 230f394

2 files changed

Lines changed: 59 additions & 17 deletions

File tree

oscps-lib/src/component.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ extern crate uom;
66

77
extern crate pubchem;
88
use anyhow::Result;
9+
use std::{thread,time::Duration};
10+
11+
// pub enum RequestStatusCodes {
12+
// BadRequest,
13+
// NotFound,
14+
// GatewayError,
15+
// Success
16+
17+
// }
18+
19+
// impl RequestStatusCodes {
20+
// pub fn value(&self) -> u32 {
21+
// match self {
22+
// RequestStatusCodes::Success => 200,
23+
// RequestStatusCodes::NotFound => 404,
24+
// RequestStatusCodes::GatewayError => 504,
25+
// RequestStatusCodes::BadRequest => 400
26+
// }
27+
// }
28+
// }
929

1030
#[allow(dead_code)]
1131
/// This will hold the list of chemicals used within the simulation
@@ -43,9 +63,23 @@ impl Chemical {
4363
ChemicalIdentifier::PubchemID(id) => pubchem::Compound::new(id),
4464
ChemicalIdentifier::CompoundName(name) => pubchem::Compound::with_name(name.as_str()),
4565
};
46-
47-
let cid_vec = pubchem_chemical_object.cids().unwrap();
48-
let cid: i32 = cid_vec[0];
66+
let mut request_counter = 0;
67+
let mut cid_vec = None;
68+
while request_counter <= 10 {
69+
match pubchem_chemical_object.cids(){
70+
Ok(cid_list) => {
71+
cid_vec = Some(cid_list);
72+
break;
73+
},
74+
_ => {
75+
request_counter = request_counter + 1;
76+
thread::sleep(Duration::from_secs(10));
77+
}
78+
};
79+
}
80+
81+
// let cid_vec = pubchem_chemical_object.cids().unwrap();
82+
let cid: i32 = cid_vec.unwrap()[0];
4983
let prop = ChemicalProperties::new(cid).unwrap();
5084
return Ok(Chemical {
5185
pubchem_obj: pubchem_chemical_object,
@@ -93,14 +127,16 @@ impl ChemicalProperties {
93127
#[cfg(test)]
94128
mod chemical_species_tests {
95129
use crate::component::{Chemical, ChemicalIdentifier};
130+
use std::{thread,time::Duration};
96131

97132
#[test]
98133
fn test_create_chemical_from_pubchem_id() {
99134
// Using a known PubChem ID, e.g., 7732 (water)
100135
let identifier = ChemicalIdentifier::PubchemID(7732);
101136

102137
let chemical = Chemical::new(identifier);
103-
138+
thread::sleep(Duration::from_secs(10));
139+
104140
assert!(
105141
chemical.is_ok(),
106142
"Failed to create chemical from PubChem ID"
@@ -119,6 +155,8 @@ mod chemical_species_tests {
119155
let identifier = ChemicalIdentifier::CompoundName(String::from("Water"));
120156

121157
let chemical = Chemical::new(identifier);
158+
thread::sleep(Duration::from_secs(10));
159+
122160

123161
assert!(chemical.is_ok(), "Failed to create chemical from name");
124162
let chemical = chemical.unwrap();

oscps-lib/src/thermodynamics.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ mod thermo_tests {
132132
use uom::si::mass::kilogram;
133133
use uom::si::pressure::pascal;
134134
use uom::si::thermodynamic_temperature::kelvin;
135+
use std::{thread,time::Duration};
135136

136137
#[test]
137138
///Test case generates an instance of the 'ThermoState' struct
@@ -146,6 +147,7 @@ mod thermo_tests {
146147
acentric_factor: 0.344, // example
147148
},
148149
};
150+
thread::sleep(Duration::from_secs(10));
149151
let water_mass = Mass::new::<kilogram>(2.0);
150152
let water_species_pair = SpeciesListPair {
151153
chemical_species: water,
@@ -167,14 +169,14 @@ mod thermo_tests {
167169

168170

169171
// Check that the mole fraction's chemical is correctly set
170-
// assert_eq!(
171-
// thermo_state.mass_list[0]
172-
// .chemical_species
173-
// .get_pubchem_obj()
174-
// .cids()
175-
// .unwrap()[0],
176-
// 962
177-
// );
172+
assert_eq!(
173+
thermo_state.mass_list[0]
174+
.chemical_species
175+
.get_pubchem_obj()
176+
.cids()
177+
.unwrap()[0],
178+
962
179+
);
178180
}
179181

180182
#[test]
@@ -189,6 +191,7 @@ mod thermo_tests {
189191
acentric_factor: 0.344, // example
190192
},
191193
};
194+
thread::sleep(Duration::from_secs(10));
192195

193196
let anisdine = Chemical {
194197
pubchem_obj: pubchem::Compound::new(7732),
@@ -199,7 +202,8 @@ mod thermo_tests {
199202
acentric_factor: 0.24, // (approximated)
200203
},
201204
};
202-
205+
thread::sleep(Duration::from_secs(10));
206+
203207
let water_mass = Mass::new::<kilogram>(2.0);
204208
let water_species_pair = SpeciesListPair {
205209
chemical_species: water,
@@ -222,9 +226,9 @@ mod thermo_tests {
222226
.mass_frac(&therm_obj.mass_list[0].chemical_species)
223227
.unwrap();
224228

225-
// assert!(
226-
// (mass_fraction - 0.2).abs() < 1e-6,
227-
// "Mole fraction calculation failed"
228-
// ); // Should be 0.2
229+
assert!(
230+
(mass_fraction - 0.2).abs() < 1e-6,
231+
"Mole fraction calculation failed"
232+
); // Should be 0.2
229233
}
230234
}

0 commit comments

Comments
 (0)