Skip to content

Commit

Permalink
cleanup tests with fixtures and new cases; create tests for #63, #64, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ModischFabrications committed Mar 17, 2024
1 parent 1a53e58 commit b476f88
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 97 deletions.
10 changes: 10 additions & 0 deletions tests/res/in/testjob_cuts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cut_width": 10,
"max_length": 1010,
"target_sizes": [
{
"length": 500,
"quantity": 4
}
]
}
10 changes: 10 additions & 0 deletions tests/res/in/testjob_equal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cut_width": 10,
"max_length": 1000,
"target_sizes": [
{
"length": 1000,
"quantity": 4
}
]
}
29 changes: 20 additions & 9 deletions tests/res/in/testjob_l.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"cut_width": 2,
"max_length": 1500,
"target_sizes": {
"500": 11,
"300": 7,
"250": 5,
"840": 3,
"100": 12
}
"cut_width": 5,
"max_length": 2000,
"target_sizes": [
{
"length": 750,
"quantity": 5
},
{
"length": 500,
"quantity": 5
},
{
"length": 300,
"quantity": 10
},
{
"length": 100,
"quantity": 15
}
]
}
23 changes: 16 additions & 7 deletions tests/res/in/testjob_m.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{
"cut_width": 2,
"max_length": 1500,
"target_sizes": {
"500": 6,
"300": 4,
"100": 4
}
"cut_width": 5,
"max_length": 1000,
"target_sizes": [
{
"length": 500,
"quantity": 4
},
{
"length": 300,
"quantity": 3
},
{
"length": 100,
"quantity": 1
}
]
}
File renamed without changes.
10 changes: 10 additions & 0 deletions tests/res/in/testjob_zero_cuts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cut_width": 0,
"max_length": 1000,
"target_sizes": [
{
"length": 500,
"quantity": 4
}
]
}
File renamed without changes.
34 changes: 17 additions & 17 deletions tests/solver/data/test_Job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from app.solver.data.Job import Job
from app.solver.data.Job import TargetSize
from tests.test_utils import generate_testjob
from tests.test_fixtures import testjob_s


def test_iterator():
job: Job = generate_testjob()
def test_iterator(testjob_s):
job: Job = testjob_s

resulting_list = []
for length in job.iterate_sizes():
Expand All @@ -22,40 +22,40 @@ def test_iterator():
]


def test_job_equal():
job1 = generate_testjob()
job2 = generate_testjob()
def test_job_equal(testjob_s):
job1 = testjob_s
job2 = testjob_s

assert job1 == job2


def test_job_length():
job1 = generate_testjob()
def test_job_length(testjob_s):
job1 = testjob_s

assert len(job1) == 6


def test_equal_hash():
job1 = generate_testjob()
job2 = generate_testjob()
def test_equal_hash(testjob_s):
job1 = testjob_s
job2 = testjob_s

assert hash(job1) == hash(job2)


def test_valid():
job1 = generate_testjob()
def test_valid(testjob_s):
job1 = testjob_s
job1.assert_valid()


def test_invalid():
invalid_job = generate_testjob()
def test_invalid(testjob_s):
invalid_job = testjob_s
invalid_job.max_length = -1
with pytest.raises(ValueError):
invalid_job.assert_valid()


def test_too_long():
job = generate_testjob()
def test_too_long(testjob_s):
job = testjob_s
job.target_sizes.append(
TargetSize(**{"length": job.max_length + 1, "quantity": 4, "name": "too long"})
)
Expand Down
22 changes: 11 additions & 11 deletions tests/solver/data/test_Result.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

from app.solver.data.Result import Result, SolverType
from tests.test_utils import generate_testjob
from tests.test_fixtures import testjob_s


def test_constructor():
job = generate_testjob()
def test_constructor(testjob_s):
job = testjob_s
result = Result(
job=job,
solver_type=SolverType.FFD,
Expand All @@ -26,8 +26,8 @@ def test_constructor():
assert result


def test_valid():
job = generate_testjob()
def test_valid(testjob_s):
job = testjob_s
result = Result(
job=job,
solver_type=SolverType.FFD,
Expand All @@ -48,8 +48,8 @@ def test_valid():
result.assert_valid()


def test_invalid():
job = generate_testjob()
def test_invalid(testjob_s):
job = testjob_s
invalid_result = Result(
job=job,
solver_type=SolverType.FFD,
Expand All @@ -71,8 +71,8 @@ def test_invalid():
invalid_result.assert_valid()


def test_equal():
job = generate_testjob()
def test_equal(testjob_s):
job = testjob_s
result1 = Result(
job=job,
solver_type=SolverType.FFD,
Expand Down Expand Up @@ -110,8 +110,8 @@ def test_equal():
assert result1 == result2


def test_exactly():
job = generate_testjob()
def test_exactly(testjob_s):
job = testjob_s
result1 = Result(
job=job,
solver_type=SolverType.FFD,
Expand Down
2 changes: 1 addition & 1 deletion tests/solver/data/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_to_json():


def test_from_json():
json_file = Path("./tests/res/in/testjob.json")
json_file = Path("./tests/res/in/testjob_s.json")
assert json_file.exists()

with open(json_file, "r") as encoded_job:
Expand Down
33 changes: 33 additions & 0 deletions tests/solver/test_large.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from app.solver.solver import (
_solve_bruteforce,
_solve_FFD,
)
from tests.test_fixtures import *


# close to the max for bruteforce!
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD])
def test_m(testjob_m, solver):
orig_job = testjob_m.model_copy(deep=True)
solved = solver(testjob_m)

assert solved == [[(500, '')],
[(500, ''), (300, '')],
[(500, ''), (300, '')],
[(500, ''), (300, ''), (100, '')]]
assert orig_job == testjob_m


@pytest.mark.parametrize("solver", [_solve_FFD])
def test_l(testjob_l, solver):
orig_job = testjob_l.model_copy(deep=True)
solved = solver(testjob_l)

assert solved == [[(750, ''), (750, ''), (300, ''), (100, '')],
[(750, ''), (750, ''), (300, ''), (100, '')],
[(750, ''), (500, ''), (500, ''), (100, ''), (100, '')],
[(500, ''), (500, ''), (500, ''), (300, ''), (100, '')],
[(300, ''), (300, ''), (300, ''), (300, ''), (300, ''), (300, ''), (100, '')],
[(300, ''), (100, ''), (100, ''), (100, ''), (100, ''), (100, ''), (100, ''), (100, ''),
(100, ''), (100, '')]]
assert orig_job == testjob_l
42 changes: 15 additions & 27 deletions tests/solver/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
from pathlib import Path

import pytest

from app.solver.data.Job import Job
from app.solver.data.Result import Result
from app.solver.solver import (
_get_trimming,
_solve_bruteforce,
_solve_gapfill,
_solve_FFD,
distribute,
)
from tests.test_utils import generate_testjob
from tests.test_fixtures import *


def test_trimmings():
Expand All @@ -30,51 +24,45 @@ def test_trimmings_raise():
_get_trimming(1500, ((300, ""), (400, ""), (600, ""), (200, "")), 2)


def test_bruteforce():
job = generate_testjob()

orig_job = job.model_copy(deep=True)
solved = _solve_bruteforce(job)
def test_bruteforce(testjob_s):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_bruteforce(testjob_s)

assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert orig_job == job
assert orig_job == testjob_s


def test_gapfill():
job = generate_testjob()

orig_job = job.model_copy(deep=True)
solved = _solve_gapfill(job)
def test_gapfill(testjob_s):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_gapfill(testjob_s)

assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert orig_job == job

assert orig_job == testjob_s

def test_FFD():
job = generate_testjob()

orig_job = job.model_copy(deep=True)
solved = _solve_FFD(job)
def test_FFD(testjob_s):
orig_job = testjob_s.model_copy(deep=True)
solved = _solve_FFD(testjob_s)

# assert solved == [[500, 500, 200, 200], [200, 200]]
assert solved == [
[(500, "Part1"), (500, "Part1"), (200, "Part2"), (200, "Part2")],
[(200, "Part2"), (200, "Part2")],
]
assert orig_job == job
assert orig_job == testjob_s


def test_full_model():
json_job = Path("./tests/res/in/testjob.json")
json_job = Path("./tests/res/in/testjob_s.json")
assert json_job.exists()

json_result = Path("./tests/res/out/testresult.json")
json_result = Path("./tests/res/out/testresult_s.json")

with open(json_job, "r") as encoded_job:
job = Job.model_validate_json(encoded_job.read())
Expand Down
46 changes: 46 additions & 0 deletions tests/solver/test_special.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from app.solver.solver import (
_solve_bruteforce,
_solve_FFD,
)
from tests.test_fixtures import *


@pytest.mark.skip(reason="bug #63")
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD])
def test_cuts(testjob_cuts, solver):
orig_job = testjob_cuts.model_copy(deep=True)
solved = solver(testjob_cuts)

assert solved == [
[(500, ''), (500, '')],
[(500, ''), (500, '')]
]
assert orig_job == testjob_cuts


@pytest.mark.skip(reason="bug #59")
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD])
def test_zero_cuts(testjob_zero_cuts, solver):
orig_job = testjob_zero_cuts.model_copy(deep=True)
solved = solver(testjob_zero_cuts)

assert solved == [
[(500, ''), (500, '')],
[(500, ''), (500, '')]
]
assert orig_job == testjob_zero_cuts


@pytest.mark.skip(reason="bug #64")
@pytest.mark.parametrize("solver", [_solve_bruteforce, _solve_FFD])
def test_equal(testjob_equal, solver):
orig_job = testjob_equal.model_copy(deep=True)
solved = solver(testjob_equal)

assert solved == [
[(1000, '')],
[(1000, '')],
[(1000, '')],
[(1000, '')]
]
assert orig_job == testjob_equal
Loading

0 comments on commit b476f88

Please sign in to comment.