Skip to content

Commit

Permalink
Merge 977ce07 into f89b9ca
Browse files Browse the repository at this point in the history
  • Loading branch information
theresatvan committed Apr 29, 2024
2 parents f89b9ca + 977ce07 commit 07b197b
Showing 1 changed file with 77 additions and 18 deletions.
95 changes: 77 additions & 18 deletions Community/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase, Client, RequestFactory
from TutorRegister.models import Post, Reply
from TutorRegister.models import Post, Reply, Vote
from django.contrib.auth.models import User
from django.urls import reverse
from django.core.cache import cache
Expand All @@ -8,6 +8,7 @@
class ViewAllPostsTest(TestCase):
def setUp(self):
self.client = Client()
# self.factory = RequestFactory()
self.tutor = User.objects.get(pk=cache.get("tutor"))
self.student = User.objects.get(pk=cache.get("student"))
self.client.login(username="test@example.com", password="testpassword")
Expand All @@ -16,6 +17,7 @@ def setUp(self):
label="resource",
title="Test title",
content="Test content",
topics="math",
)

def test_view_all_posts(self):
Expand All @@ -26,23 +28,21 @@ def test_view_all_posts(self):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts.html")

# def test_create_reply(self):
# url = reverse("Community:post_detail")
# reply_data = {
# "content": "Test reply.",
# "post_id": self.post.id,
# }
# response = self.client.post(url, reply_data, follow=True)

# self.assertRedirects(
# response,
# url,
# status_code=302,
# target_status_code=200,
# )
# self.assertTrue(
# Reply.objects.filter(post=self.post, user=self.student).exists()
# )
def test_search_posts(self):
url = reverse("Community:all_posts")
response = self.client.get(url, {"search": "content"})

self.assertEqual(len(response.context["posts"]), 1)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts.html")

def test_filter_posts(self):
url = reverse("Community:all_posts")
response = self.client.get(url, {"label": "resource", "topic": "computer_sci"})

self.assertEqual(len(response.context["posts"]), 0)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts.html")


class ViewPostDetailTest(TestCase):
Expand Down Expand Up @@ -108,3 +108,62 @@ def test_create_post_submit(self):
target_status_code=200,
)
self.assertTrue(Post.objects.filter(user=self.student).exists())


class EditDeleteVotePostViewTest(TestCase):
def setUp(self):
self.client = Client()
self.student = User.objects.get(pk=cache.get("student"))
self.client.login(username="test@example.com", password="testpassword")
self.post = Post.objects.create(
user=self.student,
label="resource",
title="Test title",
content="Test content",
topics="math",
)

def test_edit_post(self):
url = reverse("Community:edit", kwargs={"post_id": self.post.id})
redirect_url = reverse(
"Community:post_detail", kwargs={"post_id": self.post.id}
)

post_form = {
"label": "question",
"title": "Test title",
"content": "Test question content",
"topics": "computer_sci",
}

response = self.client.post(url, post_form, follow=True)
self.post.refresh_from_db()

self.assertRedirects(
response,
redirect_url,
status_code=302,
target_status_code=200,
)
self.assertEqual(self.post.content, "Test question content")

def test_delete_post(self):
url = reverse("Community:delete_post", kwargs={"post_id": self.post.id})
response = self.client.post(url, follow=True)

self.assertTrue(response.json()["success"])
self.assertFalse(Post.objects.filter(id=self.post.id).exists())

def test_vote_post(self):
url = reverse(
"Community:vote", kwargs={"post_id": self.post.id, "vote_type": "downvote"}
)
response = self.client.get(url)

self.post.refresh_from_db()

self.assertTrue(Vote.objects.filter(post=self.post, user=self.student).exists())
self.assertEqual(self.post.get_rating(), -1)
self.assertEqual(
response.json(), {"upvotes_count": 0, "downvotes_count": 1, "user_vote": -1}
)

0 comments on commit 07b197b

Please sign in to comment.