Skip to content

Commit

Permalink
Adding tests for delete_article and edit_article
Browse files Browse the repository at this point in the history
  • Loading branch information
finziamit committed Jan 18, 2023
1 parent 8529c0d commit a3e771c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
5 changes: 1 addition & 4 deletions projboard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
29 changes: 13 additions & 16 deletions projboard/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions projboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}/")
Expand All @@ -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()

0 comments on commit a3e771c

Please sign in to comment.