Skip to content

Commit

Permalink
Instalado pytest cov
Browse files Browse the repository at this point in the history
close #38

Feito teste para configuração de bucket
  • Loading branch information
renzo authored and renzon committed Sep 12, 2024
1 parent a880542 commit 82cd996
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
run: cp ../config/env-sample .env

- name: Run backend Tests
run: poetry run pytest --cache-clear
run: poetry run pytest devpro --cov=devpro --cov-fail-under=92 --cache-clear
6 changes: 6 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Para rodar os testes automáticos execute:
pytest devpro
```

Para gerar relatório de cobertura:

```bash
pytest devpro/ --cov devpro/ --cov-report html
```

# Shell do DJango

Essa template vem com [Django Extensions](https://django-extensions.readthedocs.io/) instalado. Então se recomenda usar
Expand Down
36 changes: 36 additions & 0 deletions backend/devpro/base/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

from devpro import settings


@pytest.fixture
def settings_empty_bucket():
settings.configure_storage(False)
return settings


def test_no_s3_configured_for_files(settings_empty_bucket):
settings_empty_bucket.AWS_STORAGE_BUCKET_NAME = ''
assert settings_empty_bucket.STORAGES['default'][
'BACKEND'] == 'devpro_s3_storages.handlers.FileSystemWithValidationStorage'


def test_no_s3_configured_for_static(settings_empty_bucket):
settings.AWS_STORAGE_BUCKET_NAME = ''
assert settings.STORAGES['staticfiles']['BACKEND'] == 'django.contrib.staticfiles.storage.StaticFilesStorage'


@pytest.fixture
def settings_with_bucket():
settings.configure_storage(True)
return settings


def test_s3_configured_for_files(settings_with_bucket):
assert settings.STORAGES['default']['BACKEND'] == 'devpro_s3_storages.handlers.S3FileStorage'


def test_s3_configured_for_static(settings_with_bucket):
settings_with_bucket.AWS_STORAGE_BUCKET_NAME = 'somebucket'
assert settings_with_bucket.STORAGES['staticfiles'][
'BACKEND'] == 'storages.backends.s3.S3Storage'
81 changes: 46 additions & 35 deletions backend/devpro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,43 +125,54 @@

AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME', default='').strip()

if AWS_STORAGE_BUCKET_NAME == '':
# Configuração para coletar estáticos para o Nginx
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR.parent / 'docker/staticfiles/static'
MEDIA_ROOT = BASE_DIR.parent / 'docker/mediafiles'
MEDIA_URL = '/mediafiles/'
# Muda a configuração de upload de arquivos locais para bater com produção
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.FileSystemWithValidationStorage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}
else:
STATIC_URL = f'//{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'

AWS_S3_ACCESS_KEY_ID = config('AWS_S3_ACCESS_KEY_ID')
AWS_S3_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY')
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.S3FileStorage",
"OPTIONS": {
'default_acl': 'private',
'location': 'media',

# Defining function to be able to test it
def configure_storage(has_s3_bucket: bool):
global STATIC_URL, STORAGES
if not has_s3_bucket:
global STATIC_ROOT, MEDIA_ROOT, MEDIA_URL
# Muda a configuração de upload de arquivos locais para bater com produção
# Configuração para coletar estáticos para o Nginx
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR.parent / 'docker/staticfiles/static'
MEDIA_ROOT = BASE_DIR.parent / 'docker/mediafiles'
MEDIA_URL = '/mediafiles/'
# Muda a configuração de upload de arquivos locais para bater com produção
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.FileSystemWithValidationStorage",
},
},
"staticfiles": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
'default_acl': 'public-read',
'location': 'static',
'querystring_auth': False
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
},
}
}
else:
global AWS_S3_ACCESS_KEY_ID, AWS_S3_SECRET_ACCESS_KEY

STATIC_URL = f'//{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'

AWS_S3_ACCESS_KEY_ID = config('AWS_S3_ACCESS_KEY_ID')
AWS_S3_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY')
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.S3FileStorage",
"OPTIONS": {
'default_acl': 'private',
'location': 'media',
},
},
"staticfiles": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
'default_acl': 'public-read',
'location': 'static',
'querystring_auth': False
},
},
}


configure_storage(AWS_STORAGE_BUCKET_NAME == '')

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
104 changes: 103 additions & 1 deletion backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "Template de projeto Django com as boas práticas da DevPro"
authors = ["renzo <[email protected]>"]
license = "MIT"
readme = "README.md"
package-mode=false

[tool.poetry.dependencies]
python = "^3.12"
Expand All @@ -27,6 +28,7 @@ optional = true
pytest = "^8.2.2"
pytest-django = "^4.8.0"
flake8 = "^7.1.0"
pytest-cov = "^5.0.0"

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "devpro.settings"
Expand Down

0 comments on commit 82cd996

Please sign in to comment.