diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..cad8b8d --- /dev/null +++ b/Pipfile @@ -0,0 +1,15 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +discord = "~=1.0.1" +aiosqlite = "~=0.16.1" +pytest = "*" +pytest-asyncio = "*" + +[requires] +python_version = "3.7" diff --git a/bot/cogs/contest.py b/bot/cogs/contest.py index b21a16b..faadd2b 100644 --- a/bot/cogs/contest.py +++ b/bot/cogs/contest.py @@ -66,7 +66,7 @@ class ContestCog(commands.Cog): await self.bot.db.new_period(new_period) # Handle submissions state elif period.current_state == 0: - await self.bot.db.update_period(period) + await self.bot.db.update(period) return # Handle voting state elif period.current_state == 1: diff --git a/bot/db.py b/bot/db.py index 176d958..dee77aa 100644 --- a/bot/db.py +++ b/bot/db.py @@ -14,7 +14,8 @@ logger.setLevel(constants.LOGGING_LEVEL) Guild = namedtuple('Guild', ['id', 'prefix', 'submission', 'period']) Submission = namedtuple('Submission', ['id', 'user', 'guild', 'timestamp']) -Period = namedtuple('Period', ['id', 'guild', 'current_state', 'started_at', 'voting_at', 'finished_at', '']) +Period = namedtuple('Period', ['id', 'guild', 'current_state', 'started_at', 'voting_at', 'finished_at']) +tables = [Guild, Submission, Period] class ContestDatabase(object): diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index c48428a..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -discord~=1.0.1 - -aiosqlite~=0.16.1 diff --git a/tests/bot/test_db.py b/tests/bot/test_db.py new file mode 100644 index 0000000..c1785b8 --- /dev/null +++ b/tests/bot/test_db.py @@ -0,0 +1,74 @@ +import pytest + +from bot.db import ContestDatabase, tables + + +@pytest.fixture() +async def db() -> ContestDatabase: + db = await ContestDatabase.create(':memory:') + yield db + await db.conn.close() + + +@pytest.mark.asyncio +async def test_table_setup(db) -> None: + """Test that all tables were setup by the database.""" + cur = await db.conn.cursor() + try: + for table_namedtuple in tables: + await cur.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name = ?;''', + [table_namedtuple.__name__.lower()]) + rows = list(await cur.fetchall()) + assert len(rows) == 1 + finally: + await cur.close() + + +@pytest.mark.asyncio +async def test_guild_setup(db) -> None: + await db.setup_guild(0) + guild = await db.get_guild(0) + assert guild is not None + assert guild.submission is None + + assert await db.get_guild(1) is None + + +@pytest.mark.asyncio +async def test_update(db) -> None: + pass + + +@pytest.mark.asyncio +async def test_generate_update_query(db) -> None: + pass + + +@pytest.mark.asyncio +async def test_insert(db) -> None: + """Test automatic namedtuple query""" + pass + + +@pytest.mark.asyncio +async def test_generate_insert_query(db) -> None: + """Test INSERT query generation.""" + pass + + +@pytest.mark.asyncio +async def test_submissions(db) -> None: + """Test all submission related helper functions.""" + pass + + +@pytest.mark.asyncio +async def test_guilds(db) -> None: + """Test all guild related helper functions""" + pass + + +@pytest.mark.asyncio +async def test_periods(db) -> None: + """Test all period related helper functions.""" + pass