@@ -27,20 +27,7 @@ class MyDoc(BaseDoc): |
27 | 27 |
|
28 | 28 |
|
29 | 29 | @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(): |
44 | 31 | my_docs = [ |
45 | 32 | MyDoc( |
46 | 33 | id=f'{i}', |
@@ -82,15 +69,31 @@ def test_subindex_index(index): |
82 | 69 | ) |
83 | 70 | for i in range(5) |
84 | 71 | ] |
| 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 | + |
85 | 84 |
|
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) |
87 | 88 | assert index.num_docs() == 5 |
88 | 89 | assert index._subindices['docs'].num_docs() == 25 |
89 | 90 | assert index._subindices['list_docs'].num_docs() == 25 |
90 | 91 | assert index._subindices['list_docs']._subindices['docs'].num_docs() == 125 |
91 | 92 |
|
92 | 93 |
|
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) |
94 | 97 | doc = index['1'] |
95 | 98 | assert type(doc) == MyDoc |
96 | 99 | assert doc.id == '1' |
@@ -116,7 +119,9 @@ def test_subindex_get(index): |
116 | 119 | assert np.allclose(doc.my_tens, np.ones(30) * 2) |
117 | 120 |
|
118 | 121 |
|
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) |
120 | 125 | # root level |
121 | 126 | query = np.ones((30,)) |
122 | 127 | with pytest.raises(ValueError): |
@@ -148,15 +153,19 @@ def test_find_subindex(index): |
148 | 153 | assert root_doc.id == f'{doc.id.split("-")[2]}' |
149 | 154 |
|
150 | 155 |
|
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) |
152 | 159 | del index['0'] |
153 | 160 | assert index.num_docs() == 4 |
154 | 161 | assert index._subindices['docs'].num_docs() == 20 |
155 | 162 | assert index._subindices['list_docs'].num_docs() == 20 |
156 | 163 | assert index._subindices['list_docs']._subindices['docs'].num_docs() == 100 |
157 | 164 |
|
158 | 165 |
|
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) |
160 | 169 | # Checks for individual simple_docs within list_docs |
161 | 170 | for i in range(4): |
162 | 171 | doc = index[f'{i + 1}'] |
|