Skip to content

Commit

Permalink
Build indexing: fix indexing of external versions (#10756)
Browse files Browse the repository at this point in the history
* Build indexing: fix indexing of external versions

The storage is located in another path :/

* Fix tests?
  • Loading branch information
stsewd authored Sep 20, 2023
1 parent a0285d6 commit c7e7722
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 4 additions & 1 deletion readthedocs/projects/tasks/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 38 additions & 3 deletions readthedocs/rtd_tests/tests/test_imported_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {}
Expand All @@ -51,6 +58,7 @@ def _copy_storage_dir(self):
type_="html",
version_slug=self.version.slug,
include_file=False,
version_type=self.version.type,
),
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c7e7722

Please sign in to comment.