Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smark-1 committed Dec 24, 2023
1 parent 99acd27 commit c7c927b
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions faq/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.test import TestCase, RequestFactory, override_settings
from django.shortcuts import reverse
from .views import IndexView, CategoryDetail, QuestionDetail
from . import models
from django.contrib.auth.models import User
from faq.views import IndexView, CategoryDetail, QuestionDetail
from faq import models
from django.contrib.auth.models import User, AnonymousUser


# Create your tests here.
Expand All @@ -11,27 +11,28 @@ class IndexViewTestCase(TestCase):

@override_settings(FAQ_SETTINGS=["no_category"])
def test_get_template_names_no_categories(self):
"gets correct template when not using categories"
"""gets correct template when not using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)

self.assertEquals(view.get_template_names(), "faq/questions_list.html")
self.assertNotEquals(view.get_template_names(), "faq/categories_list.html")
self.assertEqual(view.get_template_names(), "faq/questions_list.html")
self.assertNotEqual(view.get_template_names(), "faq/categories_list.html")

@override_settings(FAQ_SETTINGS=[])
def test_get_template_names_categories(self):
"gets correct template when not using categories"
"""gets correct template when not using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)

self.assertNotEquals(view.get_template_names(), "faq/questions_list.html")
self.assertEquals(view.get_template_names(), "faq/categories_list.html")
self.assertNotEqual(view.get_template_names(), "faq/questions_list.html")
self.assertEqual(view.get_template_names(), "faq/categories_list.html")


@override_settings(FAQ_SETTINGS=["no_category"])
def test_get_queryset_no_categories(self):
"gets correct query set when not using categories"
"""gets correct query set when not using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)
Expand All @@ -46,13 +47,13 @@ def test_get_queryset_no_categories(self):

@override_settings(FAQ_SETTINGS=[])
def test_get_queryset_categories(self):
"gets correct query set when using categories"
"""gets correct query set when using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)

category = models.Category.objects.create(name="category", description="this is a category")
models.Category.objects.create(name="category 2", description="this is a category")
category = models.Category.objects.create(name="category", _description="this is a category")
models.Category.objects.create(name="category 2", _description="this is a category")
models.Question.objects.create(question="category question", category=category)
models.Question.objects.create(question="category question 2", category=category)
models.Question.objects.create(question="question not in category")
Expand All @@ -63,7 +64,7 @@ def test_get_queryset_categories(self):

@override_settings(FAQ_SETTINGS=[])
def test_get_context_object_name_categories(self):
"gets correct template variable when using categories"
"""gets correct template variable when using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)
Expand All @@ -73,7 +74,7 @@ def test_get_context_object_name_categories(self):

@override_settings(FAQ_SETTINGS=["no_category"])
def test_get_context_object_name_no_categories(self):
"gets correct template variable when not using categories"
"""gets correct template variable when not using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
view = IndexView()
view.setup(request)
Expand All @@ -83,8 +84,9 @@ def test_get_context_object_name_no_categories(self):

@override_settings(FAQ_SETTINGS=["no_category"])
def test_get_context_data_not_using_categories(self):
"gets context data correctly when not using categories"
"""gets context data correctly when not using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
request.user = AnonymousUser()
view = IndexView()
view.object_list = view.get_queryset()
view.setup(request)
Expand All @@ -93,17 +95,18 @@ def test_get_context_data_not_using_categories(self):

@override_settings(FAQ_SETTINGS=[])
def test_get_context_data_using_categories(self):
"gets context data correctly when using categories"
"""gets context data correctly when using categories"""
request = RequestFactory().get(reverse("faq:index_view"))
request.user = AnonymousUser()
view = IndexView()
view.object_list = view.get_queryset()
view.setup(request)

self.assertNotIn("can_add_question", view.get_context_data())
self.assertEqual(view.get_context_data()['can_add_question'],False)

@override_settings(FAQ_SETTINGS=["no_category", "logged_in_users_can_add_question"])
def test_get_context_data_not_using_categories_logged_in_can_add(self):
"gets context data correctly when not using categories and logged_in_users_can_add_question"
"""gets context data correctly when not using categories and logged_in_users_can_add_question"""
request = RequestFactory()
request = request.get(reverse("faq:index_view"))
request.user = User.objects.create_user(username="jim", password="the")
Expand All @@ -116,14 +119,14 @@ def test_get_context_data_not_using_categories_logged_in_can_add(self):

class CategoryDetailTestCase(TestCase):
def setUp(self):
models.Category.objects.create(name="cat1", description="descript")
models.Category.objects.create(name="cat2", description="descript2")
models.Category.objects.create(name="cat1", _description="descript")
models.Category.objects.create(name="cat2", _description="descript2")
models.Category.objects.create(name="cat3")


class QuestionViewTestCase(TestCase):
def setUp(self):
category = models.Category.objects.create(name="cat1", description="descript")
category = models.Category.objects.create(name="cat1", _description="descript")

models.Question.objects.create(category=category, question="great question")

Expand All @@ -140,7 +143,7 @@ def test_anonymous_user_cant_vote(self):

class VoteQuestionTestCase(TestCase):
def setUp(self):
category = models.Category.objects.create(name="cat1", description="descript")
category = models.Category.objects.create(name="cat1", _description="descript")

models.Question.objects.create(category=category, question="great question")

Expand All @@ -156,7 +159,7 @@ def test_anonymous_user_cant_vote(self):

class VoteanswerTestCase(TestCase):
def setUp(self):
category = models.Category.objects.create(name="cat1", description="descript")
category = models.Category.objects.create(name="cat1", _description="descript")

question = models.Question.objects.create(category=category, question="great question")

Expand All @@ -168,6 +171,6 @@ def test_anonymous_user_cant_vote(self):

question = models.Question.objects.first()
response = self.client.post(
reverse("faq:vote_answer", args=(question.category.slug, question.slug, self.answer.slug)))
reverse("faq:vote_answer", args=(question.category.slug, question.slug, self.answer.slug)))

self.assertEqual(response.status_code, 302)

0 comments on commit c7c927b

Please sign in to comment.