Spaces:
Paused

File size: 1,507 Bytes
4a91ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.testclient import TestClient

def test_create_dashboard(client: TestClient, normal_user_token_headers):
    response = client.post(
        "/api/v1/dashboards/",
        headers=normal_user_token_headers,
        json={"title": "My Dashboard", "description": "Sales stats"},
    )
    assert response.status_code == 201
    assert response.json()["title"] == "My Dashboard"
    return response.json()["id"]

def test_add_dashboard_item(client: TestClient, normal_user_token_headers):
    # Setup dashboard
    dashboard_id = test_create_dashboard(client, normal_user_token_headers)
    
    # Setup Data Source
    client.post(
        "/api/v1/datasources/",
        headers=normal_user_token_headers,
        json={
            "name": "Test DB",
            "type": "mysql",
            "host": "localhost",
            "port": 3306,
            "username": "root",
            "password": "password",
            "database_name": "test_db"
        },
    )
    ds_id = client.get("/api/v1/datasources/", headers=normal_user_token_headers).json()[-1]["id"]
    
    response = client.post(
        f"/api/v1/dashboards/{dashboard_id}/items",
        headers=normal_user_token_headers,
        json={
            "data_source_id": ds_id,
            "sql_query": "SELECT * FROM sales",
            "chart_type": "bar",
            "chart_config": {"x": "date", "y": "amount"}
        },
    )
    assert response.status_code == 201
    assert response.json()["dashboard_id"] == dashboard_id