1# Copyright 2020 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Helper methods for unittests.""" 5 6from typing import Generator, Iterable, List, Optional, Set, Tuple, Type 7 8import pandas 9 10from unexpected_passes_common import builders 11from unexpected_passes_common import expectations 12from unexpected_passes_common import data_types 13from unexpected_passes_common import queries as queries_module 14 15 16def CreateStatsWithPassFails(passes: int, fails: int) -> data_types.BuildStats: 17 stats = data_types.BuildStats() 18 for _ in range(passes): 19 stats.AddPassedBuild(frozenset()) 20 for i in range(fails): 21 stats.AddFailedBuild('build_id%d' % i, frozenset()) 22 return stats 23 24 25# id_ is used instead of id since id is a python built-in. 26def FakeQueryResult(builder_name: str, id_: str, test_id: str, status: str, 27 typ_tags: Iterable[str], step_name: str) -> pandas.Series: 28 return pandas.Series( 29 data={ 30 'builder_name': builder_name, 31 'id': id_, 32 'test_id': test_id, 33 'status': status, 34 'typ_tags': list(typ_tags), 35 'step_name': step_name, 36 }) 37 38 39class SimpleBigQueryQuerier(queries_module.BigQueryQuerier): 40 41 def __init__(self, *args, **kwargs): 42 super().__init__(*args, **kwargs) 43 self.query_results = [] 44 45 def _GetSeriesForQuery(self, _) -> Generator[pandas.Series, None, None]: 46 for r in self.query_results: 47 yield r 48 49 def _GetRelevantExpectationFilesForQueryResult(self, _) -> None: 50 return None 51 52 def _StripPrefixFromTestId(self, test_id: str) -> str: 53 return test_id.split('.')[-1] 54 55 def _GetPublicCiQuery(self) -> str: 56 return 'public_ci' 57 58 def _GetInternalCiQuery(self) -> str: 59 return 'internal_ci' 60 61 def _GetPublicTryQuery(self) -> str: 62 return 'public_try' 63 64 def _GetInternalTryQuery(self) -> str: 65 return 'internal_try' 66 67 68def CreateGenericQuerier( 69 suite: Optional[str] = None, 70 project: Optional[str] = None, 71 num_samples: Optional[int] = None, 72 keep_unmatched_results: bool = False, 73 cls: Optional[Type[queries_module.BigQueryQuerier]] = None 74) -> queries_module.BigQueryQuerier: 75 suite = suite or 'pixel' 76 project = project or 'project' 77 num_samples = num_samples or 5 78 cls = cls or SimpleBigQueryQuerier 79 return cls(suite, project, num_samples, keep_unmatched_results) 80 81 82def GetArgsForMockCall(call_args_list: List[tuple], 83 call_number: int) -> Tuple[tuple, dict]: 84 """Helper to more sanely get call args from a mocked method. 85 86 Args: 87 call_args_list: The call_args_list member from the mock in question. 88 call_number: The call number to pull args from, starting at 0 for the first 89 call to the method. 90 91 Returns: 92 A tuple (args, kwargs). |args| is a list of arguments passed to the method. 93 |kwargs| is a dict containing the keyword arguments padded to the method. 94 """ 95 args = call_args_list[call_number][0] 96 kwargs = call_args_list[call_number][1] 97 return args, kwargs 98 99 100class GenericBuilders(builders.Builders): 101 #pylint: disable=useless-super-delegation 102 def __init__(self, 103 suite: Optional[str] = None, 104 include_internal_builders: bool = False): 105 super().__init__(suite, include_internal_builders) 106 #pylint: enable=useless-super-delegation 107 108 def _BuilderRunsTestOfInterest(self, _test_map) -> bool: 109 return True 110 111 def GetIsolateNames(self) -> Set[str]: 112 return set() 113 114 def GetFakeCiBuilders(self) -> dict: 115 return {} 116 117 def GetNonChromiumBuilders(self) -> Set[data_types.BuilderEntry]: 118 return set() 119 120 121def RegisterGenericBuildersImplementation() -> None: 122 builders.RegisterInstance(GenericBuilders()) 123 124 125class GenericExpectations(expectations.Expectations): 126 def GetExpectationFilepaths(self) -> list: 127 return [] 128 129 def _GetExpectationFileTagHeader(self, _) -> str: 130 return """\ 131# tags: [ linux mac win ] 132# tags: [ amd intel nvidia ] 133# results: [ Failure RetryOnFailure Skip Pass ] 134""" 135 136 def _GetKnownTags(self) -> Set[str]: 137 return set(['linux', 'mac', 'win', 'amd', 'intel', 'nvidia']) 138 139 140def CreateGenericExpectations() -> GenericExpectations: 141 return GenericExpectations() 142 143 144def RegisterGenericExpectationsImplementation() -> None: 145 expectations.RegisterInstance(CreateGenericExpectations()) 146