thot-local 0.10.0-intermediate

Local functionality for Thot data management and analysis software.
Documentation
use super::*;
use crate::types::ResourceId;
use fake::faker::filesystem::raw::FilePath;
use fake::locales::EN;
use fake::Fake;
use std::path::PathBuf;

// ********************
// *** Local Script ***
// ********************

#[test]
fn script_new_should_work() {
    let path = script_path();
    let script = LocalScript::new(path.clone()).expect("creating script should work");
    assert_eq!(&path, &script.path, "script's path should be correct");
}

// ***************
// *** Scripts ***
// ***************

#[test]
fn scripts_get_should_work() {
    // setup
    let path = script_path();
    let script = Script::new(path).expect("creating script should work");
    let rid = script.rid.clone();

    let mut scripts = Scripts::new();
    scripts.push(script);

    // test
    let found = scripts.get(&rid).expect("scripts should contain script");
    assert_eq!(&rid, &found.rid, "correct script should be retrieved");
    assert!(
        scripts.get(&ResourceId::new()).is_none(),
        "scripts should not contain unregistered"
    );
}

#[test]
fn scripts_contains_should_work() {
    // setup
    let path = script_path();
    let script = Script::new(path).expect("creating script should work");
    let rid = script.rid.clone();

    let mut scripts = Scripts::new();
    scripts.push(script);

    // test
    assert!(scripts.contains(&rid), "scripts should contain script");
    assert_eq!(
        false,
        scripts.contains(&ResourceId::new()),
        "scripts should not contain unregistered"
    );
}

#[test]
fn scripts_contains_path_should_work() {
    // setup
    let path = script_path();
    let script = Script::new(path.clone()).expect("creating script should work");

    let mut scripts = Scripts::new();
    scripts.push(script);

    // test
    assert!(
        scripts.contains_path(&path),
        "scripts should contain script"
    );

    assert_eq!(
        false,
        scripts.contains_path(&script_path()),
        "scripts should not contain random path"
    );
}

#[test]
fn scripts_by_path_should_work() {
    // setup
    let path = script_path();
    let script = Script::new(path.clone()).expect("creating script should work");
    let rid = script.rid.clone();

    let mut scripts = Scripts::new();
    scripts.push(script);

    // test
    // inserted script
    let found = scripts.by_path(&path);
    assert!(found.is_some(), "script should be found");

    let found = found.unwrap();
    assert_eq!(&rid, &found.rid, "found script should be correct");

    // not inserted script
    let rand = scripts.by_path(&script_path());
    assert!(rand.is_none(), "script should not be found");
}

// ************************
// *** helper functions ***
// ************************

fn script_path() -> ResourcePath {
    let path = PathBuf::from(FilePath(EN).fake::<String>());
    ResourcePath::new(path).expect("creating resource path should work")
}