SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit c843f212 authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Add VetCommentaryForm tests

Add more VetCommentary tests for the
three different possible valid form actions.
parent 5cebcdab
No related branches found
No related tags found
No related merge requests found
...@@ -161,17 +161,6 @@ class VetCommentaryForm(forms.Form): ...@@ -161,17 +161,6 @@ class VetCommentaryForm(forms.Form):
if self.commentary_is_refused(): if self.commentary_is_refused():
return self.COMMENTARY_REFUSAL_DICT[int(self.cleaned_data['refusal_reason'])] return self.COMMENTARY_REFUSAL_DICT[int(self.cleaned_data['refusal_reason'])]
def process_commentary(self):
"""Vet the commentary or delete it from the database"""
if self.commentary_is_accepted():
self.commentary.vetted = True
self.commentary.vetted_by = Contributor.objects.get(user=self.user)
self.commentary.save()
return self.commentary
elif self.commentary_is_modified() or self.commentary_is_refused():
self.commentary.delete()
return None
def commentary_is_accepted(self): def commentary_is_accepted(self):
self._form_is_cleaned() self._form_is_cleaned()
return int(self.cleaned_data['action_option']) == self.ACTION_ACCEPT return int(self.cleaned_data['action_option']) == self.ACTION_ACCEPT
...@@ -184,6 +173,17 @@ class VetCommentaryForm(forms.Form): ...@@ -184,6 +173,17 @@ class VetCommentaryForm(forms.Form):
self._form_is_cleaned() self._form_is_cleaned()
return int(self.cleaned_data['action_option']) == self.ACTION_REFUSE return int(self.cleaned_data['action_option']) == self.ACTION_REFUSE
def process_commentary(self):
"""Vet the commentary or delete it from the database"""
if self.commentary_is_accepted():
self.commentary.vetted = True
self.commentary.vetted_by = Contributor.objects.get(user=self.user)
self.commentary.save()
return self.commentary
elif self.commentary_is_modified() or self.commentary_is_refused():
self.commentary.delete()
return None
class CommentarySearchForm(forms.Form): class CommentarySearchForm(forms.Form):
"""Search for Commentary specified by user""" """Search for Commentary specified by user"""
......
...@@ -2,6 +2,7 @@ from django.test import TestCase ...@@ -2,6 +2,7 @@ from django.test import TestCase
from scipost.factories import UserFactory from scipost.factories import UserFactory
from .models import Commentary
from .factories import VettedCommentaryFactory, UnVettedCommentaryFactory from .factories import VettedCommentaryFactory, UnVettedCommentaryFactory
from .forms import RequestCommentaryForm, VetCommentaryForm from .forms import RequestCommentaryForm, VetCommentaryForm
from common.helpers import model_form_data from common.helpers import model_form_data
...@@ -13,18 +14,55 @@ class TestVetCommentaryForm(TestCase): ...@@ -13,18 +14,55 @@ class TestVetCommentaryForm(TestCase):
def setUp(self): def setUp(self):
self.commentary = UnVettedCommentaryFactory.create() self.commentary = UnVettedCommentaryFactory.create()
self.user = UserFactory() self.user = UserFactory()
self.form_data = {
def test_valid_form(self):
"""Test valid form data and return Commentary when accepted"""
form_data = {
'action_option': VetCommentaryForm.ACTION_ACCEPT, 'action_option': VetCommentaryForm.ACTION_ACCEPT,
'refusal_reason': VetCommentaryForm.REFUSAL_EMPTY, 'refusal_reason': VetCommentaryForm.REFUSAL_EMPTY,
'email_response_field': 'Lorem Ipsum' 'email_response_field': 'Lorem Ipsum'
} }
print( form_data)
form = VetCommentaryForm(form_data, commentary_id=self.commentary.id, user=self.user) def test_valid_accepted_form(self):
print(form.errors) """Test valid form data and return Commentary"""
form = VetCommentaryForm(self.form_data, commentary_id=self.commentary.id, user=self.user)
self.assertTrue(form.is_valid())
self.assertFalse(Commentary.objects.vetted().exists())
self.assertTrue(Commentary.objects.awaiting_vetting().exists())
# Accept Commentary in database
form.process_commentary()
self.assertTrue(Commentary.objects.vetted().exists())
self.assertFalse(Commentary.objects.awaiting_vetting().exists())
def test_valid_modified_form(self):
"""Test valid form data and delete Commentary"""
self.form_data['action_option'] = VetCommentaryForm.ACTION_MODIFY
form = VetCommentaryForm(self.form_data, commentary_id=self.commentary.id, user=self.user)
self.assertTrue(form.is_valid())
self.assertFalse(Commentary.objects.vetted().exists())
self.assertTrue(Commentary.objects.awaiting_vetting().exists())
# Delete the Commentary
form.process_commentary()
self.assertTrue(form.commentary_is_modified())
self.assertFalse(Commentary.objects.awaiting_vetting().exists())
def test_valid_rejected_form(self):
"""Test valid form data and delete Commentary"""
self.form_data['action_option'] = VetCommentaryForm.ACTION_REFUSE
self.form_data['refusal_reason'] = VetCommentaryForm.REFUSAL_UNTRACEBLE
form = VetCommentaryForm(self.form_data, commentary_id=self.commentary.id, user=self.user)
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertFalse(Commentary.objects.vetted().exists())
self.assertTrue(Commentary.objects.awaiting_vetting().exists())
# Delete the Commentary
form.process_commentary()
self.assertTrue(form.commentary_is_refused())
self.assertFalse(Commentary.objects.awaiting_vetting().exists())
# Refusal choice is ok
refusal_reason_inserted = VetCommentaryForm.COMMENTARY_REFUSAL_DICT[\
VetCommentaryForm.REFUSAL_UNTRACEBLE]
self.assertEqual(form.get_refusal_reason(), refusal_reason_inserted)
class TestRequestCommentaryForm(TestCase): class TestRequestCommentaryForm(TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment