Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Create executable
on: [push]

jobs:
lint:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
Expand Down
27 changes: 2 additions & 25 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: Pylint
name: Pre-commit

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
tool: [pylint, ruff, mypy, pyproject-fmt, eslint, prettier]
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.12
Expand All @@ -23,24 +20,4 @@ jobs:
run: |
source .venv/bin/activate
pre-commit install
pre-commit run ${{ matrix.tool }} --all-files
test:
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install uv
uv sync
- name: Run tests
run: |
source .venv/bin/activate
# Do not run test_api.py
pytest
pytest --cov=backend --cov-report=xml --cov-report=html
pre-commit run --all-files
27 changes: 27 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Tests and coverage on python

on: [push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install uv
uv sync
- name: Run tests
run: |
source .venv/bin/activate
pytest --cov=backend --cov-report=xml:coverage.xml
- name: Get Coverage
uses: orgoro/coverage@v3.2
with:
coverageFile: coverage.xml
token: ${{ secrets.GITHUB_TOKEN }}
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ repos:
types: [python]
files: ^backend/.*
require_serial: true
- id: coverage-branch
name: Generate coverage report
entry: pytest --cov=backend --cov-report=xml --cov-fail-under=60 --cov-branch
language: system
types: [python]
files: ^backend/.*
require_serial: true
- id: coverage-lines
name: Generate coverage report
entry: pytest --cov=backend --cov-report=xml --cov-fail-under=60
language: system
types: [python]
files: ^backend/.*
require_serial: true
21 changes: 21 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""This file contains the pytest fixture to run the server before tests.
It ensures that the server is running before executing any tests that require it.
"""

import subprocess
import sys
import time
from collections.abc import Generator
from typing import Any

import pytest


@pytest.fixture(scope="session", autouse=True)
def run_server() -> Generator[Any, None, None]:
"""Fixture to run the server before tests."""
with subprocess.Popen([sys.executable, "main.py", "--quiet"]) as proc:
time.sleep(2)
yield
proc.terminate()
proc.wait()
4 changes: 0 additions & 4 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
It tests the functionality of the endpoints that retrieve finger positions from musical notes.
"""

import pytest
import requests

URL = "http://localhost:8000"

# Ensure the server is running before executing these tests
pytestmark = pytest.mark.skip("Skipping tests that require a running server")


def test_get_best_pos_from_notes() -> None:
"""Test the retrieval of the best position from musical notes."""
Expand Down
50 changes: 50 additions & 0 deletions backend/tests/test_neck_instrument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
This is the test file for the neck instrument module.
It tests the functionality of the NeckInstrument class and its methods.
"""

from backend.src.instruments.neck_instrument import NeckInstrument
from backend.src.positions.neck_position import NeckPosition
from backend.src.utils.note2num import note2num

untuned_strings = ["E2", "A2", "D3", "G3", "B3", "F4"]
untuned_guitar = NeckInstrument(
name="Test Untuned Guitar",
open_strings=untuned_strings,
number_of_frets=14,
)
notes = [note2num(note) for note in ["C4", "E4", "G4"]]
possible_positions = untuned_guitar.possible_positions(notes)


def test_neck_instrument_initialization() -> None:
"""Test the initialization of a NeckInstrument object."""
assert untuned_guitar.name == "Test Untuned Guitar"
assert untuned_guitar.open_strings == [note2num(s) for s in untuned_strings]
assert untuned_guitar.number_of_frets == 14


def test_neck_instrument_possible_positions() -> None:
"""Test the possible positions method of NeckInstrument."""
assert isinstance(possible_positions, list)
assert all(isinstance(pos, NeckPosition) for pos in possible_positions)
assert len(possible_positions) > 0
assert possible_positions[0].fingers != [0, 0, 0]
# assert placements are 3 digits numbers
assert all(len(str(placement)) == 3 for placement in possible_positions[0].placements)


def test_neck_instrument_is_valid_position() -> None:
"""Test the is_valid_position method of NeckInstrument."""
valid_positions = [pos for pos in possible_positions if untuned_guitar.is_valid_position(pos)]
assert len(valid_positions) <= len(possible_positions)
# valid positions must have different strings
assert all(len(set(pos.strings)) == len(pos.strings) for pos in valid_positions)


def test_neck_instrument_position_cost() -> None:
"""Test the position_cost method of NeckInstrument."""
position = possible_positions[0]
cost = untuned_guitar.position_cost(position)
assert isinstance(cost, int)
assert cost >= 0
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
It also opens the frontend in a web browser.
"""

import sys
import webbrowser
from pathlib import Path

Expand All @@ -14,5 +15,6 @@

frontend_dir = Path(__file__).parent / "frontend"
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="static")
webbrowser.open("http://localhost:8000")
if "--quiet" not in sys.argv:
webbrowser.open("http://localhost:8000")
uvicorn.run(app, host="localhost", port=8000)
Loading