aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar scragly <[email protected]>2019-09-18 00:31:39 +1000
committerGravatar GitHub <[email protected]>2019-09-18 00:31:39 +1000
commit6036e1a67901c504dd7c62e2a50500c022a0df25 (patch)
tree8ef97d393f6635094e1fdb1e5415851ca285d7e4 /tests
parentRemove duplicate coverage.xml gitignore (diff)
parentAdd tests for `bot.converters`. (#418) (diff)
Merge branch 'master' into add-pytest-cov
Diffstat (limited to 'tests')
-rw-r--r--tests/test_converters.py93
-rw-r--r--tests/utils/test_checks.py31
2 files changed, 108 insertions, 16 deletions
diff --git a/tests/test_converters.py b/tests/test_converters.py
new file mode 100644
index 000000000..3cf774c80
--- /dev/null
+++ b/tests/test_converters.py
@@ -0,0 +1,93 @@
+import asyncio
+from datetime import datetime
+from unittest.mock import MagicMock
+
+import pytest
+from discord.ext.commands import BadArgument
+
+from bot.converters import (
+ ExpirationDate,
+ TagContentConverter,
+ TagNameConverter,
+ ValidPythonIdentifier,
+)
+
+
+ ('value', 'expected'),
+ (
+ # sorry aliens
+ ('2199-01-01T00:00:00', datetime(2199, 1, 1)),
+ )
+)
+def test_expiration_date_converter_for_valid(value: str, expected: datetime):
+ converter = ExpirationDate()
+ assert asyncio.run(converter.convert(None, value)) == expected
+
+
+ ('value', 'expected'),
+ (
+ ('hello', 'hello'),
+ (' h ello ', 'h ello')
+ )
+)
+def test_tag_content_converter_for_valid(value: str, expected: str):
+ assert asyncio.run(TagContentConverter.convert(None, value)) == expected
+
+
+ ('value', 'expected'),
+ (
+ ('', "Tag contents should not be empty, or filled with whitespace."),
+ (' ', "Tag contents should not be empty, or filled with whitespace.")
+ )
+)
+def test_tag_content_converter_for_invalid(value: str, expected: str):
+ context = MagicMock()
+ context.author = 'bob'
+
+ with pytest.raises(BadArgument, match=expected):
+ asyncio.run(TagContentConverter.convert(context, value))
+
+
+ ('value', 'expected'),
+ (
+ ('tracebacks', 'tracebacks'),
+ ('Tracebacks', 'tracebacks'),
+ (' Tracebacks ', 'tracebacks'),
+ )
+)
+def test_tag_name_converter_for_valid(value: str, expected: str):
+ assert asyncio.run(TagNameConverter.convert(None, value)) == expected
+
+
+ ('value', 'expected'),
+ (
+ ('👋', "Don't be ridiculous, you can't use that character!"),
+ ('', "Tag names should not be empty, or filled with whitespace."),
+ (' ', "Tag names should not be empty, or filled with whitespace."),
+ ('42', "Tag names can't be numbers."),
+ # Escape question mark as this is evaluated as regular expression.
+ ('x' * 128, r"Are you insane\? That's way too long!"),
+ )
+)
+def test_tag_name_converter_for_invalid(value: str, expected: str):
+ context = MagicMock()
+ context.author = 'bob'
+
+ with pytest.raises(BadArgument, match=expected):
+ asyncio.run(TagNameConverter.convert(context, value))
+
+
[email protected]('value', ('foo', 'lemon'))
+def test_valid_python_identifier_for_valid(value: str):
+ assert asyncio.run(ValidPythonIdentifier.convert(None, value)) == value
+
+
[email protected]('value', ('nested.stuff', '#####'))
+def test_valid_python_identifier_for_invalid(value: str):
+ with pytest.raises(BadArgument, match=f'`{value}` is not a valid Python identifier'):
+ asyncio.run(ValidPythonIdentifier.convert(None, value))
diff --git a/tests/utils/test_checks.py b/tests/utils/test_checks.py
index 915d074b3..7121acebd 100644
--- a/tests/utils/test_checks.py
+++ b/tests/utils/test_checks.py
@@ -1,25 +1,29 @@
from unittest.mock import MagicMock
+import pytest
+
from bot.utils import checks
-def test_with_role_check_without_guild():
- context = MagicMock()
+def context():
+ return MagicMock()
+
+
+def test_with_role_check_without_guild(context):
context.guild = None
assert not checks.with_role_check(context)
-def test_with_role_check_with_guild_without_required_role():
- context = MagicMock()
+def test_with_role_check_with_guild_without_required_role(context):
context.guild = True
context.author.roles = []
assert not checks.with_role_check(context)
-def test_with_role_check_with_guild_with_required_role():
- context = MagicMock()
+def test_with_role_check_with_guild_with_required_role(context):
context.guild = True
role = MagicMock()
role.id = 42
@@ -28,15 +32,13 @@ def test_with_role_check_with_guild_with_required_role():
assert checks.with_role_check(context, role.id)
-def test_without_role_check_without_guild():
- context = MagicMock()
+def test_without_role_check_without_guild(context):
context.guild = None
assert not checks.without_role_check(context)
-def test_without_role_check_with_unwanted_role():
- context = MagicMock()
+def test_without_role_check_with_unwanted_role(context):
context.guild = True
role = MagicMock()
role.id = 42
@@ -45,8 +47,7 @@ def test_without_role_check_with_unwanted_role():
assert not checks.without_role_check(context, role.id)
-def test_without_role_check_without_unwanted_role():
- context = MagicMock()
+def test_without_role_check_without_unwanted_role(context):
context.guild = True
role = MagicMock()
role.id = 42
@@ -55,13 +56,11 @@ def test_without_role_check_without_unwanted_role():
assert checks.without_role_check(context, role.id + 10)
-def test_in_channel_check_for_correct_channel():
- context = MagicMock()
+def test_in_channel_check_for_correct_channel(context):
context.channel.id = 42
assert checks.in_channel_check(context, context.channel.id)
-def test_in_channel_check_for_incorrect_channel():
- context = MagicMock()
+def test_in_channel_check_for_incorrect_channel(context):
context.channel.id = 42
assert not checks.in_channel_check(context, context.channel.id + 10)