Skip to content

Commit c7e7722

Browse files
authored
Build indexing: fix indexing of external versions (#10756)
* Build indexing: fix indexing of external versions The storage is located in another path :/ * Fix tests?
1 parent a0285d6 commit c7e7722

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

readthedocs/projects/tasks/search.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def _create_imported_files_and_search_index(
132132
otherwise the default one will be used.
133133
"""
134134
storage_path = version.project.get_storage_path(
135-
type_="html", version_slug=version.slug, include_file=False
135+
type_="html",
136+
version_slug=version.slug,
137+
include_file=False,
138+
version_type=version.type,
136139
)
137140
# A sync ID is a number different than the current `build` attribute (pending rename),
138141
# it's used to differentiate the files from the current sync from the previous one.

readthedocs/rtd_tests/tests/test_imported_file.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.test import TestCase
66
from django.test.utils import override_settings
77

8+
from readthedocs.builds.constants import EXTERNAL
89
from readthedocs.projects.models import HTMLFile, ImportedFile, Project
910
from readthedocs.projects.tasks.search import _create_imported_files_and_search_index
1011
from readthedocs.search.documents import PageDocument
@@ -26,13 +27,19 @@ def setUp(self):
2627
with override_settings(DOCROOT=self.test_dir):
2728
self._copy_storage_dir()
2829

29-
def tearDown(self):
30+
self._create_index()
31+
32+
def _create_index(self):
3033
try:
31-
PageDocument().search().filter().delete()
34+
PageDocument.init()
3235
except Exception:
33-
# If there are no documents, the query fails.
36+
# If the index already exists, the init fails.
3437
pass
3538

39+
def tearDown(self):
40+
# Delete index
41+
PageDocument._index.delete(ignore=404)
42+
3643
def _manage_imported_files(self, version, search_ranking=None, search_ignore=None):
3744
"""Helper function for the tests to create and sync ImportedFiles."""
3845
search_ranking = search_ranking or {}
@@ -51,6 +58,7 @@ def _copy_storage_dir(self):
5158
type_="html",
5259
version_slug=self.version.slug,
5360
include_file=False,
61+
version_type=self.version.type,
5462
),
5563
)
5664

@@ -93,6 +101,33 @@ def test_properly_created(self):
93101
{"index.html", "404.html", "test.html", "api/index.html"},
94102
)
95103

104+
def test_index_external_version(self):
105+
self.assertEqual(ImportedFile.objects.count(), 0)
106+
self.version.type = EXTERNAL
107+
self.version.save()
108+
109+
with override_settings(DOCROOT=self.test_dir):
110+
self._copy_storage_dir()
111+
112+
sync_id = self._manage_imported_files(version=self.version)
113+
self.assertEqual(ImportedFile.objects.count(), 3)
114+
self.assertEqual(
115+
set(HTMLFile.objects.all().values_list("path", flat=True)),
116+
{"index.html", "api/index.html", "404.html"},
117+
)
118+
119+
results = PageDocument().search().filter("term", build=sync_id).execute()
120+
self.assertEqual(len(results), 0)
121+
122+
sync_id = self._manage_imported_files(version=self.version)
123+
self.assertEqual(ImportedFile.objects.count(), 3)
124+
self.assertEqual(
125+
set(HTMLFile.objects.all().values_list("path", flat=True)),
126+
{"index.html", "api/index.html", "404.html"},
127+
)
128+
129+
self.assertEqual(len(results), 0)
130+
96131
def test_update_build(self):
97132
self.assertEqual(ImportedFile.objects.count(), 0)
98133
sync_id = self._manage_imported_files(self.version)

0 commit comments

Comments
 (0)