test_full_insights/
test_full_insights.rs

1/// Test WhatsApp insights query with all SQL features
2use qail_core::parser::parse;
3use qail_core::transpiler::ToSql;
4
5fn main() {
6    // Test 1: Inner CTE only (no outer CASE)
7    println!("=== Test 1: CTE only ===");
8    let qail1 = r#"with stats as (get whatsapp_messages fields count(distinct phone_number) as total_contacts, count(*) as total_messages, count(*) filter (where direction = 'outbound' and created_at > now() - 24h) as messages_sent_24h, count(*) filter (where direction = 'inbound' and created_at > now() - 24h) as messages_received_24h, count(*) filter (where direction = 'inbound' and status = 'received') as unread_messages, count(*) filter (where direction = 'outbound' and created_at > now() - 24h and status in ('delivered', 'read')) as successful_deliveries_24h) get stats"#;
9    match parse(qail1) {
10        Ok(cmd) => println!("✅ Parses: {}", cmd.to_sql()),
11        Err(e) => println!("❌ {}", e),
12    }
13
14    // Test 2: Simple outer select with CASE WHEN
15    println!("\n=== Test 2: Outer CASE WHEN ===");
16    let qail2 = r#"get stats fields total_contacts, case when messages_sent_24h > 0 then 100.0 else 0.0 end as rate"#;
17    match parse(qail2) {
18        Ok(cmd) => println!("✅ Parses: {}", cmd.to_sql()),
19        Err(e) => println!("❌ {}", e),
20    }
21
22    // Test 3: Combined - CTE with outer CASE WHEN
23    println!("\n=== Test 3: Full Query (CTE + outer CASE) ===");
24    let qail3 = r#"with stats as (get whatsapp_messages fields count(*) as total, count(*) filter (where direction = 'outbound') as sent) get stats fields total, case when sent > 0 then 100.0 else 0.0 end as rate"#;
25    match parse(qail3) {
26        Ok(cmd) => println!("✅ Parses: {}", cmd.to_sql()),
27        Err(e) => println!("❌ {}", e),
28    }
29}