| 1 |
from pathlib import Path |
| 2 |
from minizinc import default_driver, Driver |
| 3 |
import subprocess |
| 4 |
import json |
| 5 |
|
| 6 |
|
| 7 |
def _run_test(model_file): |
| 8 |
p = subprocess.run( |
| 9 |
[default_driver._executable, model_file, "--json-stream", "-a"], |
| 10 |
stdin=None, |
| 11 |
stdout=subprocess.PIPE, |
| 12 |
stderr=subprocess.PIPE, |
| 13 |
) |
| 14 |
p.check_returncode() |
| 15 |
result = [json.loads(l) for l in p.stdout.splitlines(False)] |
| 16 |
messages = [m for m in result if m["type"] in ["trace", "solution"]] |
| 17 |
assert len(messages) == 4 |
| 18 |
trace = messages[0] |
| 19 |
assert trace["type"] == "trace" |
| 20 |
assert trace["message"] == {"url": "foo.html", "userData": {"n": 4}} |
| 21 |
assert messages[1]["type"] == "solution" |
| 22 |
assert messages[2]["type"] == "solution" |
| 23 |
assert messages[3]["type"] == "solution" |
| 24 |
expected = [{"x": 3, "y": 2}, {"x": 2, "y": 3}, {"x": 3, "y": 3}] |
| 25 |
actual = [s["output"]["mzn_vis_0"] for s in messages[1:]] |
| 26 |
assert len(actual) == len(expected) |
| 27 |
for sol in actual: |
| 28 |
assert sol in expected |
| 29 |
assert actual == expected |
| 30 |
|
| 31 |
|
| 32 |
def test_vis_ann(): |
| 33 |
here = Path(__file__).resolve().parent |
| 34 |
assert isinstance(default_driver, Driver) |
| 35 |
_run_test(here / "test_vis_ann.mzn") |
| 36 |
|
| 37 |
|
| 38 |
def test_vis_custom(): |
| 39 |
here = Path(__file__).resolve().parent |
| 40 |
assert isinstance(default_driver, Driver) |
| 41 |
_run_test(here / "test_vis_custom.mzn") |