44from  django .conf  import  settings 
55from  django .db  import  connection 
66from  django .test  import  TestCase 
7+ from  django .utils  import  timezone 
8+ from  django_hosts  import  reverse 
79
10+ from  blog .models  import  Entry 
811from  releases .models  import  Release 
912
1013from  ..models  import  DOCUMENT_SEARCH_VECTOR , Document , DocumentRelease 
14+ from  ..search  import  DocumentationCategory 
1115
1216
1317class  ModelsTests (TestCase ):
@@ -465,7 +469,21 @@ def test_search_title(self):
465469class  UpdateDocTests (TestCase ):
466470    @classmethod  
467471    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+         )
469487
470488    def  test_sync_to_db (self ):
471489        self .release .sync_to_db (
@@ -477,8 +495,43 @@ def test_sync_to_db(self):
477495                }
478496            ]
479497        )
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+         )
482535
483536    def  test_clean_path (self ):
484537        self .release .sync_to_db (
@@ -490,7 +543,7 @@ def test_clean_path(self):
490543                }
491544            ]
492545        )
493-         document  =  self .release . documents .get ()
546+         document  =  self .docs_documents .get ()
494547        self .assertEqual (document .path , "foo/bar" )
495548
496549    def  test_title_strip_tags (self ):
@@ -504,7 +557,7 @@ def test_title_strip_tags(self):
504557            ]
505558        )
506559        self .assertQuerySetEqual (
507-             self .release . documents .all (),
560+             self .docs_documents .all (),
508561            ["This is the title" ],
509562            transform = attrgetter ("title" ),
510563        )
@@ -520,7 +573,7 @@ def test_title_entities(self):
520573            ]
521574        )
522575        self .assertQuerySetEqual (
523-             self .release . documents . all () ,
576+             self .docs_documents ,
524577            ["Title & title" ],
525578            transform = attrgetter ("title" ),
526579        )
@@ -533,7 +586,7 @@ def test_empty_documents(self):
533586                {"current_page_name" : "foo/3" },
534587            ]
535588        )
536-         self .assertQuerySetEqual (self .release . documents . all () , [])
589+         self .assertQuerySetEqual (self .docs_documents , [])
537590
538591    def  test_excluded_documents (self ):
539592        """ 
@@ -562,3 +615,47 @@ def test_excluded_documents(self):
562615        )
563616        document  =  release .documents .get ()
564617        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