def test_unit_wrong_input(self): self.assertRaises(ValueError, tu.apply_units, '', self.units) self.assertRaises(ValueError, tu.apply_units, 'wrong input', self.units) self.assertRaises(ValueError, tu.apply_units, 'wrong13 input', self.units) self.assertRaises(ValueError, tu.apply_units, 'wrong input42', self.units) with self.assertRaises(ValueError) as cm: tu.apply_units('42 cakes', self.units) self.assertIn('invalid unit cakes.', str(cm.exception))
def test_empty_inter(self): def inter(value): return int(float(value)) * 2 result = tu.apply_units('12.4', {}, inter=inter) self.assertEqual(result, 12 * 2) self.assertIsInstance(result, float)
def _ensure_correctly_typed(self, form, value): if isinstance(value, str): value = value.strip() if not value: return None try: value = apply_units(value, TIME_UNITS) except ValueError: raise ProcessFormError( form._cw. _('a number (in seconds) or 20s, 10min, 24h or 4d are expected' )) return timedelta(0, value)
def check_bytes(option: Optional["Option"], opt: str, value: Any) -> int: if hasattr(value, "__int__"): return value # mypy: Incompatible return value type (got "Union[float, int]", expected "int") # we force "int" using "final=int" return apply_units(value, BYTE_UNITS, final=int) # type: ignore
def test_blank_space(self): result = tu.apply_units(' 1 337 ', {}, final=int) self.assertEqual(result, 1337)
def test_empty_base(self): self.assertEqual(tu.apply_units('17', {}), 17)
def test_empty_inter_final(self): result = tu.apply_units('12.4', {}, inter=float, final=int) self.assertEqual(result, 12) self.assertIsInstance(result, int)
def check_time(option, opt, value): from logilab.common.textutils import TIME_UNITS, apply_units if isinstance(value, (int, long, float)): return value return apply_units(value, TIME_UNITS)
def check_time(option, opt, value): if isinstance(value, (int, long, float)): return value return apply_units(value, TIME_UNITS)
def test_unit_singleunit_singleletter(self): result = tu.apply_units('15m', self.units) self.assertEqual(result, 15 * self.units['m'] )
def test_unit_singleunit_multipleletter(self): result = tu.apply_units('47KB', self.units) self.assertEqual(result, 47 * self.units['kb'] )
def test_blank_coma(self): result = tu.apply_units(' 4,298.42 ', {}) self.assertEqual(result, 4298.42)
def test_blank_mixed(self): result = tu.apply_units('45, 317, 337', {}, final=int) self.assertEqual(result, 45317337)
def test_blank_base(self): result = tu.apply_units(' 42 ', {}, final=int) self.assertEqual(result, 42)
def test_unit_singleunit_caseinsensitive(self): result = tu.apply_units('47kb', self.units) self.assertEqual(result, 47 * self.units['kb'] )
def check_bytes(option, opt, value): if hasattr(value, '__int__'): return value return apply_units(value, BYTE_UNITS)
def test_unit_multipleunit(self): result = tu.apply_units('47KB 1.5MB', self.units) self.assertEqual(result, 47 * self.units['kb'] + 1.5 * self.units['mb'])
def check_bytes(option, opt, value): from logilab.common.textutils import BYTE_UNITS, apply_units if hasattr(value, '__int__'): return value return apply_units(value, BYTE_UNITS)
def test_unit_with_blank(self): result = tu.apply_units('1 000 KB', self.units) self.assertEqual(result, 1000 * self.units['kb'])
def check_time(option, opt, value): if isinstance(value, integer_types + (float,)): return value return apply_units(value, TIME_UNITS)
def test_unit_singleunit_multipleletter(self): result = tu.apply_units('47KB', self.units) self.assertEqual(result, 47 * self.units['kb'])
def test_unit_singleunit_singleletter(self): result = tu.apply_units('15m', self.units) self.assertEqual(result, 15 * self.units['m'])
def test_unit_singleunit_caseinsensitive(self): result = tu.apply_units('47kb', self.units) self.assertEqual(result, 47 * self.units['kb'])
def check_time(option, opt, value): if isinstance(value, integer_types + (float, )): return value return apply_units(value, TIME_UNITS)