mirror of
https://github.com/evennia/evennia.git
synced 2026-03-28 02:36:32 +01:00
Tests for evennia/utils/validatorfuncs.
- future() - signed_integer() - positive_integer() - unsigned_integer() - boolean - timezone - email - lock
This commit is contained in:
parent
29971de444
commit
bb44d033e2
2 changed files with 105 additions and 26 deletions
|
|
@ -4,6 +4,7 @@ from django.test import TestCase
|
|||
from evennia.utils import validatorfuncs
|
||||
import mock
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
|
||||
class TestValidatorFuncs(TestCase):
|
||||
|
|
@ -15,9 +16,7 @@ class TestValidatorFuncs(TestCase):
|
|||
@mock.patch('builtins.str')
|
||||
def test_text_raises_ValueError(self, mocked_str):
|
||||
mocked_str.side_effect = Exception
|
||||
with self.assertRaises(
|
||||
ValueError,
|
||||
msg='Input could not be converted to text (Exception)'):
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.text(None)
|
||||
|
||||
def test_color_ok(self):
|
||||
|
|
@ -26,20 +25,18 @@ class TestValidatorFuncs(TestCase):
|
|||
|
||||
def test_color_falsy_raises_ValueError(self):
|
||||
for color in [None, (), [], False, True, {}]:
|
||||
with self.assertRaises(
|
||||
ValueError,
|
||||
msg=f'(color) is not valid Color.'):
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.color(color)
|
||||
|
||||
def test_datetime_ok(self):
|
||||
for dt in ['Jan 2 12:00', 'Dec 31 00:00 2018']:
|
||||
self.assertTrue(isinstance(validatorfuncs.datetime(dt), datetime.datetime))
|
||||
for dt in ['Oct 12 1:00 1492', 'Jan 2 12:00 2020', 'Dec 31 00:00 2018']:
|
||||
self.assertTrue(
|
||||
isinstance(validatorfuncs.datetime(dt, from_tz=pytz.UTC),
|
||||
datetime.datetime))
|
||||
|
||||
def test_datetime_raises_ValueError(self):
|
||||
for dt in ['', 'January 1, 2019', '1/1/2019', 'Jan 1 2019']:
|
||||
with self.assertRaises(
|
||||
ValueError,
|
||||
msg='Date must be entered in a 24-hr format such as: '):
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.datetime(dt)
|
||||
|
||||
def test_duration_ok(self):
|
||||
|
|
@ -54,7 +51,89 @@ class TestValidatorFuncs(TestCase):
|
|||
|
||||
def test_duration_raises_ValueError(self):
|
||||
for d in ['', '1', '5days', '1Week']:
|
||||
with self.assertRaises(
|
||||
ValueError,
|
||||
msg=f"Could not convert section 'd' to Duration."):
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.duration(d)
|
||||
|
||||
def test_future_ok(self):
|
||||
year = int(datetime.datetime.utcnow().strftime("%Y"))
|
||||
for f in [f'Jan 2 12:00 {year+1}', f'Dec 31 00:00 {year+1}']:
|
||||
self.assertTrue(
|
||||
isinstance(validatorfuncs.future(f, from_tz=pytz.UTC),
|
||||
datetime.timedelta))
|
||||
|
||||
def test_future_raises_ValueError(self):
|
||||
year = int(datetime.datetime.utcnow().strftime("%Y"))
|
||||
for f in [f'Jan 2 12:00 {year-1}', f'Dec 31 00:00 {year-1}']:
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.future(f, from_tz=pytz.UTC)
|
||||
|
||||
def test_signed_integer_ok(self):
|
||||
for si in ['123', '4567890', '001', '-123', '-45', '0']:
|
||||
self.assertEqual(int(si), validatorfuncs.signed_integer(si))
|
||||
|
||||
@mock.patch('builtins.int')
|
||||
def test_signed_integer_raises_ValueError(self, mocked_int):
|
||||
for si in ['', '000', 'abc']:
|
||||
mocked_int.side_effect = ValueError
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.signed_integer(si)
|
||||
|
||||
def test_positive_integer_ok(self):
|
||||
for pi in ['123', '4567890', '001']:
|
||||
self.assertEqual(int(pi), validatorfuncs.positive_integer(pi))
|
||||
|
||||
@mock.patch('builtins.int')
|
||||
def test_positive_integer_raises_ValueError(self, mocked_int):
|
||||
for pi in ['', '000', 'abc', '-1']:
|
||||
mocked_int.side_effect = ValueError
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.positive_integer(pi)
|
||||
|
||||
def test_unsigned_integer_ok(self):
|
||||
for ui in ['123', '4567890', '001']:
|
||||
self.assertEqual(int(ui), validatorfuncs.unsigned_integer(ui))
|
||||
|
||||
@mock.patch('builtins.int')
|
||||
def test_unsigned_integer_raises_ValueError(self, mocked_int):
|
||||
for ui in ['', '000', 'abc', '-1', '0']:
|
||||
mocked_int.side_effect = ValueError
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.signed_integer(ui)
|
||||
|
||||
def test_boolean(self):
|
||||
for b in ['true', '1', 'on', 'ENABLED']:
|
||||
self.assertTrue(validatorfuncs.boolean(b))
|
||||
for b in ['FalSe', '0', 'oFF', 'disabled']:
|
||||
self.assertFalse(validatorfuncs.boolean(b))
|
||||
|
||||
def test_boolean_raises_ValueError(self):
|
||||
for b in ['', None, 1, 0, True, False, [None], {True:True}]:
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.boolean(b)
|
||||
|
||||
def test_timezone_ok(self):
|
||||
for tz in ['America/Chicago', 'GMT', 'UTC']:
|
||||
self.assertEqual(tz, validatorfuncs.timezone(tz).zone)
|
||||
|
||||
def test_timezone_raises_ValueError(self):
|
||||
for tz in ['America', None, '', 'Mars', 'DT']:
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.timezone(tz)
|
||||
|
||||
def test_email_ok(self):
|
||||
for e in ['a@a.aa', 'zeus@olympus.net']:
|
||||
self.assertEqual(e, validatorfuncs.email(e))
|
||||
|
||||
def test_email_raises_ValueError(self):
|
||||
for e in ['', None, ['abc@abc.com'], 123]:
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.email(e)
|
||||
|
||||
def test_lock_ok(self):
|
||||
for l in ['do:true;look:no', 'a:t']:
|
||||
self.assertEqual(l, validatorfuncs.lock(l))
|
||||
|
||||
def test_lock_raises_ValueError(self):
|
||||
for l in [';;;', '', ':', ':::', ';:;:']:
|
||||
with self.assertRaises(ValueError):
|
||||
validatorfuncs.lock(l)
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ def duration(entry, option_key="Duration", **kwargs):
|
|||
timedelta
|
||||
|
||||
"""
|
||||
time_string = entry.split(" ")
|
||||
time_string = entry.lower().split(" ")
|
||||
seconds = 0
|
||||
minutes = 0
|
||||
hours = 0
|
||||
|
|
@ -101,18 +101,18 @@ def duration(entry, option_key="Duration", **kwargs):
|
|||
weeks = 0
|
||||
|
||||
for interval in time_string:
|
||||
if _re.match(r"^[\d]+s$", interval.lower()):
|
||||
seconds = +int(interval.lower().rstrip("s"))
|
||||
if _re.match(r"^[\d]+s$", interval):
|
||||
seconds = +int(interval.rstrip("s"))
|
||||
elif _re.match(r"^[\d]+m$", interval):
|
||||
minutes = +int(interval.lower().rstrip("m"))
|
||||
minutes = +int(interval.rstrip("m"))
|
||||
elif _re.match(r"^[\d]+h$", interval):
|
||||
hours = +int(interval.lower().rstrip("h"))
|
||||
hours = +int(interval.rstrip("h"))
|
||||
elif _re.match(r"^[\d]+d$", interval):
|
||||
days = +int(interval.lower().rstrip("d"))
|
||||
days = +int(interval.rstrip("d"))
|
||||
elif _re.match(r"^[\d]+w$", interval):
|
||||
weeks = +int(interval.lower().rstrip("w"))
|
||||
weeks = +int(interval.rstrip("w"))
|
||||
elif _re.match(r"^[\d]+y$", interval):
|
||||
days = +int(interval.lower().rstrip("y")) * 365
|
||||
days = +int(interval.rstrip("y")) * 365
|
||||
else:
|
||||
raise ValueError(f"Could not convert section '{interval}' to a {option_key}.")
|
||||
|
||||
|
|
@ -120,7 +120,7 @@ def duration(entry, option_key="Duration", **kwargs):
|
|||
|
||||
|
||||
def future(entry, option_key="Future Datetime", from_tz=None, **kwargs):
|
||||
time = datetime(entry, option_key)
|
||||
time = datetime(entry, option_key, from_tz=from_tz)
|
||||
if time < _dt.datetime.utcnow():
|
||||
raise ValueError(f"That {option_key} is in the past! Must give a Future datetime!")
|
||||
return time
|
||||
|
|
@ -160,10 +160,10 @@ def boolean(entry, option_key="True/False", **kwargs):
|
|||
Returns:
|
||||
Boolean
|
||||
"""
|
||||
entry = entry.upper()
|
||||
error = f"Must enter 0 (false) or 1 (true) for {option_key}. Also accepts True, False, On, Off, Yes, No, Enabled, and Disabled"
|
||||
if not entry:
|
||||
if not isinstance(entry, str):
|
||||
raise ValueError(error)
|
||||
entry = entry.upper()
|
||||
if entry in ("1", "TRUE", "ON", "ENABLED", "ENABLE", "YES"):
|
||||
return True
|
||||
if entry in ("0", "FALSE", "OFF", "DISABLED", "DISABLE", "NO"):
|
||||
|
|
@ -198,7 +198,7 @@ def email(entry, option_key="Email Address", **kwargs):
|
|||
if not entry:
|
||||
raise ValueError("Email address field empty!")
|
||||
try:
|
||||
_val_email(entry) # offloading the hard work to Django!
|
||||
_val_email(str(entry)) # offloading the hard work to Django!
|
||||
except _error:
|
||||
raise ValueError(f"That isn't a valid {option_key}!")
|
||||
return entry
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue