From f568b8d92374e5dbfc7a2f8a7e782b8427905804 Mon Sep 17 00:00:00 2001 From: tedoaba Date: Tue, 22 Oct 2024 17:16:04 +0300 Subject: [PATCH] test added --- .github/workflows/ci-cd.yml | 32 ++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- tests/test_data_loader.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index ae1da35..2d7fbcc 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -13,14 +13,46 @@ jobs: runs-on: windows-latest steps: + # Checkout code - name: Checkout code uses: actions/checkout@v3 + # Set up Python - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.8' + # Cache pip dependencies to speed up the build + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + # Install dependencies - name: Install dependencies run: | pip install -r requirements.txt + + # Run linting with flake8 to ensure code quality + - name: Lint code + run: | + pip install flake8 + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + + # Run unit tests using pytest + - name: Run tests + run: | + pip install pytest pandas + pytest tests --maxfail=1 --disable-warnings + + # Optionally, upload test results + - name: Upload test results (optional) + if: always() + uses: actions/upload-artifact@v3 + with: + name: test-results + path: test-results.xml diff --git a/requirements.txt b/requirements.txt index 1157dd9..5417c9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,4 +21,5 @@ mlflow flask click flask_sqlalchemy -Flask-Migrate \ No newline at end of file +Flask-Migrate +pytest \ No newline at end of file diff --git a/tests/test_data_loader.py b/tests/test_data_loader.py index e69de29..cdef3bf 100644 --- a/tests/test_data_loader.py +++ b/tests/test_data_loader.py @@ -0,0 +1,33 @@ +import os +import sys +import unittest +from unittest.mock import patch +import pandas as pd + +sys.path.append(os.path.abspath('../src')) + +from src.data_loader import load_data + +class TestDataLoader(unittest.TestCase): + + @patch('pandas.read_csv') + def test_load_data(self, mock_read_csv): + # Create a mock DataFrame to be returned by pandas.read_csv + mock_df = pd.DataFrame({ + 'column1': [1, 2, 3], + 'column2': ['A', 'B', 'C'] + }) + + # Set the mock to return this DataFrame + mock_read_csv.return_value = mock_df + + # Call the load_data function + file_path = 'dummy/path/to/file.csv' + result = load_data(file_path) + + # Assertions + mock_read_csv.assert_called_once_with(file_path) # Check that read_csv was called with the correct file path + pd.testing.assert_frame_equal(result, mock_df) # Check that the result is as expected + +if __name__ == '__main__': + unittest.main()