diff --git a/.github/workflows/pull-request-merge-precondition.yml b/.github/workflows/pull-request-merge-precondition.yml index e41810c..ddad109 100644 --- a/.github/workflows/pull-request-merge-precondition.yml +++ b/.github/workflows/pull-request-merge-precondition.yml @@ -3,6 +3,10 @@ name: Pull Request Merge Precondition on: pull_request: +permissions: + contents: read + pull-requests: write + jobs: build: runs-on: ubuntu-latest @@ -11,7 +15,11 @@ jobs: python-version: [3.8] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 + with: + persist-credentials: true # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token + fetch-depth: 0 # otherwise, you will failed to push refs to dest repo + - uses: psf/black@stable with: options: "--check --verbose --exclude migrations" @@ -20,3 +28,19 @@ jobs: with: configuration: "--check-only --diff --profile black" requirementsFiles: "requirements.txt" + + - name: install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements-dev.txt + pip install pytest-cov + + - name: run pytest + run: | + pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=pyconkr ./ | tee pytest-coverage.txt + + #- name: Pytest coverage comment + # uses: MishaKav/pytest-coverage-comment@main + # with: + # pytest-coverage-path: ./pytest-coverage.txt + # junitxml-path: ./pytest.xml diff --git a/README.md b/README.md index 4e40c38..25ab02b 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,11 @@ * mac * brew install mysql-client * pip install -r requirements.txt + +## how to run localtesting ( sqlite3 based ) +``` +# to setup pytest and requirements +pip install -r requirements-dev.txt +# run test +pytest +``` diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..98a6f21 --- /dev/null +++ b/conftest.py @@ -0,0 +1,10 @@ +import pytest + + +# TODO +# https://djangostars.com/blog/django-pytest-testing/#header17 +@pytest.fixture +def api_client(): + from rest_framework.test import APIClient + + return APIClient() diff --git a/manage.py b/manage.py old mode 100644 new mode 100755 diff --git a/pyconkr/settings-localtest.py b/pyconkr/settings-localtest.py new file mode 100644 index 0000000..1b7ccd6 --- /dev/null +++ b/pyconkr/settings-localtest.py @@ -0,0 +1,21 @@ +import os + +from pyconkr.settings import * + +DEBUG = True + +ALLOWED_HOSTS += [ + "*", +] + + +# RDS +DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}} + +# django-storages: TODO fix to in memory? +del MEDIA_ROOT +DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" +STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage" +AWS_S3_ACCESS_KEY_ID = os.getenv("AWS_S3_ACCESS_KEY_ID") +AWS_S3_SECRET_ACCESS_KEY = os.getenv("AWS_S3_SECRET_ACCESS_KEY") +AWS_STORAGE_BUCKET_NAME = "pyconkr-api-v2-static-dev" diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..3a662ff --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +DJANGO_SETTINGS_MODULE = pyconkr.settings-localtest +# -- recommended but optional: +python_files = tests.py test_*.py *_tests.py +addopts = --no-migrations diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..112c1ef --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,5 @@ +-r requirements.txt +black +isort +pytest +pytest-django diff --git a/sponsor/tests.py b/sponsor/tests.py index 7ce503c..d2f24a3 100644 --- a/sponsor/tests.py +++ b/sponsor/tests.py @@ -1,3 +1,26 @@ -from django.test import TestCase +import pytest +from django.contrib.auth import get_user_model + +from sponsor.models import SponsorLevel + +pytestmark = pytest.mark.django_db + +UserModel = get_user_model() + + +@pytest.mark.django_db +class TestSponsorLevelModel: + pytestmark = pytest.mark.django_db + + def test_sponsor_level_creation_success(self): + assert SponsorLevel.objects.count() == 0 + SponsorLevel.objects.create( + name="test", + desc="test desc", + visible=True, + limit=1, + ) + assert SponsorLevel.objects.count() != 0 + # Create your tests here.