Skip to content

Commit

Permalink
Merge pull request #19 from pranaychandekar/tests
Browse files Browse the repository at this point in the history
added unit test workflow and integrated with codecov
  • Loading branch information
pranaychandekar authored Oct 24, 2023
2 parents e640177 + a334331 commit 8aedb64
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
74 changes: 74 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Unit Tests

on:
push:
branches:
- master
# file paths to consider in the event
paths:
- 'conf/**'
- 'src/**'
- 'tests/**'
- 'docs/**'
- '!**.md'
- '.github/workflows/**'
pull_request:
branches:
- master
# file paths to consider in the event
paths:
- 'conf/**'
- 'src/**'
- 'tests/**'
- 'docs/**'
- '!**.md'
- '.github/workflows/**'
workflow_dispatch:

jobs:
unit-test-main:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
fail-fast: false
steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
shell: bash
run: |
python -m pip install --upgrade pip
pip install wheel
pip3 install -r requirements.txt
- name: Remove coverage xml
run: |
rm -rf ./coverage.xml
- name: Unit Tests
timeout-minutes: 2
shell: bash
run: |
export IS_CI=true
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
python3 -m pytest
- name: Print coverage report
timeout-minutes: 2
shell: bash
run: |
coverage run -m pytest
coverage report -m
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}


Empty file.
4 changes: 2 additions & 2 deletions src/services/classifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Classifier Singleton
"""
import fasttext as ft
import fasttext

from src.domain.constants import MODEL_PATH
from src.configurations.app_configs import AppConfigs
Expand All @@ -24,7 +24,7 @@ def __init__(self):
model_path = AppConfigs().get_instance().get(MODEL_PATH)

# Step 02: Load the model.
self._model = ft.load_model(model_path)
self._model = fasttext.load_model(model_path)

Logger().get_instance().info(
"Finished loading the classifier model: %s", self.get_instance()
Expand Down
14 changes: 7 additions & 7 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from fastapi.testclient import TestClient
# from fastapi.testclient import TestClient

from app import app
# from app import app

client = TestClient(app)
# client = TestClient(app)


def test_build():
response = client.get("/")
assert response.status_code == 200
assert response.json()["status"] == 200
# def test_build():
# response = client.get("/")
# assert response.status_code == 200
# assert response.json()["status"] == 200
2 changes: 1 addition & 1 deletion tests/test_routers/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def test_get_response(self, mock_fasttext, mock_prediction_service, mock_logger)
loop = asyncio.get_event_loop()
result = loop.run_until_complete(get_response(prediction_service_request))

self.assertEqual(result.status_code, 200)
self.assertEqual(type(result), dict)
13 changes: 5 additions & 8 deletions tests/test_services/test_prediction_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@ class TestPredictionService(unittest.TestCase):
"""
logging.basicConfig(level=logging.FATAL)

@patch("src.services.prediction_service.Logger")
@patch("src.services.prediction_service.PredictionService")
def test_response(self, mock_prediction_service, mock_logger):
def test_response(self, mock_prediction_service):
mock_prediction_service.predict_label.return_value = "knives", 86.64
mock_logger.get_instance.return_value = logging

text = "Don't let the knives sink :P"
result = PredictionService.get_response(text)
self.assertEqual(type(result.get("label")), str)

@patch("src.services.prediction_service.Logger")
@patch("src.services.prediction_service.Classifier")
def test_predict_label(self, mock_classifier, mock_logger):
def test_predict_label(self, mock_classifier):
mock_classifier = Mock()

mock_logger.get_instance.return_value = logging

text = "Don't let the knives sink :P"
label, confidence = PredictionService.predict_label(text)

print(label, confidence)

self.assertEqual(type(label), str)
self.assertEqual(type(confidence), float)
self.assertEqual(type(confidence), type(confidence))

0 comments on commit 8aedb64

Please sign in to comment.