|  | 
| 1 | 1 | import datetime | 
| 2 | 2 | from operator import attrgetter | 
| 3 | 3 | 
 | 
|  | 4 | +import requests_mock | 
| 4 | 5 | from django.conf import settings | 
| 5 | 6 | from django.db import connection | 
| 6 | 7 | from django.test import TestCase | 
|  | 8 | +from django.utils import timezone | 
| 7 | 9 | 
 | 
|  | 10 | +from blog.models import ContentFormat, Entry | 
| 8 | 11 | from releases.models import Release | 
| 9 | 12 | 
 | 
| 10 | 13 | from ..models import Document, DocumentRelease | 
|  | 14 | +from ..search import DocumentationCategory | 
| 11 | 15 | 
 | 
| 12 | 16 | 
 | 
| 13 | 17 | class ModelsTests(TestCase): | 
| @@ -173,6 +177,7 @@ def test_get_available_languages_by_version(self): | 
| 173 | 177 | class DocumentManagerTest(TestCase): | 
| 174 | 178 |     @classmethod | 
| 175 | 179 |     def setUpTestData(cls): | 
|  | 180 | +        cls.dev_release = DocumentRelease.objects.create(lang="en") | 
| 176 | 181 |         cls.release = DocumentRelease.objects.create( | 
| 177 | 182 |             release=Release.objects.create(version="1.2.3"), | 
| 178 | 183 |         ) | 
| @@ -347,6 +352,20 @@ def setUpTestData(cls): | 
| 347 | 352 |                 "release": cls.release_fr, | 
| 348 | 353 |                 "title": "Notes de publication de Django 1.9.4", | 
| 349 | 354 |             }, | 
|  | 355 | +            { | 
|  | 356 | +                "metadata": { | 
|  | 357 | +                    "body": "Main 1", | 
|  | 358 | +                    "breadcrumbs": [ | 
|  | 359 | +                        {"path": DocumentationCategory.WEBSITE, "title": "Website"} | 
|  | 360 | +                    ], | 
|  | 361 | +                    "parents": DocumentationCategory.WEBSITE, | 
|  | 362 | +                    "title": "Title 1", | 
|  | 363 | +                    "toc": "", | 
|  | 364 | +                }, | 
|  | 365 | +                "path": "example", | 
|  | 366 | +                "release": cls.dev_release, | 
|  | 367 | +                "title": "Blog post", | 
|  | 368 | +            }, | 
| 350 | 369 |         ] | 
| 351 | 370 |         Document.objects.bulk_create(Document(**doc) for doc in documents) | 
| 352 | 371 | 
 | 
| @@ -446,6 +465,16 @@ def test_search_title(self): | 
| 446 | 465 |                 ), | 
| 447 | 466 |             ) | 
| 448 | 467 | 
 | 
|  | 468 | +    def test_website_document_items_included_english(self): | 
|  | 469 | +        self.assertQuerySetEqual( | 
|  | 470 | +            Document.objects.search("Main", self.release), | 
|  | 471 | +            ["Blog post"], | 
|  | 472 | +            transform=attrgetter("title"), | 
|  | 473 | +        ) | 
|  | 474 | + | 
|  | 475 | +    def test_website_document_items_excluded_non_english(self): | 
|  | 476 | +        self.assertEqual(Document.objects.search("Main", self.release_fr).count(), 0) | 
|  | 477 | + | 
| 449 | 478 | 
 | 
| 450 | 479 | class UpdateDocTests(TestCase): | 
| 451 | 480 |     @classmethod | 
| @@ -547,3 +576,77 @@ def test_excluded_documents(self): | 
| 547 | 576 |         ) | 
| 548 | 577 |         document = release.documents.get() | 
| 549 | 578 |         self.assertEqual(document.path, "nonexcluded/bar") | 
|  | 579 | + | 
|  | 580 | +    def test_sync_to_db_not_delete_website_docs(self): | 
|  | 581 | +        Document.objects.create( | 
|  | 582 | +            release=self.release, | 
|  | 583 | +            path="example_path", | 
|  | 584 | +            title="Title 1", | 
|  | 585 | +            metadata={ | 
|  | 586 | +                "body": "Main 1", | 
|  | 587 | +                "breadcrumbs": [ | 
|  | 588 | +                    {"path": DocumentationCategory.WEBSITE, "title": "Website"} | 
|  | 589 | +                ], | 
|  | 590 | +                "parents": DocumentationCategory.WEBSITE, | 
|  | 591 | +                "title": "Title 1", | 
|  | 592 | +                "toc": "", | 
|  | 593 | +            }, | 
|  | 594 | +        ) | 
|  | 595 | +        self.release.sync_to_db([]) | 
|  | 596 | +        self.assertEqual(Document.objects.filter(release=self.release).count(), 1) | 
|  | 597 | + | 
|  | 598 | +    def test_sync_from_sitemap_skip_non_en_dev_release(self): | 
|  | 599 | +        release = Release.objects.create(version="5.2") | 
|  | 600 | +        Entry.objects.create( | 
|  | 601 | +            pub_date=timezone.now() - datetime.timedelta(days=2), | 
|  | 602 | +            slug="a", | 
|  | 603 | +            body="<strong>test</strong>", | 
|  | 604 | +            content_format=ContentFormat.HTML, | 
|  | 605 | +            is_active=True, | 
|  | 606 | +        ) | 
|  | 607 | +        for lang, release_obj in [("fr", None), ("fr", release), ("en", release)]: | 
|  | 608 | +            doc_release = DocumentRelease.objects.create( | 
|  | 609 | +                lang=lang, | 
|  | 610 | +                release=release_obj, | 
|  | 611 | +            ) | 
|  | 612 | +            with self.subTest(lang=lang, release=release_obj): | 
|  | 613 | +                doc_release.sync_from_sitemap() | 
|  | 614 | +                self.assertFalse(Document.objects.exists()) | 
|  | 615 | + | 
|  | 616 | +    @requests_mock.mock() | 
|  | 617 | +    def test_sync_from_sitemap(self, mocker): | 
|  | 618 | +        blog_entry = Entry.objects.create( | 
|  | 619 | +            pub_date=timezone.now() - datetime.timedelta(days=2), | 
|  | 620 | +            slug="a", | 
|  | 621 | +            body="<strong>test</strong>", | 
|  | 622 | +            content_format=ContentFormat.HTML, | 
|  | 623 | +            is_active=True, | 
|  | 624 | +        ) | 
|  | 625 | +        mocker.get( | 
|  | 626 | +            blog_entry.get_absolute_url(), | 
|  | 627 | +            text="<html><main>Main 1</main><h1>Title 1</h1></html>", | 
|  | 628 | +            headers={"Content-Type": "text/html"}, | 
|  | 629 | +        ) | 
|  | 630 | +        self.release.sync_from_sitemap() | 
|  | 631 | + | 
|  | 632 | +        document = Document.objects.get(release=self.release) | 
|  | 633 | +        self.assertEqual( | 
|  | 634 | +            document.path, | 
|  | 635 | +            blog_entry.get_absolute_url(), | 
|  | 636 | +        ) | 
|  | 637 | +        self.assertEqual( | 
|  | 638 | +            document.title, | 
|  | 639 | +            "Title 1", | 
|  | 640 | +        ) | 
|  | 641 | +        self.assertEqual( | 
|  | 642 | +            document.metadata, | 
|  | 643 | +            { | 
|  | 644 | +                "body": "Main 1", | 
|  | 645 | +                "breadcrumbs": [ | 
|  | 646 | +                    {"path": DocumentationCategory.WEBSITE, "title": "Website"} | 
|  | 647 | +                ], | 
|  | 648 | +                "parents": DocumentationCategory.WEBSITE, | 
|  | 649 | +                "title": "Title 1", | 
|  | 650 | +                "toc": "", | 
|  | 651 | +            }, | 
|  | 652 | +        ) | 
0 commit comments