Skip to content

Commit 4fd3262

Browse files
committed
Add proper tests for new functionality.
1 parent a3dc114 commit 4fd3262

File tree

5 files changed

+117
-26
lines changed

5 files changed

+117
-26
lines changed

blog/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ def _md_slugify(value, separator):
3131

3232

3333
class EntryQuerySet(models.QuerySet):
34-
def published(self):
35-
return self.active().filter(pub_date__lte=timezone.now())
34+
def published(self, pub_date=None):
35+
if pub_date is None:
36+
pub_date = timezone.now()
37+
return self.active().filter(pub_date__lte=pub_date)
3638

3739
def active(self):
3840
return self.filter(is_active=True)

blog/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def test_manager_published(self):
6464
["past active"],
6565
transform=lambda entry: entry.headline,
6666
)
67+
self.assertQuerySetEqual(
68+
Entry.objects.published(self.tomorrow),
69+
["future active", "past active"],
70+
transform=lambda entry: entry.headline,
71+
)
6772

6873
def test_manager_searchable(self):
6974
"""

docs/models.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def _sync_views_to_db(self):
271271
path = reverse_path(url_name, kwargs=kwargs)
272272
request = RequestFactory().get(path, HTTP_HOST=www_host)
273273
body = resolve(path).func(request).render().text
274+
# Need to parse the body element.
274275
Document.objects.create(
275276
release=self,
276277
path=absolute_url,
@@ -302,7 +303,9 @@ def _clean_document_path(path):
302303

303304

304305
def document_url(doc):
305-
if doc.path:
306+
if doc.metadata["parents"] == "weblog":
307+
return doc.path
308+
elif doc.path:
306309
kwargs = {
307310
"lang": doc.release.lang,
308311
"version": doc.release.version,
@@ -460,16 +463,3 @@ def body(self):
460463
with open(str(self.full_path)) as fp:
461464
doc = json.load(fp)
462465
return doc["body"]
463-
464-
def document_url(self):
465-
if self.metadata["parents"] == "weblog":
466-
return self.path
467-
return reverse(
468-
"document-detail",
469-
kwargs={
470-
"lang": self.release.lang,
471-
"version": self.release.version,
472-
"url": self.path,
473-
},
474-
host="docs",
475-
)

docs/templates/docs/search_results.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ <h2>{% translate "No search query given" %}</h2>
4343
{% for result in page.object_list %}
4444
<dt>
4545
<h2 class="result-title">
46-
<a href="{{ result.document_url }}{% if not start_sel in result.headline %}{{ result.highlight|fragment }}{% endif %}">{{ result.headline|safe }}</a>
46+
<a href="{{ result.get_absolute_url }}{% if not start_sel in result.headline %}{{ result.highlight|fragment }}{% endif %}">{{ result.headline|safe }}</a>
4747
</h2>
4848
<span class="meta breadcrumbs">
4949
{% for breadcrumb in result.breadcrumbs %}
50-
<a href="{{ result.document_url }}">{{ breadcrumb.title }}</a>{% if not forloop.last %} <span class="arrow">»</span>{% endif %}
50+
<a href="{{ result.get_absolute_url }}">{{ breadcrumb.title }}</a>{% if not forloop.last %} <span class="arrow">»</span>{% endif %}
5151
{% endfor %}
5252
</span>
5353
</dt>
@@ -60,7 +60,7 @@ <h2 class="result-title">
6060
<ul class="code-links">
6161
{% for name, value in result_code_links.items %}
6262
<li>
63-
<a href="{{ result.document_url }}#{{ value.full_path }}">
63+
<a href="{{ result.get_absolute_url }}#{{ value.full_path }}">
6464
<div>
6565
<code>{{ name }}</code>
6666
{% if value.module_path %}<div class="meta">{{ value.module_path }}</div>{% endif %}

docs/tests/test_models.py

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from django.conf import settings
55
from django.db import connection
66
from django.test import TestCase
7+
from django.utils import timezone
8+
from django_hosts import reverse
79

10+
from blog.models import Entry
811
from releases.models import Release
912

1013
from ..models import DOCUMENT_SEARCH_VECTOR, Document, DocumentRelease
@@ -465,7 +468,19 @@ def test_search_title(self):
465468
class UpdateDocTests(TestCase):
466469
@classmethod
467470
def setUpTestData(cls):
468-
cls.release = DocumentRelease.objects.create()
471+
now = timezone.now()
472+
cls.release = DocumentRelease.objects.create(
473+
support_end=now + datetime.timedelta(days=1)
474+
)
475+
cls.entry = Entry.objects.create(
476+
pub_date=now,
477+
is_active=True,
478+
is_searchable=True,
479+
headline="Searchable post",
480+
slug="a",
481+
body_html="<h1>Searchable Blog Post</h1>",
482+
)
483+
cls.docs_documents = cls.release.documents.exclude(metadata__parents="weblog")
469484

470485
def test_sync_to_db(self):
471486
self.release.sync_to_db(
@@ -477,8 +492,43 @@ def test_sync_to_db(self):
477492
}
478493
]
479494
)
480-
document = self.release.documents.get()
481-
self.assertEqual(document.path, "foo/bar")
495+
document_paths = set(self.release.documents.values_list("path", flat=True))
496+
self.assertEqual(
497+
document_paths,
498+
{
499+
"foo/bar",
500+
reverse("community-ecosystem", host="www"),
501+
self.entry.get_absolute_url(),
502+
},
503+
)
504+
505+
def test_blog_to_db_skip_non_english(self):
506+
"""
507+
Releases must be English to include the blog and website results in search.
508+
"""
509+
non_english = DocumentRelease.objects.create(
510+
lang="es",
511+
release=Release.objects.create(version="88.0"),
512+
support_end=self.release.support_end,
513+
)
514+
non_english.sync_to_db([])
515+
self.assertFalse(non_english.documents.exists())
516+
517+
def test_blog_to_db_skip_no_end_support(self):
518+
"""
519+
Releases must have an end support to include the blog.
520+
"""
521+
no_end_support = DocumentRelease.objects.create(
522+
lang="en",
523+
release=Release.objects.create(version="99.0"),
524+
)
525+
no_end_support.sync_to_db([])
526+
self.assertEqual(
527+
set(no_end_support.documents.values_list("path", flat=True)),
528+
{
529+
reverse("community-ecosystem", host="www"),
530+
},
531+
)
482532

483533
def test_clean_path(self):
484534
self.release.sync_to_db(
@@ -490,7 +540,7 @@ def test_clean_path(self):
490540
}
491541
]
492542
)
493-
document = self.release.documents.get()
543+
document = self.docs_documents.get()
494544
self.assertEqual(document.path, "foo/bar")
495545

496546
def test_title_strip_tags(self):
@@ -504,7 +554,7 @@ def test_title_strip_tags(self):
504554
]
505555
)
506556
self.assertQuerySetEqual(
507-
self.release.documents.all(),
557+
self.docs_documents.all(),
508558
["This is the title"],
509559
transform=attrgetter("title"),
510560
)
@@ -520,7 +570,7 @@ def test_title_entities(self):
520570
]
521571
)
522572
self.assertQuerySetEqual(
523-
self.release.documents.all(),
573+
self.docs_documents,
524574
["Title & title"],
525575
transform=attrgetter("title"),
526576
)
@@ -533,7 +583,7 @@ def test_empty_documents(self):
533583
{"current_page_name": "foo/3"},
534584
]
535585
)
536-
self.assertQuerySetEqual(self.release.documents.all(), [])
586+
self.assertQuerySetEqual(self.docs_documents, [])
537587

538588
def test_excluded_documents(self):
539589
"""
@@ -562,3 +612,47 @@ def test_excluded_documents(self):
562612
)
563613
document = release.documents.get()
564614
self.assertEqual(document.path, "nonexcluded/bar")
615+
616+
617+
class DocumentUrlTests(TestCase):
618+
@classmethod
619+
def setUpTestData(cls):
620+
cls.release = DocumentRelease.objects.create(
621+
release=Release.objects.create(version="1.2.3"),
622+
)
623+
documents = [
624+
{
625+
"metadata": {"parents": "topics http"},
626+
"path": "topics/http/generic-views",
627+
"release": cls.release,
628+
"title": "Generic views",
629+
},
630+
# I'm not sure if this is valid or not.
631+
{
632+
"metadata": {"parents": "topics"},
633+
"path": "",
634+
"release": cls.release,
635+
"title": "Index",
636+
},
637+
]
638+
# Include the static views in the document search
639+
cls.release._sync_views_to_db()
640+
Document.objects.bulk_create(Document(**doc) for doc in documents)
641+
cls.document_index, cls.document_view, cls.document_detail = (
642+
cls.release.documents.order_by("path")
643+
)
644+
645+
def test_document_url(self):
646+
self.assertEqual(
647+
self.document_index.get_absolute_url(),
648+
"http://docs.djangoproject.localhost:8000/en/1.2.3/",
649+
)
650+
self.assertEqual(
651+
self.document_view.get_absolute_url(),
652+
"http://www.djangoproject.localhost:8000/community/ecosystem/",
653+
)
654+
self.assertEqual(
655+
self.document_detail.get_absolute_url(),
656+
"http://docs.djangoproject.localhost:8000"
657+
"/en/1.2.3/topics/http/generic-views/",
658+
)

0 commit comments

Comments
 (0)