def test_validator_datetime_isoformat(self) -> None: @validate(Parameter(name='x', validators=[DatetimeIsoFormat()])) def foo(x): return x now = datetime.now() self.assertTrue( abs((now - foo(now.isoformat()) < timedelta(milliseconds=1)))) with self.assertRaises(expected_exception=ParameterException): foo('12.12.2020') with self.assertRaises(expected_exception=ParameterException): foo('invalid')
def test_validator_max_length_include_boundary_true(self) -> None: @validate( Parameter(name='x', validators=[Max(3, include_boundary=True)])) def foo(x): return x self.assertEqual(3, foo(3)) self.assertEqual(2, foo(2)) with self.assertRaises(expected_exception=ParameterException): foo(4) with self.assertRaises(expected_exception=ParameterException): foo(3.001)
def test_validator_composite(self) -> None: @validate(Parameter(name='x', validators=[Composite([Min(3), Max(5)])])) def foo(x): return x self.assertEqual(3, foo(3)) self.assertEqual(4, foo(4)) self.assertEqual(5, foo(5)) with self.assertRaises(expected_exception=ParameterException): foo(5.0001) with self.assertRaises(expected_exception=ParameterException): foo(2.9999)
def test_validator_is_enum_convert_true(self) -> None: @validate(Parameter(name='x', validators=[IsUuid()])) def foo(x): return x for id_ in [ str(uuid1()), str(uuid3(uuid1(), 'b')), str(uuid4()), str(uuid5(uuid1(), 'b')) ]: self.assertEqual(id_, foo(id_)) with self.assertRaises(expected_exception=ParameterException): foo('invalid')
def test_validator_min_length(self) -> None: @validate(Parameter(name='x', validators=[MinLength(3)])) def foo(x): return x self.assertEqual('hi!', foo('hi!')) self.assertEqual('hello', foo('hello')) self.assertEqual([1, 2, 3], foo([1, 2, 3])) with self.assertRaises(expected_exception=ParameterException): foo('hi') with self.assertRaises(expected_exception=ParameterException): foo([1, 2]) with self.assertRaises(expected_exception=ParameterException): foo(42)
def test_validator_datetime_unix_timestamp(self) -> None: @validate(Parameter(name='x', validators=[DateTimeUnixTimestamp()])) def foo(x): return x now = datetime.now() unix_timestamp = (now - datetime(year=1970, month=1, day=1)).total_seconds() self.assertEqual(now, foo(unix_timestamp)) self.assertEqual(now, foo(str(unix_timestamp))) with self.assertRaises(expected_exception=ParameterException): foo('12.12.2020') with self.assertRaises(expected_exception=ParameterException): foo('invalid') with self.assertRaises(expected_exception=ParameterException): foo({'a': 1}) with self.assertRaises(expected_exception=ParameterException): foo(unix_timestamp * 1000)