1#!/usr/bin/env python3 2 3# Copyright (C) 2022 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16import dataclasses 17import unittest 18 19from perf_metrics import _get_column_headers 20 21 22def to_row(concatenated_keys: str) -> dict: 23 return {c: None for c in concatenated_keys} 24 25 26class PerfMetricsTest(unittest.TestCase): 27 """Tests utility functions. This is not Perf Test itself.""" 28 29 def test_get_column_headers(self): 30 31 @dataclasses.dataclass 32 class Example: 33 # each string = concatenated keys of the row object 34 row_keysets: list[str] 35 # concatenated headers 36 expected_headers: str 37 38 examples: list[Example] = [ 39 Example(['a'], 'a'), 40 Example(['ac', 'bd'], 'abcd'), 41 Example(['abe', 'cde'], 'abcde'), 42 Example(['ab', 'ba'], 'ab'), 43 Example(['abcde', 'edcba'], 'abcde'), 44 Example(['ac', 'abc'], 'abc') 45 ] 46 for e in examples: 47 rows = [to_row(kz) for kz in e.row_keysets] 48 expected_headers = [*e.expected_headers] 49 with self.subTest(rows=rows, expected_headers=expected_headers): 50 self.assertEqual(_get_column_headers(rows, allow_cycles=True), 51 expected_headers) 52 53 def test_cycles(self): 54 examples = [ 55 (['ab', 'ba'], 'a->b->a'), 56 (['abcd', 'db'], 'b->c->d->b') 57 ] 58 for (e, cycle) in examples: 59 rows = [to_row(kz) for kz in e] 60 with self.subTest(rows=rows, cycle=cycle): 61 with self.assertRaisesRegex(ValueError, 62 f'event ordering has a cycle {cycle}'): 63 _get_column_headers(rows, allow_cycles=False) 64 65 66if __name__ == '__main__': 67 unittest.main() 68