From 7ecb5682c6a6a5ef83ba138e1cf24ee4facdafe7 Mon Sep 17 00:00:00 2001 From: Victor Magueta <56882461+vmagueta@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:07:26 +0000 Subject: [PATCH] Setup (#9) * reorganized the folders, created the initial setup, and created the base html * added the make live command * fixed missing the command in PHONY * Created pipeline for CI, fixed the requirements of the project and make a new test for testing CI * Created pipeline for CI, fixed the requirements of the project and make a new test for testing CI * Fixed the error of requirements.test.txt and removed livereload untill find a way to use it with tests * Fixed the error of missing pflake on requirements * Fixed the test, removed --forked from test * Update views.py --- .github/workflows/main.yml | 76 ++++++++++++++++++++++++++++++++++++++ Makefile | 4 ++ app/tests.py | 12 +++++- app/views.py | 4 ++ kamila_project/settings.py | 24 ++++++------ pyproject.toml | 10 ++++- pytest.ini | 7 ++++ requirements.dev.txt | 7 ++++ requirements.test.txt | 7 ++++ 9 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/main.yml create mode 100644 pytest.ini diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..3e27f87 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,76 @@ +name: CI + +on: + push: + branches: + - "main" + pull_request: + branches: + - "main" + workflow_dispatch: + +jobs: + lint: + strategy: + fail-fast: true + matrix: + python-version: ['3.12'] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml') }} + restore-keys: | + ${{ runner.os }}-pip-${{ matrix.python-version }}- + - name: Install Project + run: pip install '.[test]' + - name: Look for style errors + run: flake8 app --ignore=F401,F821,E501 + - name: Checking for importing style + run: isort --profile=black --check --diff app + - name: Look for auto format errors + run: black --check --diff app + + tests: + needs: lint + strategy: + fail-fast: false + matrix: + python-version: ['3.10', '3.12'] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml') }} + restore-keys: | + ${{ runner.os }}-pip-${{ matrix.python-version }}- + - name: Install Project + run: pip install '.[test]' + - name: Run tests + run: pytest -v --junitxml=test-result.xml + - name: Publish Test Results + uses: EnricoMi/publish-unit-test-result-action@v2.16.1 + if: always() + with: + files: test-result.xml + check_name: Test Result (Python ${{ matrix.python-version }}) + permissions: + contents: read + issues: read + checks: write + pull-requests: write + \ No newline at end of file diff --git a/Makefile b/Makefile index ba7fbcc..d0cf738 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,10 @@ ipython: @.venv/bin/ipython +test: + @.venv/bin/pytest -vs -s + + lint: @.venv/bin/pflake8 app kamila_project diff --git a/app/tests.py b/app/tests.py index 7ce503c..628a767 100644 --- a/app/tests.py +++ b/app/tests.py @@ -1,3 +1,13 @@ +import pytest from django.test import TestCase +from django.urls import reverse -# Create your tests here. + +@pytest.mark.unit +def test_acess_index_true(client): + """ + Test if the index view returns a 200 status code when accesseds + """ + url = reverse("index") + response = client.get(url) + assert response.status_code == 200 diff --git a/app/views.py b/app/views.py index e6aa1cc..e9099eb 100644 --- a/app/views.py +++ b/app/views.py @@ -1,16 +1,20 @@ from django.shortcuts import render + from .models import Post def base(request): return render(request, "app/base.html") + def index(request): return render(request, "app/index.html") + def blog(request): posts = Post.objects.all().order_by("-created_at") return render(request, "app/blog.html", {"posts": posts}) + def post(request): return render(request, "app/post.html") diff --git a/kamila_project/settings.py b/kamila_project/settings.py index efcdd55..6b6b65c 100644 --- a/kamila_project/settings.py +++ b/kamila_project/settings.py @@ -40,7 +40,7 @@ "django.contrib.messages", "django.contrib.staticfiles", "app", - "livereload", + # "livereload", ] MIDDLEWARE = [ @@ -51,30 +51,28 @@ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", - "livereload.middleware.LiveReloadScript", + # "livereload.middleware.LiveReloadScript", ] ROOT_URLCONF = "kamila_project.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] - - WSGI_APPLICATION = "kamila_project.wsgi.application" # Database diff --git a/pyproject.toml b/pyproject.toml index 773f4bc..fb155a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -name = "Terapeuta Kamila Rodrigues" +name = "terapeuta_kamila_rodrigues" version = "0.1.0" description = " Official website of Kamila Rodrigues, a professional therapist." readme = "README.md" @@ -8,15 +8,21 @@ authors = [ { name = "Viana Labs", email = "contacto@vianalabs.pt" } ] dependencies = [ - "flask", + "django", ] +[tool.setuptools.packages] +find = { where = ["app", "kamila_project"] } + + [project.optional-dependencies] test = [ "flake8", "black", "isort", "pytest", + "pytest-django", + # "livereload", ] [tool.flake8] diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..b26db67 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,7 @@ +[pytest] +DJANGO_SETTINGS_MODULE = kamila_project.settings +python_files = tests.py test_*.py + +markers = + unit: marks tests as unit tests. + integration: marks tests as integration tests. diff --git a/requirements.dev.txt b/requirements.dev.txt index 3b1a852..4dae840 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -2,8 +2,15 @@ ipython ipdb pudb +pytest pytest-watch + +# auto reload +# django-livereload +# django-livereload-server + + # Code Quality flake8 pyproject-flake8 diff --git a/requirements.test.txt b/requirements.test.txt index e69de29..fcd9c50 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -0,0 +1,7 @@ +pytest +pytest-django +flake8 +pyproject-flake8 +black +isort +mypy