| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 2 | # | 
| Amin Hassani | f94b643 | 2018-01-26 17:39:47 -0800 | [diff] [blame] | 3 | # Copyright (C) 2013 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. | 
|  | 16 | # | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 17 |  | 
|  | 18 | """Unit tests for histogram.py.""" | 
|  | 19 |  | 
| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 20 | # Disable check for function names to avoid errors based on old code | 
|  | 21 | # pylint: disable-msg=invalid-name | 
|  | 22 |  | 
|  | 23 | from __future__ import absolute_import | 
|  | 24 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 25 | import unittest | 
|  | 26 |  | 
| Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 27 | from update_payload import format_utils | 
|  | 28 | from update_payload import histogram | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 29 |  | 
|  | 30 |  | 
|  | 31 | class HistogramTest(unittest.TestCase): | 
| Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 32 | """ Tests histogram""" | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | @staticmethod | 
|  | 35 | def AddHumanReadableSize(size): | 
|  | 36 | fmt = format_utils.BytesToHumanReadable(size) | 
|  | 37 | return '%s (%s)' % (size, fmt) if fmt else str(size) | 
|  | 38 |  | 
|  | 39 | def CompareToExpectedDefault(self, actual_str): | 
|  | 40 | expected_str = ( | 
|  | 41 | 'Yes |################    | 5 (83.3%)\n' | 
|  | 42 | 'No  |###                 | 1 (16.6%)' | 
|  | 43 | ) | 
|  | 44 | self.assertEqual(actual_str, expected_str) | 
|  | 45 |  | 
|  | 46 | def testExampleHistogram(self): | 
|  | 47 | self.CompareToExpectedDefault(str(histogram.Histogram( | 
|  | 48 | [('Yes', 5), ('No', 1)]))) | 
|  | 49 |  | 
|  | 50 | def testFromCountDict(self): | 
|  | 51 | self.CompareToExpectedDefault(str(histogram.Histogram.FromCountDict( | 
|  | 52 | {'Yes': 5, 'No': 1}))) | 
|  | 53 |  | 
|  | 54 | def testFromKeyList(self): | 
|  | 55 | self.CompareToExpectedDefault(str(histogram.Histogram.FromKeyList( | 
|  | 56 | ['Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes']))) | 
|  | 57 |  | 
|  | 58 | def testCustomScale(self): | 
|  | 59 | expected_str = ( | 
|  | 60 | 'Yes |#### | 5 (83.3%)\n' | 
|  | 61 | 'No  |     | 1 (16.6%)' | 
|  | 62 | ) | 
|  | 63 | actual_str = str(histogram.Histogram([('Yes', 5), ('No', 1)], scale=5)) | 
|  | 64 | self.assertEqual(actual_str, expected_str) | 
|  | 65 |  | 
|  | 66 | def testCustomFormatter(self): | 
|  | 67 | expected_str = ( | 
|  | 68 | 'Yes |################    | 5000 (4.8 KiB) (83.3%)\n' | 
|  | 69 | 'No  |###                 | 1000 (16.6%)' | 
|  | 70 | ) | 
|  | 71 | actual_str = str(histogram.Histogram( | 
|  | 72 | [('Yes', 5000), ('No', 1000)], formatter=self.AddHumanReadableSize)) | 
|  | 73 | self.assertEqual(actual_str, expected_str) | 
|  | 74 |  | 
|  | 75 |  | 
|  | 76 | if __name__ == '__main__': | 
|  | 77 | unittest.main() |