Skip to content

Commit

Permalink
Rewrite foundations of unit tests (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 authored Jul 6, 2024
1 parent e84df51 commit 0726627
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 26 deletions.
23 changes: 7 additions & 16 deletions .github/workflows/tests.inactive → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ on:

env:
POSTGRES_URI: postgresql://postgres:postgres@localhost:5432/postgres
TARGET_REVISION: rev5


jobs:
Test:
Expand All @@ -21,7 +19,7 @@ jobs:

services:
postgres:
image: no767/akari-pg:edge
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -43,11 +41,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

# - name: Setup Codecov Uploader
# run: |
# curl -Os https://uploader.codecov.io/latest/linux/codecov
# chmod +x codecov
# ./codecov
- name: Install pg_trgm
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends postgresql-client
psql $POSTGRES_URI -c 'CREATE EXTENSION IF NOT EXISTS pg_trgm;'
- name: Set up Python
id: setup-python
Expand All @@ -63,11 +61,4 @@ jobs:
run: |
RAW_PYTHON_VERSION=${{ matrix.version }}
PYTHON_VERSION=$(echo $RAW_PYTHON_VERSION | sed 's/\.//')
tox -e $PYTHON_VERSION

# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v3
# with:
# files: ./coverage.xml
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
tox -e $PYTHON_VERSION
12 changes: 9 additions & 3 deletions bot/migrations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os
import re
import traceback
from functools import wraps
Expand All @@ -12,13 +13,18 @@
from libs.utils.config import CatherineConfig
from typing_extensions import Self

path = Path(__file__).parent / "config.yml"
config = CatherineConfig(path)
# If we can't load the configuration, then let's look for the environment variable
try:
path = Path(__file__).parent / "config.yml"
config = CatherineConfig(path)
POSTGRES_URI = config["postgres"]["uri"]
except KeyError:
POSTGRES_URI = os.environ["POSTGRES_URI"]


BE = TypeVar("BE", bound=BaseException)

REVISION_FILE = re.compile(r"(?P<kind>V)(?P<version>\d+)__(?P<description>.+).sql")
POSTGRES_URI = config["postgres"]["uri"]

CREATE_MIGRATIONS_TABLE = """
CREATE TABLE IF NOT EXISTS migrations (
Expand Down
75 changes: 75 additions & 0 deletions bot/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import glob
import os
from pathlib import Path

import aiohttp
import asyncpg
import discord
import discord.ext.commands as commands
import discord.ext.test as dpytest
import pytest_asyncio
from libs.utils.config import CatherineConfig

TESTING_EXTENSIONS = [
"cogs.dictionary",
"cogs.hrt",
"cogs.pride_profiles",
"cogs.pronouns",
]

CONFIG_PATH = Path(__file__).parents[1] / "config.yml"


def load_postgres_uri() -> str:
try:
config = CatherineConfig(CONFIG_PATH)
ideal_conf = config["postgres"]["uri"]
return ideal_conf
except KeyError:
return os.environ["POSTGRES_URI"]


class TestBot(commands.Bot):
pool: asyncpg.Pool
session: aiohttp.ClientSession

def __init__(self, intents: discord.Intents):
super().__init__(command_prefix="!", intents=intents)

async def close(self) -> None:
await self.session.close()
await self.pool.close()

async def setup_hook(self) -> None:
self.pool = await asyncpg.create_pool(dsn=load_postgres_uri()) # type: ignore
self.session = aiohttp.ClientSession()

for extension in TESTING_EXTENSIONS:
await self.load_extension(extension, package="..cogs")


@pytest_asyncio.fixture
async def bot():
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
b = TestBot(intents=intents)
await b._async_setup_hook()
await b.setup_hook()
dpytest.configure(b)

yield b

await b.close()
await dpytest.empty_queue()


def pytest_sessionfinish(session, exitstatus):
"""Code to execute after all tests."""

file_list = glob.glob("./dpytest_*.dat")
for file_path in file_list:
try:
os.remove(file_path)
except Exception:
print("Error while deleting file : ", file_path)
13 changes: 13 additions & 0 deletions bot/tests/test_hrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from cogs.hrt import HRTConversion


@pytest.fixture
def cog(bot) -> HRTConversion:
return HRTConversion(bot)


def test_calc_e(cog: HRTConversion):
# In order not to return an exit code 5 from pytest
# we just make an mock test
pass
7 changes: 7 additions & 0 deletions docs/source/guides/dev/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ Python Version Support
Catherine-Chan generally follows `NEP-29 <https://numpy.org/neps/nep-0029-deprecation_policy.html>`_.
Catherine-Chan is tested against versions supported by NEP-29.

Unit Tests
----------

Since `PR #189 <https://github.com/No767/Catherine-Chan/pull/189>`_, unit tests are now used to ensure software
quality. Each feature PR should include unit tests on methods that are relevant to the cog, and features that
can tested if possible. Please see ``bot/tests`` for examples of how to write these tests.

GitHub Contributing Guidelines
-----------------------------------

Expand Down
6 changes: 3 additions & 3 deletions docs/source/guides/dev/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ Local Setup
python3 bot/migrations.py
Special Configuration Variables
---------------------
-------------------------------

Development Features
^^^^^^^^^^^^^^^^^^^^

Catherine-Chan includes an development mode allowing for continuous
reloading of extensions and library code. Once the file is saved, the
module is reloaded and changes can be reflected. This can be enabled
through the ``dev_mode`` key in the configuration file. In addition,
through the ``bot.dev_mode`` key in the configuration file. In addition,
Jishaku is bundled with the bot, allowing for easy debugging and
faster development.

Expand All @@ -81,7 +81,7 @@ Prometheus Metrics
^^^^^^^^^^^^^^^^^^

Catherine-Chan also includes an Prometheus endpoint for metrics.
This can enabled through the ``prometheus.enabled`` key. If
This can enabled through the ``bot.prometheus.enabled`` key. If
you don't need this feature, feel free to entirely disable it.
Disabling this feature does not affect the bot, as the cog
responsible for this feature is an extension that can be
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ exclude = [
"**/.mypy_cache",
"**/.dmpypy.json",
"docs",
"docker"
"docker",
"bot/tests/**"
]
reportMissingImports = true
typeCheckingMode = "basic"
Expand All @@ -33,9 +34,9 @@ ignore = ["E501", "N999", "E402", "S101"]
select = ["E", "F", "N", "ASYNC", "S", "ERA"]

[tool.pytest.ini_options]
minversion = "6.0"
minversion = "8.0"
addopts = "-ra"
testpaths = [
"tests"
"bot/tests"
]
pythonpath = ["bot"]
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dpytest>=0.7.0,<1
sphinx>=7.3.7,<8
furo>=2024.5.6,<2025
sphinx-copybutton>=0.5.2,<1
sphinx-autobuild>=2024.4.16,<2025
sphinx-autobuild>=2024.4.16,<2025
sphinxext-opengraph>=0.8.2,<1
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ no_package=true

[testenv]
description = run unit tests
passenv = POSTGRES_URI
deps =
pytest>=8.2.1,<9
pytest-asyncio>=0.23.7,<1
Expand Down

0 comments on commit 0726627

Please sign in to comment.