1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import os 5import unittest 6 7from telemetry import story 8from telemetry import page as page_module 9from telemetry import value 10 11 12class TestBase(unittest.TestCase): 13 def setUp(self): 14 story_set = story.StorySet(base_dir=os.path.dirname(__file__)) 15 story_set.AddStory( 16 page_module.Page("http://www.bar.com/", story_set, story_set.base_dir)) 17 story_set.AddStory( 18 page_module.Page("http://www.baz.com/", story_set, story_set.base_dir)) 19 story_set.AddStory( 20 page_module.Page("http://www.foo.com/", story_set, story_set.base_dir)) 21 self.story_set = story_set 22 23 @property 24 def pages(self): 25 return self.story_set.stories 26 27class ValueForTest(value.Value): 28 @classmethod 29 def MergeLikeValuesFromSamePage(cls, values): 30 pass 31 32 @classmethod 33 def MergeLikeValuesFromDifferentPages(cls, values): 34 pass 35 36 def GetBuildbotDataType(self, output_context): 37 pass 38 39 def GetBuildbotValue(self): 40 pass 41 42 def GetChartAndTraceNameForComputedSummaryResult( 43 self, trace_tag): 44 pass 45 46 def GetRepresentativeNumber(self): 47 pass 48 49 def GetRepresentativeString(self): 50 pass 51 52 @staticmethod 53 def GetJSONTypeName(): 54 pass 55 56class ValueForAsDictTest(ValueForTest): 57 @staticmethod 58 def GetJSONTypeName(): 59 return 'baz' 60 61class ValueForFromDictTest(ValueForTest): 62 @staticmethod 63 def FromDict(value_dict, page_dict): 64 kwargs = value.Value.GetConstructorKwArgs(value_dict, page_dict) 65 return ValueForFromDictTest(**kwargs) 66 67 @staticmethod 68 def GetJSONTypeName(): 69 return 'value_for_from_dict_test' 70 71class ValueTest(TestBase): 72 def testCompat(self): 73 page0 = self.pages[0] 74 page1 = self.pages[0] 75 76 a = value.Value(page0, 'x', 'unit', important=False, description=None, 77 tir_label='foo', grouping_keys=None) 78 b = value.Value(page1, 'x', 'unit', important=False, description=None, 79 tir_label='foo', grouping_keys=None) 80 self.assertTrue(b.IsMergableWith(a)) 81 82 a = value.Value(page0, 'x', 'unit', important=False, description=None, 83 tir_label='foo', grouping_keys=None) 84 b = value.Value(page0, 'x', 'unit', important=False, description=None, 85 tir_label='bar', grouping_keys=None) 86 self.assertTrue(b.IsMergableWith(a)) 87 88 def testIncompat(self): 89 page0 = self.pages[0] 90 91 a = value.Value(page0, 'x', 'unit', important=False, description=None, 92 tir_label=None, grouping_keys=None) 93 b = value.Value(page0, 'x', 'incompatUnit', important=False, 94 tir_label=None, description=None, grouping_keys=None) 95 self.assertFalse(b.IsMergableWith(a)) 96 97 a = value.Value(page0, 'x', 'unit', important=False, description=None, 98 tir_label=None, grouping_keys=None) 99 b = value.Value(page0, 'x', 'unit', important=True, description=None, 100 tir_label=None, grouping_keys=None) 101 self.assertFalse(b.IsMergableWith(a)) 102 103 a = value.Value(page0, 'x', 'unit', important=False, description=None, 104 tir_label=None, grouping_keys=None) 105 b = ValueForTest(page0, 'x', 'unit', important=True, description=None, 106 tir_label=None, grouping_keys=None) 107 self.assertFalse(b.IsMergableWith(a)) 108 109 def testNameMustBeString(self): 110 with self.assertRaises(ValueError): 111 value.Value(None, 42, 'unit', important=False, description=None, 112 tir_label=None, grouping_keys=None) 113 114 def testUnitsMustBeString(self): 115 with self.assertRaises(ValueError): 116 value.Value(None, 'x', 42, important=False, description=None, 117 tir_label=None, grouping_keys=None) 118 119 def testImportantMustBeBool(self): 120 with self.assertRaises(ValueError): 121 value.Value(None, 'x', 'unit', important='foo', description=None, 122 tir_label=None, grouping_keys=None) 123 124 def testDescriptionMustBeStringOrNone(self): 125 with self.assertRaises(ValueError): 126 value.Value(None, 'x', 'unit', important=False, description=42, 127 tir_label=None, grouping_keys=None) 128 129 def testInteractionRecordMustBeStringOrNone(self): 130 with self.assertRaises(ValueError): 131 value.Value(None, 'x', 'unit', important=False, description=None, 132 tir_label=42, grouping_keys=None) 133 134 def testGroupingKeysMustBeDictOrNone(self): 135 with self.assertRaises(ValueError): 136 value.Value(None, 'x', 'unit', important=False, description=None, 137 tir_label=42, grouping_keys='foo') 138 139 def testAsDictBaseKeys(self): 140 v = ValueForAsDictTest(None, 'x', 'unit', important=True, description=None, 141 tir_label='bar', grouping_keys={'foo': 'baz'}) 142 d = v.AsDict() 143 144 self.assertEquals(d, { 145 'name': 'x', 146 'type': 'baz', 147 'units': 'unit', 148 'important': True, 149 'tir_label': 'bar', 150 'grouping_keys': {'foo': 'baz'} 151 }) 152 153 def testAsDictWithPage(self): 154 page0 = self.pages[0] 155 156 v = ValueForAsDictTest(page0, 'x', 'unit', important=False, 157 description=None, tir_label=None, grouping_keys=None) 158 d = v.AsDict() 159 160 self.assertIn('page_id', d) 161 162 def testAsDictWithoutPage(self): 163 v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None, 164 tir_label=None, grouping_keys=None) 165 d = v.AsDict() 166 167 self.assertNotIn('page_id', d) 168 169 def testAsDictWithDescription(self): 170 v = ValueForAsDictTest(None, 'x', 'unit', important=False, 171 description='Some description.', 172 tir_label=None, grouping_keys=None) 173 d = v.AsDict() 174 self.assertEqual('Some description.', d['description']) 175 176 def testAsDictWithoutDescription(self): 177 v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None, 178 tir_label=None, grouping_keys=None) 179 self.assertNotIn('description', v.AsDict()) 180 181 def testAsDictWithInteractionRecord(self): 182 v = ValueForAsDictTest(None, 'x', 'unit', important=False, 183 description='Some description.', 184 tir_label='foo', grouping_keys=None) 185 d = v.AsDict() 186 self.assertEqual('foo', d['tir_label']) 187 188 def testAsDictWithoutInteractionRecord(self): 189 v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None, 190 tir_label=None, grouping_keys=None) 191 self.assertNotIn('tir_label', v.AsDict()) 192 193 def testFromDictBaseKeys(self): 194 d = { 195 'type': 'value_for_from_dict_test', 196 'name': 'x', 197 'units': 'unit' 198 } 199 200 v = value.Value.FromDict(d, None) 201 self.assertEquals(v.name, 'x') 202 self.assertTrue(isinstance(v, ValueForFromDictTest)) 203 self.assertEquals(v.units, 'unit') 204 205 def testFromDictWithPage(self): 206 page0 = self.pages[0] 207 page_dict = {page0.id: page0} 208 209 d = { 210 'type': 'value_for_from_dict_test', 211 'name': 'x', 212 'units': 'unit', 213 'page_id': page0.id 214 } 215 216 v = value.Value.FromDict(d, page_dict) 217 218 self.assertEquals(v.page.id, page0.id) 219 220 def testFromDictWithPageId0(self): 221 page_dict = {0: 'foo'} 222 223 d = { 224 'type': 'value_for_from_dict_test', 225 'name': 'x', 226 'units': 'unit', 227 'page_id': 0 228 } 229 230 v = value.Value.FromDict(d, page_dict) 231 232 self.assertEquals(v.page, 'foo') 233 234 def testFromDictWithoutPage(self): 235 d = { 236 'type': 'value_for_from_dict_test', 237 'name': 'x', 238 'units': 'unit' 239 } 240 241 v = value.Value.FromDict(d, {}) 242 243 self.assertEquals(v.page, None) 244 245 def testFromDictWithDescription(self): 246 d = { 247 'type': 'value_for_from_dict_test', 248 'name': 'x', 249 'units': 'unit', 250 'description': 'foo' 251 } 252 253 v = value.Value.FromDict(d, {}) 254 self.assertEquals(v.description, 'foo') 255 256 def testFromDictWithoutDescription(self): 257 d = { 258 'type': 'value_for_from_dict_test', 259 'name': 'x', 260 'units': 'unit' 261 } 262 263 v = value.Value.FromDict(d, {}) 264 self.assertEquals(v.description, None) 265 266 def testFromDictWithInteractionRecord(self): 267 d = { 268 'type': 'value_for_from_dict_test', 269 'name': 'x', 270 'units': 'unit', 271 'description': 'foo', 272 'tir_label': 'bar' 273 } 274 275 v = value.Value.FromDict(d, {}) 276 self.assertEquals(v.tir_label, 'bar') 277 278 def testFromDictWithoutInteractionRecord(self): 279 d = { 280 'type': 'value_for_from_dict_test', 281 'name': 'x', 282 'units': 'unit' 283 } 284 285 v = value.Value.FromDict(d, {}) 286 self.assertEquals(v.tir_label, None) 287 288 def testFromDictWithGroupingKeys(self): 289 d = { 290 'type': 'value_for_from_dict_test', 291 'name': 'x', 292 'units': 'unit', 293 'description': 'foo', 294 'tir_label': 'bar', 295 'grouping_keys': {'foo': 'bar'} 296 } 297 298 v = value.Value.FromDict(d, {}) 299 self.assertEquals(v.grouping_keys, {'foo': 'bar'}) 300 301 def testFromDictWithoutGroupingKeys(self): 302 d = { 303 'type': 'value_for_from_dict_test', 304 'name': 'x', 305 'units': 'unit' 306 } 307 308 v = value.Value.FromDict(d, {}) 309 self.assertEquals(v.grouping_keys, {}) 310 311 def testListOfValuesFromListOfDicts(self): 312 d0 = { 313 'type': 'value_for_from_dict_test', 314 'name': 'x', 315 'units': 'unit' 316 } 317 d1 = { 318 'type': 'value_for_from_dict_test', 319 'name': 'y', 320 'units': 'unit' 321 } 322 vs = value.Value.ListOfValuesFromListOfDicts([d0, d1], {}) 323 self.assertEquals(vs[0].name, 'x') 324 self.assertEquals(vs[1].name, 'y') 325