Skip to content

Commit

Permalink
make pytest raise tests more specific, point mypy and flake8 to speci…
Browse files Browse the repository at this point in the history
…fic directories
  • Loading branch information
ModischFabrications committed Apr 11, 2024
1 parent 000518e commit c6c75eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ jobs:
- name: Lint with flake8 🔍
run: |
# stop the build if there are Python syntax errors or undefined names
pipenv run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
pipenv run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
pipenv run flake8 tests/ app/ --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings
pipenv run flake8 tests/ app/ --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics
- name: Test with mypy 🔍
run: |
# stop the build if there are typing errors
pipenv run python -m mypy .
pipenv run python -m mypy app/ tests/
- name: Test with pytest 🔍
run: |
Expand Down
24 changes: 19 additions & 5 deletions tests/solver/data/test_Job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from pydantic import ValidationError

from app.solver.data.Job import Job, INS, NS
from app.solver.data.Job import QNS
Expand Down Expand Up @@ -61,28 +62,41 @@ def test_invalid(testjob_s):


def test_too_long(testjob_s):
with pytest.raises(ValueError):
with pytest.raises(ValidationError, match='target sizes longer than the stock'):
_ = Job(
max_length=100,
stocks=(INS(length=100),),
cut_width=5,
required=(QNS(length=101, quantity=1),)
)


def test_too_long_multi(testjob_s):
with pytest.raises(ValidationError, match='target sizes longer than the stock'):
_ = Job(stocks=(INS(length=100, quantity=100), INS(length=500)), cut_width=10,
required=(QNS(length=500, quantity=2), QNS(length=1000, quantity=2)))


def test_solver_multi_overflow():
with pytest.raises(ValueError):
with pytest.raises(ValueError, match='more targets than the stock available'):
_ = Job(stocks=(INS(length=1100, quantity=1), INS(length=500, quantity=1)), cut_width=10,
required=(QNS(length=500, quantity=2), QNS(length=1000, quantity=2)))


def test_solver_skip_too_short():
job = Job(stocks=(INS(length=100, quantity=10), INS(length=1000)), cut_width=10,
required=(QNS(length=500, quantity=2), QNS(length=1000, quantity=2)))

assert list(job.iterate_stocks()) == [NS(length=1000), NS(length=1000), NS(length=1000)]


def test_to_json():
job = Job(
stocks=(INS(length=1200),),
cut_width=5,
required=[
required=(
QNS(length=300, quantity=4, name="Part1"),
QNS(length=200, quantity=3),
],
),
)
assert (
job.model_dump_json(exclude_defaults=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/solver/data/test_Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_constructor(testjob_s):
def test_invalid(testjob_s):
job = testjob_s

with pytest.raises(ValueError):
with pytest.raises(ValueError, match='greater than 0'):
_ = Result(
job=job,
solver_type=SolverType.FFD,
Expand All @@ -49,7 +49,7 @@ def test_invalid(testjob_s):
trimming=1000
),))

with pytest.raises(ValueError):
with pytest.raises(ValueError, match='Field required'):
_ = Result(
job=job,
# no solver
Expand All @@ -63,7 +63,7 @@ def test_invalid(testjob_s):
trimming=1000
),))

with pytest.raises(ValueError):
with pytest.raises(ValueError, match='no cuts'):
_ = Result(
job=job,
solver_type=SolverType.FFD,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_invalid(testjob_s, testresult_s):
reply = client.post("/solve", json=testdict)
assert reply.status_code != 200
json_result = reply.json()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match='Field required'):
assert Result.model_validate(json_result) == testresult_s


Expand Down

0 comments on commit c6c75eb

Please sign in to comment.