Files
titlebot-ng-ng/test_getvotes.py
T
2026-10-31 01:12:01 -05:00

45 lines
2.0 KiB
Python

from contextlib import closing
from pathlib import Path
import sqlite3
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import AsyncMock, patch
import get_votes
class GetVotesTests(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.enterContext(patch("get_votes.__file__", str(self.root / "get_votes.py")))
self.room = SimpleNamespace(
config=SimpleNamespace(room_id="!test:matrix.org"),
count_votes=AsyncMock(return_value="Suggestion 1: 3 votes"),
)
with closing(sqlite3.connect(self.root / "votes__test_matrix_org.db")) as conn, conn:
conn.execute("CREATE TABLE votes (selected_submission TEXT, voter_display_name TEXT)")
async def test_emoji_votes_are_reported_when_web_database_is_empty(self):
result = await get_votes.get_vote_results(self.room)
self.assertIn("Suggestion 1: 3 votes", result)
self.assertIn("No web votes have been cast yet", result)
self.assertNotIn("No votes have been cast yet", result)
async def test_web_and_emoji_votes_have_separate_totals(self):
with closing(sqlite3.connect(self.root / "votes__test_matrix_org.db")) as conn, conn:
conn.execute("INSERT INTO votes VALUES ('Web title', 'alice')")
result = await get_votes.get_vote_results(self.room)
self.assertIn("Suggestion 1: 3 votes", result)
self.assertIn("Web Vote Results", result)
self.assertIn("Web title: 1 vote", result)
async def test_missing_web_database_does_not_hide_emoji_votes(self):
self.room.config.room_id = "!other:matrix.org"
result = await get_votes.get_vote_results(self.room)
self.assertIn("Suggestion 1: 3 votes", result)
self.assertIn("No web-vote database", result)
self.assertFalse((self.root / "votes__other_matrix_org.db").exists())