Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-de committed Apr 30, 2021
1 parent ca88d6f commit 7154fa7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{
use chrono::{DateTime, FixedOffset};
use chrono_tz::Tz;
use derivative::Derivative;
use failure::ResultExt;
use log::info;
use serde::{Deserialize, Serialize};
use std::path;
Expand Down Expand Up @@ -222,9 +223,10 @@ pub fn read<P: AsRef<path::Path>>(path: P) -> Result<Model> {
let p = path.as_ref();
if p.is_file() {
// if it's a file, we consider it to be a zip (and an error will be returned if it is not)
read_from_zip(path)
Ok(read_from_zip(p).with_context(|_| format!("impossible to read ziped ntfs {:?}", p))?)
} else if p.is_dir() {
read_from_path(path)
Ok(read_from_path(p)
.with_context(|_| format!("impossible to read ntfs directory from {:?}", p))?)
} else {
Err(failure::format_err!(
"file {:?} is neither a file nor a directory, cannot read a ntfs from it",
Expand Down
Binary file added tests/fixtures/ziped_ntfs/minimal_ntfs.zip
Binary file not shown.
49 changes: 41 additions & 8 deletions tests/read_ntfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ where
ids
}

#[test]
fn minimal() {
let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap();

fn test_minimal_ntfs(ntm: &Model) {
assert_eq!(8, ntm.stop_areas.len());
assert_eq!(12, ntm.stop_points.len());
assert_eq!(3, ntm.commercial_modes.len());
Expand Down Expand Up @@ -88,6 +85,46 @@ fn minimal() {
);
}

#[test]
fn minimal() {
let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap();
test_minimal_ntfs(&ntm);
}

#[test]
fn ziped_minimal() {
let ntm = transit_model::ntfs::read("tests/fixtures/ziped_ntfs/minimal_ntfs.zip").unwrap();
test_minimal_ntfs(&ntm);
}

#[test]
#[should_panic(
expected = "ErrorMessage { msg: \"file \\\"tests/fixtures/i_m_not_here\\\" is neither a file nor a directory, cannot read a ntfs from it\" }"
)]
fn unexistent_file() {
// reading a file that does not exists will lead to an error
let _ = transit_model::ntfs::read("tests/fixtures/i_m_not_here").unwrap();
}

#[test]
#[should_panic(
expected = "InvalidArchive(\"Could not find central directory end\")\n\nimpossible to read ziped ntfs \"tests/fixtures/ntfs/stops.txt\""
)]
fn file_not_a_ntfs() {
// reading a file that is not either a directory with the ntfs files nor a zip archive will lead to an error
// here we read the stops.txt
let _ = transit_model::ntfs::read("tests/fixtures/ntfs/stops.txt").unwrap();
}

#[test]
#[should_panic(
expected = "ErrorMessage { msg: \"file \\\"tests/fixtures/netex_france/contributors.txt\\\" not found\" }\n\nimpossible to read ntfs directory from \"tests/fixtures/netex_france\""
)]
fn directory_not_a_ntfs() {
// reading a directory that does not contain the ntfs files will lead to an error
let _ = transit_model::ntfs::read("tests/fixtures/netex_france").unwrap();
}

#[test]
fn ntfs_stop_zones() {
let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap();
Expand Down Expand Up @@ -139,10 +176,6 @@ fn test_minimal_platforms_stay_same() {
#[test]
fn test_minimal_fares_stay_same_with_empty_of_fares() {
let ntm = transit_model::ntfs::read("tests/fixtures/ntfs2ntfs/empty_od_fares").unwrap();
println!("prices: {}", ntm.prices_v1.len());
for p in &ntm.prices_v1 {
println!("p == {:?}", p);
}
test_in_tmp_dir(|output_dir| {
transit_model::ntfs::write(&ntm, output_dir, get_test_datetime()).unwrap();
compare_output_dir_with_expected(
Expand Down

0 comments on commit 7154fa7

Please sign in to comment.