Skip to content

Commit

Permalink
test: add unit tests and coverage configuration (cyclotruc#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohamed Azerkane <mohamed.azerkane@cure51.com>
  • Loading branch information
ebawen and MohamedAzerkane authored Dec 14, 2024
1 parent 9d7019e commit 823f01b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[run]
source = src
omit =
src/tests/*
src/*/__init__.py

[report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
if __name__ == .__main__.:
pass
raise ImportError
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ You can also replace `hub` with `ingest` in any github url to access the corespo
- FastAPI - Backend framework
- [apianalytics.dev](https://www.apianalytics.dev/) - Usage tracking

## 📦 Running Tests

To run the tests, first install the test dependencies:
```bash
pip install -r requirements.txt
```

Then run the tests with coverage:
```bash
cd src
pytest --cov
```

To generate a coverage HTML report:
```bash
pytest --cov --cov-report=html
```
The report will be available in `htmlcov/index.html`

## 📦 Installation

1. Clone the repository:
Expand Down
7 changes: 7 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[pytest]
pythonpath = src
testpaths = src/tests
asyncio_mode = auto

# Coverage configuration
addopts = --cov=utils --cov=ingest --cov-report=term-missing --cov-report=html
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ fastapi[standard]
uvicorn
fastapi-analytics
slowapi
tokencost
tokencost
pytest
pytest-asyncio
pytest-cov
8 changes: 8 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
import sys

# Get the absolute path of the src directory
src_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Add the path to PYTHONPATH
sys.path.insert(0, src_path)
27 changes: 27 additions & 0 deletions src/tests/test_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from utils.clone import clone_repo
from unittest.mock import patch, AsyncMock

@pytest.mark.asyncio
async def test_clone_repo_with_commit():
query = {
'commit': 'a' * 40, # Simulating a valid commit hash
'branch': 'main',
'url': 'https://github.com/user/repo',
'local_path': '/tmp/repo'
}
with patch('asyncio.create_subprocess_exec', new_callable=AsyncMock) as mock_exec:
await clone_repo(query)
assert mock_exec.call_count == 2 # Ensure both clone and checkout are called

@pytest.mark.asyncio
async def test_clone_repo_without_commit():
query = {
'commit': None,
'branch': 'main',
'url': 'https://github.com/user/repo',
'local_path': '/tmp/repo'
}
with patch('asyncio.create_subprocess_exec', new_callable=AsyncMock) as mock_exec:
await clone_repo(query)
assert mock_exec.call_count == 1 # Ensure only clone is called
17 changes: 17 additions & 0 deletions src/tests/test_parse_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from utils.parse_url import parse_url

def test_parse_url_valid():
url = "https://github.com/user/repo"
max_file_size = 100
result = parse_url(url, max_file_size)
assert result["user_name"] == "user"
assert result["repo_name"] == "repo"
assert result["url"] == "https://github.com/user/repo"
assert result["max_file_size"] == 100 * 1024

def test_parse_url_invalid():
url = "https://invalid.com/user/repo"
max_file_size = 100
with pytest.raises(ValueError, match="Invalid GitHub URL"):
parse_url(url, max_file_size)

0 comments on commit 823f01b

Please sign in to comment.