Skip to content

Commit

Permalink
Merge pull request #28 from wbcsd/mock-data
Browse files Browse the repository at this point in the history
feat: replace demo data with new methodology informed values
  • Loading branch information
zeitgeist authored Apr 16, 2024
2 parents 799e6d4 + d401455 commit 9bce8b2
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 169 deletions.
54 changes: 47 additions & 7 deletions endpoint/src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct CarbonFootprint {
pub i_luc_ghg_emissions: Option<PositiveDecimal>,

#[serde(skip_serializing_if = "Option::is_none")]
pub biogenic_carbon_withdrawal: Option<PositiveDecimal>,
pub biogenic_carbon_withdrawal: Option<NegativeDecimal>,

#[serde(skip_serializing_if = "Option::is_none")]
pub aircraft_ghg_emissions: Option<PositiveDecimal>,
Expand All @@ -78,14 +78,11 @@ pub struct CarbonFootprint {
pub ipcc_characterization_factors_sources: IpccCharacterizationFactorsSources,

pub cross_sectoral_standards_used: CrossSectoralStandardSet,
pub product_or_sector_specific_rules: ProductOrSectorSpecificRuleSet,
pub product_or_sector_specific_rules: Option<ProductOrSectorSpecificRuleSet>,

#[serde(skip_serializing_if = "Option::is_none")]
pub biogenic_accounting_methodology: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub boundary_processes_description: Option<String>,

pub biogenic_accounting_methodology: Option<BiogenicAccountingMethodology>,
pub boundary_processes_description: String,
pub reference_period_start: DateTime<Utc>,
pub reference_period_end: DateTime<Utc>,

Expand Down Expand Up @@ -187,10 +184,27 @@ pub struct IpccCharacterizationFactorsSource(String);
#[serde(crate = "rocket::serde")]
pub struct IpccCharacterizationFactorsSources(pub Vec<IpccCharacterizationFactorsSource>);

#[derive(Debug, Serialize, JsonSchema, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde")]
pub enum BiogenicAccountingMethodology {
#[serde(rename = "PEF")]
Pef,
#[serde(rename = "ISO")]
Iso,
#[serde(rename = "GHPG")]
Ghpg,
#[serde(rename = "Quantis")]
Quantis,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
pub struct PositiveDecimal(Decimal);

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
pub struct NegativeDecimal(Decimal);

/// a f64 in the 0..5 range
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
Expand Down Expand Up @@ -451,6 +465,12 @@ impl From<Decimal> for PositiveDecimal {
}
}

impl From<Decimal> for NegativeDecimal {
fn from(f: Decimal) -> NegativeDecimal {
NegativeDecimal(f)
}
}

impl From<Decimal> for WrappedDecimal {
fn from(f: Decimal) -> WrappedDecimal {
WrappedDecimal(f)
Expand Down Expand Up @@ -626,6 +646,26 @@ impl JsonSchema for PositiveDecimal {
}
}

impl JsonSchema for NegativeDecimal {
fn schema_name() -> String {
"NegativeDecimal".into()
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let mut s = match String::json_schema(gen) {
Schema::Object(s) => s,
Schema::Bool(_) => panic!("Unexpected base schema"),
};

s.string = Some(Box::new(StringValidation {
pattern: Some(String::from("^(-\\d+(\\.\\d+)?)|0$")),
..Default::default()
}));

Schema::Object(s)
}
}

impl JsonSchema for WrappedDecimal {
fn schema_name() -> String {
"Decimal".into()
Expand Down
33 changes: 6 additions & 27 deletions endpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ fn get_list_with_filter_eq_test() {

assert_eq!(rocket::http::Status::Ok, resp.status());
let json: PfListingResponseInner = resp.into_json().unwrap();
assert_eq!(json.data.len(), 5);
assert_eq!(json.data.len(), 1);
}

#[test]
Expand All @@ -677,7 +677,7 @@ fn get_list_with_filter_lt_test() {

let bearer_token = format!("Bearer {jwt}");

let get_list_with_limit_uri = "/2/footprints?$filter=updated+lt+'2023-01-01T00:00:00.000Z'";
let get_list_with_limit_uri = "/2/footprints?$filter=updated+lt+'2023-06-27T13:00:00.000Z'";

let resp = client
.get(get_list_with_limit_uri)
Expand All @@ -687,7 +687,7 @@ fn get_list_with_filter_lt_test() {

assert_eq!(rocket::http::Status::Ok, resp.status());
let json: PfListingResponseInner = resp.into_json().unwrap();
assert_eq!(json.data.len(), 3);
assert_eq!(json.data.len(), 1);
}

#[test]
Expand All @@ -703,7 +703,7 @@ fn get_list_with_filter_eq_and_lt_test() {
let jwt = auth::encode_token(&token, key_pair).ok().unwrap();
let bearer_token = format!("Bearer {jwt}");

let get_list_with_limit_uri = "/2/footprints?$filter=(pcf/geographyCountry+eq+'FR')+and+(updated+lt+'2023-01-01T00:00:00.000Z')";
let get_list_with_limit_uri = "/2/footprints?$filter=(pcf/geographyCountry+eq+'FR')+and+(updated+lt+'2023-06-27T12:12:04.000Z')";

let resp = client
.get(get_list_with_limit_uri)
Expand Down Expand Up @@ -743,7 +743,7 @@ fn get_list_with_filter_any_test() {

assert_eq!(rocket::http::Status::Ok, resp.status());
let json: PfListingResponseInner = resp.into_json().unwrap();
assert_eq!(json.data.len(), 8);
assert_eq!(json.data.len(), 1);

let get_list_with_limit_uri =
"/2/footprints?$filter=productIds/any(productId:(productId+eq+'urn:gtin:12345'))";
Expand Down Expand Up @@ -774,7 +774,6 @@ fn get_list_with_limit_test() {

let get_list_with_limit_uri = "/2/footprints?limit=3";
let expected_next_link1 = "/2/footprints?offset=3&limit=3";
let expected_next_link2 = "/2/footprints?offset=6&limit=3";

{
let resp = client
Expand All @@ -799,34 +798,14 @@ fn get_list_with_limit_test() {
{
let resp = client
.get(expected_next_link1)
.header(rocket::http::Header::new(
"Authorization",
bearer_token.clone(),
))
.header(rocket::http::Header::new("Host", EXAMPLE_HOST))
.dispatch();

assert_eq!(rocket::http::Status::Ok, resp.status());
let link_header = resp.headers().get("link").next().unwrap().to_string();
assert_eq!(
link_header,
format!("<https://api.pathfinder.sine.dev{expected_next_link2}>; rel=\"next\"")
);
let json: PfListingResponseInner = resp.into_json().unwrap();
assert_eq!(json.data.len(), 3);
}

{
let resp = client
.get(expected_next_link2)
.header(rocket::http::Header::new("Authorization", bearer_token))
.header(rocket::http::Header::new("Host", EXAMPLE_HOST))
.dispatch();

assert_eq!(rocket::http::Status::Ok, resp.status());
assert_eq!(resp.headers().get("link").next(), None);
let json: PfListingResponseInner = resp.into_json().unwrap();
assert_eq!(json.data.len(), 2);
assert_eq!(json.data.len(), 1);
}
}

Expand Down
Loading

0 comments on commit 9bce8b2

Please sign in to comment.