Skip to content

Commit

Permalink
Merge pull request #131 from bounswe/practice-app/testing/comment-#122
Browse files Browse the repository at this point in the history
Unit Tests for the Comment Endpoint
  • Loading branch information
burakmert236 authored May 18, 2022
2 parents 679716f + a497908 commit 0c79b02
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 4 deletions.
3 changes: 0 additions & 3 deletions practice-app/comment/tests.py

This file was deleted.

Empty file.
66 changes: 66 additions & 0 deletions practice-app/comment/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.test import TestCase
from comment.models import Comment
from category.models import Category
from post.models import Post
from django.contrib.auth.models import User
from comment.forms import *

class TestForms(TestCase):

def setUp(self):
category = Category.objects.create(
name = 'name',
definition = 'definition'
)

user = User.objects.create_user(username='halil',email = "email", password="12345")
user.save()

post = Post.objects.create(
title = 'title_post_data',
body = 'body',
category = category,
user = user,
country = 'turkey',
covid19cases = {
'death': 500,
'case' : 1000,
},
)

comment1 = Comment.objects.create(
body = 'comment1body',
user = user,
post = post,
)

comment2 = Comment.objects.create(
body = 'comment2body',
user = user,
post = post,
)

def test_get_form_valid_data(self):
getForm = commentGetForm(data={
'post_id' : 1
})
self.assertTrue(getForm.is_valid())

def test_get_form_no_data(self):
getForm = commentGetForm(data={})
self.assertFalse(getForm.is_valid())
self.assertEquals(len(getForm.errors), 1)

def test_post_form_valid_data(self):
postForm = commentPostForm(data={
'body' : 'new comment',
'post_id' : 1,
'city_name' : 'istanbul'
})

self.assertTrue(postForm.is_valid())

def test_post_form_no_data(self):
postForm = commentPostForm(data={})
self.assertFalse(postForm.is_valid())
self.assertEquals(len(postForm.errors), 3)
39 changes: 39 additions & 0 deletions practice-app/comment/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.test import TestCase
from comment.models import Comment
from category.models import Category
from post.models import Post
from django.contrib.auth.models import User

class TestModel(TestCase):
def setUp(self):
category = Category.objects.create(
name = 'name',
definition = 'definition'
)

user = User.objects.create_user(username='halil',email = "email", password="12345")
user.save()

post = Post.objects.create(
title = 'title_post_data',
body = 'body',
category = category,
user = user,
country = 'turkey',
covid19cases = {
'death': 500,
'case' : 1000,
},
)

comment_data = {
'body' : 'body_of_the_comment',
'user' : user,
'post' : post,
'city_name' : 'istanbul'
}

self._comment = Comment.objects.create(**comment_data)

def test_str(self):
self.assertEquals(self._comment.__str__(), 'body_of_the_comment')
23 changes: 23 additions & 0 deletions practice-app/comment/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.test import SimpleTestCase
from django.urls import resolve,reverse
from comment.views import CommentApiView, getComments, postComment, index

class TestUrls(SimpleTestCase):

def test_api_url_resolves(self):
url = reverse('commentApi', args=[1])
self.assertEquals(resolve(url).func.view_class, CommentApiView)

def test_get_url_resolves(self):
url = reverse('getComments')
self.assertEquals(resolve(url).func, getComments)

def test_post_url_resolves(self):
url = reverse('postComment')
self.assertEquals(resolve(url).func, postComment)

def test_index_url_resolves(self):
url = reverse('commentIndex')
self.assertEquals(resolve(url).func, index)


134 changes: 134 additions & 0 deletions practice-app/comment/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from django.test import TestCase, Client
from django.urls import reverse
from comment.models import Comment
from category.models import Category
from post.models import Post
from django.contrib.auth.models import User

class TestViews(TestCase):
def setUp(self):
self.client = Client()
self.api_url = reverse('commentApi', args=[1]) # api/<int:post_id>/
self.get_url = reverse('getComments') # 'list/'
self.post_url = reverse('postComment') # 'create/'
self.index_url = reverse('commentIndex') # ''

category = Category.objects.create(
name = 'name',
definition = 'definition'
)

user1 = User.objects.create_user(username='halil',email = "email", password="12345")
user2 = User.objects.create_user(username='burak',email = "email", password="12345")

user1.save()
user2.save()

post = Post.objects.create(
title = 'title_post_data',
body = 'body',
category = category,
user = user1,
country = 'turkey',
covid19cases = {
'death': 500,
'case' : 1000,
},
)

comment_data = {
'body' : 'body_of_the_1st_comment',
'user' : user2,
'post' : post,
'city_name' : 'istanbul'
}

self.create_comment_data = {
'body': 'comment create body',
'post' : post,
'city_name' : 'istanbul'
}

self.comment_with_wrong_post = {
'body': 'comment create body',
'post' : post,
'city_name' : 'istanbul'
}

self._comment = Comment.objects.create(**comment_data)

# API tests:
def test_api_GET(self):
# Performing a proper GET:
response = self.client.get(self.api_url, {'post_id':1})
self.assertEquals(response.status_code, 200)
self.assertEquals(response.data[0]['body'],'body_of_the_1st_comment')

def test_api_POST_create_comment(self):
# Performing a proper POST to create a new comment:
self.client.login(username='burak', password='12345')
response = self.client.post(self.api_url, self.create_comment_data)
self.assertEquals(response.status_code, 201)
self.assertEquals(response.data['body'],'comment create body')

def test_api_POST_not_authenticated(self):
# Non authenticated users cannot perform POST:
response = self.client.post(self.api_url, self.create_comment_data)
self.assertEquals(response.status_code, 403)

def test_api_POST_missing_input(self):
# There should be input:
self.client.login(username='burak', password='12345')
response = self.client.post(self.api_url)
self.assertEquals(response.status_code, 400)
self.assertEquals(response.data['error'],'Missing input')

def test_api_POST_no_post(self):
# There cannot be a comment for a nonexisting post:
self.client.login(username='burak', password='12345')
response = self.client.post(reverse('commentApi', args=[2]), {
'body' : 'comment for a nonexisting post',
'city_name' : 'ankara'
})
self.assertEquals(response.status_code, 404)
self.assertEquals(response.data['error'],'There is no post with id 2')

def test_api_POST_no_body(self):
# There cannot be a comment without a body:
self.client.login(username='burak', password='12345')
response = self.client.post(self.api_url, {
'city_name' : 'ankara'
})
self.assertEquals(response.status_code, 400)
self.assertEquals(response.data['error'],'Missing body of the comment')

def test_api_POST_empty_body(self):
# There cannot be a comment with an empty body:
self.client.login(username='burak', password='12345')
response = self.client.post(self.api_url, {
'body' : '',
'city_name' : 'ankara'
})
self.assertEquals(response.status_code, 400)
self.assertEquals(response.data['error'],'Body of the comment cannot be empty')

# Frontend tests:
def test_index_GET(self):
response= self.client.get(self.index_url)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'comment.html')

def test_get_comments_GET(self):
response = self.client.get(self.get_url, {'post_id' : 1})
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'listComments.html')

def test_post_coment_POST(self):
self.client.login(username='burak', password='12345')
response = self.client.post(self.post_url, {
'body' : 'a comment for post',
'post_id' : 1,
'city_name' : 'ankara'
})
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'comment.html')
10 changes: 9 additions & 1 deletion practice-app/comment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ def post(self, request, post_id, *args, **kwargs):
return Response(
{'error':'Missing input'},
status=status.HTTP_400_BAD_REQUEST)
if request.data['body'] == '':

try:
request.data['body']
except:
return Response(
{'error':'Missing body of the comment'},
status=status.HTTP_400_BAD_REQUEST)

if request.data['body'] == '':
return Response(
{'error':'Body of the comment cannot be empty'},
status=status.HTTP_400_BAD_REQUEST)

city_name = request.data.get('city_name')
url = "https://weatherapi-com.p.rapidapi.com/current.json"
Expand Down

0 comments on commit 0c79b02

Please sign in to comment.