ci: 🧪 add tests and test workflow

This commit is contained in:
m5ka 2024-03-26 13:50:37 +00:00
parent 2187af7b10
commit 1eae3db8d1
5 changed files with 111 additions and 0 deletions

52
.github/workflows/test.yaml vendored Normal file
View file

@ -0,0 +1,52 @@
name: test
on:
pull_request:
branches:
- '*'
push:
branches:
- 'main'
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- '5432:5432'
steps:
- uses: actions/checkout@v4
- name: install python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: set up poetry
uses: abatilo/actions-poetry@v2
with:
poetry-version: '1.8.2'
- name: set up virtual environment
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- name: cache virtual environment
uses: actions/cache@v4
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: install dependencies
run: poetry install --with test
- name: check linter and formatter
run: |
poetry run ruff check .
poetry run ruff format --check .
- name: check migrations
run: poetry run python manage.py makemigrations --check
- name: run tests
run: poetry run pytest

0
moku/tests/__init__.py Normal file
View file

31
moku/tests/conftest.py Normal file
View file

@ -0,0 +1,31 @@
import re
import pytest
from moku.constants import Verbs
from moku.models.post import Post
from moku.models.user import User
@pytest.fixture
def post(user: User) -> Post:
"""Generate a test post."""
return Post.objects.create(
emoji="🌭", food="sausage surprise", verb=Verbs.COOKED, created_by=user
)
@pytest.fixture(scope="session")
def re_uuid() -> re.Pattern:
"""Regex pattern to match a UUID."""
return re.compile(r"[2-9A-HJ-NP-Za-km-z]{22}$")
@pytest.fixture
def user() -> User:
"""Generate a test user."""
return User.objects.create_user(
username="jean",
email="jean.slater@example.com",
password="sausage_surprise123!",
)

15
moku/tests/test_post.py Normal file
View file

@ -0,0 +1,15 @@
import re
import pytest
from moku.models.post import Post
@pytest.mark.django_db
def test_post(post: Post, re_uuid: re.Pattern):
"""Test that a post is created successfully with all required data."""
assert isinstance(post.pk, int)
assert post.pk > 0
assert post.pk == post.id
assert re_uuid.match(post.uuid) is not None
assert post.text == '<a href="/user/jean">@jean</a> cooked sausage surprise'

13
moku/tests/test_user.py Normal file
View file

@ -0,0 +1,13 @@
import pytest
from moku.models.user import User
@pytest.mark.django_db
def test_user(user: User):
"""Test that a user is created successfully with all required data."""
assert isinstance(user.pk, int)
assert user.pk > 0
assert user.id == user.pk
assert str(user) == "jean"
assert user.get_absolute_url() == "/user/jean"