@@ -27,20 +27,7 @@ class MyDoc(BaseDoc):
2727
2828
2929@pytest .fixture (scope = 'session' )
30- def index ():
31- index = HnswDocumentIndex [MyDoc ](work_dir = './tmp' )
32- return index
33-
34-
35- def test_subindex_init (index ):
36- assert isinstance (index ._subindices ['docs' ], HnswDocumentIndex )
37- assert isinstance (index ._subindices ['list_docs' ], HnswDocumentIndex )
38- assert isinstance (
39- index ._subindices ['list_docs' ]._subindices ['docs' ], HnswDocumentIndex
40- )
41-
42-
43- def test_subindex_index (index ):
30+ def index_docs ():
4431 my_docs = [
4532 MyDoc (
4633 id = f'{ i } ' ,
@@ -82,15 +69,31 @@ def test_subindex_index(index):
8269 )
8370 for i in range (5 )
8471 ]
72+ return my_docs
73+
74+
75+ def test_subindex_init (tmpdir , index_docs ):
76+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
77+ index .index (index_docs )
78+ assert isinstance (index ._subindices ['docs' ], HnswDocumentIndex )
79+ assert isinstance (index ._subindices ['list_docs' ], HnswDocumentIndex )
80+ assert isinstance (
81+ index ._subindices ['list_docs' ]._subindices ['docs' ], HnswDocumentIndex
82+ )
83+
8584
86- index .index (my_docs )
85+ def test_subindex_index (tmpdir , index_docs ):
86+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
87+ index .index (index_docs )
8788 assert index .num_docs () == 5
8889 assert index ._subindices ['docs' ].num_docs () == 25
8990 assert index ._subindices ['list_docs' ].num_docs () == 25
9091 assert index ._subindices ['list_docs' ]._subindices ['docs' ].num_docs () == 125
9192
9293
93- def test_subindex_get (index ):
94+ def test_subindex_get (tmpdir , index_docs ):
95+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
96+ index .index (index_docs )
9497 doc = index ['1' ]
9598 assert type (doc ) == MyDoc
9699 assert doc .id == '1'
@@ -116,7 +119,9 @@ def test_subindex_get(index):
116119 assert np .allclose (doc .my_tens , np .ones (30 ) * 2 )
117120
118121
119- def test_find_subindex (index ):
122+ def test_find_subindex (tmpdir , index_docs ):
123+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
124+ index .index (index_docs )
120125 # root level
121126 query = np .ones ((30 ,))
122127 with pytest .raises (ValueError ):
@@ -148,15 +153,19 @@ def test_find_subindex(index):
148153 assert root_doc .id == f'{ doc .id .split ("-" )[2 ]} '
149154
150155
151- def test_subindex_del (index ):
156+ def test_subindex_del (tmpdir , index_docs ):
157+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
158+ index .index (index_docs )
152159 del index ['0' ]
153160 assert index .num_docs () == 4
154161 assert index ._subindices ['docs' ].num_docs () == 20
155162 assert index ._subindices ['list_docs' ].num_docs () == 20
156163 assert index ._subindices ['list_docs' ]._subindices ['docs' ].num_docs () == 100
157164
158165
159- def test_subindex_contain (index ):
166+ def test_subindex_contain (tmpdir , index_docs ):
167+ index = HnswDocumentIndex [MyDoc ](work_dir = str (tmpdir ))
168+ index .index (index_docs )
160169 # Checks for individual simple_docs within list_docs
161170 for i in range (4 ):
162171 doc = index [f'{ i + 1 } ' ]
0 commit comments