aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2019-11-10 13:49:01 -0800
committerGravatar GitHub <[email protected]>2019-11-10 13:49:01 -0800
commitf3dbe477e45dd023f6c138246c353942c4d4212b (patch)
tree6d4d7be46ef0d03518b6a34dc81c5e4daec01e23 /tests
parent[kaizen] Remove now duplicate channel check (diff)
parentMerge pull request #641 from kwzrd/unittest-links (diff)
Merge branch 'master' into checkpoint-changes
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/rules/test_links.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/bot/rules/test_links.py b/tests/bot/rules/test_links.py
new file mode 100644
index 000000000..be832843b
--- /dev/null
+++ b/tests/bot/rules/test_links.py
@@ -0,0 +1,101 @@
+import unittest
+from typing import List, NamedTuple, Tuple
+
+from bot.rules import links
+from tests.helpers import async_test
+
+
+class FakeMessage(NamedTuple):
+ author: str
+ content: str
+
+
+class Case(NamedTuple):
+ recent_messages: List[FakeMessage]
+ relevant_messages: Tuple[FakeMessage]
+ culprit: Tuple[str]
+ total_links: int
+
+
+def msg(author: str, total_links: int) -> FakeMessage:
+ """Makes a message with *total_links* links."""
+ content = " ".join(["https://pydis.com"] * total_links)
+ return FakeMessage(author=author, content=content)
+
+
+class LinksTests(unittest.TestCase):
+ """Tests applying the `links` rule."""
+
+ def setUp(self):
+ self.config = {
+ "max": 2,
+ "interval": 10
+ }
+
+ @async_test
+ async def test_links_within_limit(self):
+ """Messages with an allowed amount of links."""
+ cases = (
+ [msg("bob", 0)],
+ [msg("bob", 2)],
+ [msg("bob", 3)], # Filter only applies if len(messages_with_links) > 1
+ [msg("bob", 1), msg("bob", 1)],
+ [msg("bob", 2), msg("alice", 2)] # Only messages from latest author count
+ )
+
+ for recent_messages in cases:
+ last_message = recent_messages[0]
+
+ with self.subTest(
+ last_message=last_message,
+ recent_messages=recent_messages,
+ config=self.config
+ ):
+ self.assertIsNone(
+ await links.apply(last_message, recent_messages, self.config)
+ )
+
+ @async_test
+ async def test_links_exceeding_limit(self):
+ """Messages with a a higher than allowed amount of links."""
+ cases = (
+ Case(
+ [msg("bob", 1), msg("bob", 2)],
+ (msg("bob", 1), msg("bob", 2)),
+ ("bob",),
+ 3
+ ),
+ Case(
+ [msg("alice", 1), msg("alice", 1), msg("alice", 1)],
+ (msg("alice", 1), msg("alice", 1), msg("alice", 1)),
+ ("alice",),
+ 3
+ ),
+ Case(
+ [msg("alice", 2), msg("bob", 3), msg("alice", 1)],
+ (msg("alice", 2), msg("alice", 1)),
+ ("alice",),
+ 3
+ )
+ )
+
+ for recent_messages, relevant_messages, culprit, total_links in cases:
+ last_message = recent_messages[0]
+
+ with self.subTest(
+ last_message=last_message,
+ recent_messages=recent_messages,
+ relevant_messages=relevant_messages,
+ culprit=culprit,
+ total_links=total_links,
+ config=self.config
+ ):
+ desired_output = (
+ f"sent {total_links} links in {self.config['interval']}s",
+ culprit,
+ relevant_messages
+ )
+ self.assertTupleEqual(
+ await links.apply(last_message, recent_messages, self.config),
+ desired_output
+ )