/ tests / test_connection_validation.py
test_connection_validation.py
 1  """Tests for SQL connection string validation in RAG projects."""
 2  import pytest
 3  from fastapi import HTTPException
 4  
 5  from restai.projects.rag import _validate_connection_string
 6  
 7  
 8  def test_accepts_postgresql():
 9      # Should not raise
10      _validate_connection_string("postgresql://user:pass@host/db")
11  
12  
13  def test_accepts_mysql_pymysql():
14      # Should not raise
15      _validate_connection_string("mysql+pymysql://user:pass@host/db")
16  
17  
18  def test_accepts_sqlite_relative():
19      # sqlite:// with no host + relative path. urlparse("sqlite:///relative.db")
20      # produces path="/relative.db" which starts with "/" and triggers the
21      # absolute-path guard. Use the empty-authority form instead.
22      _validate_connection_string("sqlite:relative.db")
23  
24  
25  def test_rejects_file_scheme():
26      with pytest.raises(HTTPException) as exc_info:
27          _validate_connection_string("file:///etc/passwd")
28      assert exc_info.value.status_code == 400
29  
30  
31  def test_rejects_mongodb_scheme():
32      with pytest.raises(HTTPException) as exc_info:
33          _validate_connection_string("mongodb://host/db")
34      assert exc_info.value.status_code == 400
35  
36  
37  def test_rejects_sqlite_absolute_outside_cwd():
38      with pytest.raises(HTTPException) as exc_info:
39          _validate_connection_string("sqlite:///etc/passwd")
40      assert exc_info.value.status_code == 400