Skip to content

Commit

Permalink
Merge pull request #9 from viktorvorobev/pr-fix-python
Browse files Browse the repository at this point in the history
Fix Python CI
  • Loading branch information
viktorvorobev authored Jun 15, 2024
2 parents 2ecc6a2 + d8fa5cf commit 46da0c4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/python-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ jobs:
defaults:
run:
working-directory: ./py
strategy:
matrix:
command: [lint, style-check, type-check, test]
steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Upgrade PIP
run: python -m pip install --upgrade pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Lint with flake8
run: make style-check
- name: Type check with mypy
run: make type-check
- name: Lint with pylint
run: make lint
- name: Test
run: make test
run: pip install -e .[dev]
- name: Run ${{ matrix.command }}
run: make ${{ matrix.command }}
4 changes: 2 additions & 2 deletions py/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ type-check:
mypy algo

lint:
pylint --fail-under=10 algo tests --max-line-length=$(LINE_LENGTH) --load-plugins pylint_quotes
ruff check .

style-check:
flake8 algo tests --max-line-length=$(LINE_LENGTH)
ruff format . --check

test:
pytest tests
4 changes: 1 addition & 3 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ authors = [
[project.optional-dependencies]
dev = [
"pytest",
"pylint",
"pylint-quotes",
"flake8",
"ruff",
"mypy",
]

Expand Down
43 changes: 32 additions & 11 deletions py/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for search functions
"""

import random
from dataclasses import dataclass

Expand All @@ -15,44 +16,64 @@ class FuncToTest:
"""
Data class to hold test function with some test parameters
"""

func: callable
sort_required: bool


SEARCH_FUNCTIONS = [
pytest.param(FuncToTest(func=search.linear_search, sort_required=False), id='linear_search'),
pytest.param(FuncToTest(func=search.binary_search, sort_required=True), id='binary_search'),
pytest.param(FuncToTest(func=search.binary_search_recursive, sort_required=True), id='binary_search_recursive'),
pytest.param(
FuncToTest(func=search.linear_search, sort_required=False),
id="linear_search",
),
pytest.param(
FuncToTest(func=search.binary_search, sort_required=True),
id="binary_search",
),
pytest.param(
FuncToTest(func=search.binary_search_recursive, sort_required=True),
id="binary_search_recursive",
),
]


@pytest.fixture(name='search_func', params=SEARCH_FUNCTIONS)
@pytest.fixture(name="search_func", params=SEARCH_FUNCTIONS)
def search_func_fixture(request):
"""Fixture that returns test function with its meta information"""
return request.param


@pytest.fixture(name='data_sample',
params=[10, 11, 1_000_000, 1_000_001],
ids=['small_even_len', 'small_odd_len', 'big_even_len', 'big_odd_len'])
@pytest.fixture(
name="data_sample",
params=[10, 11, 1_000_000, 1_000_001],
ids=["small_even_len", "small_odd_len", "big_even_len", "big_odd_len"],
)
def data_sample_fixture(request):
"""Fixture that generates data sample"""
return list(random.sample(range(-1 * MAX_VALUE, MAX_VALUE), request.param))


@pytest.fixture(name='should_be_found', params=[True, False], ids=['should_found', 'should_not_found'])
@pytest.fixture(
name="should_be_found",
params=[True, False],
ids=["should_found", "should_not_found"],
)
def should_be_found_fixture(request):
"""Fixture flag that shows whether target value should be found in test data array or not"""
return request.param


@pytest.fixture(name='should_be_sorted', params=[True, False], ids=['should_be_sorted', 'should_not_be_sorted'])
@pytest.fixture(
name="should_be_sorted",
params=[True, False],
ids=["should_be_sorted", "should_not_be_sorted"],
)
def should_be_sorted_fixture(request):
"""Fixture flag that shows whether test data array should be sorted or not"""
return request.param


@pytest.fixture(name='test_data')
@pytest.fixture(name="test_data")
def test_data_fixture(should_be_found, should_be_sorted, data_sample, search_func):
"""
Fixture that prepares test data
Expand All @@ -62,7 +83,7 @@ def test_data_fixture(should_be_found, should_be_sorted, data_sample, search_fun
:return: array to search in, target value to search, expected result
"""
if not should_be_sorted and search_func.sort_required:
pytest.skip('Skip test due to unsorted input and required sorted data')
pytest.skip("Skip test due to unsorted input and required sorted data")

if should_be_sorted:
data_sample.sort()
Expand Down
32 changes: 20 additions & 12 deletions py/tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,41 @@
MAX_VALUE = 1_000_000

SORT_FUNCTIONS = [
pytest.param(sort.bubble_sort, id='bubble_sort'),
pytest.param(sort.merge_sort, id='merge_sort'),
pytest.param(sort.quick_sort, id='quick_sort'),
pytest.param(sort.select_sort, id='select_sort'),
pytest.param(sort.bubble_sort, id="bubble_sort"),
pytest.param(sort.merge_sort, id="merge_sort"),
pytest.param(sort.quick_sort, id="quick_sort"),
pytest.param(sort.select_sort, id="select_sort"),
]


@pytest.fixture(name='sort_func', params=SORT_FUNCTIONS)
@pytest.fixture(name="sort_func", params=SORT_FUNCTIONS)
def sort_func_fixture(request):
"""Fixture that returns test function with its meta information"""
return request.param


SAMPLE_LENS = [0, 1, 2, 3, 4, 10, 11, ]
SAMPLE_LENS = [
0,
1,
2,
3,
4,
10,
11,
]


@pytest.fixture(name='data_sample',
params=SAMPLE_LENS,
ids=[f'len_{i}' for i in SAMPLE_LENS])
@pytest.fixture(
name="data_sample", params=SAMPLE_LENS, ids=[f"len_{i}" for i in SAMPLE_LENS]
)
def data_sample_fixture(request):
"""Fixture that generates array to sort"""
if not request.param:
return []
return list(random.sample(range(-1 * MAX_VALUE, MAX_VALUE), request.param))


@pytest.fixture(name='test_data')
@pytest.fixture(name="test_data")
def test_data_fixture(data_sample, sort_func):
"""
Fixture that prepares test data
Expand All @@ -50,7 +58,7 @@ def test_data_fixture(data_sample, sort_func):
def test_search(test_data):
"""Test function that covers all cases with sort algorithms"""
sort_func, data, expected_result = test_data
print(f'{data = }')
print(f"{data = }")
result = sort_func(data)
print(f'{result = }')
print(f"{result = }")
assert expected_result == result

0 comments on commit 46da0c4

Please sign in to comment.