Skip to content

Commit

Permalink
docs: Test Parameter JSON examples. (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetuk authored Apr 16, 2024
1 parent b3d743e commit f228359
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 38 deletions.
39 changes: 4 additions & 35 deletions pywr-schema/src/parameters/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,7 @@ impl TryFromV1Parameter<MaxParameterV1> for MaxParameter {
/// # Examples
///
/// ```json
/// {
/// "type": "Division",
/// "numerator": {
/// "type": "MonthlyProfile",
/// "values": [1, 4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12]
/// },
/// "denominator": {
/// "type": "Constant",
/// "value": 0.3
/// }
/// }
#[doc = include_str!("doc_examples/division.json")]
/// ```
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct DivisionParameter {
Expand Down Expand Up @@ -330,14 +320,7 @@ impl TryFromV1Parameter<DivisionParameterV1> for DivisionParameter {
/// # Examples
///
/// ```json
/// {
/// "type": "Min",
/// "parameter": {
/// "type": "MonthlyProfile",
/// "values": [1, 4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12]
/// },
/// "threshold": 2
/// }
#[doc = include_str!("doc_examples/min.json")]
/// ```
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct MinParameter {
Expand Down Expand Up @@ -442,14 +425,7 @@ impl TryFromV1Parameter<NegativeParameterV1> for NegativeParameter {
/// # Examples
///
/// ```json
/// {
/// "type": "NegativeMax",
/// "metric": {
/// "type": "MonthlyProfile",
/// "values": [-1, -4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12]
/// },
/// "threshold": 2
/// }
#[doc = include_str!("doc_examples/negative_max.json")]
/// ```
/// In January this parameter returns 2, in February 4.
///
Expand Down Expand Up @@ -508,14 +484,7 @@ impl TryFromV1Parameter<NegativeMaxParameterV1> for NegativeMaxParameter {
/// # Examples
///
/// ```json
/// {
/// "type": "NegativeMin",
/// "metric": {
/// "type": "MonthlyProfile",
/// "values": [-1, -4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12]
/// },
/// "threshold": 2
/// }
#[doc = include_str!("doc_examples/negative_min.json")]
/// ```
/// In January this parameter returns 1, in February 2.
///
Expand Down
29 changes: 29 additions & 0 deletions pywr-schema/src/parameters/doc_examples/division.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "my-division",
"type": "Division",
"numerator": {
"type": "InlineParameter",
"definition": {
"name": "my-monthly-profile",
"type": "MonthlyProfile",
"values": [
1,
4,
5,
9,
1,
5,
10,
8,
11,
9,
11,
12
]
}
},
"denominator": {
"type": "Constant",
"value": 0.3
}
}
26 changes: 26 additions & 0 deletions pywr-schema/src/parameters/doc_examples/min.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "my-min",
"type": "Min",
"parameter": {
"type": "InlineParameter",
"definition": {
"name": "my-monthly-profile",
"type": "MonthlyProfile",
"values": [
1,
4,
5,
9,
1,
5,
10,
8,
11,
9,
11,
12
]
}
},
"threshold": 2
}
26 changes: 26 additions & 0 deletions pywr-schema/src/parameters/doc_examples/negative_max.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "my-negative-max",
"type": "NegativeMax",
"metric": {
"type": "InlineParameter",
"definition": {
"name": "my-monthly-profile",
"type": "MonthlyProfile",
"values": [
-1,
-4,
5,
9,
1,
5,
10,
8,
11,
9,
11,
12
]
}
},
"threshold": 2
}
26 changes: 26 additions & 0 deletions pywr-schema/src/parameters/doc_examples/negative_min.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "my-negative-min",
"type": "NegativeMin",
"metric": {
"type": "InlineParameter",
"definition": {
"name": "my-monthly-profile",
"type": "MonthlyProfile",
"values": [
-1,
-4,
5,
9,
1,
5,
10,
8,
11,
9,
11,
12
]
}
},
"threshold": 2
}
7 changes: 4 additions & 3 deletions pywr-schema/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ mod tests {
use std::fs;
use std::path::PathBuf;

/// Test all of the documentation examples successfully deserialize.
/// Test all the documentation examples successfully deserialize.
#[test]
fn test_doc_examples() {
let mut doc_examples = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand All @@ -753,8 +753,9 @@ mod tests {
for entry in fs::read_dir(doc_examples).unwrap() {
let p = entry.unwrap().path();
if p.is_file() {
let data = fs::read_to_string(p).unwrap();
let _: Parameter = serde_json::from_str(&data).unwrap();
let data = fs::read_to_string(&p).unwrap_or_else(|_| panic!("Failed to read file: {:?}", p));
let _: Parameter =
serde_json::from_str(&data).unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p));
}
}
}
Expand Down

0 comments on commit f228359

Please sign in to comment.