5
5
from django .test import TestCase
6
6
from django .test .utils import override_settings
7
7
8
+ from readthedocs .builds .constants import EXTERNAL
8
9
from readthedocs .projects .models import HTMLFile , ImportedFile , Project
9
10
from readthedocs .projects .tasks .search import _create_imported_files_and_search_index
10
11
from readthedocs .search .documents import PageDocument
@@ -26,13 +27,19 @@ def setUp(self):
26
27
with override_settings (DOCROOT = self .test_dir ):
27
28
self ._copy_storage_dir ()
28
29
29
- def tearDown (self ):
30
+ self ._create_index ()
31
+
32
+ def _create_index (self ):
30
33
try :
31
- PageDocument (). search (). filter (). delete ()
34
+ PageDocument . init ()
32
35
except Exception :
33
- # If there are no documents , the query fails.
36
+ # If the index already exists , the init fails.
34
37
pass
35
38
39
+ def tearDown (self ):
40
+ # Delete index
41
+ PageDocument ._index .delete (ignore = 404 )
42
+
36
43
def _manage_imported_files (self , version , search_ranking = None , search_ignore = None ):
37
44
"""Helper function for the tests to create and sync ImportedFiles."""
38
45
search_ranking = search_ranking or {}
@@ -51,6 +58,7 @@ def _copy_storage_dir(self):
51
58
type_ = "html" ,
52
59
version_slug = self .version .slug ,
53
60
include_file = False ,
61
+ version_type = self .version .type ,
54
62
),
55
63
)
56
64
@@ -93,6 +101,33 @@ def test_properly_created(self):
93
101
{"index.html" , "404.html" , "test.html" , "api/index.html" },
94
102
)
95
103
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
+
96
131
def test_update_build (self ):
97
132
self .assertEqual (ImportedFile .objects .count (), 0 )
98
133
sync_id = self ._manage_imported_files (self .version )
0 commit comments