| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: | 10 # lowing conditions: |
| 10 # | 11 # |
| 11 # The above copyright notice and this permission notice shall be included | 12 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. | 13 # in all copies or substantial portions of the Software. |
| 13 # | 14 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. | 21 # IN THE SOFTWARE. |
| 21 | |
| 22 """Tests for gsutil utility functions.""" | 22 """Tests for gsutil utility functions.""" |
| 23 | 23 |
| 24 from __future__ import absolute_import |
| 25 |
| 24 from gslib import util | 26 from gslib import util |
| 27 import gslib.tests.testcase as testcase |
| 25 from gslib.util import CompareVersions | 28 from gslib.util import CompareVersions |
| 26 import gslib.tests.testcase as testcase | |
| 27 | 29 |
| 28 | 30 |
| 29 class TestUtil(testcase.GsUtilUnitTestCase): | 31 class TestUtil(testcase.GsUtilUnitTestCase): |
| 30 """Tests for utility functions.""" | 32 """Tests for utility functions.""" |
| 31 | 33 |
| 32 def test_MakeHumanReadable(self): | 34 def test_MakeHumanReadable(self): |
| 35 """Tests converting byte counts to human-readable strings.""" |
| 33 self.assertEqual(util.MakeHumanReadable(0), '0 B') | 36 self.assertEqual(util.MakeHumanReadable(0), '0 B') |
| 34 self.assertEqual(util.MakeHumanReadable(1023), '1023 B') | 37 self.assertEqual(util.MakeHumanReadable(1023), '1023 B') |
| 35 self.assertEqual(util.MakeHumanReadable(1024), '1 KB') | 38 self.assertEqual(util.MakeHumanReadable(1024), '1 KB') |
| 36 self.assertEqual(util.MakeHumanReadable(1024 ** 2), '1 MB') | 39 self.assertEqual(util.MakeHumanReadable(1024 ** 2), '1 MB') |
| 37 self.assertEqual(util.MakeHumanReadable(1024 ** 3), '1 GB') | 40 self.assertEqual(util.MakeHumanReadable(1024 ** 3), '1 GB') |
| 38 self.assertEqual(util.MakeHumanReadable(1024 ** 3 * 5.3), '5.3 GB') | 41 self.assertEqual(util.MakeHumanReadable(1024 ** 3 * 5.3), '5.3 GB') |
| 39 self.assertEqual(util.MakeHumanReadable(1024 ** 4 * 2.7), '2.7 TB') | 42 self.assertEqual(util.MakeHumanReadable(1024 ** 4 * 2.7), '2.7 TB') |
| 40 self.assertEqual(util.MakeHumanReadable(1024 ** 5), '1 PB') | 43 self.assertEqual(util.MakeHumanReadable(1024 ** 5), '1 PB') |
| 41 self.assertEqual(util.MakeHumanReadable(1024 ** 6), '1 EB') | 44 self.assertEqual(util.MakeHumanReadable(1024 ** 6), '1 EB') |
| 42 | 45 |
| 43 def test_MakeBitsHumanReadable(self): | 46 def test_MakeBitsHumanReadable(self): |
| 47 """Tests converting bit counts to human-readable strings.""" |
| 44 self.assertEqual(util.MakeBitsHumanReadable(0), '0 bit') | 48 self.assertEqual(util.MakeBitsHumanReadable(0), '0 bit') |
| 45 self.assertEqual(util.MakeBitsHumanReadable(1023), '1023 bit') | 49 self.assertEqual(util.MakeBitsHumanReadable(1023), '1023 bit') |
| 46 self.assertEqual(util.MakeBitsHumanReadable(1024), '1 Kbit') | 50 self.assertEqual(util.MakeBitsHumanReadable(1024), '1 Kbit') |
| 47 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 2), '1 Mbit') | 51 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 2), '1 Mbit') |
| 48 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3), '1 Gbit') | 52 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3), '1 Gbit') |
| 49 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3 * 5.3), '5.3 Gbit') | 53 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3 * 5.3), '5.3 Gbit') |
| 50 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 4 * 2.7), '2.7 Tbit') | 54 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 4 * 2.7), '2.7 Tbit') |
| 51 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 5), '1 Pbit') | 55 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 5), '1 Pbit') |
| 52 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 6), '1 Ebit') | 56 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 6), '1 Ebit') |
| 53 | 57 |
| 54 def test_HumanReadableToBytes(self): | 58 def test_HumanReadableToBytes(self): |
| 59 """Tests converting human-readable strings to byte counts.""" |
| 55 self.assertEqual(util.HumanReadableToBytes('1'), 1) | 60 self.assertEqual(util.HumanReadableToBytes('1'), 1) |
| 56 self.assertEqual(util.HumanReadableToBytes('15'), 15) | 61 self.assertEqual(util.HumanReadableToBytes('15'), 15) |
| 57 self.assertEqual(util.HumanReadableToBytes('15.3'), 15) | 62 self.assertEqual(util.HumanReadableToBytes('15.3'), 15) |
| 58 self.assertEqual(util.HumanReadableToBytes('15.7'), 16) | 63 self.assertEqual(util.HumanReadableToBytes('15.7'), 16) |
| 59 self.assertEqual(util.HumanReadableToBytes('1023'), 1023) | 64 self.assertEqual(util.HumanReadableToBytes('1023'), 1023) |
| 60 self.assertEqual(util.HumanReadableToBytes('1k'), 1024) | 65 self.assertEqual(util.HumanReadableToBytes('1k'), 1024) |
| 61 self.assertEqual(util.HumanReadableToBytes('2048'), 2048) | 66 self.assertEqual(util.HumanReadableToBytes('2048'), 2048) |
| 62 self.assertEqual(util.HumanReadableToBytes('1 K'), 1024) | 67 self.assertEqual(util.HumanReadableToBytes('1 K'), 1024) |
| 63 self.assertEqual(util.HumanReadableToBytes('1 mb'), 1024 ** 2) | 68 self.assertEqual(util.HumanReadableToBytes('1 mb'), 1024 ** 2) |
| 64 self.assertEqual(util.HumanReadableToBytes('1 GB'), 1024 ** 3) | 69 self.assertEqual(util.HumanReadableToBytes('1 GB'), 1024 ** 3) |
| 65 self.assertEqual(util.HumanReadableToBytes('1T'), 1024 ** 4) | 70 self.assertEqual(util.HumanReadableToBytes('1T'), 1024 ** 4) |
| 66 self.assertEqual(util.HumanReadableToBytes('1\t pb'), 1024 ** 5) | 71 self.assertEqual(util.HumanReadableToBytes('1\t pb'), 1024 ** 5) |
| 67 self.assertEqual(util.HumanReadableToBytes('1e'), 1024 ** 6) | 72 self.assertEqual(util.HumanReadableToBytes('1e'), 1024 ** 6) |
| 68 | 73 |
| 69 def test_CompareVersions(self): | 74 def test_CompareVersions(self): |
| 75 """Tests CompareVersions for various use cases.""" |
| 70 # CompareVersions(first, second) returns (g, m), where | 76 # CompareVersions(first, second) returns (g, m), where |
| 71 # g is True if first known to be greater than second, else False. | 77 # g is True if first known to be greater than second, else False. |
| 72 # m is True if first known to be greater by at least 1 major version, | 78 # m is True if first known to be greater by at least 1 major version, |
| 73 (g, m) = CompareVersions('3.37', '3.2') | 79 (g, m) = CompareVersions('3.37', '3.2') |
| 74 self.assertTrue(g) | 80 self.assertTrue(g) |
| 75 self.assertFalse(m) | 81 self.assertFalse(m) |
| 76 (g, m) = CompareVersions('7', '2') | 82 (g, m) = CompareVersions('7', '2') |
| 77 self.assertTrue(g) | 83 self.assertTrue(g) |
| 78 self.assertTrue(m) | 84 self.assertTrue(m) |
| 79 (g, m) = CompareVersions('3.32', '3.32pre') | 85 (g, m) = CompareVersions('3.32', '3.32pre') |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 self.assertFalse(m) | 120 self.assertFalse(m) |
| 115 (g, m) = CompareVersions('3.3', '3.4') | 121 (g, m) = CompareVersions('3.3', '3.4') |
| 116 self.assertFalse(g) | 122 self.assertFalse(g) |
| 117 self.assertFalse(m) | 123 self.assertFalse(m) |
| 118 (g, m) = CompareVersions('4.1', '3.33') | 124 (g, m) = CompareVersions('4.1', '3.33') |
| 119 self.assertTrue(g) | 125 self.assertTrue(g) |
| 120 self.assertTrue(m) | 126 self.assertTrue(m) |
| 121 (g, m) = CompareVersions('3.10', '3.1') | 127 (g, m) = CompareVersions('3.10', '3.1') |
| 122 self.assertTrue(g) | 128 self.assertTrue(g) |
| 123 self.assertFalse(m) | 129 self.assertFalse(m) |
| OLD | NEW |