diff --git a/.github/workflows/validation_unit_tests.yaml b/.github/workflows/validation_unit_tests.yaml new file mode 100644 index 0000000..22b3988 --- /dev/null +++ b/.github/workflows/validation_unit_tests.yaml @@ -0,0 +1,19 @@ +name: Validation Unit Tests + +on: + pull_request: + +jobs: + validation_unit_tests: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + pip install pydantic pytest + + - name: Run validation unit tests + run: | + pytest \ No newline at end of file diff --git a/pyperf/pyperf_run b/pyperf/pyperf_run index 3a37533..d3f3967 100755 --- a/pyperf/pyperf_run +++ b/pyperf/pyperf_run @@ -323,6 +323,8 @@ if [ $to_pbench -eq 0 ]; then fi generate_csv_file ${pyresults} + $TOOLS_BIN/csv_to_json $to_json_flags --csv_file ${pyresults}.csv --output_file ${pyresults}.json + $TOOLS_BIN/verify_results $to_verify_flags --file ${pyresults}.json --schema_file $to_script_dir/../result_schema.py --class_name PyperfResult else source ~/.bashrc arguments="${arguments} --test_iterations ${to_times_to_run}" diff --git a/result_schema.py b/result_schema.py new file mode 100644 index 0000000..b9bfa5a --- /dev/null +++ b/result_schema.py @@ -0,0 +1,13 @@ +from enum import Enum +import pydantic + +class DurationUnit(str, Enum): + sec = "sec" + ms = "ms" + us = "us" + ns = "ns" + +class PyperfResult(pydantic.BaseModel): + Test: str = pydantic.Field(description="Name of the test that was ran") + Avg: float = pydantic.Field(description="Duration, unit is determined by duration_unit field", allow_inf_nan=False, gt=0) + Unit: DurationUnit = pydantic.Field(description="Unit of duration") diff --git a/tests/test_duration.py b/tests/test_duration.py new file mode 100644 index 0000000..312f912 --- /dev/null +++ b/tests/test_duration.py @@ -0,0 +1,26 @@ +from pathlib import Path +import pydantic +import sys +import pytest + +file = Path(__file__).resolve() +parent = file.parent.parent +sys.path.append(str(parent)) + +from result_schema import PyperfResult, DurationUnit + +@pytest.mark.parametrize("duration", [ + "-12", "0", "0.0", "inf", "nan", + "abc", "123abc", "123abc", "12abc34", + "123.123.123", "-0", "-0.0", "-inf", "-nan" + ]) +def test_duration_invalid(duration): + with pytest.raises(pydantic.ValidationError): + PyperfResult(Test="test", Avg=duration, Unit=DurationUnit.sec) + +@pytest.mark.parametrize("duration", [ + "123", "123.456", "0.000001", "0.000000001", "1234567890", "1234567890.1234567890" +]) +def test_duration_valid(duration): + result = PyperfResult(Test="test", Avg=duration, Unit=DurationUnit.sec) + assert result.Avg == float(duration) \ No newline at end of file diff --git a/tests/test_duration_unit.py b/tests/test_duration_unit.py new file mode 100644 index 0000000..1c561e9 --- /dev/null +++ b/tests/test_duration_unit.py @@ -0,0 +1,26 @@ +from pathlib import Path +import pydantic +import sys +import pytest + +file = Path(__file__).resolve() +parent = file.parent.parent +sys.path.append(str(parent)) + +from result_schema import PyperfResult, DurationUnit + +@pytest.mark.parametrize("duration_unit", [ + "MS", "Day", "Year", "Hour", "cat", "dog", + "123", "-12034" + ]) +def test_duration_unit_invalid(duration_unit): + with pytest.raises(pydantic.ValidationError): + PyperfResult(Test="test", Avg=12, Unit=duration_unit) + +@pytest.mark.parametrize("duration_unit", [ + DurationUnit.sec, DurationUnit.ms, DurationUnit.us, DurationUnit.ns, + "sec", "ms", "us", "ns" + ]) +def test_duration_unit_invalid(duration_unit): + result = PyperfResult(Test="test", Avg=12, Unit=duration_unit) + assert result.Unit == duration_unit