Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi committed Feb 3, 2024
1 parent ea8df93 commit ba34288
Show file tree
Hide file tree
Showing 5 changed files with 727 additions and 789 deletions.
28 changes: 20 additions & 8 deletions python/publish/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from publish import logger
from publish.github_action import GithubAction
from publish.unittestresults import UnitTestCaseResults, UnitTestRunResults, UnitTestRunDeltaResults, \
UnitTestRunResultsOrDeltaResults, get_stats_delta, create_unit_test_case_results
UnitTestRunResultsOrDeltaResults, get_stats_delta, get_diff_value


@dataclass(frozen=True)
Expand Down Expand Up @@ -105,7 +105,12 @@ def without_exceptions(self) -> 'PublishData':
)

def without_suite_details(self) -> 'PublishData':
return dataclasses.replace(self, stats=self.stats.without_suite_details())
return dataclasses.replace(
self,
stats=self.stats.without_suite_details() if self.stats is not None else None,
stats_with_delta=self.stats_with_delta.without_suite_details() if self.stats_with_delta is not None else None,
before_stats=self.before_stats.without_suite_details() if self.before_stats is not None else None
)

def without_cases(self) -> 'PublishData':
return dataclasses.replace(self, cases=None)
Expand All @@ -129,12 +134,15 @@ def _format(cls, stats: Mapping[str, Any], thousands_separator: str) -> Dict[str
def _formatted_stats_and_delta(cls,
stats: Optional[Mapping[str, Any]],
stats_with_delta: Optional[Mapping[str, Any]],
before_stats: Optional[Mapping[str, Any]],
thousands_separator: str) -> Mapping[str, Any]:
d = {}
if stats is not None:
d.update(stats=cls._format(stats, thousands_separator))
if stats_with_delta is not None:
d.update(stats_with_delta=cls._format(stats_with_delta, thousands_separator))
if before_stats is not None:
d.update(before_stats=cls._format(before_stats, thousands_separator))
return d

def _as_dict(self) -> Dict[str, Any]:
Expand All @@ -160,29 +168,33 @@ def to_dict(self, thousands_separator: str, with_suite_details: bool, with_cases

# provide formatted stats and delta
d.update(formatted=self._formatted_stats_and_delta(
d.get('stats'), d.get('stats_with_delta'), thousands_separator
d.get('stats'), d.get('stats_with_delta'), d.get('before_stats'), thousands_separator
))

return d

def to_reduced_dict(self, thousands_separator: str) -> Mapping[str, Any]:
# remove exceptions, suite details and cases
data = self.without_exceptions().without_suite_details().without_cases()._as_dict()
data = self.without_exceptions().without_summary_with_digest().without_suite_details().without_cases()._as_dict()

# replace some large fields with their lengths and delete individual test cases if present
def reduce(d: Dict[str, Any]) -> Dict[str, Any]:
d = deepcopy(d)
if d.get('stats', {}).get('errors') is not None:
d['stats']['errors'] = len(d['stats']['errors'])
if d.get('stats_with_delta', {}).get('errors') is not None:
d['stats_with_delta']['errors'] = len(d['stats_with_delta']['errors'])
if d.get('before_stats', {}).get('errors') is not None:
d['before_stats']['errors'] = len(d['before_stats']['errors'])
if d.get('stats', {}).get('errors') is not None and \
d.get('before_stats', {}).get('errors') is not None and \
d.get('stats_with_delta', {}).get('errors') is not None:
d['stats_with_delta']['errors'] = get_diff_value(d['stats']['errors'], d['before_stats']['errors'])
if d.get('annotations') is not None:
d['annotations'] = len(d['annotations'])
return d

data = reduce(data)
data.update(formatted=self._formatted_stats_and_delta(
data.get('stats'), data.get('stats_with_delta'), thousands_separator
data.get('stats'), data.get('stats_with_delta'), data.get('before_stats'), thousands_separator
))

return data
Expand Down Expand Up @@ -480,7 +492,7 @@ def write_json(data: PublishData, writer, settings: Settings):
settings.json_thousands_separator,
settings.json_suite_details,
settings.json_test_case_results
), writer, ensure_ascii=False)
), writer, ensure_ascii=False, indent=2)

def publish_job_summary(self, title: str, data: PublishData):
title = title
Expand Down
7 changes: 7 additions & 0 deletions python/publish/unittestresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ class UnitTestRunDeltaResults:
suites: Numeric
duration: Numeric

suite_details: Optional[List[UnitTestSuite]]

tests: Numeric
tests_succ: Numeric
tests_skip: Numeric
Expand Down Expand Up @@ -378,6 +380,9 @@ def d(value: Numeric) -> int:
def without_exceptions(self) -> 'UnitTestRunDeltaResults':
return dataclasses.replace(self, errors=[error.without_exception() for error in self.errors])

def without_suite_details(self) -> 'UnitTestRunDeltaResults':
return dataclasses.replace(self, suite_details=None)


UnitTestRunResultsOrDeltaResults = Union[UnitTestRunResults, UnitTestRunDeltaResults]

Expand Down Expand Up @@ -498,6 +503,8 @@ def get_stats_delta(stats: UnitTestRunResults,
suites=get_diff_value(stats.suites, reference_stats.suites),
duration=get_diff_value(stats.duration, reference_stats.duration, 'duration'),

suite_details=stats.suite_details,

tests=get_diff_value(stats.tests, reference_stats.tests),
tests_succ=get_diff_value(stats.tests_succ, reference_stats.tests_succ),
tests_skip=get_diff_value(stats.tests_skip, reference_stats.tests_skip),
Expand Down
18 changes: 9 additions & 9 deletions python/test/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def test_get_short_summary_md(self):

def test_get_short_summary_md_with_delta(self):
self.assertEqual(get_short_summary_md(UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='commit',
Expand Down Expand Up @@ -612,7 +612,7 @@ def test_get_commit_line_md(self):
self.assertEqual(get_commit_line_md(stats), 'Results for commit commit.')

stats_with_delta = UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='commit', reference_type='type', reference_commit='ref'
Expand All @@ -622,7 +622,7 @@ def test_get_commit_line_md(self):
for ref_type, ref in [(None, None), ('type', None), (None, 'ref')]:
with self.subTest(ref_type=ref_type, ref=ref):
stats_with_delta = UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='commit', reference_type=ref_type, reference_commit=ref
Expand Down Expand Up @@ -693,7 +693,7 @@ def test_get_long_summary_with_runs_md_with_errors(self):

def test_get_long_summary_with_runs_md_with_deltas(self):
self.assertEqual(get_long_summary_with_runs_md(UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand Down Expand Up @@ -824,7 +824,7 @@ def test_get_long_summary_without_runs_md_with_errors(self):

def test_get_long_summary_without_runs_md_with_delta(self):
self.assertEqual(get_long_summary_without_runs_md(UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(0, 0),
runs=n(4, -5), runs_succ=n(5, 6), runs_skip=n(6, -7), runs_fail=n(7, 8), runs_error=n(0, 0),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand All @@ -836,7 +836,7 @@ def test_get_long_summary_without_runs_md_with_delta(self):

def test_get_long_summary_without_runs_md_with_errors_and_deltas(self):
self.assertEqual(get_long_summary_without_runs_md(UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(4, -5), runs_succ=n(5, 6), runs_skip=n(6, -7), runs_fail=n(7, 8), runs_error=n(8, -9),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def test_get_long_summary_with_digest_md_with_delta(self):
with mock.patch('gzip.time.time', return_value=0):
actual = get_long_summary_with_digest_md(
UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand Down Expand Up @@ -1107,7 +1107,7 @@ def test_get_long_summary_with_digest_md_with_delta_and_parse_errors(self):
with mock.patch('gzip.time.time', return_value=0):
actual = get_long_summary_with_digest_md(
UnitTestRunDeltaResults(
files=n(1, 2), errors=errors, suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=errors, suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand All @@ -1134,7 +1134,7 @@ def test_get_long_summary_with_digest_md_with_delta_and_parse_errors(self):
def test_get_long_summary_with_digest_md_with_delta_results_only(self):
with self.assertRaises(ValueError) as context:
get_long_summary_with_digest_md(UnitTestRunDeltaResults(
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4),
files=n(1, 2), errors=[], suites=n(2, -3), duration=d(3, 4), suite_details=self.details,
tests=n(4, -5), tests_succ=n(5, 6), tests_skip=n(6, -7), tests_fail=n(7, 8), tests_error=n(8, -9),
runs=n(9, 10), runs_succ=n(10, -11), runs_skip=n(11, 12), runs_fail=n(12, -13), runs_error=n(13, 14),
commit='123456789abcdef0', reference_type='type', reference_commit='0123456789abcdef'
Expand Down
Loading

0 comments on commit ba34288

Please sign in to comment.