with-statement closes the iterator, even on early break.
(iter_collection)
| 162 | |
| 163 | |
| 164 | def test_iter_docs_context_manager(iter_collection): |
| 165 | """with-statement closes the iterator, even on early break.""" |
| 166 | iter_collection.insert(_make_docs(10)) |
| 167 | iter_collection.flush() |
| 168 | |
| 169 | # Full traversal inside a with-block. |
| 170 | count = 0 |
| 171 | with iter_collection.iter_docs(include_vector=False) as docs: |
| 172 | for doc in docs: |
| 173 | assert doc.field("id") is not None |
| 174 | count += 1 |
| 175 | assert count == 10 |
| 176 | iter_collection.flush() # slot released after exhaustion |
| 177 | |
| 178 | # Early break: leaving the with-block must release the slot. |
| 179 | with iter_collection.iter_docs(include_vector=False) as docs: |
| 180 | for doc in docs: |
| 181 | break |
| 182 | iter_collection.flush() |
| 183 | |
| 184 | # Exceptions inside the block must not leak the slot either. |
| 185 | with pytest.raises(RuntimeError): |
| 186 | with iter_collection.iter_docs(include_vector=False) as docs: |
| 187 | for doc in docs: |
| 188 | raise RuntimeError("boom") |
| 189 | iter_collection.flush() |
| 190 | |
| 191 | |
| 192 | def test_iter_docs_empty(iter_collection): |
nothing calls this directly
no test coverage detected