Home | History | Annotate | Download | only in value
      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.
      4 import os
      5 import unittest
      6 
      7 from telemetry import value
      8 from telemetry.page import page_set
      9 
     10 
     11 class TestBase(unittest.TestCase):
     12   def setUp(self):
     13     self.page_set =  page_set.PageSet(file_path=os.path.dirname(__file__))
     14     self.page_set.AddPageWithDefaultRunNavigate("http://www.bar.com/")
     15     self.page_set.AddPageWithDefaultRunNavigate("http://www.baz.com/")
     16     self.page_set.AddPageWithDefaultRunNavigate("http://www.foo.com/")
     17 
     18   @property
     19   def pages(self):
     20     return self.page_set.pages
     21 
     22 class ValueForTest(value.Value):
     23   @classmethod
     24   def MergeLikeValuesFromSamePage(cls, values):
     25     pass
     26 
     27   @classmethod
     28   def MergeLikeValuesFromDifferentPages(cls, values,
     29                                         group_by_name_suffix=False):
     30     pass
     31 
     32   def GetBuildbotDataType(self, output_context):
     33     pass
     34 
     35   def GetBuildbotValue(self):
     36     pass
     37 
     38   def GetChartAndTraceNameForComputedSummaryResult(
     39       self, trace_tag):
     40     pass
     41 
     42   def GetRepresentativeNumber(self):
     43     pass
     44 
     45   def GetRepresentativeString(self):
     46     pass
     47 
     48   @staticmethod
     49   def GetJSONTypeName():
     50     pass
     51 
     52 class ValueForAsDictTest(ValueForTest):
     53   @staticmethod
     54   def GetJSONTypeName():
     55     return 'baz'
     56 
     57 class ValueForFromDictTest(ValueForTest):
     58   @staticmethod
     59   def FromDict(value_dict, page_dict):
     60     kwargs = value.Value.GetConstructorKwArgs(value_dict, page_dict)
     61     return ValueForFromDictTest(**kwargs)
     62 
     63   @staticmethod
     64   def GetJSONTypeName():
     65     return 'value_for_from_dict_test'
     66 
     67 class ValueTest(TestBase):
     68   def testCompat(self):
     69     page0 = self.pages[0]
     70     page1 = self.pages[0]
     71 
     72     a = value.Value(page0, 'x', 'unit', important=False, description=None)
     73     b = value.Value(page1, 'x', 'unit', important=False, description=None)
     74     self.assertTrue(b.IsMergableWith(a))
     75 
     76   def testIncompat(self):
     77     page0 = self.pages[0]
     78 
     79     a = value.Value(page0, 'x', 'unit', important=False, description=None)
     80     b = value.Value(page0, 'x', 'incompatUnit', important=False,
     81                     description=None)
     82     self.assertFalse(b.IsMergableWith(a))
     83 
     84     a = value.Value(page0, 'x', 'unit', important=False, description=None)
     85     b = value.Value(page0, 'x', 'unit', important=True, description=None)
     86     self.assertFalse(b.IsMergableWith(a))
     87 
     88     a = value.Value(page0, 'x', 'unit', important=False, description=None)
     89     b = ValueForTest(page0, 'x', 'unit', important=True, description=None)
     90     self.assertFalse(b.IsMergableWith(a))
     91 
     92   def testAsDictBaseKeys(self):
     93     v = ValueForAsDictTest(None, 'x', 'unit', important=True, description=None)
     94     d = v.AsDict()
     95 
     96     self.assertEquals(d, {
     97           'name': 'x',
     98           'type': 'baz',
     99           'units': 'unit',
    100           'important': True
    101         })
    102 
    103   def testAsDictWithPage(self):
    104     page0 = self.pages[0]
    105 
    106     v = ValueForAsDictTest(page0, 'x', 'unit', important=False,
    107                            description=None)
    108     d = v.AsDict()
    109 
    110     self.assertIn('page_id', d)
    111 
    112   def testAsDictWithoutPage(self):
    113     v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None)
    114     d = v.AsDict()
    115 
    116     self.assertNotIn('page_id', d)
    117 
    118   def testAsDictWithDescription(self):
    119     v = ValueForAsDictTest(None, 'x', 'unit', important=False,
    120                            description='Some description.')
    121     d = v.AsDict()
    122     self.assertEqual('Some description.', d['description'])
    123 
    124   def testAsDictWithoutDescription(self):
    125     v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None)
    126     self.assertNotIn('description', v.AsDict())
    127 
    128   def testFromDictBaseKeys(self):
    129     d = {
    130       'type': 'value_for_from_dict_test',
    131       'name': 'x',
    132       'units': 'unit'
    133     }
    134 
    135     v = value.Value.FromDict(d, None)
    136     self.assertEquals(v.name, 'x')
    137     self.assertTrue(isinstance(v, ValueForFromDictTest))
    138     self.assertEquals(v.units, 'unit')
    139 
    140   def testFromDictWithPage(self):
    141     page0 = self.pages[0]
    142     page_dict = {page0.id: page0}
    143 
    144     d = {
    145       'type': 'value_for_from_dict_test',
    146       'name': 'x',
    147       'units': 'unit',
    148       'page_id': page0.id
    149     }
    150 
    151     v = value.Value.FromDict(d, page_dict)
    152 
    153     self.assertEquals(v.page.id, page0.id)
    154 
    155   def testFromDictWithoutPage(self):
    156     d = {
    157       'type': 'value_for_from_dict_test',
    158       'name': 'x',
    159       'units': 'unit'
    160     }
    161 
    162     v = value.Value.FromDict(d, {})
    163 
    164     self.assertEquals(v.page, None)
    165 
    166   def testFromDictWithDescription(self):
    167     d = {
    168           'type': 'value_for_from_dict_test',
    169           'name': 'x',
    170           'units': 'unit',
    171           'description': 'foo'
    172         }
    173 
    174     v = value.Value.FromDict(d, {})
    175     self.assertEquals(v.description, 'foo')
    176 
    177   def testFromDictWithoutDescription(self):
    178     d = {
    179           'type': 'value_for_from_dict_test',
    180           'name': 'x',
    181           'units': 'unit'
    182         }
    183 
    184     v = value.Value.FromDict(d, {})
    185     self.assertEquals(v.description, None)
    186 
    187   def testListOfValuesFromListOfDicts(self):
    188     d0 = {
    189           'type': 'value_for_from_dict_test',
    190           'name': 'x',
    191           'units': 'unit'
    192         }
    193     d1 = {
    194           'type': 'value_for_from_dict_test',
    195           'name': 'y',
    196           'units': 'unit'
    197         }
    198     vs = value.Value.ListOfValuesFromListOfDicts([d0, d1], {})
    199     self.assertEquals(vs[0].name, 'x')
    200     self.assertEquals(vs[1].name, 'y')
    201