From 0b64fcc866677969bbfb0191f76c8ca14de4b5a2 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 14:07:01 +0200 Subject: [PATCH 01/15] 44 - przygotowywanie srodowiska do testow - utworzenie settings_test oraz pytest --- backend/.pre-commit-config.yaml | 11 +++++++++-- backend/core/settings.py | 10 +++++++--- backend/core/settings_test.py | 17 +++++++++++++++++ backend/pytest.ini | 4 ++++ docker-compose.yml | 6 ++---- 5 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 backend/core/settings_test.py create mode 100644 backend/pytest.ini diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 5075b52..223ed6d 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -5,9 +5,16 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - - id: check-yaml - - id: end-of-file-fixer - id: trailing-whitespace + # - id: end-of-file-fixer + - id: check-yaml + - id: debug-statements + + - repo: https://github.com/psf/black + rev: 23.7.0 + hooks: + - id: black + files: ^backend/ - repo: https://github.com/pre-commit/mirrors-mypy rev: 'v1.5.1' hooks: diff --git a/backend/core/settings.py b/backend/core/settings.py index 17496c8..2f7572b 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -79,6 +79,8 @@ }, ] +PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) + WSGI_APPLICATION = "core.wsgi.application" # Database @@ -93,8 +95,10 @@ "HOST": os.environ.get("DB_HOST", ""), "PORT": os.environ.get("DB_PORT", "5432"), }, - - "default": {"ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / "db.sqlite3"} + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + }, } # Password validation @@ -142,7 +146,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" REST_FRAMEWORK = { - "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", + "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", "PAGE_SIZE": 9999, "DEFAULT_PERMISSION_CLASSES": [ "rest_framework.permissions.IsAuthenticated", diff --git a/backend/core/settings_test.py b/backend/core/settings_test.py new file mode 100644 index 0000000..ca40e54 --- /dev/null +++ b/backend/core/settings_test.py @@ -0,0 +1,17 @@ +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": ":memory:", + } +} + +MIDDLEWARE_CLASSES = [ + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", +] + +DEBUG = False + +EMAIL_BACKEND = "django.core.mail.backends.dummy.EmailBackend" diff --git a/backend/pytest.ini b/backend/pytest.ini new file mode 100644 index 0000000..ced3bea --- /dev/null +++ b/backend/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +python_files = tests.py test_*.py +addopts = -s -q --disable-warnings --doctest-modules +norecursedirs = .git .cache tmp* \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3c09124..c4a740b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,8 +13,6 @@ services: - static:/code/static/ ports: - "8080:8000" - env_file: - - environments.env frontend: build: @@ -23,8 +21,8 @@ services: - "88:80" depends_on: - backend - env_file: - - environments.env + db: + image: postgres:latest app: image: 'jc21/nginx-proxy-manager:latest' container_name: nginxproxymanager From 7d411782c7b7ff25971bc54b5b88ce0e9c1a1100 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 14:07:29 +0200 Subject: [PATCH 02/15] 44 - dodano end-of-file-fixer hooks --- backend/.pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 223ed6d..0d6b912 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -6,7 +6,7 @@ repos: rev: v4.4.0 hooks: - id: trailing-whitespace - # - id: end-of-file-fixer + - id: end-of-file-fixer - id: check-yaml - id: debug-statements From 4d3e7e46e0775f7d103179942a21797758fe975d Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 14:14:38 +0200 Subject: [PATCH 03/15] 44 - poprawiono yamle oraz ini --- backend/.pre-commit-config.yaml | 32 ++++++++------------------------ backend/.pre.commit-config.yaml | 10 ++++++++++ backend/pytest.ini | 2 +- 3 files changed, 19 insertions(+), 25 deletions(-) create mode 100644 backend/.pre.commit-config.yaml diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 0d6b912..fd16ba2 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -1,26 +1,10 @@ -default_language_version: - # default language version for each language - python: python3.11 +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: debug-statements - - - repo: https://github.com/psf/black - rev: 23.7.0 - hooks: - - id: black - files: ^backend/ - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.5.1' - hooks: - - id: mypy - exclude: "[a-zA-Z]*/(migrations)/(.)*" - additional_dependencies: [ - djangorestframework-stubs, - django-stubs, - ] + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files diff --git a/backend/.pre.commit-config.yaml b/backend/.pre.commit-config.yaml new file mode 100644 index 0000000..fd16ba2 --- /dev/null +++ b/backend/.pre.commit-config.yaml @@ -0,0 +1,10 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files diff --git a/backend/pytest.ini b/backend/pytest.ini index ced3bea..df7fdd4 100644 --- a/backend/pytest.ini +++ b/backend/pytest.ini @@ -1,4 +1,4 @@ [pytest] python_files = tests.py test_*.py addopts = -s -q --disable-warnings --doctest-modules -norecursedirs = .git .cache tmp* \ No newline at end of file +norecursedirs = .git .cache tmp* From 986eabb966d2cba8999e9a5e632f5ba1f18d2167 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 14:17:03 +0200 Subject: [PATCH 04/15] 44 - poprawiono yamle --- backend/.pre-commit-config.yaml | 32 ++++++++++++++++++++++++-------- backend/.pre.commit-config.yaml | 32 ++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index fd16ba2..0d6b912 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -1,10 +1,26 @@ -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks +default_language_version: + # default language version for each language + python: python3.11 repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: debug-statements + + - repo: https://github.com/psf/black + rev: 23.7.0 + hooks: + - id: black + files: ^backend/ + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.5.1' + hooks: + - id: mypy + exclude: "[a-zA-Z]*/(migrations)/(.)*" + additional_dependencies: [ + djangorestframework-stubs, + django-stubs, + ] diff --git a/backend/.pre.commit-config.yaml b/backend/.pre.commit-config.yaml index fd16ba2..2eaf148 100644 --- a/backend/.pre.commit-config.yaml +++ b/backend/.pre.commit-config.yaml @@ -1,10 +1,26 @@ -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks +default_language_version: + # default language version for each language + python: python3.11 repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: debug-statements + + - repo: https://github.com/psf/black + rev: 23.7.0 + hooks: + - id: black + files: ^backend/ + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.5.1' + hooks: + - id: mypy + exclude: "[a-zA-Z]*/(migrations)/(.)*" + additional_dependencies: [ + djangorestframework-stubs, + django-stubs, + ] From e8bbd4913f5e75efd0c6b3e1ef12c0e2ea9657da Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 14:57:22 +0200 Subject: [PATCH 05/15] 44 - dodano pierwszy test modelu --- .github/workflows/coverage.yaml | 85 +++++++++++++++++++++++++++++ backend/.coveragerc | 7 +++ backend/.pre-commit-config.yaml | 20 +++---- backend/.pre.commit-config.yaml | 26 --------- backend/authentication/tests.py | 3 - backend/history/tests.py | 3 - backend/photos/tests.py | 1 - backend/photos/tests/__init__.py | 0 backend/photos/tests/test_models.py | 9 +++ backend/pytest.ini | 4 +- 10 files changed, 114 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/coverage.yaml create mode 100644 backend/.coveragerc delete mode 100644 backend/.pre.commit-config.yaml delete mode 100644 backend/authentication/tests.py delete mode 100644 backend/history/tests.py delete mode 100644 backend/photos/tests.py create mode 100644 backend/photos/tests/__init__.py create mode 100644 backend/photos/tests/test_models.py diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml new file mode 100644 index 0000000..029971b --- /dev/null +++ b/.github/workflows/coverage.yaml @@ -0,0 +1,85 @@ +name: coverage-example + +env: + DOCKER_BUILDKIT: 1 + COMPOSE_DOCKER_CLI_BUILD: 1 + +on: + pull_request: + branches: + - main + - develop + - master + + push: + branches: + - main + - develop + - master + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Build the Stack + run: docker-compose build + - name: Tear down the Stack + run: docker-compose down + + pytest-coverage: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Run DB Migrations + run: docker-compose run --rm django python manage.py migrate + - name: Run Django Tests + run: docker-compose run django coverage run -m pytest + - name: Generate coverage.lcov + run: docker-compose -f docker-compose.yml run django coverage lcov + - name: Tear down the Stack + run: docker-compose down + - name: Coveralls Parallel + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + file: "./backend/coverage.lcov" + base-path: "./backend" + flag-name: run-1 + parallel: true + uses: coverallsapp/github-action@v2 + + vitest-coverage: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Vitest Coverage + run: docker-compose run frontend npx vitest --coverage --no-watch + - name: Report Coverage + if: always() + uses: davelosert/vitest-coverage-report-action@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + working-directory: frontend + - name: Coveralls Parallel + uses: coverallsapp/github-action@v2 + with: + flag-name: run-2 + parallel: true + + finish: + runs-on: ubuntu-latest + if: ${{ always() }} + needs: [ pytest-coverage, vitest-coverage ] + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Coveralls Finished + uses: coverallsapp/github-action@v2 + with: + parallel-finished: true + carryforward: "run-1,run-2" \ No newline at end of file diff --git a/backend/.coveragerc b/backend/.coveragerc new file mode 100644 index 0000000..0a14deb --- /dev/null +++ b/backend/.coveragerc @@ -0,0 +1,7 @@ +[run] +omit = */migrations/* + +[paths] +source = + apps/ + config/ diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 0d6b912..364ab9d 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -6,7 +6,7 @@ repos: rev: v4.4.0 hooks: - id: trailing-whitespace - - id: end-of-file-fixer + # - id: end-of-file-fixer - id: check-yaml - id: debug-statements @@ -15,12 +15,12 @@ repos: hooks: - id: black files: ^backend/ - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.5.1' - hooks: - - id: mypy - exclude: "[a-zA-Z]*/(migrations)/(.)*" - additional_dependencies: [ - djangorestframework-stubs, - django-stubs, - ] +# - repo: https://github.com/pre-commit/mirrors-mypy +# rev: 'v1.5.1' +# hooks: +# - id: mypy +# exclude: "[a-zA-Z]*/(migrations)/(.)*" +# additional_dependencies: [ +# djangorestframework-stubs, +# django-stubs, +# ] diff --git a/backend/.pre.commit-config.yaml b/backend/.pre.commit-config.yaml deleted file mode 100644 index 2eaf148..0000000 --- a/backend/.pre.commit-config.yaml +++ /dev/null @@ -1,26 +0,0 @@ -default_language_version: - # default language version for each language - python: python3.11 -repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.3.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: debug-statements - - - repo: https://github.com/psf/black - rev: 23.7.0 - hooks: - - id: black - files: ^backend/ - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.5.1' - hooks: - - id: mypy - exclude: "[a-zA-Z]*/(migrations)/(.)*" - additional_dependencies: [ - djangorestframework-stubs, - django-stubs, - ] diff --git a/backend/authentication/tests.py b/backend/authentication/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/backend/authentication/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/backend/history/tests.py b/backend/history/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/backend/history/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/backend/photos/tests.py b/backend/photos/tests.py deleted file mode 100644 index a39b155..0000000 --- a/backend/photos/tests.py +++ /dev/null @@ -1 +0,0 @@ -# Create your tests here. diff --git a/backend/photos/tests/__init__.py b/backend/photos/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/photos/tests/test_models.py b/backend/photos/tests/test_models.py new file mode 100644 index 0000000..50b8544 --- /dev/null +++ b/backend/photos/tests/test_models.py @@ -0,0 +1,9 @@ +import pytest + +from photos.models import Photo + + +@pytest.mark.django_db +def test_photo_model_create(): + photo = Photo.objects.create() + assert isinstance(photo, Photo), "Should create a Photo instance" diff --git a/backend/pytest.ini b/backend/pytest.ini index df7fdd4..9f809d1 100644 --- a/backend/pytest.ini +++ b/backend/pytest.ini @@ -1,4 +1,6 @@ [pytest] -python_files = tests.py test_*.py +DJANGO_SETTINGS_MODULE = core.settings + +python_files = tests.py test_*.py *_tests.py addopts = -s -q --disable-warnings --doctest-modules norecursedirs = .git .cache tmp* From 200bb49fe7d95e8bf57e055708c3614b09e43d77 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:00:55 +0200 Subject: [PATCH 06/15] 44 - dodano github actions --- .github/workflows/coverage.yaml | 85 --------------------------------- .github/workflows/main.yaml | 61 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 85 deletions(-) delete mode 100644 .github/workflows/coverage.yaml create mode 100644 .github/workflows/main.yaml diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml deleted file mode 100644 index 029971b..0000000 --- a/.github/workflows/coverage.yaml +++ /dev/null @@ -1,85 +0,0 @@ -name: coverage-example - -env: - DOCKER_BUILDKIT: 1 - COMPOSE_DOCKER_CLI_BUILD: 1 - -on: - pull_request: - branches: - - main - - develop - - master - - push: - branches: - - main - - develop - - master - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Build the Stack - run: docker-compose build - - name: Tear down the Stack - run: docker-compose down - - pytest-coverage: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Run DB Migrations - run: docker-compose run --rm django python manage.py migrate - - name: Run Django Tests - run: docker-compose run django coverage run -m pytest - - name: Generate coverage.lcov - run: docker-compose -f docker-compose.yml run django coverage lcov - - name: Tear down the Stack - run: docker-compose down - - name: Coveralls Parallel - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - file: "./backend/coverage.lcov" - base-path: "./backend" - flag-name: run-1 - parallel: true - uses: coverallsapp/github-action@v2 - - vitest-coverage: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Vitest Coverage - run: docker-compose run frontend npx vitest --coverage --no-watch - - name: Report Coverage - if: always() - uses: davelosert/vitest-coverage-report-action@v1 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - working-directory: frontend - - name: Coveralls Parallel - uses: coverallsapp/github-action@v2 - with: - flag-name: run-2 - parallel: true - - finish: - runs-on: ubuntu-latest - if: ${{ always() }} - needs: [ pytest-coverage, vitest-coverage ] - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Coveralls Finished - uses: coverallsapp/github-action@v2 - with: - parallel-finished: true - carryforward: "run-1,run-2" \ No newline at end of file diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..111956b --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,61 @@ +name: backend-example + +env: + DOCKER_BUILDKIT: 1 + COMPOSE_DOCKER_CLI_BUILD: 1 + +on: + pull_request: + branches: + - main + - develop + - master + - dev + + push: + branches: + - main + - develop + - master + - dev +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Build the Stack + run: docker-compose build + - name: Tear down the Stack + run: docker-compose down + + pytest: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Run DB Migrations + run: docker-compose run --rm django python manage.py migrate + - name: Run Django Tests + run: docker-compose run django pytest -s -v --no-migrations + + linters: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Flake8 + run: docker-compose run django flake8 . --config=setup.cfg + - name: Isort + run: docker-compose run django isort . --settings-path=.isort.cfg + + mypy: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Checkout mypy + run: docker-compose run django mypy . --config-file mypy.ini \ No newline at end of file From 22b157f1e6bc4ce2c80c18409fae6a67a8cae375 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:02:40 +0200 Subject: [PATCH 07/15] 44 - dodano github actions --- .github/workflows/coverage.yaml | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 .github/workflows/coverage.yaml diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml new file mode 100644 index 0000000..8679bd9 --- /dev/null +++ b/.github/workflows/coverage.yaml @@ -0,0 +1,54 @@ +name: coverage-example + +env: + DOCKER_BUILDKIT: 1 + COMPOSE_DOCKER_CLI_BUILD: 1 + +on: + pull_request: + branches: + - main + - develop + - master + - dev + + push: + branches: + - main + - develop + - master + - dev + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Build the Stack + run: docker-compose build + - name: Tear down the Stack + run: docker-compose down + + pytest-coverage: + runs-on: ubuntu-latest + needs: docker + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + - name: Run DB Migrations + run: docker-compose run --rm django python manage.py migrate + - name: Run Django Tests + run: docker-compose run django coverage run -m pytest + - name: Generate coverage.lcov + run: docker-compose -f docker-compose.yml run django coverage lcov + - name: Tear down the Stack + run: docker-compose down + - name: Coveralls Parallel + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + file: "./backend/coverage.lcov" + base-path: "./backend" + flag-name: run-1 + parallel: true + uses: coverallsapp/github-action@v2 \ No newline at end of file From db4c94927d5f25f7867966bb2bfbdf7f1c4ce551 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:07:06 +0200 Subject: [PATCH 08/15] 44 - poprawiono settingsy --- backend/core/settings.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/core/settings.py b/backend/core/settings.py index 2f7572b..9a4a97c 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -87,14 +87,14 @@ # https://docs.djangoproject.com/en/4.1/ref/settings/#databases DATABASES = { - "default": { - "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": os.environ.get("DB_NAME", ""), - "USER": os.environ.get("DB_USER", ""), - "PASSWORD": os.environ.get("DB_PASSWORD", ""), - "HOST": os.environ.get("DB_HOST", ""), - "PORT": os.environ.get("DB_PORT", "5432"), - }, + # "default": { + # "ENGINE": "django.db.backends.postgresql_psycopg2", + # "NAME": os.environ.get("DB_NAME", ""), + # "USER": os.environ.get("DB_USER", ""), + # "PASSWORD": os.environ.get("DB_PASSWORD", ""), + # "HOST": os.environ.get("DB_HOST", ""), + # "PORT": os.environ.get("DB_PORT", "5432"), + # }, "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / "db.sqlite3", From a4ce92406739bbb2ca7a1bc911263bc13e79f033 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:12:24 +0200 Subject: [PATCH 09/15] 44 - poprawiono actions --- .github/workflows/coverage.yaml | 6 +++--- .github/workflows/main.yaml | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml index 8679bd9..35974af 100644 --- a/.github/workflows/coverage.yaml +++ b/.github/workflows/coverage.yaml @@ -37,11 +37,11 @@ jobs: - name: Checkout Code Repository uses: actions/checkout@v3 - name: Run DB Migrations - run: docker-compose run --rm django python manage.py migrate + run: docker-compose run --rm backend python manage.py migrate - name: Run Django Tests - run: docker-compose run django coverage run -m pytest + run: docker-compose run backend coverage run -m pytest - name: Generate coverage.lcov - run: docker-compose -f docker-compose.yml run django coverage lcov + run: docker-compose -f docker-compose.yml run backend coverage lcov - name: Tear down the Stack run: docker-compose down - name: Coveralls Parallel diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 111956b..73dd6af 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -36,9 +36,9 @@ jobs: - name: Checkout Code Repository uses: actions/checkout@v3 - name: Run DB Migrations - run: docker-compose run --rm django python manage.py migrate + run: docker-compose run --rm backend python manage.py migrate - name: Run Django Tests - run: docker-compose run django pytest -s -v --no-migrations + run: docker-compose run backend pytest -s -v --no-migrations linters: runs-on: ubuntu-latest @@ -47,9 +47,9 @@ jobs: - name: Checkout Code Repository uses: actions/checkout@v3 - name: Flake8 - run: docker-compose run django flake8 . --config=setup.cfg + run: docker-compose run backend flake8 . --config=setup.cfg - name: Isort - run: docker-compose run django isort . --settings-path=.isort.cfg + run: docker-compose run backend isort . --settings-path=.isort.cfg mypy: runs-on: ubuntu-latest @@ -58,4 +58,4 @@ jobs: - name: Checkout Code Repository uses: actions/checkout@v3 - name: Checkout mypy - run: docker-compose run django mypy . --config-file mypy.ini \ No newline at end of file + run: docker-compose run backend mypy . --config-file mypy.ini \ No newline at end of file From e9c02865191c08aea261dcc81a427bc2b3462690 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:17:52 +0200 Subject: [PATCH 10/15] 44 - poprawiono actions --- .github/workflows/main.yaml | 61 ------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 .github/workflows/main.yaml diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml deleted file mode 100644 index 73dd6af..0000000 --- a/.github/workflows/main.yaml +++ /dev/null @@ -1,61 +0,0 @@ -name: backend-example - -env: - DOCKER_BUILDKIT: 1 - COMPOSE_DOCKER_CLI_BUILD: 1 - -on: - pull_request: - branches: - - main - - develop - - master - - dev - - push: - branches: - - main - - develop - - master - - dev -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Build the Stack - run: docker-compose build - - name: Tear down the Stack - run: docker-compose down - - pytest: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Run DB Migrations - run: docker-compose run --rm backend python manage.py migrate - - name: Run Django Tests - run: docker-compose run backend pytest -s -v --no-migrations - - linters: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Flake8 - run: docker-compose run backend flake8 . --config=setup.cfg - - name: Isort - run: docker-compose run backend isort . --settings-path=.isort.cfg - - mypy: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Checkout mypy - run: docker-compose run backend mypy . --config-file mypy.ini \ No newline at end of file From b94b9df91a7c3be772615cbda746bd3af589f816 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:23:20 +0200 Subject: [PATCH 11/15] 44 - usunieto actions --- .github/workflows/coverage.yaml | 54 --------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 .github/workflows/coverage.yaml diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml deleted file mode 100644 index 35974af..0000000 --- a/.github/workflows/coverage.yaml +++ /dev/null @@ -1,54 +0,0 @@ -name: coverage-example - -env: - DOCKER_BUILDKIT: 1 - COMPOSE_DOCKER_CLI_BUILD: 1 - -on: - pull_request: - branches: - - main - - develop - - master - - dev - - push: - branches: - - main - - develop - - master - - dev - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Build the Stack - run: docker-compose build - - name: Tear down the Stack - run: docker-compose down - - pytest-coverage: - runs-on: ubuntu-latest - needs: docker - steps: - - name: Checkout Code Repository - uses: actions/checkout@v3 - - name: Run DB Migrations - run: docker-compose run --rm backend python manage.py migrate - - name: Run Django Tests - run: docker-compose run backend coverage run -m pytest - - name: Generate coverage.lcov - run: docker-compose -f docker-compose.yml run backend coverage lcov - - name: Tear down the Stack - run: docker-compose down - - name: Coveralls Parallel - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - file: "./backend/coverage.lcov" - base-path: "./backend" - flag-name: run-1 - parallel: true - uses: coverallsapp/github-action@v2 \ No newline at end of file From c68bc0aa081b2d514b947f7ad6c03b347e47dbe1 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:36:57 +0200 Subject: [PATCH 12/15] 44 - dodano pliki --- backend/.pre-commit-config.yaml | 18 +++++++++--------- backend/Dockerfile | 2 +- backend/core/models.py | 18 +++++++++--------- backend/entrypoint.sh | 1 - backend/enviroments.env | 1 - backend/mypy.ini | 14 +------------- backend/photos/admin.py | 6 ++---- backend/photos/models.py | 10 ++++++---- backend/requirements.txt | 2 ++ 9 files changed, 30 insertions(+), 42 deletions(-) diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 364ab9d..de32a8a 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -15,12 +15,12 @@ repos: hooks: - id: black files: ^backend/ -# - repo: https://github.com/pre-commit/mirrors-mypy -# rev: 'v1.5.1' -# hooks: -# - id: mypy -# exclude: "[a-zA-Z]*/(migrations)/(.)*" -# additional_dependencies: [ -# djangorestframework-stubs, -# django-stubs, -# ] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.5.1' + hooks: + - id: mypy + exclude: "[a-zA-Z]*/(migrations)/(.)*" + additional_dependencies: [ + djangorestframework-stubs, + django-stubs, + ] diff --git a/backend/Dockerfile b/backend/Dockerfile index ae7e0e0..d611fc9 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -13,4 +13,4 @@ EXPOSE 8000 RUN ["chmod", "+x", "./entrypoint.sh"] -CMD "./entrypoint.sh" \ No newline at end of file +CMD "./entrypoint.sh" diff --git a/backend/core/models.py b/backend/core/models.py index 5fa2dfd..763ce3e 100644 --- a/backend/core/models.py +++ b/backend/core/models.py @@ -1,15 +1,15 @@ -from django.db import models from uuid import uuid4 +from django.db import models + class BaseModel(models.Model): - uuid = models.UUIDField( - primary_key=True, - default=uuid4, - editable=False) - created_date = models.DateTimeField(auto_now_add=True) - updated_date = models.DateTimeField(auto_now=True) - is_deleted = models.BooleanField(default=False) + uuid: models.UUIDField = models.UUIDField( + primary_key=True, default=uuid4, editable=False + ) + created_date: models.DateTimeField = models.DateTimeField(auto_now_add=True) + updated_date: models.DateTimeField = models.DateTimeField(auto_now=True) + is_deleted: models.BooleanField = models.BooleanField(default=False) class Meta: - abstract = True \ No newline at end of file + abstract = True diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index b1b8593..a476cfa 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -10,4 +10,3 @@ echo "Starting server" gunicorn --config gunicorn-cfg.py core.wsgi exec "$@" - diff --git a/backend/enviroments.env b/backend/enviroments.env index 5929f52..e482f49 100644 --- a/backend/enviroments.env +++ b/backend/enviroments.env @@ -8,4 +8,3 @@ DB_USER=s208 DB_PASSWORD=C6A2_ff8d9d DB_HOST=psql01.mikr.us DB_PORT=5432 - diff --git a/backend/mypy.ini b/backend/mypy.ini index af3d98a..30bc736 100644 --- a/backend/mypy.ini +++ b/backend/mypy.ini @@ -1,10 +1,7 @@ [mypy] -# results. python_version = 3.11 -plugins = - mypy_django_plugin.main, - mypy_drf_plugin.main +plugins = mypy_django_plugin.main, mypy_drf_plugin.main ignore_missing_imports = True warn_unused_ignores = True @@ -15,12 +12,3 @@ exclude = venv [mypy.plugins.django-stubs] django_settings_module = "core.settings" namespace_packages = True - -[mypy-*.migrations.*] -ignore_errors = True - -[mypy-tests.*] -disallow_untyped_defs = False - -[mypy_django_plugin] -ignore_missing_model_attributes = True diff --git a/backend/photos/admin.py b/backend/photos/admin.py index 08a7e6f..09e6f62 100644 --- a/backend/photos/admin.py +++ b/backend/photos/admin.py @@ -3,15 +3,13 @@ from photos.models import Photo, PhotoPositions +@admin.register(Photo) class PhotoAdmin(admin.ModelAdmin): date_hierarchy = "created_date" list_display = ["uuid", "status", "photo", "created_date", "column_id", "order"] +@admin.register(PhotoPositions) class PhotoPositionsAdmin(admin.ModelAdmin): date_hierarchy = "created_date" list_display = ["uuid", "created_date"] - - -admin.site.register(Photo, PhotoAdmin) -admin.site.register(PhotoPositions, PhotoPositionsAdmin) diff --git a/backend/photos/models.py b/backend/photos/models.py index 612cfec..5d93533 100644 --- a/backend/photos/models.py +++ b/backend/photos/models.py @@ -22,15 +22,17 @@ class Photo(BaseModel): (POSITIONED, "positioned at last configuration"), (UNPOSITIONED, "not positioned"), ] - photo = models.ImageField( + photo: models.ImageField = models.ImageField( upload_to=upload_to, max_length=100, verbose_name="photos", validators=[FileExtensionValidator(["jpg", "jpeg"])], ) - column_id = models.IntegerField(default=1) - order = models.IntegerField(default=0) - status = models.CharField(choices=STATUS_PHOTO, max_length=20, default=NEW) + column_id: models.IntegerField = models.IntegerField(default=1) + order: models.IntegerField = models.IntegerField(default=0) + status: models.CharField = models.CharField( + choices=STATUS_PHOTO, max_length=20, default=NEW + ) def __str__(self): return f"{self.uuid} - {self.photo}" diff --git a/backend/requirements.txt b/backend/requirements.txt index 444b140..d128f57 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -13,3 +13,5 @@ PyJWT==2.7.0 pytz==2023.3 sqlparse==0.4.3 whitenoise==6.5.0 + +pytest~=7.4.0 \ No newline at end of file From 264a69f15de883a884c94f9a7b83476258677870 Mon Sep 17 00:00:00 2001 From: kwiats Date: Sun, 20 Aug 2023 15:37:22 +0200 Subject: [PATCH 13/15] 44 - dodano pliki --- backend/photos/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/photos/models.py b/backend/photos/models.py index 5d93533..3c38680 100644 --- a/backend/photos/models.py +++ b/backend/photos/models.py @@ -13,7 +13,7 @@ def upload_to(instance, filename): class PhotoPositions(BaseModel): - columns = models.JSONField(null=True) + columns: models.JSONField = models.JSONField(null=True) class Photo(BaseModel): From 47beec0ec63f7119678a71495cd80db1cdaa5f24 Mon Sep 17 00:00:00 2001 From: kwiats Date: Mon, 21 Aug 2023 19:03:03 +0200 Subject: [PATCH 14/15] Przygotowano srodowisko do testo --- backend/authentication/views.py | 6 ---- backend/core/settings.py | 14 ++++++---- backend/entrypoint.sh | 6 ++-- backend/photos/tests/factory.py | 40 +++++++++++++++++++++++++++ backend/photos/tests/test_models.py | 20 ++++++++++++-- backend/photos/tests/test_services.py | 0 backend/requirements.txt | 29 +++++++++++++++++-- 7 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 backend/photos/tests/factory.py create mode 100644 backend/photos/tests/test_services.py diff --git a/backend/authentication/views.py b/backend/authentication/views.py index 9c58df9..d48ebb2 100644 --- a/backend/authentication/views.py +++ b/backend/authentication/views.py @@ -1,10 +1,4 @@ -import http - -import jwt -from rest_framework.response import Response -from rest_framework.views import APIView from rest_framework_simplejwt.views import TokenObtainPairView -from django.contrib.auth import authenticate, login from authentication.serializers import TokenSerializer diff --git a/backend/core/settings.py b/backend/core/settings.py index 9a4a97c..1e88bf0 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -89,10 +89,10 @@ DATABASES = { # "default": { # "ENGINE": "django.db.backends.postgresql_psycopg2", - # "NAME": os.environ.get("DB_NAME", ""), - # "USER": os.environ.get("DB_USER", ""), - # "PASSWORD": os.environ.get("DB_PASSWORD", ""), - # "HOST": os.environ.get("DB_HOST", ""), + # "NAME": os.environ.get("DB_NAME", "photo-library-ns"), + # "USER": os.environ.get("DB_USER", "photo-library-ns"), + # "PASSWORD": os.environ.get("DB_PASSWORD", "photo-library-ns#01"), + # "HOST": os.environ.get("DB_HOST", "db"), # "PORT": os.environ.get("DB_PORT", "5432"), # }, "default": { @@ -133,8 +133,10 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.1/howto/static-files/ -STATIC_URL = "/staticfiles/" -STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") +STATIC_URL = "/static/" +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static"), +] MEDIA_URL = "/media/" diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index a476cfa..8a6b676 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -1,10 +1,12 @@ #!/bin/bash -# Collect static files + +echo "Migrations" +python3 manage.py migrate --no-input + echo "Collect static files" python3 manage.py collectstatic --no-input -# Start server echo "Starting server" # python3 manage.py runserver 0.0.0.0:8000 gunicorn --config gunicorn-cfg.py core.wsgi diff --git a/backend/photos/tests/factory.py b/backend/photos/tests/factory.py new file mode 100644 index 0000000..6122cd9 --- /dev/null +++ b/backend/photos/tests/factory.py @@ -0,0 +1,40 @@ +import json + +from factory import LazyFunction +from factory.django import DjangoModelFactory +from faker import Faker +from faker.providers import file, misc, date_time + +from photos.models import Photo, PhotoPositions + +faker = Faker() +faker.add_provider(file) +faker.add_provider(misc) +faker.add_provider(date_time) + +STATUS = [x[0] for x in Photo.STATUS_PHOTO] + + +class PhotoFactory(DjangoModelFactory): + uuid = LazyFunction(lambda: faker.uuid4()) + photo = LazyFunction(lambda: faker.file_path(depth=3, extension="jpg")) + column_id = LazyFunction(lambda: faker.random_int(min=0, max=3)) + order = LazyFunction(lambda: faker.random_int()) + status = LazyFunction(lambda: faker.random_choices(elements=STATUS)) + created_date = LazyFunction(lambda: faker.date_time()) + + class Meta: + model = Photo + + +class PhotoPositionsFactory(DjangoModelFactory): + columns = LazyFunction( + lambda: { + "1": json.dumps([PhotoFactory.build() for _ in range(faker.random_int())]), + "2": json.dumps([PhotoFactory.build() for _ in range(faker.random_int())]), + "3": json.dumps([PhotoFactory.build() for _ in range(faker.random_int())]), + } + ) + + class Meta: + model = PhotoPositions diff --git a/backend/photos/tests/test_models.py b/backend/photos/tests/test_models.py index 50b8544..ea54070 100644 --- a/backend/photos/tests/test_models.py +++ b/backend/photos/tests/test_models.py @@ -1,9 +1,23 @@ +import datetime + import pytest +from django.db.models.fields.files import ImageFieldFile from photos.models import Photo +from photos.tests.factory import PhotoFactory @pytest.mark.django_db -def test_photo_model_create(): - photo = Photo.objects.create() - assert isinstance(photo, Photo), "Should create a Photo instance" +class TestPhoto: + @pytest.fixture + def photo(self): + return PhotoFactory.create() + + def test_instance_photo(self, photo): + assert isinstance(photo, Photo) + assert isinstance(photo.uuid, str) + assert isinstance(photo.photo, ImageFieldFile) + assert isinstance(photo.column_id, int) + assert isinstance(photo.status, list) + assert isinstance(photo.order, int) + assert isinstance(photo.created_date, datetime.datetime) diff --git a/backend/photos/tests/test_services.py b/backend/photos/tests/test_services.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/requirements.txt b/backend/requirements.txt index d128f57..3d73232 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,17 +1,42 @@ asgiref==3.6.0 +certifi==2023.7.22 +charset-normalizer==3.2.0 +coverage==7.3.0 Django==4.1.7 django-cors-headers==3.14.0 django-environ==0.10.0 django-rest-framework==0.1.0 +django-stubs==4.2.3 +django-stubs-ext==4.2.2 djangorestframework==3.14.0 djangorestframework-simplejwt==5.2.2 +djangorestframework-stubs==3.14.2 +Faker==12.0.1 gunicorn==21.2.0 +idna==3.4 +iniconfig==2.0.0 +mixer==7.2.2 +mypy==1.5.1 +mypy-extensions==1.0.0 packaging==23.1 Pillow==10.0.0 +pluggy==1.2.0 psycopg2-binary==2.9.6 PyJWT==2.7.0 +pytest==7.4.0 +pytest-cov==4.1.0 +pytest-django==4.5.2 +pytest-sugar==0.9.7 +python-dateutil==2.8.2 pytz==2023.3 +requests==2.31.0 +six==1.16.0 sqlparse==0.4.3 +termcolor==2.3.0 +types-pytz==2023.3.0.1 +types-PyYAML==6.0.12.11 +types-requests==2.31.0.2 +types-urllib3==1.26.25.14 +typing_extensions==4.7.1 +urllib3==2.0.4 whitenoise==6.5.0 - -pytest~=7.4.0 \ No newline at end of file From e918a9aa1a5387882175d0dc941c039e2a4b05a8 Mon Sep 17 00:00:00 2001 From: kwiats Date: Mon, 21 Aug 2023 19:04:57 +0200 Subject: [PATCH 15/15] dodano DB do docker-compose --- docker-compose.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c4a740b..b75004d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,8 @@ services: - static:/code/static/ ports: - "8080:8000" + depends_on: + - db frontend: build: @@ -21,8 +23,18 @@ services: - "88:80" depends_on: - backend + db: - image: postgres:latest + image: postgres:13-alpine + environment: + - POSTGRES_DB=photo-library-ns + - POSTGRES_USER=photo-library-ns + - POSTGRES_PASSWORD=photo-library-ns#01 + ports: + - "5432:5432" + volumes: + - postgres-db:/var/lib/postgresql/data + app: image: 'jc21/nginx-proxy-manager:latest' container_name: nginxproxymanager @@ -41,4 +53,4 @@ services: volumes: media: static: - + postgres-db: