From a3e771cc675f9002e8108bf74db35383f9ed9cba Mon Sep 17 00:00:00 2001 From: Amit Finzi Date: Wed, 18 Jan 2023 19:12:42 +0200 Subject: [PATCH] Adding tests for delete_article and edit_article --- projboard/forms.py | 5 +---- projboard/tests/test_client.py | 29 +++++++++++++---------------- projboard/views.py | 10 ++++++---- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/projboard/forms.py b/projboard/forms.py index 2aae5be..4cd4683 100644 --- a/projboard/forms.py +++ b/projboard/forms.py @@ -41,7 +41,4 @@ def clean(self): class EditArticleForm(forms.ModelForm): class Meta: model = Article - fields = ('user_id', 'title', 'subject_id', 'content') - widgets = { - 'user_id': forms.HiddenInput(), - } + fields = ('title', 'subject_id', 'content') diff --git a/projboard/tests/test_client.py b/projboard/tests/test_client.py index 1b10736..ccba02d 100644 --- a/projboard/tests/test_client.py +++ b/projboard/tests/test_client.py @@ -29,10 +29,10 @@ WRONG_USER_MESSAGE = "User Name Not Valid" INVALID_ARTICLE = 1234567 VALID_TITLE = 'valid title' -VALID_SUBJECT = 'Software' +VALID_SUBJECT = 5 VALID_CONTENT = 'valid content' NEW_TITLE = 'new title' -NEW_SUBJECT = 'Hobbies' +NEW_SUBJECT = 4 NEW_CONTENT = 'new content' @@ -269,22 +269,19 @@ def test_fail_delete_article(client): @pytest.mark.django_db def test_edit_article(client, article): - response = client.get(f"/edit_article/{article.id}/", {"title": NEW_TITLE, - "subject_id": NEW_SUBJECT, - "content": NEW_CONTENT, - "user_id": article.user_id}) - assert response.status_code == 200 - - template_names = set(tmpl.origin.template_name for tmpl in response.templates) - assert 'editArticle/edit_article.html' in template_names - - # Test article before edit - assert article in set(Article.objects.all()) - assert article.content != NEW_CONTENT + # test the article before edit assert article.title != NEW_TITLE - assert article.subject_id != NEW_SUBJECT - # Test article after edit + # test article editing + data = {"title": NEW_TITLE, "subject_id": NEW_SUBJECT, "content": NEW_CONTENT} + response = client.post(f"/edit_article/{article.id}/", data=data) + assert response.status_code == 302 + assert response.url == f"/my_articles/{article.user_id.nickname}/" + + edited_article = Article.objects.get(pk=article.id) + assert edited_article.title == NEW_TITLE + assert edited_article.subject_id.id == NEW_SUBJECT + assert edited_article.content == NEW_CONTENT @pytest.mark.django_db diff --git a/projboard/views.py b/projboard/views.py index 39cd816..f024988 100644 --- a/projboard/views.py +++ b/projboard/views.py @@ -135,12 +135,15 @@ def edit_article(request, article_pk=None): try: article = Article.objects.get(id=article_pk) user_id = article.user_id + title = article.title subject_id = article.subject_id content = article.content - initial = {'user_id': user_id, 'title': title, 'subject_id': subject_id, 'content': content} + + initial = {'title': title, 'subject_id': subject_id, 'content': content} + if request.method == "POST": - form = EditArticleForm(request.POST, initial) + form = EditArticleForm(request.POST) if form.is_valid(): article.edit(form.data['title'], form.data['content'], form.cleaned_data['subject_id']) return redirect(f"/my_articles/{user_id.nickname}/") @@ -150,8 +153,7 @@ def edit_article(request, article_pk=None): form = EditArticleForm(initial=initial) return render(request, 'editArticle/edit_article.html', { - 'user_id': user_id, - 'form': form, + 'form': form }) except Article.DoesNotExist: raise Http404()