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
@@ -465,7 +468,19 @@ def test_search_title(self):
465
468
class UpdateDocTests (TestCase ):
466
469
@classmethod
467
470
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" )
469
484
470
485
def test_sync_to_db (self ):
471
486
self .release .sync_to_db (
@@ -477,8 +492,43 @@ def test_sync_to_db(self):
477
492
}
478
493
]
479
494
)
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
+ )
482
532
483
533
def test_clean_path (self ):
484
534
self .release .sync_to_db (
@@ -490,7 +540,7 @@ def test_clean_path(self):
490
540
}
491
541
]
492
542
)
493
- document = self .release . documents .get ()
543
+ document = self .docs_documents .get ()
494
544
self .assertEqual (document .path , "foo/bar" )
495
545
496
546
def test_title_strip_tags (self ):
@@ -504,7 +554,7 @@ def test_title_strip_tags(self):
504
554
]
505
555
)
506
556
self .assertQuerySetEqual (
507
- self .release . documents .all (),
557
+ self .docs_documents .all (),
508
558
["This is the title" ],
509
559
transform = attrgetter ("title" ),
510
560
)
@@ -520,7 +570,7 @@ def test_title_entities(self):
520
570
]
521
571
)
522
572
self .assertQuerySetEqual (
523
- self .release . documents . all () ,
573
+ self .docs_documents ,
524
574
["Title & title" ],
525
575
transform = attrgetter ("title" ),
526
576
)
@@ -533,7 +583,7 @@ def test_empty_documents(self):
533
583
{"current_page_name" : "foo/3" },
534
584
]
535
585
)
536
- self .assertQuerySetEqual (self .release . documents . all () , [])
586
+ self .assertQuerySetEqual (self .docs_documents , [])
537
587
538
588
def test_excluded_documents (self ):
539
589
"""
@@ -562,3 +612,47 @@ def test_excluded_documents(self):
562
612
)
563
613
document = release .documents .get ()
564
614
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