File size: 3,958 Bytes
ed91833
654e910
 
 
 
ed91833
b22f9a0
ed91833
999f24c
ed91833
1ef298a
ed91833
 
 
999f24c
1ef298a
ed91833
 
 
999f24c
ed91833
 
 
 
999f24c
ed91833
 
 
 
999f24c
1ef298a
 
 
999f24c
ed91833
 
 
999f24c
ed91833
 
 
1ef298a
 
999f24c
1ef298a
 
 
 
 
999f24c
 
654e910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b22f9a0
654e910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import socket
import pytest
import requests

from langchain.schema import Document
from backend.app.vectorstore import get_vector_db, get_qdrant_client


def test_directory_creation():
    get_vector_db()
    assert os.path.exists("static/data")
    assert os.path.exists("static/data/langchain_rag_tutorial.html")


# TODO remove this test when data ingrestion layer is implemented
def test_html_content():
    with open("static/data/langchain_rag_tutorial.html", "r", encoding="utf-8") as f:
        content = f.read()

    # Check for some expected content from the LangChain RAG tutorial
    assert "RAG" in content
    assert "LangChain" in content


def test_vector_store_similarity_search():
    """Test that the vector store can perform similarity search"""
    # Test query
    query = "What is RAG?"

    # Get vector db instance and perform similarity search
    vector_db = get_vector_db()
    results = vector_db.similarity_search(query, k=2)

    # Verify we get results
    assert len(results) == 2
    assert isinstance(results[0], Document)

    # Verify the results contain relevant content
    combined_content = " ".join([doc.page_content for doc in results]).lower()
    assert "rag" in combined_content
    assert "retrieval" in combined_content


def test_vector_db_singleton():
    """Test that get_vector_db returns the same instance each time"""
    # Get two instances
    instance1 = get_vector_db()
    instance2 = get_vector_db()

    assert instance1 is instance2


def test_qdrant_cloud_connection():
    """Test basic connectivity to Qdrant Cloud"""
    # Skip test if not configured for cloud
    if not os.environ.get("QDRANT_URL") or not os.environ.get("QDRANT_API_KEY"):

        pytest.skip("Qdrant Cloud credentials not configured")

    try:
        # Print URL for debugging (excluding any path components)
        qdrant_url = os.environ.get("QDRANT_URL", "")
        print(f"Attempting to connect to Qdrant at: {qdrant_url}")

        # Try to parse the URL components
        from urllib.parse import urlparse

        parsed_url = urlparse(qdrant_url)
        print(f"Scheme: {parsed_url.scheme}")
        print(f"Hostname: {parsed_url.hostname}")
        print(f"Port: {parsed_url.port}")
        print(f"Path: {parsed_url.path}")

        client = get_qdrant_client()
        client.get_collections()
        assert True, "Connection successful"
    except Exception as e:
        assert False, f"Failed to connect to Qdrant Cloud: {str(e)}"


def test_external_connectivity():
    """Test basic external connectivity and DNS resolution.
    Test needed since Docker gave an issue with this before. Couldn't resolve Qdrant host.
    """

    # Skip test if not configured for cloud
    if not os.environ.get("QDRANT_URL") or not os.environ.get("QDRANT_API_KEY"):
        pytest.skip("Qdrant Cloud credentials not configured")

    # Test DNS resolution first
    try:
        # Try to resolve google.com
        google_ip = socket.gethostbyname("google.com")
        print(f"Successfully resolved google.com to {google_ip}")

        # If we have Qdrant URL, try to resolve that too
        qdrant_url = os.environ.get("QDRANT_URL", "")
        if qdrant_url:
            qdrant_host = (
                qdrant_url.replace("https://", "").replace("http://", "").split("/")[0]
            )
            print(f"Qdrant host: {qdrant_host}")
            qdrant_ip = socket.gethostbyname(qdrant_host)
            print(f"Successfully resolved Qdrant host {qdrant_host}")
    except socket.gaierror as e:
        assert False, f"DNS resolution failed: {str(e)}"

    # Test HTTP connectivity
    try:
        response = requests.get("https://www.google.com", timeout=5)
        assert (
            response.status_code == 200
        ), "Expected successful response from google.com"
    except requests.exceptions.RequestException as e:
        assert False, f"Failed to connect to google.com: {str(e)}"