-
Notifications
You must be signed in to change notification settings - Fork 13
Issues/172 #188
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
base: dev
Are you sure you want to change the base?
Issues/172 #188
Changes from 26 commits
0af2166
d6d7c91
d8ea660
142768a
a1b66fa
794fd43
3e99ef6
28cd6ec
737e2d7
374074d
26ebf94
b52ed01
43b32f2
5c5ce15
ec4db0d
72b4975
9fa1cb8
9ae7c6b
b373667
3b07d7b
8ed0d2b
5abcd46
5a699c9
d8cc7b9
46b8b57
833fdef
4165228
5eb4d09
aaa7b7f
47ef6d7
530c29e
74e0241
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.egg-info |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ## NDIF API Service | ||
|
|
||
| ## Testing | ||
|
|
||
| install dependencies from requirements.in, ideally in a virtual environment | ||
|
||
|
|
||
| run pytests with | ||
| ```bash | ||
| pytest | ||
| ``` | ||
|
|
||
| run pytests in certain file with | ||
| ```bash | ||
| pytest -k test_app | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=61.0", "wheel"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "api" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.10" | ||
|
||
| [tool.setuptools.packages.find] | ||
| where = ["."] | ||
| include = ["src*"] | ||
|
|
||
| [tool.pytest.ini_options] | ||
| minversion = "6.0" | ||
| testpaths = ["tests"] | ||
| pythonpath = ["."] | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
|
||
| "httpx>=0.28.1", | ||
| "pytest>=8.4.0", | ||
| "pytest-asyncio>=1.3.0", | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import os | ||
| os.environ["DEV_MODE"] = "true" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You set this here, but also directly in some of the tests, was this intentional?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the two tests were specifically testing the behavior in dev mode so I wanted it to be explicit. The top level DEV_MODE is just to avoid start the db connection in db.py. In e2e testing we might want to set this to be false with a separate conftest.py |
||
| os.environ.setdefault("BROKER_URL", "redis://localhost:6379") | ||
|
|
||
| from unittest.mock import AsyncMock, Mock, patch | ||
|
|
||
| mock_redis_manager = Mock() | ||
| mock_redis_client = AsyncMock() | ||
| mock_socket_manager = Mock() | ||
|
|
||
| patch("socketio.AsyncRedisManager", return_value=mock_redis_manager).start() | ||
| patch("redis.asyncio.Redis.from_url", return_value=mock_redis_client).start() | ||
| patch("fastapi_socketio.SocketManager", return_value=mock_socket_manager).start() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import pytest | ||
| from httpx import AsyncClient, ASGITransport | ||
| from src.app import app | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ping(): | ||
| async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: | ||
| response = await client.get("/ping") | ||
| assert response.status_code == 200 | ||
| assert response.json() == "pong" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import asyncio | ||
| from unittest.mock import patch | ||
| from src.dependencies import check_hotswapping_access, validate_nnsight_version, validate_python_version, authenticate_api_key | ||
| import pytest | ||
| import os | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_validate_python_version(): | ||
| with patch("sys.version", "3.12.10"): | ||
| res = await validate_python_version("3.12") | ||
| assert res == "3.12" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_validate_nnsight_version(): | ||
| os.environ["MIN_NNSIGHT_VERSION"] = "0.5.10" | ||
| res = await validate_nnsight_version("0.5.10") | ||
| assert res == "0.5.10" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_authenticate_api_key_dev_mode(): | ||
| os.environ["DEV_MODE"] = "true" | ||
| res = await authenticate_api_key("test-key") | ||
| assert res == "test-key" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_hotswapping_access_dev_mode(): | ||
| os.environ["DEV_MODE"] = "true" | ||
| res = await check_hotswapping_access("test-key") | ||
| assert res == True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
||
|
|
||
| from src.providers.util import verify_connection | ||
|
|
||
|
|
||
| def test_verify_connection_success(): | ||
| with patch("socket.create_connection") as mock_conn: | ||
| mock_conn.return_value.__enter__.return_value = MagicMock() | ||
| assert verify_connection("127.0.0.1", 8000) is True | ||
| mock_conn.assert_called_once_with(("127.0.0.1", 8000), timeout=2) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import time | ||
| from functools import lru_cache | ||
| from src.queue.util import cache_maintainer | ||
|
||
|
|
||
|
|
||
| def test_cache_maintainer(): | ||
| calls = {"count": 0} | ||
|
|
||
| @cache_maintainer(1) # clear every 1 second | ||
| @lru_cache(None) | ||
| def compute(x): | ||
| calls["count"] += 1 | ||
| return x * 2 | ||
|
|
||
| # First call — calls function | ||
| assert compute(5) == 10 | ||
| assert calls["count"] == 1 | ||
|
|
||
| # Second call immediately — should use cache | ||
| assert compute(5) == 10 | ||
| assert calls["count"] == 1 # NO new calls | ||
|
|
||
| # Wait for 1 second → decorator clears cache | ||
| time.sleep(1.1) | ||
|
|
||
| # Next call — should trigger a new computation | ||
| assert compute(5) == 10 | ||
| assert calls["count"] == 2 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of assuming a package manager and instructing them how to install it, just directly link to pytest docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point