-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
68 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ serde.workspace = true | |
|
||
[dev-dependencies] | ||
serde_json.workspace = true | ||
test-each = { version = "0.2.1" } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
mod common; | ||
use chrono_tz::Tz; | ||
use ocpi_tariffs::{ | ||
ocpi::{cdr::Cdr, tariff::OcpiTariff}, | ||
pricer::Pricer, | ||
}; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn test_json_files() { | ||
let mut should_panic = false; | ||
#[test_each::file(glob = "ocpi-tariffs/test_data/*/cdr*.json", name(segments = 2))] | ||
fn test_json(cdr: &str, path: PathBuf) { | ||
let tariff = std::fs::read_to_string(path.parent().unwrap().join("tariff.json")).unwrap(); | ||
|
||
for json_test in common::collect_json_tests().unwrap() { | ||
let tariff = json_test.tariff; | ||
let cdr = serde_json::from_str(cdr).unwrap(); | ||
let tariff = serde_json::from_str(&tariff).unwrap(); | ||
|
||
eprintln!("\ntesting directory {:?}", json_test.path); | ||
validate_cdr(cdr, tariff).unwrap(); | ||
} | ||
|
||
pub fn validate_cdr(cdr: Cdr, tariff: OcpiTariff) -> Result<(), ocpi_tariffs::Error> { | ||
let pricer = Pricer::with_tariffs(&cdr, &[tariff], Tz::UTC); | ||
let report = pricer.build_report()?; | ||
|
||
assert_eq!( | ||
cdr.total_cost, | ||
report.total_cost.unwrap_or_default().with_scale(), | ||
"total_cost" | ||
); | ||
|
||
assert_eq!( | ||
cdr.total_energy, | ||
report.total_energy.with_scale(), | ||
"total_energy" | ||
); | ||
assert_eq!( | ||
cdr.total_energy_cost.unwrap_or_default(), | ||
report.total_energy_cost.unwrap_or_default().with_scale(), | ||
"total_energy_cost" | ||
); | ||
|
||
assert_eq!(cdr.total_time, report.total_time, "total_time"); | ||
|
||
assert_eq!( | ||
cdr.total_time_cost.unwrap_or_default(), | ||
report.total_time_cost.unwrap_or_default().with_scale(), | ||
"total_time_cost" | ||
); | ||
|
||
assert_eq!( | ||
cdr.total_parking_time.unwrap_or_default(), | ||
report.total_parking_time, | ||
"total_parking_time" | ||
); | ||
|
||
for (name, cdr) in json_test.cdrs { | ||
eprint!(" testing json cdr `{}`: ", name); | ||
assert_eq!( | ||
cdr.total_parking_cost.unwrap_or_default(), | ||
report.total_parking_cost.unwrap_or_default().with_scale(), | ||
"total_parking_cost" | ||
); | ||
|
||
let result = std::panic::catch_unwind(|| { | ||
common::validate_cdr(cdr, tariff.clone()).unwrap(); | ||
}); | ||
assert_eq!( | ||
cdr.total_reservation_cost.unwrap_or_default(), | ||
report | ||
.total_reservation_cost | ||
.unwrap_or_default() | ||
.with_scale(), | ||
"total_reservation_cost" | ||
); | ||
|
||
if result.is_err() { | ||
should_panic = true; | ||
} else { | ||
eprintln!("success"); | ||
} | ||
} | ||
} | ||
assert_eq!( | ||
cdr.total_fixed_cost.unwrap_or_default(), | ||
report.total_fixed_cost.unwrap_or_default().with_scale(), | ||
"total_fixed_cost" | ||
); | ||
|
||
if should_panic { | ||
panic!("not all json tests succeeded") | ||
} | ||
Ok(()) | ||
} |