4
4
from django .conf import settings
5
5
from django .db import connection
6
6
from django .test import TestCase
7
+ from django .utils import timezone
8
+ from django_hosts import reverse
7
9
10
+ from blog .models import Entry
8
11
from releases .models import Release
9
12
10
13
from ..models import DOCUMENT_SEARCH_VECTOR , Document , DocumentRelease
14
+ from ..search import DocumentationCategory
11
15
12
16
13
17
class ModelsTests (TestCase ):
@@ -465,7 +469,21 @@ def test_search_title(self):
465
469
class UpdateDocTests (TestCase ):
466
470
@classmethod
467
471
def setUpTestData (cls ):
468
- cls .release = DocumentRelease .objects .create ()
472
+ now = timezone .now ()
473
+ cls .release = DocumentRelease .objects .create (
474
+ support_end = now + datetime .timedelta (days = 1 )
475
+ )
476
+ cls .entry = Entry .objects .create (
477
+ pub_date = now ,
478
+ is_active = True ,
479
+ is_searchable = True ,
480
+ headline = "Searchable post" ,
481
+ slug = "a" ,
482
+ body_html = "<h1>Searchable Blog Post</h1>" ,
483
+ )
484
+ cls .docs_documents = cls .release .documents .exclude (
485
+ metadata__parents = DocumentationCategory .WEBSITE .value
486
+ )
469
487
470
488
def test_sync_to_db (self ):
471
489
self .release .sync_to_db (
@@ -477,8 +495,43 @@ def test_sync_to_db(self):
477
495
}
478
496
]
479
497
)
480
- document = self .release .documents .get ()
481
- self .assertEqual (document .path , "foo/bar" )
498
+ document_paths = set (self .release .documents .values_list ("path" , flat = True ))
499
+ self .assertEqual (
500
+ document_paths ,
501
+ {
502
+ "foo/bar" ,
503
+ reverse ("community-ecosystem" , host = "www" ),
504
+ self .entry .get_absolute_url (),
505
+ },
506
+ )
507
+
508
+ def test_blog_to_db_skip_non_english (self ):
509
+ """
510
+ Releases must be English to include the blog and website results in search.
511
+ """
512
+ non_english = DocumentRelease .objects .create (
513
+ lang = "es" ,
514
+ release = Release .objects .create (version = "88.0" ),
515
+ support_end = self .release .support_end ,
516
+ )
517
+ non_english .sync_to_db ([])
518
+ self .assertFalse (non_english .documents .exists ())
519
+
520
+ def test_blog_to_db_skip_no_end_support (self ):
521
+ """
522
+ Releases must have an end support to include the blog.
523
+ """
524
+ no_end_support = DocumentRelease .objects .create (
525
+ lang = "en" ,
526
+ release = Release .objects .create (version = "99.0" ),
527
+ )
528
+ no_end_support .sync_to_db ([])
529
+ self .assertEqual (
530
+ set (no_end_support .documents .values_list ("path" , flat = True )),
531
+ {
532
+ reverse ("community-ecosystem" , host = "www" ),
533
+ },
534
+ )
482
535
483
536
def test_clean_path (self ):
484
537
self .release .sync_to_db (
@@ -490,7 +543,7 @@ def test_clean_path(self):
490
543
}
491
544
]
492
545
)
493
- document = self .release . documents .get ()
546
+ document = self .docs_documents .get ()
494
547
self .assertEqual (document .path , "foo/bar" )
495
548
496
549
def test_title_strip_tags (self ):
@@ -504,7 +557,7 @@ def test_title_strip_tags(self):
504
557
]
505
558
)
506
559
self .assertQuerySetEqual (
507
- self .release . documents .all (),
560
+ self .docs_documents .all (),
508
561
["This is the title" ],
509
562
transform = attrgetter ("title" ),
510
563
)
@@ -520,7 +573,7 @@ def test_title_entities(self):
520
573
]
521
574
)
522
575
self .assertQuerySetEqual (
523
- self .release . documents . all () ,
576
+ self .docs_documents ,
524
577
["Title & title" ],
525
578
transform = attrgetter ("title" ),
526
579
)
@@ -533,7 +586,7 @@ def test_empty_documents(self):
533
586
{"current_page_name" : "foo/3" },
534
587
]
535
588
)
536
- self .assertQuerySetEqual (self .release . documents . all () , [])
589
+ self .assertQuerySetEqual (self .docs_documents , [])
537
590
538
591
def test_excluded_documents (self ):
539
592
"""
@@ -562,3 +615,47 @@ def test_excluded_documents(self):
562
615
)
563
616
document = release .documents .get ()
564
617
self .assertEqual (document .path , "nonexcluded/bar" )
618
+
619
+
620
+ class DocumentUrlTests (TestCase ):
621
+ @classmethod
622
+ def setUpTestData (cls ):
623
+ cls .release = DocumentRelease .objects .create (
624
+ release = Release .objects .create (version = "1.2.3" ),
625
+ )
626
+ documents = [
627
+ {
628
+ "metadata" : {"parents" : "topics http" },
629
+ "path" : "topics/http/generic-views" ,
630
+ "release" : cls .release ,
631
+ "title" : "Generic views" ,
632
+ },
633
+ # I'm not sure if this is valid or not.
634
+ {
635
+ "metadata" : {},
636
+ "path" : "" ,
637
+ "release" : cls .release ,
638
+ "title" : "Index" ,
639
+ },
640
+ ]
641
+ # Include the static views in the document search
642
+ cls .release ._sync_views_to_db ()
643
+ Document .objects .bulk_create (Document (** doc ) for doc in documents )
644
+ cls .document_index , cls .document_view , cls .document_detail = (
645
+ cls .release .documents .order_by ("path" )
646
+ )
647
+
648
+ def test_document_url (self ):
649
+ self .assertEqual (
650
+ self .document_index .get_absolute_url (),
651
+ "http://docs.djangoproject.localhost:8000/en/1.2.3/" ,
652
+ )
653
+ self .assertEqual (
654
+ self .document_view .get_absolute_url (),
655
+ "http://www.djangoproject.localhost:8000/community/ecosystem/" ,
656
+ )
657
+ self .assertEqual (
658
+ self .document_detail .get_absolute_url (),
659
+ "http://docs.djangoproject.localhost:8000"
660
+ "/en/1.2.3/topics/http/generic-views/" ,
661
+ )
0 commit comments