Create tests for first lib-component: overview; Fixes #1
All checks were successful
CI/CD Pipeline / test (push) Successful in 40s

This commit is contained in:
2024-02-05 11:36:06 +01:00
parent ac8c7cd29a
commit 0763a0fb42
35 changed files with 2365 additions and 0 deletions

View File

@ -81,6 +81,7 @@ fn parse_from_str(content: &str, skip_first: bool) -> Result<(bool, Vec<String>)
Ok((true, ret))
}
#[cfg(not(test))]
/// Fetches the json content of the given overview (`law_id`) from the RIS API.
///
/// # Errors
@ -98,3 +99,56 @@ fn fetch_page(overview_id: usize, page: usize) -> Result<String, Error> {
.into_string()?,
)
}
#[cfg(test)]
fn fetch_page(overview_id: usize, page: usize) -> Result<String, Error> {
use std::fs;
let expected_filename = format!("./data/cache/law-{overview_id}-{page}");
match fs::read_to_string(&expected_filename) {
Ok(data) => Ok(data),
Err(_) => {
info!("Not finding law_id {overview_id} (page {page}) in the cache, downloading...");
let data = ureq::post("https://data.bka.gv.at/ris/api/v2.6/Bundesrecht")
.send_form(&[
("Applikation", "BrKons"),
("Gesetzesnummer", &format!("{overview_id}")),
("DokumenteProSeite", "OneHundred"),
("Seitennummer", &format!("{page}")),
("Fassung.FassungVom", &current_date()),
])?
.into_string()?;
fs::write(expected_filename, &data).expect("Unable to write file");
Ok(data)
}
}
}
#[cfg(test)]
mod tests {
use crate::risparser::overview::parse;
use pretty_assertions::assert_eq;
use std::fs;
#[test]
fn overview() {
let paths = fs::read_dir("./data/expected/overview/")
.expect("No folder with expected overview files");
for path in paths {
let path = path.unwrap();
let file_path = format!("{}", path.path().display());
let law_id = format!("{}", path.file_name().into_string().unwrap());
let law_id: usize = law_id
.parse()
.expect("Filename needs to be usize (= law_id in RIS)");
let expected = fs::read_to_string(file_path).expect("Could not read file {file_path}.");
let expected = expected.trim().split('\n').collect::<Vec<&str>>();
let actual = parse(law_id).unwrap();
assert_eq!(actual, expected);
}
}
}