Commit 33e04208 authored by Florent Chehab's avatar Florent Chehab
Browse files

tweaks and switch to django-pytest

parent 041d5304
[run] [run]
omit = *migrations* omit =
source = . *migrations*
tests/*
source = .
...@@ -8,4 +8,5 @@ db.sqlite3 ...@@ -8,4 +8,5 @@ db.sqlite3
node_modules node_modules
.coverage .coverage
htmlcov htmlcov
.vscode .vscode
\ No newline at end of file .pytest_cache
...@@ -14,9 +14,7 @@ testing: ...@@ -14,9 +14,7 @@ testing:
- export DATABASE_URL=postgres://postgres:@postgres:5432/python-test-app - export DATABASE_URL=postgres://postgres:@postgres:5432/python-test-app
- pip install -r requirements.txt --quiet - pip install -r requirements.txt --quiet
- ./manage.py check - ./manage.py check
- coverage3 run manage.py test - pytest -n 4 general/ frontend/ backend/ --cov-report html
- coverage3 report
- coverage3 html
artifacts: artifacts:
paths: paths:
- htmlcov/ - htmlcov/
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
"**/.DS_Store": true, "**/.DS_Store": true,
"**/__pycache__":true, "**/__pycache__":true,
"**/*.pyc":true, "**/*.pyc":true,
"htmlcov":true "htmlcov":false,
"env":true,
".coverage":true
}, },
"cSpell.language": "en,fr-FR,fr", "cSpell.language": "en,fr-FR,fr",
"python.linting.flake8Enabled": true "python.linting.flake8Enabled": true
......
...@@ -29,7 +29,7 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -29,7 +29,7 @@ class MyModelSerializer(serializers.ModelSerializer):
model = MyModel model = MyModel
def moderation_required(self): def moderation_required(self):
user = self.context['request'].user user = self.get_user_in_request()
request = self.context['request'] request = self.context['request']
if user.is_staff: if user.is_staff:
...@@ -44,8 +44,11 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -44,8 +44,11 @@ class MyModelSerializer(serializers.ModelSerializer):
return False return False
def get_user_in_request(self):
return self.context['request'].user
def set_model_attr_no_moder(self, moderated_and_updated): def set_model_attr_no_moder(self, moderated_and_updated):
user = self.context['request'].user user = self.get_user_in_request()
now = timezone.now() now = timezone.now()
self.validated_data['moderated_by'] = user self.validated_data['moderated_by'] = user
self.validated_data['moderated_on'] = now self.validated_data['moderated_on'] = now
...@@ -66,17 +69,17 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -66,17 +69,17 @@ class MyModelSerializer(serializers.ModelSerializer):
content_type=ct, object_id=self.instance.pk) content_type=ct, object_id=self.instance.pk)
def save(self, **kwargs): def save(self, **kwargs):
user = self.get_user_in_request()
self.clean_validated_data() self.clean_validated_data()
if self.moderation_required(): if self.moderation_required():
print("Modération requise") print("Modération requise")
user = self.set_model_attr_for_moder()
if self.instance is None: # we need to create the main model if self.instance is None: # we need to create the main model
self.instance = super( self.instance = super(
MyModelSerializer, self).save(**kwargs) MyModelSerializer, self).save(**kwargs)
objs_pending_db = self.get_pending_models() objs_pending_db = self.get_pending_models()
user = self.context['request'].user
self.clean_validated_data() # Make that it is done... self.clean_validated_data() # Make that it is done...
if len(objs_pending_db) > 0: if len(objs_pending_db) > 0:
......
# from django.test import TestCase
# Create your tests here.
[pytest]
DJANGO_SETTINGS_MODULE = general.settings
python_files = tests.py test_*.py *_tests.py
addopts = --cov --cov-config .coveragerc --cov-report term
...@@ -13,6 +13,8 @@ django-imagekit==4.0.2 ...@@ -13,6 +13,8 @@ django-imagekit==4.0.2
django-reversion==2.0.13 django-reversion==2.0.13
django-reversion-compare==0.8.4 django-reversion-compare==0.8.4
reverse-geocoder==1.5.1 reverse-geocoder==1.5.1
coverage==4.5.1 pytest-django==3.4.2
pytest-cov==2.5.1
pytest-xdist==1.23.0
flake8 flake8
django-debug-toolbar==1.9.1 django-debug-toolbar==1.9.1
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment