#[cfg(test)] mod tests { use std::path::Path; use assert_json_diff::assert_json_eq; use log::debug; use power_utils::{assert_json_match_fixture, tests::load_json_fixture}; fn init() { let _global_logger = env_logger::builder().is_test(true).try_init(); } /// Given: /// - A module /// /// When: /// - That module is mixed with itself /// /// Then: /// - No error is thrown; and /// - The result is a valid JSON module #[test] fn merge_itself() { init(); let first_module = Path::new("tests/fixtures/multiple_contents.json"); assert!(first_module.exists()); let second_module = Path::new("tests/fixtures/multiple_contents.json"); assert!(second_module.exists()); let result = power_mixer::mix(first_module, second_module).unwrap(); let expected = load_json_fixture("tests/fixtures/multiple_contents.json".to_owned()); assert_json_match_fixture!(result, expected); } /// Given: /// - A nodule with types /// - A module without types or content /// /// When: /// - The two modules are mixed into a new module /// - The two modules are mixed in reverse order into a new module /// /// Then: /// - No error is thrown; and /// - The results are valid JSON modules /// - The first resulting module has the values of the second module for id and title /// - The second resulting module has the values of the first module for id and title #[test] fn merge_is_order_dependent() { init(); let first_module = Path::new("tests/fixtures/multiple_contents.json"); assert!(first_module.exists()); let second_module = Path::new("tests/fixtures/no_types_or_content.json"); assert!(second_module.exists()); let first_plus_second = power_mixer::mix(first_module, second_module).unwrap(); let first_expected = load_json_fixture("tests/fixtures/output/first_plus_second.output.json".to_owned()); assert_json_match_fixture!(first_plus_second, first_expected); let second_plus_first = power_mixer::mix(second_module, first_module).unwrap(); let second_expected = load_json_fixture("tests/fixtures/output/second_plus_first.output.json".to_owned()); assert_json_match_fixture!(second_plus_first, second_expected); } /// Given: /// - A nodule with types /// - A module with contents /// /// When: /// - The two modules are mixed into a new module /// /// Then: /// - No error is thrown; and /// - The results are valid JSON modules /// - The resulting module has both types and contents, independent of order #[test] fn merge_types_with_contents() { init(); let contents_module = Path::new("tests/fixtures/multiple_contents.json"); assert!(contents_module.exists()); let types_module = Path::new("tests/fixtures/single_type.json"); assert!(types_module.exists()); let first_result = power_mixer::mix(contents_module, types_module).unwrap(); let first_expected = load_json_fixture("tests/fixtures/output/contents_and_types.output.json".to_owned()); assert_json_match_fixture!(first_result, first_expected); let second_result = power_mixer::mix(types_module, contents_module).unwrap(); let second_expected = load_json_fixture("tests/fixtures/output/types_and_contents.output.json".to_owned()); assert_json_match_fixture!(second_result, second_expected); } /// Given: /// - A nodule with multiple contents /// - A module with one content with the same id /// /// When: /// - The two modules are mixed into a new module /// /// Then: /// - No error is thrown; and /// - The results are valid JSON modules /// - The resulting module has all contents, with the later module overwriting the previous one. #[test] fn merge_with_contents_of_same_id() { init(); let first_module = Path::new("tests/fixtures/multiple_contents.json"); assert!(first_module.exists()); let second_module = Path::new("tests/fixtures/contents_with_same_id.json"); assert!(second_module.exists()); let result = power_mixer::mix(first_module, second_module).unwrap(); let expected = load_json_fixture( "tests/fixtures/output/content_overwritten_by_id.output.json".to_owned(), ); assert_json_match_fixture!(result, expected); } /// Given: /// - A nodule with multiple types and contents /// - A module with one type with the same id /// /// When: /// - The two modules are mixed into a new module /// /// Then: /// - No error is thrown; and /// - The results are valid JSON modules /// - The resulting module has all contents and types, with the later module overwriting the previous one. #[test] fn merge_with_types_of_same_id() { init(); let first_module = Path::new("tests/fixtures/contents_and_types.json"); assert!(first_module.exists()); let second_module = Path::new("tests/fixtures/types_with_same_id.json"); assert!(second_module.exists()); let result = power_mixer::mix(first_module, second_module).unwrap(); let expected = load_json_fixture( "tests/fixtures/output/type_overwritten_by_id.output.json".to_owned(), ); assert_json_match_fixture!(result, expected); } }