Skip to content

Commit 823f01b

Browse files
test: add unit tests and coverage configuration (#28)
Co-authored-by: Mohamed Azerkane <mohamed.azerkane@cure51.com>
1 parent 9d7019e commit 823f01b

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

.coveragerc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[run]
2+
source = src
3+
omit =
4+
src/tests/*
5+
src/*/__init__.py
6+
7+
[report]
8+
exclude_lines =
9+
pragma: no cover
10+
def __repr__
11+
raise NotImplementedError
12+
if __name__ == .__main__.:
13+
pass
14+
raise ImportError

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ You can also replace `hub` with `ingest` in any github url to access the corespo
2222
- FastAPI - Backend framework
2323
- [apianalytics.dev](https://www.apianalytics.dev/) - Usage tracking
2424

25+
## 📦 Running Tests
26+
27+
To run the tests, first install the test dependencies:
28+
```bash
29+
pip install -r requirements.txt
30+
```
31+
32+
Then run the tests with coverage:
33+
```bash
34+
cd src
35+
pytest --cov
36+
```
37+
38+
To generate a coverage HTML report:
39+
```bash
40+
pytest --cov --cov-report=html
41+
```
42+
The report will be available in `htmlcov/index.html`
43+
2544
## 📦 Installation
2645

2746
1. Clone the repository:

pytest.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[pytest]
2+
pythonpath = src
3+
testpaths = src/tests
4+
asyncio_mode = auto
5+
6+
# Coverage configuration
7+
addopts = --cov=utils --cov=ingest --cov-report=term-missing --cov-report=html

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ fastapi[standard]
22
uvicorn
33
fastapi-analytics
44
slowapi
5-
tokencost
5+
tokencost
6+
pytest
7+
pytest-asyncio
8+
pytest-cov

src/tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
import sys
3+
4+
# Get the absolute path of the src directory
5+
src_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
6+
7+
# Add the path to PYTHONPATH
8+
sys.path.insert(0, src_path)

src/tests/test_clone.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from utils.clone import clone_repo
3+
from unittest.mock import patch, AsyncMock
4+
5+
@pytest.mark.asyncio
6+
async def test_clone_repo_with_commit():
7+
query = {
8+
'commit': 'a' * 40, # Simulating a valid commit hash
9+
'branch': 'main',
10+
'url': 'https://github.com/user/repo',
11+
'local_path': '/tmp/repo'
12+
}
13+
with patch('asyncio.create_subprocess_exec', new_callable=AsyncMock) as mock_exec:
14+
await clone_repo(query)
15+
assert mock_exec.call_count == 2 # Ensure both clone and checkout are called
16+
17+
@pytest.mark.asyncio
18+
async def test_clone_repo_without_commit():
19+
query = {
20+
'commit': None,
21+
'branch': 'main',
22+
'url': 'https://github.com/user/repo',
23+
'local_path': '/tmp/repo'
24+
}
25+
with patch('asyncio.create_subprocess_exec', new_callable=AsyncMock) as mock_exec:
26+
await clone_repo(query)
27+
assert mock_exec.call_count == 1 # Ensure only clone is called

src/tests/test_parse_url.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from utils.parse_url import parse_url
3+
4+
def test_parse_url_valid():
5+
url = "https://github.com/user/repo"
6+
max_file_size = 100
7+
result = parse_url(url, max_file_size)
8+
assert result["user_name"] == "user"
9+
assert result["repo_name"] == "repo"
10+
assert result["url"] == "https://github.com/user/repo"
11+
assert result["max_file_size"] == 100 * 1024
12+
13+
def test_parse_url_invalid():
14+
url = "https://invalid.com/user/repo"
15+
max_file_size = 100
16+
with pytest.raises(ValueError, match="Invalid GitHub URL"):
17+
parse_url(url, max_file_size)

0 commit comments

Comments
 (0)