Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for breadcrumbs #1549

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def document_url(doc):
return reverse("document-index", host="docs", kwargs=kwargs)


class DocumentManager(models.Manager):
class DocumentQuerySet(models.QuerySet):
def breadcrumbs(self, document):
# get an ascending list of parent paths except the root path ('.')
parent_paths = list(Path(document.path).parents)[:-1]
Expand All @@ -252,8 +252,7 @@ def search(self, query_text, release):
search_rank = SearchRank(models.F("search"), search_query)
similarity = TrigramSimilarity("title", query_text)
return (
self.get_queryset()
.prefetch_related(
self.prefetch_related(
Prefetch(
"release",
queryset=DocumentRelease.objects.only("lang", "release"),
Expand Down Expand Up @@ -289,7 +288,7 @@ def search(self, query_text, release):
)
)
else:
return self.get_queryset().none()
return self.none()

def search_reset(self):
"""Set to null all not null Document's search vector fields."""
Expand Down Expand Up @@ -324,7 +323,7 @@ class Document(models.Model):
search = SearchVectorField(null=True, editable=False)
config = models.SlugField(default=DEFAULT_TEXT_SEARCH_CONFIG)

objects = DocumentManager()
objects = DocumentQuerySet.as_manager()

class Meta:
indexes = [
Expand Down
72 changes: 47 additions & 25 deletions docs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ def setUpTestData(cls):
'<p>See <a class="reference internal" href="../../../ref/class-based-views/">'
'<span class="doc">Built-in class-based views API</span></a>.</p>\n</div>\n'
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "topics", "title": "Using Django"},
{"path": "topics/http", "title": "Handling HTTP requests"},
],
"parents": "topics http",
"slug": "generic-views",
"title": "Generic views",
Expand All @@ -436,7 +439,9 @@ def setUpTestData(cls):
'<a class="reference external" href="https://code.djangoproject.com/ticket/13560">bug</a> that\n'
"affected datetime form field widgets when localization was enabled.</p>\n</div>\n"
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "releases", "title": "Release notes"},
],
"parents": "releases",
"slug": "1.2.1",
"title": "Django 1.2.1 release notes",
Expand All @@ -455,7 +460,9 @@ def setUpTestData(cls):
'where <code class="docutils literal"><span class="pre">utils.http.is_safe_url()</span></code> crashes on bytestring URLs '
'(<a class="reference external" href="https://code.djangoproject.com/ticket/26308">#26308</a>).</p>\n</div>\n'
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "releases", "title": "Release notes"},
],
"parents": "releases",
"slug": "1.9.4",
"title": "Django 1.9.4 release notes",
Expand All @@ -473,7 +480,10 @@ def setUpTestData(cls):
'<p>Voir <a class="reference internal" href="../../../ref/class-based-views/">'
'<span class="doc">API des vues intégrées fondées sur les classes.</span></a>.</p>\n</div>\n'
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "topics", "title": "Using Django"},
{"path": "topics/http", "title": "Handling HTTP requests"},
],
"parents": "topics http",
"slug": "generic-views",
"title": "Vues génériques",
Expand All @@ -494,7 +504,9 @@ def setUpTestData(cls):
'<a class="reference external" href="https://code.djangoproject.com/ticket/13560">bug</a> that\n'
"affected datetime form field widgets when localization was enabled.</p>\n</div>\n"
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "releases", "title": "Release notes"},
],
"parents": "releases",
"slug": "1.2.1",
"title": "Notes de publication de Django 1.2.1",
Expand All @@ -514,7 +526,9 @@ def setUpTestData(cls):
'where <code class="docutils literal"><span class="pre">utils.http.is_safe_url()</span></code> crashes on bytestring URLs '
'(<a class="reference external" href="https://code.djangoproject.com/ticket/26308">#26308</a>).</p>\n</div>\n'
),
"breadcrumbs": [],
"breadcrumbs": [
{"path": "releases", "title": "Release notes"},
],
"parents": "releases",
"slug": "1.9.4",
"title": "Notes de publication de Django 1.9.4",
Expand All @@ -531,12 +545,6 @@ def setUp(self):
Document.objects.search_update()

def test_search(self):
query_text = "django"
document_list = list(
Document.objects.search(query_text, self.release).values_list(
"rank", "path", "headline", "highlight"
)
)
expected_list = [
(
0.96982837,
Expand All @@ -558,32 +566,46 @@ def test_search(self):
),
),
]
self.assertSequenceEqual(document_list, expected_list)
self.assertQuerySetEqual(
Document.objects.search("django", self.release),
expected_list,
transform=attrgetter("rank", "path", "headline", "highlight"),
)

def test_websearch(self):
query_text = 'django "release notes" -packaging'
document_queryset = Document.objects.search(
query_text, self.release
).values_list("title", "rank")
document_list = [("Django 1.9.4 release notes", 1.5675676)]
self.assertSequenceEqual(list(document_queryset), document_list)
self.assertQuerySetEqual(
Document.objects.search('django "release notes" -packaging', self.release),
[("Django 1.9.4 release notes", 1.5675676)],
transform=attrgetter("title", "rank"),
)

def test_multilingual_search(self):
query_text = "publication"
queryset = Document.objects.search(query_text, self.release_fr).values_list(
"title", "rank"
)
self.assertSequenceEqual(
queryset,
self.assertQuerySetEqual(
Document.objects.search("publication", self.release_fr),
[
("Notes de publication de Django 1.2.1", 1.0693262),
("Notes de publication de Django 1.9.4", 1.0458658),
],
transform=attrgetter("title", "rank"),
)

def test_empty_search(self):
self.assertSequenceEqual(Document.objects.search("", self.release), [])

def test_search_breadcrumbs(self):
doc = (
Document.objects.filter(title="Generic views")
.search("generic", self.release)
.get()
)
self.assertEqual(
doc.breadcrumbs,
[
{"path": "topics", "title": "Using Django"},
{"path": "topics/http", "title": "Handling HTTP requests"},
],
)

def test_search_reset(self):
self.assertEqual(Document.objects.exclude(search=None).count(), 6)
self.assertEqual(Document.objects.search_reset(), 6)
Expand Down
Loading