From c7e772239e8c7e013cd02ea78474aade057261cc Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 20 Sep 2023 12:24:46 -0500 Subject: [PATCH] Build indexing: fix indexing of external versions (#10756) * Build indexing: fix indexing of external versions The storage is located in another path :/ * Fix tests? --- readthedocs/projects/tasks/search.py | 5 ++- .../rtd_tests/tests/test_imported_file.py | 41 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/readthedocs/projects/tasks/search.py b/readthedocs/projects/tasks/search.py index bcab6200a77..9fb8c9dbbb6 100644 --- a/readthedocs/projects/tasks/search.py +++ b/readthedocs/projects/tasks/search.py @@ -132,7 +132,10 @@ def _create_imported_files_and_search_index( otherwise the default one will be used. """ storage_path = version.project.get_storage_path( - type_="html", version_slug=version.slug, include_file=False + type_="html", + version_slug=version.slug, + include_file=False, + version_type=version.type, ) # A sync ID is a number different than the current `build` attribute (pending rename), # it's used to differentiate the files from the current sync from the previous one. diff --git a/readthedocs/rtd_tests/tests/test_imported_file.py b/readthedocs/rtd_tests/tests/test_imported_file.py index 82e25f98b88..7ea1b83ee0c 100644 --- a/readthedocs/rtd_tests/tests/test_imported_file.py +++ b/readthedocs/rtd_tests/tests/test_imported_file.py @@ -5,6 +5,7 @@ from django.test import TestCase from django.test.utils import override_settings +from readthedocs.builds.constants import EXTERNAL from readthedocs.projects.models import HTMLFile, ImportedFile, Project from readthedocs.projects.tasks.search import _create_imported_files_and_search_index from readthedocs.search.documents import PageDocument @@ -26,13 +27,19 @@ def setUp(self): with override_settings(DOCROOT=self.test_dir): self._copy_storage_dir() - def tearDown(self): + self._create_index() + + def _create_index(self): try: - PageDocument().search().filter().delete() + PageDocument.init() except Exception: - # If there are no documents, the query fails. + # If the index already exists, the init fails. pass + def tearDown(self): + # Delete index + PageDocument._index.delete(ignore=404) + def _manage_imported_files(self, version, search_ranking=None, search_ignore=None): """Helper function for the tests to create and sync ImportedFiles.""" search_ranking = search_ranking or {} @@ -51,6 +58,7 @@ def _copy_storage_dir(self): type_="html", version_slug=self.version.slug, include_file=False, + version_type=self.version.type, ), ) @@ -93,6 +101,33 @@ def test_properly_created(self): {"index.html", "404.html", "test.html", "api/index.html"}, ) + def test_index_external_version(self): + self.assertEqual(ImportedFile.objects.count(), 0) + self.version.type = EXTERNAL + self.version.save() + + with override_settings(DOCROOT=self.test_dir): + self._copy_storage_dir() + + sync_id = self._manage_imported_files(version=self.version) + self.assertEqual(ImportedFile.objects.count(), 3) + self.assertEqual( + set(HTMLFile.objects.all().values_list("path", flat=True)), + {"index.html", "api/index.html", "404.html"}, + ) + + results = PageDocument().search().filter("term", build=sync_id).execute() + self.assertEqual(len(results), 0) + + sync_id = self._manage_imported_files(version=self.version) + self.assertEqual(ImportedFile.objects.count(), 3) + self.assertEqual( + set(HTMLFile.objects.all().values_list("path", flat=True)), + {"index.html", "api/index.html", "404.html"}, + ) + + self.assertEqual(len(results), 0) + def test_update_build(self): self.assertEqual(ImportedFile.objects.count(), 0) sync_id = self._manage_imported_files(self.version)