-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add e2e tests; fix identified problems
- move scripts/demo.py to examples/demo.py - add tests/* with pytest scripts - fix ArtifactEmitter to take optionality into account; fix low-level model Diagnosis.message to have the correct ty.Optional annotation - export the JSON type alias from ocptv.output since it's useful in tests Signed-off-by: mimir-d <mimir-d@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.pytest.ini_options] | ||
pythonpath = [ | ||
"." | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import json | ||
|
||
from ocptv.output import Writer, JSON | ||
|
||
|
||
class MockWriter(Writer): | ||
def __init__(self): | ||
self.lines: list[str] = [] | ||
|
||
def write(self, buffer: str): | ||
self.lines.append(buffer) | ||
|
||
def decoded_obj(self, index: int) -> dict[str, JSON]: | ||
"""Decode an expected object from given output index""" | ||
return json.loads(self.lines[index]) | ||
|
||
|
||
def assert_json(line: str, expected: JSON): | ||
actual = json.loads(line) | ||
|
||
# remove timestamps, cant easily compare | ||
if isinstance(expected, dict) and "timestamp" in actual: | ||
del actual["timestamp"] | ||
|
||
if isinstance(expected, dict) and "timestamp" in expected: | ||
del expected["timestamp"] | ||
|
||
assert actual == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import pytest | ||
import typing as ty | ||
|
||
import ocptv | ||
from ocptv import LogSeverity, TestStatus, TestResult, DiagnosisType | ||
from ocptv.output import JSON | ||
|
||
TestStatus.__test__ = False # type: ignore | ||
TestResult.__test__ = False # type: ignore | ||
|
||
from .mocks import MockWriter, assert_json | ||
|
||
|
||
@pytest.fixture | ||
def writer(): | ||
w = MockWriter() | ||
ocptv.configOutput(w) | ||
return w | ||
|
||
|
||
def test_simple_run(writer: MockWriter): | ||
run = ocptv.TestRun( | ||
name="test", | ||
version="1.0", | ||
command_line="cl", | ||
parameters={"param": "test"}, | ||
) | ||
run.start() | ||
run.end(status=TestStatus.COMPLETE, result=TestResult.PASS) | ||
|
||
assert len(writer.lines) == 3 | ||
assert_json( | ||
writer.lines[1], | ||
{ | ||
"testRunArtifact": { | ||
"testRunStart": { | ||
"name": "test", | ||
"version": "1.0", | ||
"commandLine": "cl", | ||
"parameters": { | ||
"param": "test", | ||
}, | ||
"dutInfo": [], | ||
}, | ||
}, | ||
"sequenceNumber": 1, | ||
}, | ||
) | ||
|
||
assert_json( | ||
writer.lines[2], | ||
{ | ||
"testRunArtifact": { | ||
"testRunEnd": { | ||
"status": "COMPLETE", | ||
"result": "PASS", | ||
}, | ||
}, | ||
"sequenceNumber": 2, | ||
}, | ||
) | ||
|
||
|
||
def test_run_scope(writer: MockWriter): | ||
run = ocptv.TestRun(name="test", version="1.0") | ||
with run.scope(): | ||
pass | ||
|
||
assert len(writer.lines) == 3 | ||
assert_json( | ||
writer.lines[2], | ||
{ | ||
"testRunArtifact": { | ||
"testRunEnd": { | ||
"status": "COMPLETE", | ||
"result": "PASS", | ||
}, | ||
}, | ||
"sequenceNumber": 2, | ||
}, | ||
) | ||
|
||
|
||
def test_run_skip_by_exception(writer: MockWriter): | ||
run = ocptv.TestRun(name="run_skip", version="1.0") | ||
with run.scope(): | ||
raise ocptv.TestRunError( | ||
status=TestStatus.SKIP, result=TestResult.NOT_APPLICABLE | ||
) | ||
|
||
assert len(writer.lines) == 3 | ||
assert_json( | ||
writer.lines[2], | ||
{ | ||
"testRunArtifact": { | ||
"testRunEnd": { | ||
"status": "SKIP", | ||
"result": "NOT_APPLICABLE", | ||
}, | ||
}, | ||
"sequenceNumber": 2, | ||
}, | ||
) | ||
|
||
|
||
def test_run_with_diagnosis(writer: MockWriter): | ||
class Verdict: | ||
PASS = "pass-default" | ||
|
||
run = ocptv.TestRun(name="run_with_diagnosis", version="1.0") | ||
with run.scope(): | ||
run.add_log(LogSeverity.INFO, "run info") | ||
step = run.add_step("step0") | ||
with step.scope(): | ||
step.add_diagnosis(DiagnosisType.PASS, verdict=Verdict.PASS) | ||
|
||
assert len(writer.lines) == 7 | ||
assert "schemaVersion" in writer.decoded_obj(0) | ||
|
||
assert "testRunArtifact" in writer.decoded_obj(1) | ||
artifact = ty.cast(dict[str, JSON], writer.decoded_obj(1)["testRunArtifact"]) | ||
assert "testRunStart" in artifact | ||
|
||
assert "testRunArtifact" in writer.decoded_obj(2) | ||
artifact = ty.cast(dict[str, JSON], writer.decoded_obj(2)["testRunArtifact"]) | ||
assert "log" in artifact | ||
|
||
assert_json( | ||
writer.lines[3], | ||
{ | ||
"testStepArtifact": { | ||
"testStepStart": { | ||
"name": "step0", | ||
}, | ||
"testStepId": "0", | ||
}, | ||
"sequenceNumber": 3, | ||
}, | ||
) | ||
|
||
assert_json( | ||
writer.lines[4], | ||
{ | ||
"testStepArtifact": { | ||
"diagnosis": { | ||
"type": "PASS", | ||
"verdict": Verdict.PASS, | ||
}, | ||
"testStepId": "0", | ||
}, | ||
"sequenceNumber": 4, | ||
}, | ||
) | ||
|
||
assert_json( | ||
writer.lines[5], | ||
{ | ||
"testStepArtifact": { | ||
"testStepEnd": { | ||
"status": "COMPLETE", | ||
}, | ||
"testStepId": "0", | ||
}, | ||
"sequenceNumber": 5, | ||
}, | ||
) | ||
|
||
assert "testRunArtifact" in writer.decoded_obj(6) | ||
artifact = ty.cast(dict[str, JSON], writer.decoded_obj(6)["testRunArtifact"]) | ||
assert "testRunEnd" in artifact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
|
||
import ocptv | ||
|
||
from .mocks import MockWriter, assert_json | ||
|
||
|
||
@pytest.fixture | ||
def writer(): | ||
w = MockWriter() | ||
ocptv.configOutput(w) | ||
return w | ||
|
||
|
||
def test_schema_version_is_emitted(writer: MockWriter): | ||
run = ocptv.TestRun(name="test", version="1.0") | ||
run.start() | ||
|
||
assert len(writer.lines) == 2 | ||
assert_json( | ||
writer.lines[0], | ||
{ | ||
"schemaVersion": { | ||
"major": ocptv.OCPVersion.VERSION_2_0.value[0], | ||
"minor": ocptv.OCPVersion.VERSION_2_0.value[1], | ||
}, | ||
"sequenceNumber": 0, | ||
}, | ||
) |