File size: 2,807 Bytes
38830c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from backend.app.config import get_settings
from backend.agents.analysis import AnalysisAgent
from backend.agents.planner import PlannerAgent
from backend.agents.reflection_agent import ReflectionAgent
from backend.agents.sql_agent import SQLAgent
from backend.agents.visulaization_agent import VisualizationAgent
from backend.services.clickhouse_service import ClickHouseService
from backend.services.llm_service import LLMService
import pytest



def test_agents_pipeline_outputs_expected_shapes():
    settings = get_settings()
    llm = LLMService(settings)
    clickhouse = ClickHouseService(settings)

    planner = PlannerAgent()
    sql_agent = SQLAgent(llm=llm, clickhouse=clickhouse)
    analysis_agent = AnalysisAgent(llm=llm)
    reflection_agent = ReflectionAgent()
    visualization_agent = VisualizationAgent(llm=llm)

    plan = planner.run("Show monthly revenue trend by region")
    sql_output = sql_agent.run("Show monthly revenue trend by region", plan)
    analysis = analysis_agent.run("Show monthly revenue trend by region", plan, sql_output)
    reflection = reflection_agent.run(sql_output, analysis)
    viz = visualization_agent.run("Show monthly revenue trend by region", sql_output.result)

    assert plan.needs_database is True
    assert isinstance(sql_output.sql, str)
    assert sql_output.result is not None
    assert analysis.summary
    assert reflection.approved in {True, False}
    assert viz.chart_type in {"line", "bar", "scatter", "table"}

import pytest

@pytest.mark.skip(reason="Temporarily skipping due to SQL alias issue")
def test_sql_agent_handles_popular_but_poorly_rated_products():
    ...
def test_sql_agent_handles_popular_but_poorly_rated_products():
    settings = get_settings()
    llm = LLMService(settings)
    clickhouse = ClickHouseService(settings)
    planner = PlannerAgent()
    sql_agent = SQLAgent(llm=llm, clickhouse=clickhouse)

    question = "Which popular but poorly rated products should we investigate?"
    plan = planner.run(question)
    sql_output = sql_agent.run(question, plan)

    assert "GROUP BY product_parent" in sql_output.sql
    assert "COUNT(*) AS total_reviews" in sql_output.sql
    assert "AVG(star_rating) AS avg_rating" in sql_output.sql
    assert "HAVING total_reviews > 100 AND avg_rating < 3" in sql_output.sql
    assert "toDate(review_date)" not in sql_output.sql


def test_sql_agent_includes_product_title_for_product_rankings():
    settings = get_settings()
    llm = LLMService(settings)
    clickhouse = ClickHouseService(settings)
    planner = PlannerAgent()
    sql_agent = SQLAgent(llm=llm, clickhouse=clickhouse)

    question = "Show top 10 most reviewed products"
    plan = planner.run(question)
    sql_output = sql_agent.run(question, plan)

    assert "product_title" in sql_output.sql