Skip to content

Tests cleanup and bringing back to life #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
POETRY_VERSION=1.8.3 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

ENV PATH="$POETRY_HOME/bin:$PATH"
Expand All @@ -28,7 +27,7 @@ RUN rustc --version && cargo --version
WORKDIR /app

# Install python dependencies using poetry
COPY pyproject.toml poetry.lock ./
COPY pyproject.toml poetry.lock poetry.toml ./
RUN poetry install --only main --no-root

# Final stage
Expand Down
3 changes: 1 addition & 2 deletions backend/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
POETRY_VERSION=1.8.3 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

ENV PATH="$POETRY_HOME/bin:$PATH"
Expand All @@ -28,7 +27,7 @@ RUN rustc --version && cargo --version
WORKDIR /app

# Install python dependencies using poetry
COPY pyproject.toml poetry.lock ./
COPY pyproject.toml poetry.lock poetry.toml ./
RUN poetry install --with main,test --no-root

# Final stage
Expand Down
1,025 changes: 512 additions & 513 deletions backend/poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions backend/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[virtualenvs]
create = true
in-project = true
2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ DEP002 = ["psycopg2", "itsdangerous"]

[tool.pytest.ini_options]
pythonpath = ["."]
testpaths = ["backend/tests"]
testpaths = ["./tests"]
addopts = "-v -s"
19 changes: 6 additions & 13 deletions backend/tests/collection/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
from fastapi import BackgroundTasks, HTTPException, UploadFile
from rag_solution.repository.collection_repository import CollectionRepository
from rag_solution.repository.user_repository import UserRepository
from rag_solution.schemas.collection_schema import (CollectionInput,
CollectionOutput)
from rag_solution.schemas.collection_schema import (CollectionInput, CollectionOutput)
from rag_solution.schemas.user_schema import UserInput, UserOutput
from rag_solution.services.collection_service import CollectionService
from rag_solution.services.file_management_service import FileManagementService
from rag_solution.services.user_collection_service import UserCollectionService
from rag_solution.services.user_service import UserService
from rag_solution.services.user_team_service import UserTeamService
from sqlalchemy.orm import Session
import logging

Expand All @@ -37,8 +35,8 @@ def collection_repository(db_session):
return CollectionRepository(db_session)

@pytest.fixture
def user_service(db_session: Session, user_team_service: UserTeamService):
return UserService(db_session, user_team_service)
def user_service(db_session: Session):
return UserService(db_session)

@pytest.fixture
def file_management_service(db_session):
Expand All @@ -55,11 +53,6 @@ def collection_service(db_session):
logger.debug("CollectionService initialized successfully")
return service

@pytest.fixture
def user_team_service(db_session):
return UserTeamService(db_session)


def test_create_collection(user_service: UserService, collection_service: CollectionService):
user = user_service.create_user(UserInput(ibm_id="test_ibm_id", email="[email protected]", name="Test User"))
collection_input = CollectionInput(name="Test Collection", is_private=True, users=[user.id])
Expand Down Expand Up @@ -142,20 +135,20 @@ def test_create_collection_with_documents(
assert created_collection.is_private == collection_input.is_private
assert len(created_collection.user_ids) == 1
assert user.id in created_collection.user_ids
collection_files = file_management_service.get_files(user.id, created_collection.id)
collection_files = file_management_service.get_files(created_collection.id)
assert len(collection_files) == 2
assert "test_file_1.txt" in collection_files
assert "test_file_2.txt" in collection_files
for file in files:
file_path = file_management_service.get_file_path(user.id, created_collection.id, file.filename)
file_path = file_management_service.get_file_path(created_collection.id, file.filename)
assert file_path.exists()
with open(file_path, "rb") as f:
content = f.read()
if file.filename == "test_file_1.txt":
assert content == file_content_1
elif file.filename == "test_file_2.txt":
assert content == file_content_2
file_management_service.delete_files(user.id, created_collection.id, ["test_file_1.txt", "test_file_2.txt"])
file_management_service.delete_files(created_collection.id, ["test_file_1.txt", "test_file_2.txt"])
collection_service.delete_collection(created_collection.id)
user_service.delete_user(user.id)

Expand Down
21 changes: 1 addition & 20 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import asyncio
import uuid

import chromadb
import pymupdf
import pytest
import weaviate
from core.config import settings
from elasticsearch import Elasticsearch

from rag_solution.models.question import SuggestedQuestion
from vectordbs.milvus_store import MilvusStore
from fastapi.testclient import TestClient
from pinecone import Pinecone
from pymilvus import connections
from rag_solution.file_management.database import engine as sync_engine, Base, SessionLocal
from rag_solution.file_management.database import engine as sync_engine, Base
# Remove or comment out these lines:
from rag_solution.models.collection import Collection
from rag_solution.models.file import File
Expand All @@ -25,25 +19,12 @@
from rag_solution.schemas.user_schema import UserInput
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from vectordbs.vector_store import VectorStore
import logging
from vectordbs.factory import get_datastore
from vectordbs.error_types import CollectionError
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


#from main import app

# DATABASE_URL = (
# f"postgresql://{settings.collectiondb_user}:{settings.collectiondb_pass}@{settings.collectiondb_host}:{settings.collectiondb_port}/{settings.collectiondb_name}"
# )
# # Use synchronous database URL

# print (f"**DATABASE URL: {DATABASE_URL}")
# sync_engine = create_engine(DATABASE_URL)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=sync_engine)

@pytest.fixture(scope="session")
def db_engine():
"""Create a synchronous engine for the database session."""
Expand Down
66 changes: 0 additions & 66 deletions backend/tests/data_ingestion/test_ingestion.py

This file was deleted.

52 changes: 42 additions & 10 deletions backend/tests/router/test_collection_router.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import unittest
import pytest
from uuid import uuid4
from unittest.mock import patch
from fastapi.testclient import TestClient
from rag_solution.router.collection_router import create_collection

from main import app

class TestCollectionRouter(unittest.TestCase):
def setUp(self) -> None:
self.client = TestClient(create_collection)
client = TestClient(app)

def test_create_collection(self):
# Implement test logic here
pass
UUID_EXAMPLE = uuid4()

@pytest.fixture
def input_data():
return {
"collection_id": UUID_EXAMPLE
}

if __name__ == "__main__":
unittest.main()
@pytest.fixture
def headers():
return {
"Authorization": "Bearer mock_access_token",
}

def test_get_collection_users(input_data):
with patch('rag_solution.services.user_collection_service.UserCollectionService') as MockService:
mock_service = MockService.return_value
mock_service.get_collection_users.return_value = []

response = client.get(f"/api/collections/{input_data['collection_id']}/users")

assert response.status_code == 200

mock_service.get_collection_users.assert_called_once_with(
input_data["collection_id"]
)

def test_remove_all_users_from_collection(input_data):
with patch('rag_solution.services.user_collection_service.UserCollectionService') as MockService:
mock_service = MockService.return_value
mock_service.remove_all_users_from_collection.return_value = []

response = client.delete(f"/api/collections/{input_data['collection_id']}/users")

assert response.status_code == 200

mock_service.remove_all_users_from_collection.assert_called_once_with(
input_data["collection_id"]
)
16 changes: 0 additions & 16 deletions backend/tests/router/test_file_router.py

This file was deleted.

26 changes: 18 additions & 8 deletions backend/tests/router/test_team_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from fastapi.testclient import TestClient
from uuid import uuid4
from unittest.mock import patch
from rag_solution.router.team_router import (
get_team_service, create_team, get_team,
update_team, delete_team, list_teams
)

from main import app

client = TestClient(app)
Expand Down Expand Up @@ -61,7 +58,7 @@ def test_update_team(team_data, headers):
mock_service = MockService.return_value
mock_service.update_team.return_value = team_data

response = client.put(f"/teams/{team_data['team_id']}", json=team_data, headers=headers)
response = client.put(f"/api/teams/{team_data['team_id']}", json=team_data, headers=headers)

assert response.status_code == 200

Expand All @@ -74,7 +71,7 @@ def test_delete_team(team_data, headers):
mock_service = MockService.return_value
mock_service.delete_team.return_value = True

response = client.delete(f"/teams/{team_data['team_id']}", headers=headers)
response = client.delete(f"/api/teams/{team_data['team_id']}", headers=headers)

assert response.status_code == 200

Expand All @@ -87,8 +84,21 @@ def test_list_teams(headers):
mock_service = MockService.return_value
mock_service.list_teams.return_value = []

response = client.get("/teams", headers=headers)
response = client.get("/api/teams", headers=headers)

assert response.status_code == 200

mock_service.list_teams.assert_called_once()

def test_get_team_users(user_team_data):
with patch('rag_solution.services.user_team_service.UserTeamService') as MockService:
mock_service = MockService.return_value
mock_service.get_team_users.return_value = []

response = client.get(f"/api/teams/{user_team_data['team_id']}")

assert response.status_code == 200

mock_service.list_teams.assert_called_once()
mock_service.get_team_users.assert_called_once_with(
user_team_data["team_id"]
)
Loading
Loading