use super::*;
use crate::result::Result;
use crate::types::{ResourceId, ResourcePath};
use fake::faker::filesystem::raw::FileName;
use fake::faker::lorem::raw::Word;
use fake::locales::EN;
use fake::Fake;
use rand::Rng;
use std::path::PathBuf;
#[test]
fn local_asset_new_should_work() {
let file = mock_resource_path();
let asset = LocalAsset::new(file.clone()).expect("new should work");
assert_eq!(Some(file), asset.path, "file should be initialized");
}
#[test]
fn local_asset_default_should_work() {
let asset = LocalAsset::default();
assert_eq!(None, asset.path, "file should be none");
}
#[test]
fn assets_get_should_work() {
let assets = mock_assets(2).expect("setup should work");
let r0 = &assets.assets[0].properties.rid;
let r1 = ResourceId::new();
let f0 = assets.get(r0).expect("asset should be found");
assert_eq!(r0, &f0.properties.rid, "correct asset should be returned");
assert!(assets.get(&r1).is_none(), "asset should not be found");
}
#[test]
fn assets_index_by_lid_should_work() {
let n_assets: usize = rand::thread_rng().gen_range(1..100);
let mut assets = mock_assets(n_assets).expect("setup should work");
let i_target = rand::thread_rng().gen_range(0..n_assets);
let lid = Word(EN).fake::<String>();
let a0 = &mut assets.assets[i_target];
a0.properties.rid.lid = Some(lid.clone());
let i0 = assets.index_by_lid(&lid).expect("asset should be found");
assert_eq!(i_target, i0, "correct index should be found");
assert!(
assets.index_by_lid(&Word(EN).fake::<String>()).is_none(),
"asset should not be found"
);
}
#[test]
fn assets_index_by_path_should_work() {
let n_assets: usize = rand::thread_rng().gen_range(1..100);
let assets = mock_assets(n_assets).expect("setup should work");
let i_target = rand::thread_rng().gen_range(0..n_assets);
let a0 = &assets.assets[i_target];
let p0 = a0.path.clone().expect("path should have a value");
let p1 = mock_resource_path();
let i0 = assets.index_by_path(&p0).expect("asset should be found");
assert_eq!(i_target, i0, "correct index should be returned");
assert!(
assets.index_by_path(&p1).is_none(),
"asset should not be found"
);
}
#[test]
fn assets_contains_should_work() {
let assets = mock_assets(2).expect("setup should work");
let r0 = assets.assets[0].properties.rid.clone();
let r1 = ResourceId::new();
assert!(assets.contains(&r0), "asset should be contained");
assert!(!assets.contains(&r1), "asset should not be contained");
}
#[test]
fn assets_contains_lid_should_work() {
let mut assets = mock_assets(2).expect("setup should work");
let lid = Word(EN).fake::<String>();
let a0 = &mut assets.assets[0];
a0.properties.rid.lid = Some(lid.clone());
assert!(assets.contains_lid(&lid), "asset should be contained");
assert!(
!assets.contains_lid(&Word(EN).fake::<String>()),
"asset should not be contained"
);
}
#[test]
fn assets_contains_path_should_work() {
let assets = mock_assets(2).expect("setup should work");
let p0 = assets.assets[0]
.path
.clone()
.expect("path should have a value");
let p1 = mock_resource_path();
assert!(assets.contains_path(&p0), "asset should be contained");
assert!(!assets.contains_path(&p1), "asset should not be contained");
}
#[test]
fn assets_rid_of_path_should_work() {
let assets = mock_assets(2).expect("setup should work");
let a0 = &assets.assets[0];
let r0 = a0.properties.rid.clone();
let p0 = a0.path.clone().expect("path should have value");
let p1 = mock_resource_path();
assert_eq!(
Some(r0),
assets.rid_of_path(&p0),
"resource id should match"
);
assert_eq!(None, assets.rid_of_path(&p1), "asset should not be found");
}
fn mock_resource_path() -> ResourcePath {
ResourcePath::new(PathBuf::from(FileName(EN).fake::<String>()))
.expect("creating resource path should work")
}
fn mock_assets(n_assets: usize) -> Result<Assets> {
let mut assets = Assets::new();
for _ in 0..n_assets {
let a = Asset::new(mock_resource_path())?;
assets.assets.push(a);
}
Ok(assets)
}