|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +"""Tests for eval functions not covered by test_eval.py.""" |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from programbench.eval.eval import ( |
| 12 | + EvaluationResult, |
| 13 | + TestBranchError, |
| 14 | + TestResult, |
| 15 | + count_testcases, |
| 16 | +) |
| 17 | +from programbench.eval.eval_batch import _can_reprocess |
| 18 | + |
| 19 | + |
| 20 | +JUNIT_XML_THREE_CASES = """\ |
| 21 | +<?xml version="1.0" encoding="utf-8"?> |
| 22 | +<testsuites> |
| 23 | + <testsuite name="pytest" tests="3"> |
| 24 | + <testcase classname="t" name="a" time="0.01"/> |
| 25 | + <testcase classname="t" name="b" time="0.02"/> |
| 26 | + <testcase classname="t" name="c" time="0.03"/> |
| 27 | + </testsuite> |
| 28 | +</testsuites> |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +class TestCountTestcases: |
| 33 | + @pytest.mark.parametrize( |
| 34 | + ("xml", "expected"), |
| 35 | + [ |
| 36 | + ("", 0), |
| 37 | + (" \n ", 0), |
| 38 | + ("<not valid xml>", 0), |
| 39 | + (JUNIT_XML_THREE_CASES, 3), |
| 40 | + ], |
| 41 | + ) |
| 42 | + def test_counts(self, xml, expected): |
| 43 | + assert count_testcases(xml) == expected |
| 44 | + |
| 45 | + |
| 46 | +class TestEvaluationResultSummarize: |
| 47 | + def test_clean_run(self): |
| 48 | + result = EvaluationResult( |
| 49 | + test_results=[ |
| 50 | + TestResult(name="t1", branch="b1", status="passed", extra={}), |
| 51 | + TestResult(name="t2", branch="b1", status="passed", extra={}), |
| 52 | + ], |
| 53 | + solution_branch="submission", |
| 54 | + ) |
| 55 | + s = result.summarize() |
| 56 | + assert "100" in s |
| 57 | + assert "2/2" in s |
| 58 | + assert "submission" in s |
| 59 | + |
| 60 | + def test_with_error_code(self): |
| 61 | + result = EvaluationResult(error_code="compile_failed", error_details="gcc not found") |
| 62 | + s = result.summarize() |
| 63 | + assert "compile_failed" in s |
| 64 | + assert "gcc not found" in s |
| 65 | + |
| 66 | + def test_with_branch_errors(self): |
| 67 | + result = EvaluationResult( |
| 68 | + test_results=[TestResult(name="t1", branch="b1", status="passed", extra={})], |
| 69 | + test_branch_errors={"b2": [TestBranchError(error_code="timeout", error_details="")]}, |
| 70 | + ) |
| 71 | + assert "b2" in result.summarize() |
| 72 | + |
| 73 | + def test_with_system_errors(self): |
| 74 | + result = EvaluationResult( |
| 75 | + test_results=[TestResult(name="t1", branch="b1", status="system_error", extra={})], |
| 76 | + ) |
| 77 | + assert "system_errors=1" in result.summarize() |
| 78 | + |
| 79 | + def test_with_warnings(self): |
| 80 | + result = EvaluationResult(warnings=["something unexpected"]) |
| 81 | + assert "warnings=1" in result.summarize() |
| 82 | + |
| 83 | + |
| 84 | +class TestCanReprocess: |
| 85 | + def test_error_code_is_reprocessable(self): |
| 86 | + assert _can_reprocess(EvaluationResult(error_code="compile_failed")) |
| 87 | + |
| 88 | + def test_all_branches_tagged_in_log(self): |
| 89 | + result = EvaluationResult( |
| 90 | + test_branches=["b1", "b2"], |
| 91 | + log=[ |
| 92 | + {"step": "results_read", "branch": "b1", "returncode": 0, "output": "<xml/>"}, |
| 93 | + {"step": "results_read", "branch": "b2", "returncode": 0, "output": "<xml/>"}, |
| 94 | + ], |
| 95 | + ) |
| 96 | + assert _can_reprocess(result) |
| 97 | + |
| 98 | + def test_missing_branch_in_log_not_reprocessable(self): |
| 99 | + result = EvaluationResult( |
| 100 | + test_branches=["b1", "b2"], |
| 101 | + log=[ |
| 102 | + {"step": "results_read", "branch": "b1", "returncode": 0, "output": "<xml/>"}, |
| 103 | + ], |
| 104 | + ) |
| 105 | + assert not _can_reprocess(result) |
| 106 | + |
| 107 | + def test_branch_with_error_excluded_from_check(self): |
| 108 | + result = EvaluationResult( |
| 109 | + test_branches=["b1", "b2"], |
| 110 | + test_branch_errors={"b2": [TestBranchError(error_code="fail", error_details="")]}, |
| 111 | + log=[ |
| 112 | + {"step": "results_read", "branch": "b1", "returncode": 0, "output": "<xml/>"}, |
| 113 | + ], |
| 114 | + ) |
| 115 | + assert _can_reprocess(result) |
0 commit comments