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