-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/add unit tests #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
498c66e
add pytest to dev dep
nikilok 151f8d5
add tests for search service
nikilok 3325170
add ci.yml to run tests in ci,update readme and update the lock file
nikilok 1119a57
fix formatting issue
nikilok 1afa245
generalize the fixture using contextlib decorator and passing in the …
nikilok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,5 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| # Ensure the app module is importable for all tests | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | ||
nikilok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,62 @@ | ||
| import contextlib | ||
| from unittest.mock import patch | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from app.services.search import search_companies | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_data(): | ||
| return pd.DataFrame( | ||
| { | ||
| "Organisation Name": ["Foo Company", "Xyz Motion", "Test Company"], | ||
| "Town/City": ["London", "London", "Manchester"], | ||
| "County": ["Greater London", "Greater London", "Greater Manchester"], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| # Generalized fixture factory for patching skilled_worker_data_current | ||
| @pytest.fixture | ||
| def patch_skilled_worker_data_current(): | ||
| @contextlib.contextmanager | ||
| def _patch(data): | ||
| with patch( | ||
| "app.services.search.skilled_worker_data_current" | ||
| ) as mock_skilled_worker_data_current: | ||
| mock_skilled_worker_data_current.__getitem__.side_effect = data.__getitem__ | ||
| mock_skilled_worker_data_current.loc.__getitem__.side_effect = ( | ||
| data.loc.__getitem__ | ||
| ) | ||
| yield mock_skilled_worker_data_current | ||
|
|
||
| return _patch | ||
|
|
||
|
|
||
| def test_exact_match(patch_skilled_worker_data_current, mock_data): | ||
| with patch_skilled_worker_data_current(mock_data): | ||
| results = search_companies("Foo Company", threshold=90) | ||
| assert any("Foo Company" in r.Organisation_Name for r in results) | ||
|
|
||
|
|
||
| def test_fuzzy_match(patch_skilled_worker_data_current, mock_data): | ||
| with patch_skilled_worker_data_current(mock_data): | ||
| results = search_companies("Foo", threshold=70) | ||
| assert any("Foo Company" in r.Organisation_Name for r in results) | ||
|
|
||
|
|
||
| def test_no_match(patch_skilled_worker_data_current, mock_data): | ||
| with patch_skilled_worker_data_current(mock_data): | ||
| results = search_companies("Nonexistent Company", threshold=90) | ||
| assert not results | ||
|
|
||
|
|
||
| def test_nan_handling(patch_skilled_worker_data_current): | ||
| nan_data = pd.DataFrame( | ||
| {"Organisation Name": [None], "Town/City": [None], "County": [None]} | ||
| ) | ||
| with patch_skilled_worker_data_current(nan_data): | ||
| results = search_companies("anything", threshold=0) | ||
| assert results[0].Organisation_Name == "" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.