-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- DirectoryWatcher is now integrated in the API. If a new article is added to the "data_from_A" directory, it will be detected, processed and outputted into the entity_mentions.json file - Fixed up tests - Removed unused imports in main.py
- Loading branch information
1 parent
05b8e40
commit a476aa8
Showing
5 changed files
with
107 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
import sys | ||
from unittest.mock import patch | ||
|
||
sys.path.append(".") | ||
from main import app | ||
from main import app, DIRECTORY_TO_WATCH | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_SlashEntityMentionsIsUp(): | ||
with TestClient(app) as client: | ||
res = client.get("/entitymentions") | ||
assert res.status_code == 200 | ||
client.__exit__ | ||
client.close() | ||
with patch('main.DIRECTORY_TO_WATCH', 'data_from_A/'): | ||
with TestClient(app) as client: | ||
res = client.get("/entitymentions?article=test.txt") | ||
assert res.status_code == 200 | ||
client.__exit__ | ||
client.close() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_SlashEntityMentionsReturnsJsonArray(): | ||
with TestClient(app) as client: | ||
res = client.get("/entitymentions") | ||
assert type(res.json()) == list | ||
assert type(res.json()[0]) == dict | ||
client.__exit__ | ||
client.close() | ||
with patch('main.DIRECTORY_TO_WATCH', 'data_from_A/'): | ||
with TestClient(app) as client: | ||
res = client.get("/entitymentions?article=test.txt") | ||
assert type(res.json()) == list | ||
assert type(res.json()[0]) == dict | ||
client.__exit__ | ||
client.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
from lib.DirectoryWatcher import DirectoryWatcher | ||
import asyncio | ||
|
||
@pytest.mark.asyncio | ||
async def test_on_created(): | ||
# Mock the async_callback function | ||
async_callback_mock = MagicMock() | ||
watcher = DirectoryWatcher(directory='/path/to/watch', async_callback=async_callback_mock) | ||
|
||
# Mock asyncio.run to avoid the RuntimeError | ||
with patch('asyncio.run'): | ||
# Simulate an on_created event | ||
event_mock = MagicMock(is_directory=False, src_path='/path/to/file.txt') | ||
watcher.on_created(event_mock) | ||
|
||
# Ensure that async_callback is called with the correct parameters | ||
async_callback_mock.assert_called_once_with('/path/to/file.txt') | ||
|
||
@pytest.mark.asyncio | ||
async def test_start_and_stop_watching(): | ||
# Mock the observer and async_callback | ||
observer_mock = MagicMock() | ||
async_callback_mock = MagicMock() | ||
|
||
# Create a watcher with mocked components | ||
watcher = DirectoryWatcher(directory='/path/to/watch', async_callback=async_callback_mock) | ||
watcher.observer = observer_mock | ||
|
||
# Start watching | ||
watcher_thread = watcher.start_watching() | ||
|
||
# Ensure that observer is scheduled and started | ||
observer_mock.schedule.assert_called_once() | ||
observer_mock.start.assert_called_once() | ||
|
||
# Stop watching | ||
watcher.stop_watching() | ||
|
||
# Ensure that observer is stopped and joined | ||
observer_mock.stop.assert_called_once() | ||
observer_mock.join.assert_called_once() | ||
|
||
# Ensure that the watcher thread has completed | ||
watcher_thread.join() | ||
|
||
@pytest.mark.asyncio | ||
async def test_run_once(): | ||
# Create a watcher with mocked components | ||
watcher = DirectoryWatcher(directory='/path/to/watch', async_callback=lambda x: x) | ||
|
||
# Ensure that run_once does not raise any exceptions | ||
await watcher.run_once() | ||
|
||
# You can add more tests as needed, especially for edge cases and specific behaviors. |
This file was deleted.
Oops, something went wrong.