Initial commit
This commit is contained in:
99
tests/tests.rs
Normal file
99
tests/tests.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use Brig::{fetch_article, parse, get_links, get_categories, get_references, get_templates, get_template_parameters};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
async fn get_rust_article_content() -> String {
|
||||
let result = fetch_article("en", "Rust (programming language)").await;
|
||||
assert!(result.is_ok(), "Should successfully fetch a valid article");
|
||||
let content = result.unwrap();
|
||||
assert!(!content.is_empty(), "Content should not be empty");
|
||||
content
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_article_valid() {
|
||||
// Scenario A: Valid access
|
||||
let content = get_rust_article_content().await;
|
||||
assert!(!content.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_article_invalid() {
|
||||
// Scenario Z: Incorrect article
|
||||
let result = fetch_article("en", "ThisPageDoesNotExist_12345_XYZ").await;
|
||||
assert!(result.is_err(), "Should fail to fetch a non-existent article");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_parse_article() {
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
assert!(!parsed.nodes.is_empty(), "Should parse fetched content");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_links_integration() {
|
||||
// Scenario C: Valid access -> Status 200 + links of the article.
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
let links = get_links(&parsed.nodes);
|
||||
assert!(!links.is_empty(), "Should find links in the Rust article");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_categories_integration() {
|
||||
// Scenario B: Valid access -> Status 200 + categories of the article.
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
let categories = get_categories(&parsed.nodes);
|
||||
assert!(!categories.is_empty(), "Should find categories in the Rust article");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_references_integration() {
|
||||
// Scenario D: Valid access -> Status 200 + references of the article.
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
let references = get_references(&parsed.nodes);
|
||||
assert!(!references.is_empty(), "Should find references in the Rust article");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_templates_integration() {
|
||||
// Scenario E: Valid access -> Status 200 + templates of the article.
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
let templates = get_templates(&parsed.nodes);
|
||||
assert!(!templates.is_empty(), "Should find templates in the Rust article");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_template_parameters_integration() {
|
||||
// Scenario F: Valid access -> Status 200 + parameters of a template.
|
||||
let content = get_rust_article_content().await;
|
||||
let parsed = parse(&content);
|
||||
let templates = get_templates(&parsed.nodes);
|
||||
|
||||
// "Infobox programming language" is likely present on the Rust page.
|
||||
let target_template = "Infobox programming language";
|
||||
|
||||
if templates.contains(&target_template.to_string()) {
|
||||
let params = get_template_parameters(&parsed.nodes, target_template);
|
||||
assert!(!params.is_empty(), "Should find parameters for Infobox programming language");
|
||||
} else {
|
||||
// Fallback: try to find *any* template that has parameters if the specific one isn't found
|
||||
// This makes the test more robust to content changes on Wikipedia
|
||||
let mut found_params = false;
|
||||
for template_name in templates {
|
||||
let params = get_template_parameters(&parsed.nodes, &template_name);
|
||||
if !params.is_empty() {
|
||||
found_params = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(found_params, "Should find parameters for at least one template");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user