summaryrefslogtreecommitdiff
path: root/extra/usb_power/stats_manager_unittest.py
blob: 2fb1080f4d8597ab8483abedbc22fb1569316221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for StatsManager."""

from __future__ import print_function
import json
import os
import re
import shutil
import tempfile
import unittest

import stats_manager

class TestStatsManager(unittest.TestCase):
  """Test to verify StatsManager methods work as expected.

  StatsManager should collect raw data, calculate their statistics, and save
  them in expected format.
  """

  def _populate_dummy_stats(self):
    """Create a populated & processed StatsManager to test data retrieval."""
    self.data.AddSample('A', 99999.5)
    self.data.AddSample('A', 100000.5)
    self.data.AddSample('A', 'ERROR')
    self.data.SetUnit('A', 'uW')
    self.data.SetUnit('A', 'mW')
    self.data.AddSample('B', 1.5)
    self.data.AddSample('B', 2.5)
    self.data.AddSample('B', 3.5)
    self.data.SetUnit('B', 'mV')
    self.data.CalculateStats()

  def _populate_dummy_stats_no_unit(self):
    self.data.AddSample('B', 1000)
    self.data.AddSample('A', 200)
    self.data.SetUnit('A', 'blue')

  def setUp(self):
    """Set up StatsManager and create a temporary directory for test."""
    self.tempdir = tempfile.mkdtemp()
    self.data = stats_manager.StatsManager()

  def tearDown(self):
    """Delete the temporary directory and its content."""
    shutil.rmtree(self.tempdir)

  def test_AddSample(self):
    """Adding a sample successfully adds a sample."""
    self.data.AddSample('Test', 1000)
    self.data.SetUnit('Test', 'test')
    self.data.CalculateStats()
    summary = self.data.GetSummary()
    self.assertEqual(1, summary['Test']['count'])

  def test_AddSampleNoFloat(self):
    """Adding a non number gets ignored and doesn't raise an exception."""
    self.data.AddSample('Test', 17)
    self.data.AddSample('Test', 'fiesta')
    self.data.SetUnit('Test', 'test')
    self.data.CalculateStats()
    summary = self.data.GetSummary()
    self.assertEqual(1, summary['Test']['count'])

  def test_AddSampleNoUnit(self):
    """Not adding a unit does not cause an exception on CalculateStats()."""
    self.data.AddSample('Test', 17)
    self.data.CalculateStats()
    summary = self.data.GetSummary()
    self.assertEqual(1, summary['Test']['count'])

  def test_UnitSuffix(self):
    """Unit gets appended as a suffix in the displayed summary."""
    self.data.AddSample('test', 250)
    self.data.SetUnit('test', 'mw')
    self.data.CalculateStats()
    summary_str = self.data.SummaryToString()
    self.assertIn('test_mw', summary_str)

  def test_DoubleUnitSuffix(self):
    """If domain already ends in unit, verify that unit doesn't get appended."""
    self.data.AddSample('test_mw', 250)
    self.data.SetUnit('test_mw', 'mw')
    self.data.CalculateStats()
    summary_str = self.data.SummaryToString()
    self.assertIn('test_mw', summary_str)
    self.assertNotIn('test_mw_mw', summary_str)

  def test_GetRawData(self):
    """GetRawData returns exact same data as fed in."""
    self._populate_dummy_stats()
    raw_data = self.data.GetRawData()
    self.assertListEqual([99999.5, 100000.5], raw_data['A'])
    self.assertListEqual([1.5, 2.5, 3.5], raw_data['B'])

  def test_GetSummary(self):
    """GetSummary returns expected stats about the data fed in."""
    self._populate_dummy_stats()
    summary = self.data.GetSummary()
    self.assertEqual(2, summary['A']['count'])
    self.assertAlmostEqual(100000.5, summary['A']['max'])
    self.assertAlmostEqual(99999.5, summary['A']['min'])
    self.assertAlmostEqual(0.5, summary['A']['stddev'])
    self.assertAlmostEqual(100000.0, summary['A']['mean'])
    self.assertEqual(3, summary['B']['count'])
    self.assertAlmostEqual(3.5, summary['B']['max'])
    self.assertAlmostEqual(1.5, summary['B']['min'])
    self.assertAlmostEqual(0.81649658092773, summary['B']['stddev'])
    self.assertAlmostEqual(2.5, summary['B']['mean'])

  def test_SaveRawData(self):
    """SaveRawData stores same data as fed in."""
    self._populate_dummy_stats()
    dirname = 'unittest_raw_data'
    expected_files = set(['A_mW.txt', 'B_mV.txt'])
    fnames = self.data.SaveRawData(self.tempdir, dirname)
    files_returned = set([os.path.basename(f) for f in fnames])
    # Assert that only the expected files got returned.
    self.assertEqual(expected_files, files_returned)
    # Assert that only the returned files are in the outdir.
    self.assertEqual(set(os.listdir(os.path.join(self.tempdir, dirname))),
                     files_returned)
    for fname in fnames:
      with open(fname, 'r') as f:
        if 'A_mW' in fname:
          self.assertEqual('99999.50', f.readline().strip())
          self.assertEqual('100000.50', f.readline().strip())
        if 'B_mV' in fname:
          self.assertEqual('1.50', f.readline().strip())
          self.assertEqual('2.50', f.readline().strip())
          self.assertEqual('3.50', f.readline().strip())

  def test_SaveRawDataNoUnit(self):
    """SaveRawData appends no unit suffix if the unit is not specified."""
    self._populate_dummy_stats_no_unit()
    self.data.CalculateStats()
    outdir = 'unittest_raw_data'
    files = self.data.SaveRawData(self.tempdir, outdir)
    files = [os.path.basename(f) for f in files]
    # Verify nothing gets appended to domain for filename if no unit exists.
    self.assertIn('B.txt', files)

  def test_SummaryToStringHideDomains(self):
    """Keys indicated in hide_domains are not printed in the summary."""
    data = stats_manager.StatsManager(hide_domains=['A-domain'])
    data.AddSample('A-domain', 17)
    data.AddSample('B-domain', 17)
    data.CalculateStats()
    summary_str = data.SummaryToString()
    self.assertIn('B-domain', summary_str)
    self.assertNotIn('A-domain', summary_str)

  def test_SummaryToStringOrder(self):
    """Order passed into StatsManager is honoured when formatting summary."""
    # StatsManager that should print D & B first, and the subsequent elements
    # are sorted.
    d_b_a_c_regexp = re.compile('D-domain.*B-domain.*A-domain.*C-domain',
                                re.DOTALL)
    data = stats_manager.StatsManager(order=['D-domain', 'B-domain'])
    data.AddSample('A-domain', 17)
    data.AddSample('B-domain', 17)
    data.AddSample('C-domain', 17)
    data.AddSample('D-domain', 17)
    data.CalculateStats()
    summary_str = data.SummaryToString()
    self.assertRegexpMatches(summary_str, d_b_a_c_regexp)

  def test_SaveSummary(self):
    """SaveSummary properly dumps the summary into a file."""
    self._populate_dummy_stats()
    fname = 'unittest_summary.txt'
    expected_fname = os.path.join(self.tempdir, fname)
    fname = self.data.SaveSummary(self.tempdir, fname)
    # Assert the reported fname is the same as the expected fname
    self.assertEqual(expected_fname, fname)
    # Assert only the reported fname is output (in the tempdir)
    self.assertEqual(set([os.path.basename(fname)]),
                     set(os.listdir(self.tempdir)))
    with open(fname, 'r') as f:
      self.assertEqual(
          '@@   NAME  COUNT       MEAN  STDDEV        MAX       MIN\n',
          f.readline())
      self.assertEqual(
          '@@   A_mW      2  100000.00    0.50  100000.50  99999.50\n',
          f.readline())
      self.assertEqual(
          '@@   B_mV      3       2.50    0.82       3.50      1.50\n',
          f.readline())

  def test_SaveSummaryJSON(self):
    """SaveSummaryJSON saves the added data properly in JSON format."""
    self._populate_dummy_stats()
    fname = 'unittest_summary.json'
    expected_fname = os.path.join(self.tempdir, fname)
    fname = self.data.SaveSummaryJSON(self.tempdir, fname)
    # Assert the reported fname is the same as the expected fname
    self.assertEqual(expected_fname, fname)
    # Assert only the reported fname is output (in the tempdir)
    self.assertEqual(set([os.path.basename(fname)]),
                     set(os.listdir(self.tempdir)))
    with open(fname, 'r') as f:
      summary = json.load(f)
      self.assertAlmostEqual(100000.0, summary['A']['mean'])
      self.assertEqual('milliwatt', summary['A']['unit'])
      self.assertAlmostEqual(2.5, summary['B']['mean'])
      self.assertEqual('millivolt', summary['B']['unit'])

  def test_SaveSummaryJSONNoUnit(self):
    """SaveSummaryJSON marks unknown units properly as N/A."""
    self._populate_dummy_stats_no_unit()
    self.data.CalculateStats()
    fname = 'unittest_summary.json'
    fname = self.data.SaveSummaryJSON(self.tempdir, fname)
    with open(fname, 'r') as f:
      summary = json.load(f)
      self.assertEqual('blue', summary['A']['unit'])
      # if no unit is specified, JSON should save 'N/A' as the unit.
      self.assertEqual('N/A', summary['B']['unit'])

if __name__ == '__main__':
  unittest.main()