Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ htmlcov/
.coverage
.coverage.*
.cache
.pytest_cache/
nosetests.xml
coverage.xml
*.cover
Expand Down Expand Up @@ -104,3 +105,13 @@ ENV/
inputs/
log/
data/

# Claude Code settings
.claude/*

# IDE files
.vscode/
.idea/
*.swp
*.swo
*~
4,400 changes: 4,400 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[tool.poetry]
name = "ml-algorithms"
version = "0.1.0"
description = "A collection of machine learning algorithms and examples"
authors = ["Your Name <your.email@example.com>"]
readme = "README.md"
packages = [{include = "basic_algorithm"}, {include = "RL"}, {include = "keras"}, {include = "nlp"}, {include = "number_recognization"}, {include = "opencv"}, {include = "sklearn"}, {include = "tensorflow"}]

[tool.poetry.dependencies]
python = ">=3.8,<3.12"
numpy = "^1.21.0"
pandas = "^1.3.0"
matplotlib = "^3.4.0"
scikit-learn = "^1.0.0"
tensorflow = "^2.11.0"
opencv-python = "^4.5.0"
nltk = "^3.6.0"

[tool.poetry.group.test.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.0"


[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"--strict-markers",
"--strict-config",
"--verbose",
"--cov=basic_algorithm",
"--cov=RL",
"--cov=keras",
"--cov=nlp",
"--cov=number_recognization",
"--cov=opencv",
"--cov=sklearn",
"--cov=tensorflow",
"--cov-report=term-missing",
"--cov-report=html:htmlcov",
"--cov-report=xml:coverage.xml",
"--cov-fail-under=80"
]
markers = [
"unit: Unit tests",
"integration: Integration tests",
"slow: Slow running tests"
]
filterwarnings = [
"ignore::UserWarning",
"ignore::DeprecationWarning"
]

[tool.coverage.run]
source = ["basic_algorithm", "RL", "keras", "nlp", "number_recognization", "opencv", "sklearn", "tensorflow"]
omit = [
"*/tests/*",
"*/test_*",
"*/__pycache__/*",
"*/migrations/*",
"*/venv/*",
"*/.venv/*"
]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise AssertionError",
"raise NotImplementedError",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:"
]

[tool.coverage.html]
directory = "htmlcov"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file added tests/__init__.py
Empty file.
113 changes: 113 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Shared pytest fixtures and configuration for the test suite.
"""

import pytest
import tempfile
import shutil
from pathlib import Path
from unittest.mock import Mock, MagicMock


@pytest.fixture
def temp_dir():
"""Create a temporary directory for test files."""
temp_path = tempfile.mkdtemp()
yield Path(temp_path)
shutil.rmtree(temp_path)


@pytest.fixture
def mock_config():
"""Create a mock configuration object."""
config = Mock()
config.learning_rate = 0.01
config.epochs = 100
config.batch_size = 32
config.test_size = 0.2
config.random_state = 42
return config


@pytest.fixture
def sample_data():
"""Generate sample training data for testing."""
try:
import numpy as np
np.random.seed(42)
X = np.random.rand(100, 4)
y = np.random.randint(0, 3, 100)
return X, y
except ImportError:
return [[1, 2, 3, 4]] * 100, [0, 1, 2] * 33 + [0]


@pytest.fixture
def sample_regression_data():
"""Generate sample regression data for testing."""
try:
import numpy as np
np.random.seed(42)
X = np.random.rand(100, 2)
y = X[:, 0] * 2 + X[:, 1] * 3 + np.random.normal(0, 0.1, 100)
return X, y
except ImportError:
return [[1, 2]] * 100, [5.0] * 100


@pytest.fixture
def mock_model():
"""Create a mock machine learning model."""
model = MagicMock()
model.fit.return_value = model
model.predict.return_value = [0, 1, 2]
model.score.return_value = 0.85
return model


@pytest.fixture
def sample_image():
"""Create a sample image array for computer vision tests."""
try:
import numpy as np
return np.random.randint(0, 256, (28, 28, 1), dtype=np.uint8)
except ImportError:
return [[[128]] * 28] * 28


@pytest.fixture
def sample_text_data():
"""Create sample text data for NLP tests."""
return [
"This is a positive example.",
"This is a negative example.",
"Another positive text sample.",
"Another negative text sample."
]


@pytest.fixture
def mock_file_path(temp_dir):
"""Create a mock file path in temporary directory."""
file_path = temp_dir / "test_file.txt"
file_path.write_text("test content")
return str(file_path)


@pytest.fixture
def cleanup_files():
"""Fixture to track and cleanup files created during tests."""
created_files = []

def track_file(filepath):
created_files.append(filepath)
return filepath

yield track_file

# Cleanup
for filepath in created_files:
try:
Path(filepath).unlink(missing_ok=True)
except Exception:
pass
Empty file added tests/integration/__init__.py
Empty file.
83 changes: 83 additions & 0 deletions tests/test_infrastructure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Validation tests to verify the testing infrastructure is working correctly.
"""

import pytest
import sys
from pathlib import Path


def test_pytest_is_working():
"""Basic test to verify pytest is working."""
assert True


def test_python_version():
"""Test that we're running on a supported Python version."""
assert sys.version_info >= (3, 8)


def test_project_structure():
"""Test that the expected project structure exists."""
project_root = Path(__file__).parent.parent

# Check main directories exist
assert (project_root / "basic_algorithm").exists()
assert (project_root / "RL").exists()
assert (project_root / "tests").exists()

# Check test directories exist
assert (project_root / "tests" / "unit").exists()
assert (project_root / "tests" / "integration").exists()


def test_imports_work():
"""Test that basic imports work."""
try:
import pytest
assert True
except ImportError as e:
pytest.fail(f"Failed to import required packages: {e}")


@pytest.mark.unit
def test_unit_marker():
"""Test that the unit marker works."""
assert True


@pytest.mark.integration
def test_integration_marker():
"""Test that the integration marker works."""
assert True


@pytest.mark.slow
def test_slow_marker():
"""Test that the slow marker works."""
assert True


def test_fixtures_available(temp_dir, mock_config, sample_data):
"""Test that common fixtures are available."""
assert temp_dir.exists()
assert hasattr(mock_config, 'learning_rate')
X, y = sample_data
assert len(X) == len(y)


class TestCoverageSetup:
"""Test class to verify coverage setup."""

def test_class_based_tests(self):
"""Test that class-based tests are discovered."""
assert True

def test_method_coverage(self):
"""Test method for coverage verification."""
result = self._helper_method()
assert result == "tested"

def _helper_method(self):
"""Helper method to test coverage of private methods."""
return "tested"
Empty file added tests/unit/__init__.py
Empty file.