Ran black on sources

This commit is contained in:
Griatch 2019-10-27 22:37:32 +01:00
parent 7fed39ad46
commit b83f710864
9 changed files with 235 additions and 255 deletions

View file

@ -5,19 +5,23 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0008_auto_20190128_1820'),
]
dependencies = [("accounts", "0008_auto_20190128_1820")]
operations = [
migrations.AlterField(
model_name='accountdb',
name='db_typeclass_path',
field=models.CharField(db_index=True, help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name='typeclass'),
model_name="accountdb",
name="db_typeclass_path",
field=models.CharField(
db_index=True,
help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.",
max_length=255,
null=True,
verbose_name="typeclass",
),
),
migrations.AlterField(
model_name='accountdb',
name='last_name',
field=models.CharField(blank=True, max_length=150, verbose_name='last name'),
model_name="accountdb",
name="last_name",
field=models.CharField(blank=True, max_length=150, verbose_name="last name"),
),
]

View file

@ -5,14 +5,18 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('comms', '0017_auto_20190128_1820'),
]
dependencies = [("comms", "0017_auto_20190128_1820")]
operations = [
migrations.AlterField(
model_name='channeldb',
name='db_typeclass_path',
field=models.CharField(db_index=True, help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name='typeclass'),
),
model_name="channeldb",
name="db_typeclass_path",
field=models.CharField(
db_index=True,
help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.",
max_length=255,
null=True,
verbose_name="typeclass",
),
)
]

View file

@ -7,19 +7,35 @@ import re
class Migration(migrations.Migration):
dependencies = [
('objects', '0010_auto_20190128_1820'),
]
dependencies = [("objects", "0010_auto_20190128_1820")]
operations = [
migrations.AlterField(
model_name='objectdb',
name='db_sessid',
field=models.CharField(help_text='csv list of session ids of connected Account, if any.', max_length=32, null=True, validators=[django.core.validators.RegexValidator(re.compile('^\\d+(?:,\\d+)*\\Z'), code='invalid', message='Enter only digits separated by commas.')], verbose_name='session id'),
model_name="objectdb",
name="db_sessid",
field=models.CharField(
help_text="csv list of session ids of connected Account, if any.",
max_length=32,
null=True,
validators=[
django.core.validators.RegexValidator(
re.compile("^\\d+(?:,\\d+)*\\Z"),
code="invalid",
message="Enter only digits separated by commas.",
)
],
verbose_name="session id",
),
),
migrations.AlterField(
model_name='objectdb',
name='db_typeclass_path',
field=models.CharField(db_index=True, help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name='typeclass'),
model_name="objectdb",
name="db_typeclass_path",
field=models.CharField(
db_index=True,
help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.",
max_length=255,
null=True,
verbose_name="typeclass",
),
),
]

View file

@ -5,14 +5,18 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('scripts', '0012_auto_20190128_1820'),
]
dependencies = [("scripts", "0012_auto_20190128_1820")]
operations = [
migrations.AlterField(
model_name='scriptdb',
name='db_typeclass_path',
field=models.CharField(db_index=True, help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name='typeclass'),
),
model_name="scriptdb",
name="db_typeclass_path",
field=models.CharField(
db_index=True,
help_text="this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.",
max_length=255,
null=True,
verbose_name="typeclass",
),
)
]

View file

@ -5,14 +5,19 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('typeclasses', '0012_attrs_to_picklev4_may_be_slow'),
]
dependencies = [("typeclasses", "0012_attrs_to_picklev4_may_be_slow")]
operations = [
migrations.AlterField(
model_name='tag',
name='db_tagtype',
field=models.CharField(blank=True, db_index=True, help_text='overall type of Tag', max_length=16, null=True, verbose_name='tagtype'),
),
model_name="tag",
name="db_tagtype",
field=models.CharField(
blank=True,
db_index=True,
help_text="overall type of Tag",
max_length=16,
null=True,
verbose_name="tagtype",
),
)
]

View file

@ -67,7 +67,12 @@ class Tag(models.Model):
)
# this is None, alias or permission
db_tagtype = models.CharField(
"tagtype", max_length=16, null=True, blank=True, help_text="overall type of Tag", db_index=True
"tagtype",
max_length=16,
null=True,
blank=True,
help_text="overall type of Tag",
db_index=True,
)
class Meta(object):

View file

@ -168,9 +168,9 @@ class TestEvEditor(CommandTest):
)
self.call(
eveditor.CmdLineInput(),
"First test \"line\".",
raw_string="First test \"line\".",
msg="01First test \"line\" .",
'First test "line".',
raw_string='First test "line".',
msg='01First test "line" .',
)
self.call(
eveditor.CmdLineInput(),
@ -178,7 +178,9 @@ class TestEvEditor(CommandTest):
raw_string="Second 'line'.",
msg="02Second 'line' .",
)
self.assertEqual(self.char1.ndb._eveditor.get_buffer(), "First test \"line\".\nSecond 'line'.")
self.assertEqual(
self.char1.ndb._eveditor.get_buffer(), "First test \"line\".\nSecond 'line'."
)
self.call(
eveditor.CmdEditorGroup(),
"",
@ -195,21 +197,9 @@ class TestEvEditor(CommandTest):
cmdstring=":",
msg="Line Editor []\n01\n[l:01 w:000 c:0000](:h for help)",
)
self.call(
eveditor.CmdLineInput(), "line 1.",
raw_string="line 1.",
msg="01line 1.",
)
self.call(
eveditor.CmdLineInput(), "line 2.",
raw_string="line 2.",
msg="02line 2.",
)
self.call(
eveditor.CmdLineInput(), "line 3.",
raw_string="line 3.",
msg="03line 3.",
)
self.call(eveditor.CmdLineInput(), "line 1.", raw_string="line 1.", msg="01line 1.")
self.call(eveditor.CmdLineInput(), "line 2.", raw_string="line 2.", msg="02line 2.")
self.call(eveditor.CmdLineInput(), "line 3.", raw_string="line 3.", msg="03line 3.")
self.call(
eveditor.CmdEditorGroup(),
"2:3",
@ -222,16 +212,14 @@ class TestEvEditor(CommandTest):
cmdstring=":s",
msg="Search-replaced line -> LINE for lines 1-2.",
)
self.assertEqual(self.char1.ndb._eveditor.get_buffer(),
"LINE 1.\nLINE 2.\nline 3.")
self.assertEqual(self.char1.ndb._eveditor.get_buffer(), "LINE 1.\nLINE 2.\nline 3.")
self.call(
eveditor.CmdEditorGroup(),
"line MINE",
cmdstring=":s",
msg="Search-replaced line -> MINE for lines 1-3.",
)
self.assertEqual(self.char1.ndb._eveditor.get_buffer(),
"LINE 1.\nLINE 2.\nMINE 3.")
self.assertEqual(self.char1.ndb._eveditor.get_buffer(), "LINE 1.\nLINE 2.\nMINE 3.")
def test_eveditor_COLON_DD(self):
eveditor.EvEditor(self.char1)
@ -241,26 +229,11 @@ class TestEvEditor(CommandTest):
cmdstring=":",
msg="Line Editor []\n01\n[l:01 w:000 c:0000](:h for help)",
)
self.call(eveditor.CmdLineInput(), "line 1.", raw_string="line 1.", msg="01line 1.")
self.call(eveditor.CmdLineInput(), "line 2.", raw_string="line 2.", msg="02line 2.")
self.call(eveditor.CmdLineInput(), "line 3.", raw_string="line 3.", msg="03line 3.")
self.call(
eveditor.CmdLineInput(), "line 1.",
raw_string="line 1.",
msg="01line 1.",
)
self.call(
eveditor.CmdLineInput(), "line 2.",
raw_string="line 2.",
msg="02line 2.",
)
self.call(
eveditor.CmdLineInput(), "line 3.",
raw_string="line 3.",
msg="03line 3.",
)
self.call(
eveditor.CmdEditorGroup(),
"",
cmdstring=":DD",
msg="Cleared 3 lines from buffer.",
eveditor.CmdEditorGroup(), "", cmdstring=":DD", msg="Cleared 3 lines from buffer."
)
self.assertEqual(self.char1.ndb._eveditor.get_buffer(), "")
@ -272,17 +245,8 @@ class TestEvEditor(CommandTest):
cmdstring=":",
msg="Line Editor []\n01\n[l:01 w:000 c:0000](:h for help)",
)
self.call(
eveditor.CmdLineInput(), "line 1",
raw_string="line 1",
msg="01line 1",
)
self.call(
eveditor.CmdEditorGroup(),
"1:2",
cmdstring=":f",
msg="Flood filled lines 1-2.",
)
self.call(eveditor.CmdLineInput(), "line 1", raw_string="line 1", msg="01line 1")
self.call(eveditor.CmdEditorGroup(), "1:2", cmdstring=":f", msg="Flood filled lines 1-2.")
self.assertEqual(self.char1.ndb._eveditor.get_buffer(), "line 1")
def test_eveditor_COLON_J(self):
@ -293,44 +257,13 @@ class TestEvEditor(CommandTest):
cmdstring=":",
msg="Line Editor []\n01\n[l:01 w:000 c:0000](:h for help)",
)
self.call(
eveditor.CmdLineInput(), "line 1",
raw_string="line 1",
msg="01line 1",
)
self.call(
eveditor.CmdLineInput(), "l 2",
raw_string="l 2",
msg="02l 2",
)
self.call(
eveditor.CmdLineInput(), "l 3",
raw_string="l 3",
msg="03l 3",
)
self.call(
eveditor.CmdLineInput(), "l 4",
raw_string="l 4",
msg="04l 4",
)
self.call(
eveditor.CmdEditorGroup(),
"2 r",
cmdstring=":j",
msg="Right-justified line 2.",
)
self.call(
eveditor.CmdEditorGroup(),
"3 c",
cmdstring=":j",
msg="Center-justified line 3.",
)
self.call(
eveditor.CmdEditorGroup(),
"4 f",
cmdstring=":j",
msg="Full-justified line 4.",
)
self.call(eveditor.CmdLineInput(), "line 1", raw_string="line 1", msg="01line 1")
self.call(eveditor.CmdLineInput(), "l 2", raw_string="l 2", msg="02l 2")
self.call(eveditor.CmdLineInput(), "l 3", raw_string="l 3", msg="03l 3")
self.call(eveditor.CmdLineInput(), "l 4", raw_string="l 4", msg="04l 4")
self.call(eveditor.CmdEditorGroup(), "2 r", cmdstring=":j", msg="Right-justified line 2.")
self.call(eveditor.CmdEditorGroup(), "3 c", cmdstring=":j", msg="Center-justified line 3.")
self.call(eveditor.CmdEditorGroup(), "4 f", cmdstring=":j", msg="Full-justified line 4.")
l1, l2, l3, l4 = tuple(self.char1.ndb._eveditor.get_buffer().split("\n"))
self.assertEqual(l1, "line 1")
self.assertEqual(l2, " " * 75 + "l 2")
@ -345,11 +278,7 @@ class TestEvEditor(CommandTest):
cmdstring=":",
msg="Line Editor []\n01\n[l:01 w:000 c:0000](:h for help)",
)
self.call(
eveditor.CmdLineInput(), "line 1.",
raw_string="line 1.",
msg="01line 1.",
)
self.call(eveditor.CmdLineInput(), "line 1.", raw_string="line 1.", msg="01line 1.")
self.call(
eveditor.CmdEditorGroup(),
"",

View file

@ -7,19 +7,28 @@ import mock
class TestText2Html(TestCase):
def test_re_color(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.re_color("foo"))
self.assertEqual(
"<span class=\"color-001\">red</span>foo",
parser.re_color(ansi.ANSI_UNHILITE + ansi.ANSI_RED + "red" + ansi.ANSI_NORMAL + "foo"))
'<span class="color-001">red</span>foo',
parser.re_color(ansi.ANSI_UNHILITE + ansi.ANSI_RED + "red" + ansi.ANSI_NORMAL + "foo"),
)
self.assertEqual(
"<span class=\"bgcolor-001\">red</span>foo",
parser.re_color(ansi.ANSI_BACK_RED + "red" + ansi.ANSI_NORMAL + "foo"))
'<span class="bgcolor-001">red</span>foo',
parser.re_color(ansi.ANSI_BACK_RED + "red" + ansi.ANSI_NORMAL + "foo"),
)
self.assertEqual(
"<span class=\"bgcolor-001\"><span class=\"color-002\">red</span></span>foo",
parser.re_color(ansi.ANSI_BACK_RED + ansi.ANSI_UNHILITE + ansi.ANSI_GREEN + "red" + ansi.ANSI_NORMAL + "foo"))
'<span class="bgcolor-001"><span class="color-002">red</span></span>foo',
parser.re_color(
ansi.ANSI_BACK_RED
+ ansi.ANSI_UNHILITE
+ ansi.ANSI_GREEN
+ "red"
+ ansi.ANSI_NORMAL
+ "foo"
),
)
@unittest.skip("parser issues")
def test_re_bold(self):
@ -28,41 +37,53 @@ class TestText2Html(TestCase):
self.assertEqual(
# "a <strong>red</strong>foo", # TODO: why not?
"a <strong>redfoo</strong>",
parser.re_bold(
"a " + ansi.ANSI_HILITE + "red" + ansi.ANSI_UNHILITE + "foo"))
parser.re_bold("a " + ansi.ANSI_HILITE + "red" + ansi.ANSI_UNHILITE + "foo"),
)
@unittest.skip("parser issues")
def test_re_underline(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.re_underline("foo"))
self.assertEqual(
"a <span class=\"underline\">red</span>" + ansi.ANSI_NORMAL + "foo",
'a <span class="underline">red</span>' + ansi.ANSI_NORMAL + "foo",
parser.re_underline(
"a " + ansi.ANSI_UNDERLINE + "red"
"a "
+ ansi.ANSI_UNDERLINE
+ "red"
+ ansi.ANSI_NORMAL # TODO: why does it keep it?
+ "foo"))
+ "foo"
),
)
@unittest.skip("parser issues")
def test_re_blinking(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.re_blinking("foo"))
self.assertEqual(
"a <span class=\"blink\">red</span>" + ansi.ANSI_NORMAL + "foo",
'a <span class="blink">red</span>' + ansi.ANSI_NORMAL + "foo",
parser.re_blinking(
"a " + ansi.ANSI_BLINK + "red"
"a "
+ ansi.ANSI_BLINK
+ "red"
+ ansi.ANSI_NORMAL # TODO: why does it keep it?
+ "foo"))
+ "foo"
),
)
@unittest.skip("parser issues")
def test_re_inversing(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.re_inversing("foo"))
self.assertEqual(
"a <span class=\"inverse\">red</span>" + ansi.ANSI_NORMAL + "foo",
'a <span class="inverse">red</span>' + ansi.ANSI_NORMAL + "foo",
parser.re_inversing(
"a " + ansi.ANSI_INVERSE + "red"
"a "
+ ansi.ANSI_INVERSE
+ "red"
+ ansi.ANSI_NORMAL # TODO: why does it keep it?
+ "foo"))
+ "foo"
),
)
@unittest.skip("parser issues")
def test_remove_bells(self):
@ -71,38 +92,40 @@ class TestText2Html(TestCase):
self.assertEqual(
"a red" + ansi.ANSI_NORMAL + "foo",
parser.remove_bells(
"a " + ansi.ANSI_BEEP + "red"
"a "
+ ansi.ANSI_BEEP
+ "red"
+ ansi.ANSI_NORMAL # TODO: why does it keep it?
+ "foo"))
+ "foo"
),
)
def test_remove_backspaces(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.remove_backspaces("foo"))
self.assertEqual("redfoo",
parser.remove_backspaces("a\010redfoo"))
self.assertEqual("redfoo", parser.remove_backspaces("a\010redfoo"))
def test_convert_linebreaks(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.convert_linebreaks("foo"))
self.assertEqual(
"a<br> redfoo<br>",
parser.convert_linebreaks("a\n redfoo\n"))
self.assertEqual("a<br> redfoo<br>", parser.convert_linebreaks("a\n redfoo\n"))
@unittest.skip("parser issues")
def test_convert_urls(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.convert_urls("foo"))
self.assertEqual(
"a <a href=\"http://redfoo\" target=\"_blank\">http://redfoo</a> runs",
parser.convert_urls("a http://redfoo runs"))
# TODO: doesn't URL encode correctly
'a <a href="http://redfoo" target="_blank">http://redfoo</a> runs',
parser.convert_urls("a http://redfoo runs"),
)
# TODO: doesn't URL encode correctly
def test_re_double_space(self):
parser = text2html.HTML_PARSER
self.assertEqual("foo", parser.re_double_space("foo"))
self.assertEqual(
"a &nbsp;red &nbsp;&nbsp;&nbsp;foo",
parser.re_double_space("a red foo"))
"a &nbsp;red &nbsp;&nbsp;&nbsp;foo", parser.re_double_space("a red foo")
)
def test_sub_mxp_links(self):
parser = text2html.HTML_PARSER
@ -112,63 +135,43 @@ class TestText2Html(TestCase):
r"""<a id="mxplink" href="#" """
"""onclick="Evennia.msg(&quot;text&quot;,[&quot;cmd&quot;],{});"""
"""return false;">text</a>""",
parser.sub_mxp_links(mocked_match))
parser.sub_mxp_links(mocked_match),
)
def test_sub_text(self):
parser = text2html.HTML_PARSER
mocked_match = mock.Mock()
mocked_match.groupdict.return_value = {
"htmlchars": "foo"
}
self.assertEqual(
"foo",
parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {
"htmlchars": "",
"lineend": "foo",
}
self.assertEqual(
"<br>",
parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {
"htmlchars": "",
"lineend": "",
"firstspace": "foo"
}
self.assertEqual(
" &nbsp;",
parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {"htmlchars": "foo"}
self.assertEqual("foo", parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "foo"}
self.assertEqual("<br>", parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "", "firstspace": "foo"}
self.assertEqual(" &nbsp;", parser.sub_text(mocked_match))
parser.tabstop = 2
mocked_match.groupdict.return_value = {
"htmlchars": "",
"lineend": "",
"firstspace": "",
"space": "\t"
"space": "\t",
}
self.assertEqual(
" &nbsp;&nbsp;",
parser.sub_text(mocked_match))
self.assertEqual(" &nbsp;&nbsp;", parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {
"htmlchars": "",
"lineend": "",
"firstspace": "",
"space": " ",
"spacestart": " "
"spacestart": " ",
}
mocked_match.group.return_value = " \t "
self.assertEqual(
"&nbsp;&nbsp;&nbsp;&nbsp;",
parser.sub_text(mocked_match))
self.assertEqual("&nbsp;&nbsp;&nbsp;&nbsp;", parser.sub_text(mocked_match))
mocked_match.groupdict.return_value = {
"htmlchars": "",
"lineend": "",
"firstspace": "",
"space": "",
"spacestart": ""
"spacestart": "",
}
self.assertEqual(
None,
parser.sub_text(mocked_match))
self.assertEqual(None, parser.sub_text(mocked_match))
def test_parse_html(self):
self.assertEqual("foo", text2html.parse_html("foo"))
@ -176,12 +179,24 @@ class TestText2Html(TestCase):
self.assertEqual(
"""<span class="blink"><span class="bgcolor-006">Hello </span><span class="underline"><span class="err">W</span><span class="err">o</span><span class="err">r</span><span class="err">l</span><span class="err">d</span><span class="err">!<span class="bgcolor-002">!</span></span></span></span>""",
text2html.parse_html(
ansi.ANSI_BLINK + ansi.ANSI_BACK_CYAN + "Hello " + ansi.ANSI_NORMAL
+ ansi.ANSI_UNDERLINE + ansi.ANSI_RED + "W"
+ ansi.ANSI_GREEN + "o"
+ ansi.ANSI_YELLOW + "r"
+ ansi.ANSI_BLUE + "l"
+ ansi.ANSI_MAGENTA + "d"
+ ansi.ANSI_CYAN + "!"
+ ansi.ANSI_BACK_GREEN + "!"))
ansi.ANSI_BLINK
+ ansi.ANSI_BACK_CYAN
+ "Hello "
+ ansi.ANSI_NORMAL
+ ansi.ANSI_UNDERLINE
+ ansi.ANSI_RED
+ "W"
+ ansi.ANSI_GREEN
+ "o"
+ ansi.ANSI_YELLOW
+ "r"
+ ansi.ANSI_BLUE
+ "l"
+ ansi.ANSI_MAGENTA
+ "d"
+ ansi.ANSI_CYAN
+ "!"
+ ansi.ANSI_BACK_GREEN
+ "!"
),
)

View file

@ -8,20 +8,19 @@ import pytz
class TestValidatorFuncs(TestCase):
def test_text_ok(self):
for val in [None, -123, 'abc', 1.234, {1:True, 2:False}, ['a', 1]]:
for val in [None, -123, "abc", 1.234, {1: True, 2: False}, ["a", 1]]:
self.assertEqual(str(val), validatorfuncs.text(val))
@mock.patch('builtins.str')
@mock.patch("builtins.str")
def test_text_raises_ValueError(self, mocked_str):
mocked_str.side_effect = Exception
with self.assertRaises(ValueError):
validatorfuncs.text(None)
def test_color_ok(self):
for color in ['r', 'g', 'b', 'H', 'R', 'M', '^']:
self.assertEqual(color, validatorfuncs.color(color))
for color in ["r", "g", "b", "H", "R", "M", "^"]:
self.assertEqual(color, validatorfuncs.color(color))
def test_color_falsy_raises_ValueError(self):
for color in [None, (), [], False, True, {}]:
@ -29,126 +28,125 @@ class TestValidatorFuncs(TestCase):
validatorfuncs.color(color)
def test_datetime_ok(self):
for dt in ['Oct 12 1:00 1492', 'Jan 2 12:00 2020', 'Dec 31 00:00 2018']:
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))
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']:
for dt in ["", "January 1, 2019", "1/1/2019", "Jan 1 2019"]:
with self.assertRaises(ValueError):
validatorfuncs.datetime(dt)
def test_duration_ok(self):
for d in ['1d', '2w', '3h', '4s', '5m', '6y']:
self.assertTrue(
isinstance(validatorfuncs.duration(d), datetime.timedelta))
for d in ["1d", "2w", "3h", "4s", "5m", "6y"]:
self.assertTrue(isinstance(validatorfuncs.duration(d), datetime.timedelta))
self.assertEqual(
datetime.timedelta(1+6*365, 2, 0, 0, 3, 4, 5),
validatorfuncs.duration('1d 2s 3m 4h 5w 6y'))
datetime.timedelta(1 + 6 * 365, 2, 0, 0, 3, 4, 5),
validatorfuncs.duration("1d 2s 3m 4h 5w 6y"),
)
# values may be duplicated
self.assertEqual(
datetime.timedelta((1+7)+(6+12)*365, 2+8, 0, 0, 3+9, 4+10, 5+11),
validatorfuncs.duration('1d 2s 3m 4h 5w 6y 7d 8s 9m 10h 11w 12y'))
datetime.timedelta((1 + 7) + (6 + 12) * 365, 2 + 8, 0, 0, 3 + 9, 4 + 10, 5 + 11),
validatorfuncs.duration("1d 2s 3m 4h 5w 6y 7d 8s 9m 10h 11w 12y"),
)
def test_duration_raises_ValueError(self):
for d in ['', '1', '5days', '1Week']:
for d in ["", "1", "5days", "1Week"]:
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}']:
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.datetime))
isinstance(validatorfuncs.future(f, from_tz=pytz.UTC), datetime.datetime)
)
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}']:
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))
for si in ["123", "4567890", "001", "-123", "-45", "0"]:
self.assertEqual(int(si), validatorfuncs.signed_integer(si))
@mock.patch('builtins.int')
@mock.patch("builtins.int")
def test_signed_integer_raises_ValueError(self, mocked_int):
for si in ['', '000', 'abc']:
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))
for pi in ["123", "4567890", "001"]:
self.assertEqual(int(pi), validatorfuncs.positive_integer(pi))
@mock.patch('builtins.int')
@mock.patch("builtins.int")
def test_positive_integer_raises_ValueError(self, mocked_int):
mocked_int.return_value = -1
with self.assertRaises(ValueError):
validatorfuncs.positive_integer(str(-1))
for pi in ['', '000', 'abc', '-1']:
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', '0']:
self.assertEqual(int(ui), validatorfuncs.unsigned_integer(ui))
for ui in ["123", "4567890", "001", "0"]:
self.assertEqual(int(ui), validatorfuncs.unsigned_integer(ui))
@mock.patch('builtins.int')
@mock.patch("builtins.int")
def test_unsigned_integer_raises_ValueError(self, mocked_int):
mocked_int.return_value = -1
with self.assertRaises(ValueError):
validatorfuncs.unsigned_integer(str(-1))
for ui in ['', '000', 'abc', '-1', '0']:
for ui in ["", "000", "abc", "-1", "0"]:
mocked_int.side_effect = ValueError
with self.assertRaises(ValueError):
validatorfuncs.unsigned_integer(ui)
def test_boolean(self):
for b in ['true', '1', 'on', 'ENABLED']:
for b in ["true", "1", "on", "ENABLED"]:
self.assertTrue(validatorfuncs.boolean(b))
for b in ['FalSe', '0', 'oFF', 'disabled']:
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}]:
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']:
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']:
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']:
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]:
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']:
for l in ["do:true;look:no", "a:t"]:
self.assertEqual(l, validatorfuncs.lock(l))
def test_lock_raises_ValueError(self):
for l in [';;;', '', ':', ':::', ';:;:', 'x:', ':y']:
for l in [";;;", "", ":", ":::", ";:;:", "x:", ":y"]:
with self.assertRaises(ValueError):
validatorfuncs.lock(l)
with self.assertRaises(ValueError):
validatorfuncs.lock('view:',
access_options=())
validatorfuncs.lock("view:", access_options=())
with self.assertRaises(ValueError):
validatorfuncs.lock('view:',
access_options=('look'))
validatorfuncs.lock("view:", access_options=("look"))