|
1 | 1 | import datetime |
| 2 | +import sys |
2 | 3 | from datetime import timezone |
3 | 4 | from typing import Sequence, Tuple |
4 | 5 | from unittest import TestCase |
|
7 | 8 | from click.testing import CliRunner |
8 | 9 | from dateutil.tz import tzlocal |
9 | 10 |
|
10 | | -from launchable.utils.click import DATETIME_WITH_TZ, KEY_VALUE, convert_to_seconds |
| 11 | +from launchable.utils.click import DATETIME_WITH_TZ, KEY_VALUE, PercentageType, convert_to_seconds |
| 12 | + |
| 13 | + |
| 14 | +class PercentageTypeTest(TestCase): |
| 15 | + ERROR_MSG = "Expected percentage like 50% but got" |
| 16 | + WINDOWS_ERROR_MSG = "please write '50%%' to pass in '50%'" |
| 17 | + |
| 18 | + def test_invalid_value_windows(self): |
| 19 | + pct = PercentageType() |
| 20 | + orig_platform = sys.platform |
| 21 | + sys.platform = "win32" |
| 22 | + try: |
| 23 | + with self.assertRaises(click.BadParameter) as cm: |
| 24 | + pct.convert("50", None, None) |
| 25 | + msg = str(cm.exception) |
| 26 | + self.assertIn(self.ERROR_MSG + " '50'", msg) |
| 27 | + self.assertIn(self.WINDOWS_ERROR_MSG, msg) |
| 28 | + finally: |
| 29 | + sys.platform = orig_platform |
| 30 | + |
| 31 | + def test_invalid_value_non_windows(self): |
| 32 | + pct = PercentageType() |
| 33 | + orig_platform = sys.platform |
| 34 | + sys.platform = "linux" |
| 35 | + try: |
| 36 | + with self.assertRaises(click.BadParameter) as cm: |
| 37 | + pct.convert("50", None, None) |
| 38 | + msg = str(cm.exception) |
| 39 | + self.assertIn(self.ERROR_MSG + " '50'", msg) |
| 40 | + self.assertNotIn(self.WINDOWS_ERROR_MSG, msg) |
| 41 | + finally: |
| 42 | + sys.platform = orig_platform |
| 43 | + |
| 44 | + def test_invalid_float(self): |
| 45 | + pct = PercentageType() |
| 46 | + with self.assertRaises(click.BadParameter) as cm: |
| 47 | + pct.convert("abc%", None, None) |
| 48 | + msg = str(cm.exception) |
| 49 | + self.assertIn(self.ERROR_MSG + " 'abc%'", msg) |
| 50 | + |
| 51 | + def test_valid(self): |
| 52 | + pct = PercentageType() |
| 53 | + self.assertEqual(pct.convert("50%", None, None), 0.5) |
| 54 | + self.assertEqual(pct.convert("0%", None, None), 0.0) |
| 55 | + self.assertEqual(pct.convert("100%", None, None), 1.0) |
11 | 56 |
|
12 | 57 |
|
13 | 58 | class DurationTypeTest(TestCase): |
|
0 commit comments