tests: add check for multiple expected outputs
This commit is contained in:
parent
cc90b5e196
commit
592a2c661c
51
test.py
51
test.py
|
@ -35,6 +35,7 @@ from sys import stderr
|
||||||
from textwrap import indent
|
from textwrap import indent
|
||||||
from traceback import format_exception
|
from traceback import format_exception
|
||||||
from typing import Final, NamedTuple
|
from typing import Final, NamedTuple
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
import surplus
|
import surplus
|
||||||
|
|
||||||
|
@ -44,13 +45,14 @@ MINIMUM_PASS_RATE: Final[float] = 0.7 # because results can be flaky
|
||||||
|
|
||||||
class ContinuityTest(NamedTuple):
|
class ContinuityTest(NamedTuple):
|
||||||
query: str
|
query: str
|
||||||
expected: str
|
expected: list[str]
|
||||||
|
|
||||||
|
|
||||||
class TestFailure(NamedTuple):
|
class TestFailure(NamedTuple):
|
||||||
test: ContinuityTest
|
test: ContinuityTest
|
||||||
exception: Exception
|
exception: Exception
|
||||||
output: str
|
output: str
|
||||||
|
test_stderr: StringIO
|
||||||
|
|
||||||
|
|
||||||
tests: list[ContinuityTest] = [
|
tests: list[ContinuityTest] = [
|
||||||
|
@ -60,27 +62,41 @@ tests: list[ContinuityTest] = [
|
||||||
),
|
),
|
||||||
ContinuityTest(
|
ContinuityTest(
|
||||||
query="9R3J+R9 Singapore",
|
query="9R3J+R9 Singapore",
|
||||||
expected=(
|
expected=[
|
||||||
|
(
|
||||||
"Thomson Plaza\n"
|
"Thomson Plaza\n"
|
||||||
"301 Upper Thomson Road\n"
|
"301 Upper Thomson Road\n"
|
||||||
"Sin Ming, Bishan\n"
|
"Sin Ming, Bishan\n"
|
||||||
"574408\n"
|
"574408\n"
|
||||||
"Central, Singapore"
|
"Central, Singapore"
|
||||||
),
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
ContinuityTest(
|
ContinuityTest(
|
||||||
query="3RQ3+HW3 Pemping, Batam City, Riau Islands, Indonesia",
|
query="3RQ3+HW3 Pemping, Batam City, Riau Islands, Indonesia",
|
||||||
expected=("Batam\n" "Kepulauan Riau, Indonesia"),
|
expected=[
|
||||||
|
("Batam\n" "Kepulauan Riau, Indonesia"),
|
||||||
|
("Batam\n" "Sumatera, Kepulauan Riau, Indonesia"),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
ContinuityTest(
|
ContinuityTest(
|
||||||
query="St Lucia, Queensland, Australia G227+XF",
|
query="St Lucia, Queensland, Australia G227+XF",
|
||||||
expected=(
|
expected=[
|
||||||
|
(
|
||||||
"The University of Queensland\n"
|
"The University of Queensland\n"
|
||||||
"Macquarie Street\n"
|
"Macquarie Street\n"
|
||||||
"St Lucia, Greater Brisbane\n"
|
"St Lucia, Greater Brisbane\n"
|
||||||
"4072\n"
|
"4072\n"
|
||||||
"Queensland, Australia"
|
"Queensland, Australia"
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"The University of Queensland\n"
|
||||||
|
"Macquarie Street\n"
|
||||||
|
"St Lucia, Greater Brisbane, Dutton Park\n"
|
||||||
|
"4072\n"
|
||||||
|
"Queensland, Australia"
|
||||||
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
ContinuityTest(
|
ContinuityTest(
|
||||||
query="Ngee Ann Polytechnic, Singapore",
|
query="Ngee Ann Polytechnic, Singapore",
|
||||||
|
@ -95,7 +111,7 @@ tests: list[ContinuityTest] = [
|
||||||
ContinuityTest(
|
ContinuityTest(
|
||||||
query="1.3521, 103.8198",
|
query="1.3521, 103.8198",
|
||||||
expected=(
|
expected=(
|
||||||
"MacRitchie Nature Trail"
|
"MacRitchie Nature Trail\n"
|
||||||
"Central Water Catchment\n"
|
"Central Water Catchment\n"
|
||||||
"574325\n"
|
"574325\n"
|
||||||
"Central, Singapore"
|
"Central, Singapore"
|
||||||
|
@ -122,8 +138,13 @@ def main() -> int:
|
||||||
for idx, test in enumerate(tests, start=1):
|
for idx, test in enumerate(tests, start=1):
|
||||||
print(f"[{idx}/{len(tests)}] {test.query}")
|
print(f"[{idx}/{len(tests)}] {test.query}")
|
||||||
|
|
||||||
|
test_stderr = StringIO()
|
||||||
|
|
||||||
output: str = ""
|
output: str = ""
|
||||||
behaviour = surplus.Behaviour(test.query)
|
behaviour = surplus.Behaviour(
|
||||||
|
test.query,
|
||||||
|
stderr=test_stderr,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
query = surplus.parse_query(behaviour)
|
query = surplus.parse_query(behaviour)
|
||||||
|
@ -138,11 +159,11 @@ def main() -> int:
|
||||||
|
|
||||||
output = result.get()
|
output = result.get()
|
||||||
|
|
||||||
if output != test.expected:
|
if output not in test.expected:
|
||||||
raise ContinuityFailure("did not match output")
|
raise ContinuityFailure("did not match any expected outputs")
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
failures.append(TestFailure(test=test, exception=exc, output=output))
|
failures.append(TestFailure(test=test, exception=exc, output=output, stderr=test_stderr))
|
||||||
stderr.write(indent(text="(fail)", prefix=INDENT * " ") + "\n\n")
|
stderr.write(indent(text="(fail)", prefix=INDENT * " ") + "\n\n")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -163,18 +184,20 @@ def main() -> int:
|
||||||
+ (indent(text=fail.test.expected, prefix=(2 * INDENT) * " ") + "\n\n")
|
+ (indent(text=fail.test.expected, prefix=(2 * INDENT) * " ") + "\n\n")
|
||||||
+ (indent(text="Actual:", prefix=INDENT * " ") + "\n")
|
+ (indent(text="Actual:", prefix=INDENT * " ") + "\n")
|
||||||
+ (indent(text=repr(fail.output), prefix=(2 * INDENT) * " ") + "\n")
|
+ (indent(text=repr(fail.output), prefix=(2 * INDENT) * " ") + "\n")
|
||||||
+ (indent(text=fail.output, prefix=(2 * INDENT) * " "))
|
+ (indent(text=fail.output, prefix=(2 * INDENT) * " ") + "\n\n")
|
||||||
|
+ (indent(text="stderr:", prefix=INDENT * " ") + "\n")
|
||||||
|
+ (indent(text=fail.test_stderr.getvalue(), prefix=(2 * INDENT) * " "))
|
||||||
)
|
)
|
||||||
|
|
||||||
passes = len(tests) - len(failures)
|
passes = len(tests) - len(failures)
|
||||||
pass_rate = round(passes / len(tests), 2)
|
pass_rate = passes / len(tests)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"complete: {passes} passed, {len(failures)} failed "
|
f"complete: {passes} passed, {len(failures)} failed "
|
||||||
f"({pass_rate * 100:.0f}%/{pass_rate * 100:.0f}%)"
|
f"({pass_rate * 100:.0f}%/{MINIMUM_PASS_RATE * 100:.0f}%)"
|
||||||
)
|
)
|
||||||
|
|
||||||
if passes < MINIMUM_PASS_RATE:
|
if pass_rate < MINIMUM_PASS_RATE:
|
||||||
print("continuity pass rate is under minimum, test suite failed ;<")
|
print("continuity pass rate is under minimum, test suite failed ;<")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue