Skip to content

Commit

Permalink
[#1709] Fixed and simplified broken tests for search
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van der Schoor committed Sep 12, 2023
1 parent ac449e8 commit 6317218
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
94 changes: 53 additions & 41 deletions src/open_inwoner/search/tests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,168 +1,180 @@
import urllib

from django.contrib import messages
from django.test import override_settings
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

from django_webtest import WebTest
from furl import furl

from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.pdc.tests.factories import CategoryFactory, TagFactory
from open_inwoner.pdc.tests.factories import CategoryFactory, ProductFactory, TagFactory

from ..models import Feedback
from .factories import FeedbackFactory
from .utils import ESMixin


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class TestFeedbackFunctionality(ESMixin, WebTest):
def setUp(self):
self.user = UserFactory()
self.feedback = FeedbackFactory.build()
self.category1 = CategoryFactory.build()
self.category2 = CategoryFactory.build()
self.tag1 = TagFactory.build()
self.tag2 = TagFactory.build()
self.product1 = ProductFactory.create(
name="Name",
summary="Some summary",
content="Some content",
keywords=["keyword1", "keyword2"],
)
self.update_index()

def test_positive_feedback_is_saved_with_authenticated_user_and_without_filters(
self,
):
self.app.set_user(user=self.user)
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

self.assertNotEquals(response.context["paginator"].count, 0)
feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_negative_feedback_is_saved_with_authenticated_user_and_without_filters(
self,
):
self.app.set_user(user=self.user)
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "false"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertFalse(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_positive_feedback_is_saved_with_unauthenticated_user_and_without_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_negative_feedback_is_saved_with_unauthenticated_user_and_without_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "false"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertFalse(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_positive_feedback_is_saved_with_authenticated_user_and_with_filters(self):
category1 = CategoryFactory()
category2 = CategoryFactory()
tag1 = TagFactory()
tag2 = TagFactory()
self.product1.tags.add(tag1, tag2)
self.product1.categories.add(category1, category2)

self.update_index()

self.app.set_user(user=self.user)
params = {
"query": [self.feedback.search_query],
"categories": [self.category1.name, self.category2.name],
"tags": [self.tag1.name, self.tag2.name],
"query": ["keyword1"],
"categories": [category1.slug, category2.slug],
"tags": [tag1.slug, tag2.slug],
}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(
feedback.search_query,
f"query: {self.feedback.search_query} | categories: {self.category1.name}, {self.category2.name} | tags: {self.tag1.name}, {self.tag2.name}",
f"query: keyword1 | categories: {category1.slug}, {category2.slug} | tags: {tag1.slug}, {tag2.slug}",
)
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_positive_feedback_is_saved_with_unauthenticated_user_and_with_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_feedback_form_not_displayed_after_submit(self):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = furl(reverse("search:search")).add(params).url
response = self.app.get(url)

self.assertIsNotNone(response.html.find(id="feedback_form"))
self.assertEqual(list(response.context["messages"]), [])

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"

post_response = feedback_form.submit().follow()
Expand Down
8 changes: 4 additions & 4 deletions src/open_inwoner/search/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_not_show_results_and_filter_without_search(self):
# check that facet fields are not shown
self.assertNotIn(facet, search_form.fields)

results_div = response.html.find("div", {"class": "search-results"})
results_div = response.html.find("div", {"class": "search-results__list"})
self.assertIsNone(results_div) # check that results are not shown

def test_show_results_and_filter_with_search(self):
Expand All @@ -74,7 +74,7 @@ def test_show_results_and_filter_with_search(self):
results_div = response.html.find("div", {"class": "search-results"})
self.assertIsNotNone(results_div) # check that results are shown

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 2)

def test_search_with_filter(self):
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_search_with_filter(self):
results_div = response.html.find("div", {"class": "search-results"})
self.assertIsNotNone(results_div) # check that results are shown

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 1)
self.assertEqual(results[0].slug, self.product1.slug)

Expand All @@ -117,7 +117,7 @@ def test_pagination_links(self):

self.assertEqual(response.status_code, 200)

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 21)

pagination_div = response.html.find("div", {"class": "pagination"})
Expand Down

0 comments on commit 6317218

Please sign in to comment.