Spaces:
Paused

File size: 3,176 Bytes
4a91ea9
121b932
4a91ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121b932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from fastapi.testclient import TestClient
import io

def test_create_data_source(client: TestClient, normal_user_token_headers):
    response = 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"
        },
    )
    assert response.status_code == 201

def test_read_data_sources(client: TestClient, normal_user_token_headers):
    response = client.get("/api/v1/datasources/", headers=normal_user_token_headers)
    assert response.status_code == 200
    assert isinstance(response.json(), list)

def test_upload_data_source(client: TestClient, normal_user_token_headers):
    # Mock CSV file
    file_content = b"col1,col2\n1,2\n3,4"
    file = io.BytesIO(file_content)
    file.name = "test.csv"
    response = client.post(
        "/api/v1/datasources/upload",
        headers=normal_user_token_headers,
        data={"name": "Test CSV Upload"},
        files={"file": ("test.csv", file, "text/csv")}
    )
    assert response.status_code == 200
    data = response.json()
    assert data["name"] == "Test CSV Upload"
    assert data["type"] == "csv"

def test_test_connection_endpoint(client: TestClient, normal_user_token_headers):
    # Get a data source first
    response = client.get("/api/v1/datasources/", headers=normal_user_token_headers)
    ds_id = response.json()[0]["id"]

    # Test connection by id
    res = client.post(f"/api/v1/datasources/{ds_id}/test", headers=normal_user_token_headers)
    assert res.status_code == 200

def test_test_connection_params(client: TestClient, normal_user_token_headers):
    res = client.post(
        "/api/v1/datasources/test-connection",
        headers=normal_user_token_headers,
        json={
            "name": "Test Params",
            "type": "postgresql",
            "host": "localhost",
            "username": "user",
            "database_name": "db"
        }
    )
    assert res.status_code == 200 or res.status_code == 400

def test_update_data_source(client: TestClient, normal_user_token_headers):
    response = client.get("/api/v1/datasources/", headers=normal_user_token_headers)
    ds_id = response.json()[0]["id"]
    
    update_res = client.put(
        f"/api/v1/datasources/{ds_id}",
        headers=normal_user_token_headers,
        json={
            "name": "Updated DB",
            "type": "mysql",
            "host": "localhost",
            "port": 3306,
            "username": "root",
            "password": "",
            "database_name": "test_db"
        }
    )
    assert update_res.status_code == 200
    assert update_res.json()["name"] == "Updated DB"

def test_delete_data_source(client: TestClient, normal_user_token_headers):
    response = client.get("/api/v1/datasources/", headers=normal_user_token_headers)
    ds_id = response.json()[0]["id"]
    
    del_res = client.delete(f"/api/v1/datasources/{ds_id}", headers=normal_user_token_headers)
    assert del_res.status_code == 204