use std::time::Duration;
#[test]
fn test_init_compiles() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
}
#[test]
fn test_init_with_config_compiles() {
use finance_query::edgar;
let _ = edgar::init_with_config(
"user@example.com",
"my-financial-app", Duration::from_secs(60), );
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_resolve_cik_rate_limiting() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let cik1 = edgar::resolve_cik("AAPL").await.unwrap();
let cik2 = edgar::resolve_cik("MSFT").await.unwrap();
let cik3 = edgar::resolve_cik("GOOGL").await.unwrap();
assert!(cik1 > 0);
assert!(cik2 > 0);
assert!(cik3 > 0);
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_resolve_cik() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let cik = edgar::resolve_cik("AAPL").await.unwrap();
println!("Apple CIK: {}", cik); assert_eq!(cik, 320193);
let cik2 = edgar::resolve_cik("AAPL").await.unwrap(); assert_eq!(cik, cik2);
let cik3 = edgar::resolve_cik("aapl").await.unwrap(); assert_eq!(cik, cik3);
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_submissions_filing_history() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let cik = edgar::resolve_cik("AAPL").await.unwrap();
let submissions = edgar::submissions(cik).await.unwrap();
if let Some(name) = &submissions.name {
println!("Company: {}", name);
}
if let Some(cik_str) = &submissions.cik {
println!("CIK: {}", cik_str);
}
if let Some(sic) = &submissions.sic {
println!("SIC: {}", sic);
}
if let Some(fiscal_year_end) = &submissions.fiscal_year_end {
println!("Fiscal Year End: {}", fiscal_year_end);
}
if let Some(filings) = &submissions.filings
&& let Some(recent) = &filings.recent
{
for i in 0..5.min(recent.accession_number.len()) {
let form = &recent.form[i];
let date = &recent.filing_date[i];
let accession = &recent.accession_number[i];
println!("{} filed on {}: {}", form, date, accession);
}
assert!(!recent.accession_number.is_empty());
}
assert!(submissions.name.is_some());
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_company_facts_xbrl() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let cik = edgar::resolve_cik("AAPL").await.unwrap();
let facts = edgar::company_facts(cik).await.unwrap();
if let Some(us_gaap) = facts.facts.get("us-gaap") {
if let Some(revenue) = us_gaap.0.get("Revenues") {
if let Some(label) = &revenue.label {
println!("Revenue concept: {}", label);
}
if let Some(description) = &revenue.description {
println!("Description: {}", description);
}
if let Some(usd_data) = revenue.units.get("USD") {
for point in usd_data.iter().take(5) {
if let (Some(fy), Some(val)) = (point.fy, point.val) {
println!("FY {}: ${}", fy, val);
}
}
}
}
if let Some(assets) = us_gaap.0.get("Assets")
&& let Some(usd_data) = assets.units.get("USD")
{
for point in usd_data.iter().take(5) {
if let (Some(fy), Some(val)) = (point.fy, point.val) {
println!("FY {}: ${}", fy, val);
}
}
}
}
assert!(!facts.facts.is_empty());
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_search_basic() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let results = edgar::search(
"artificial intelligence",
None, None, None, None, None, )
.await
.unwrap();
if let Some(hits) = &results.hits {
if let Some(total) = &hits.total
&& let Some(value) = total.value
{
println!("Total hits: {}", value);
}
for hit in &hits.hits {
if let Some(source) = &hit._source {
let form = source.form.as_deref().unwrap_or("Unknown");
let file_date = source.file_date.as_deref().unwrap_or("Unknown");
println!("{} filed on {}", form, file_date);
if !source.display_names.is_empty() {
println!(" Companies: {:?}", source.display_names);
}
}
}
}
assert!(results.hits.is_some());
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_search_filtered() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
let results = edgar::search(
"machine learning",
Some(&["10-K"]), Some("2024-01-01"), Some("2024-12-31"), None,
None,
)
.await
.unwrap();
assert!(results.hits.is_some());
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_search_common_form_filters() {
use finance_query::edgar;
let _ = edgar::init("user@example.com");
edgar::search("query", Some(&["10-K"]), None, None, None, None)
.await
.unwrap();
edgar::search("query", Some(&["10-Q"]), None, None, None, None)
.await
.unwrap();
edgar::search("query", Some(&["8-K"]), None, None, None, None)
.await
.unwrap();
edgar::search(
"query",
Some(&["10-K", "10-Q", "8-K"]),
None,
None,
None,
None,
)
.await
.unwrap();
}
#[tokio::test]
#[ignore = "requires network access"]
async fn test_complete_example() {
use finance_query::edgar;
let ticker = "AAPL";
let _ = edgar::init("user@example.com");
println!("Resolving {} to CIK...", ticker);
let cik = edgar::resolve_cik(ticker).await.unwrap();
println!("CIK: {}\n", cik);
println!("Fetching filing history...");
let submissions = edgar::submissions(cik).await.unwrap();
if let Some(name) = &submissions.name {
println!("Company: {}", name);
}
if let Some(sic_description) = &submissions.sic_description {
println!("Industry: {}", sic_description);
}
if let Some(filings) = &submissions.filings
&& let Some(recent) = &filings.recent
{
println!("\nRecent filings:");
for i in 0..10.min(recent.form.len()) {
let form = &recent.form[i];
if form == "10-K" || form == "10-Q" {
let date = &recent.filing_date[i];
println!(" {} filed on {}", form, date);
}
}
}
println!("\nFetching XBRL financial data...");
let facts = edgar::company_facts(cik).await.unwrap();
if let Some(us_gaap) = facts.facts.get("us-gaap") {
if let Some(revenue) = us_gaap.0.get("Revenues")
&& let Some(usd) = revenue.units.get("USD")
{
println!("\nRevenue Trend:");
for point in usd.iter().take(5) {
if let (Some(fy), Some(val)) = (point.fy, point.val) {
println!(" FY {}: ${:>15}", fy, val);
}
}
}
if let Some(assets) = us_gaap.0.get("Assets")
&& let Some(usd) = assets.units.get("USD")
{
println!("\nAssets:");
for point in usd.iter().take(3) {
if let (Some(fy), Some(val)) = (point.fy, point.val) {
println!(" FY {}: ${:>15}", fy, val);
}
}
}
}
println!("\nSearching for 'artificial intelligence' mentions...");
let search_results = edgar::search(
"artificial intelligence",
Some(&["10-K", "10-Q"]),
Some("2024-01-01"),
None,
None,
None,
)
.await
.unwrap();
if let Some(hits) = &search_results.hits {
let count = hits.total.as_ref().and_then(|t| t.value).unwrap_or(0);
println!("Found {} mentions", count);
}
assert!(cik > 0);
assert!(facts.facts.contains_key("us-gaap"));
}