|
| 1 | +# Copyright © 2026 Pathway |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from leann import LeannSearcher |
| 8 | + |
| 9 | +import pathway as pw |
| 10 | + |
| 11 | + |
| 12 | +class DocumentSchema(pw.Schema): |
| 13 | + text: str |
| 14 | + |
| 15 | + |
| 16 | +class DocumentWithMetadataSchema(pw.Schema): |
| 17 | + text: str |
| 18 | + title: str |
| 19 | + category: str |
| 20 | + |
| 21 | + |
| 22 | +def test_basic_write(tmp_path: Path): |
| 23 | + """Test basic writing to a LEANN index.""" |
| 24 | + index_path = tmp_path / "test_basic.leann" |
| 25 | + |
| 26 | + table = pw.debug.table_from_rows( |
| 27 | + schema=DocumentSchema, |
| 28 | + rows=[ |
| 29 | + ("LEANN is a vector database",), |
| 30 | + ("Pathway enables streaming",), |
| 31 | + ("Vector search is powerful",), |
| 32 | + ], |
| 33 | + ) |
| 34 | + |
| 35 | + pw.io.leann.write( |
| 36 | + table, |
| 37 | + index_path=str(index_path), |
| 38 | + text_column=table.text, |
| 39 | + backend_name="hnsw", |
| 40 | + ) |
| 41 | + |
| 42 | + pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 43 | + |
| 44 | + # Verify the index was created (LEANN creates multiple files with base name) |
| 45 | + # Check for the metadata file which is always created |
| 46 | + meta_file = Path(str(index_path) + ".meta.json") |
| 47 | + assert meta_file.exists(), f"Expected {meta_file} to exist" |
| 48 | + |
| 49 | + searcher = LeannSearcher(str(index_path)) |
| 50 | + results = searcher.search("vector database", top_k=1) |
| 51 | + assert len(results) > 0 |
| 52 | + |
| 53 | + |
| 54 | +def test_write_with_metadata_columns(tmp_path: Path): |
| 55 | + """Test writing with metadata columns.""" |
| 56 | + index_path = tmp_path / "test_metadata.leann" |
| 57 | + |
| 58 | + table = pw.debug.table_from_rows( |
| 59 | + schema=DocumentWithMetadataSchema, |
| 60 | + rows=[ |
| 61 | + ("Introduction to LEANN", "LEANN Guide", "tutorial"), |
| 62 | + ("Advanced vector search", "Search Tips", "advanced"), |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + pw.io.leann.write( |
| 67 | + table, |
| 68 | + index_path=str(index_path), |
| 69 | + text_column=table.text, |
| 70 | + metadata_columns=[table.title, table.category], |
| 71 | + backend_name="hnsw", |
| 72 | + ) |
| 73 | + |
| 74 | + pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 75 | + assert Path(str(index_path) + ".meta.json").exists() |
| 76 | + |
| 77 | + |
| 78 | +def test_write_with_custom_text_column(tmp_path: Path): |
| 79 | + """Test writing with a custom text column name.""" |
| 80 | + index_path = tmp_path / "test_custom_column.leann" |
| 81 | + |
| 82 | + class ContentSchema(pw.Schema): |
| 83 | + content: str |
| 84 | + id: int |
| 85 | + |
| 86 | + table = pw.debug.table_from_rows( |
| 87 | + schema=ContentSchema, |
| 88 | + rows=[ |
| 89 | + ("First document content", 1), |
| 90 | + ("Second document content", 2), |
| 91 | + ], |
| 92 | + ) |
| 93 | + |
| 94 | + pw.io.leann.write( |
| 95 | + table, |
| 96 | + index_path=str(index_path), |
| 97 | + text_column=table.content, |
| 98 | + backend_name="hnsw", |
| 99 | + ) |
| 100 | + pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 101 | + assert Path(str(index_path) + ".meta.json").exists() |
| 102 | + |
| 103 | + |
| 104 | +def test_write_creates_parent_directories(tmp_path: Path): |
| 105 | + """Test that write() creates parent directories if they don't exist.""" |
| 106 | + index_path = tmp_path / "nested" / "dir" / "test.leann" |
| 107 | + |
| 108 | + table = pw.debug.table_from_rows( |
| 109 | + schema=DocumentSchema, |
| 110 | + rows=[("Test document",)], |
| 111 | + ) |
| 112 | + |
| 113 | + pw.io.leann.write( |
| 114 | + table, |
| 115 | + index_path=str(index_path), |
| 116 | + text_column=table.text, |
| 117 | + backend_name="hnsw", |
| 118 | + ) |
| 119 | + |
| 120 | + pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 121 | + assert Path(str(index_path) + ".meta.json").exists() |
| 122 | + |
| 123 | + |
| 124 | +def test_empty_table(tmp_path: Path): |
| 125 | + """Test behavior with an empty table.""" |
| 126 | + index_path = tmp_path / "test_empty.leann" |
| 127 | + |
| 128 | + table = pw.debug.table_from_rows( |
| 129 | + schema=DocumentSchema, |
| 130 | + rows=[], |
| 131 | + ) |
| 132 | + |
| 133 | + pw.io.leann.write( |
| 134 | + table, |
| 135 | + index_path=str(index_path), |
| 136 | + text_column=table.text, |
| 137 | + backend_name="hnsw", |
| 138 | + ) |
| 139 | + |
| 140 | + pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 141 | + |
| 142 | + # Index should not be created for empty table |
| 143 | + assert not Path(str(index_path) + ".meta.json").exists() |
| 144 | + |
| 145 | + |
| 146 | +def test_write_with_diskann_backend(tmp_path: Path): |
| 147 | + pass |
| 148 | + # TODO: uncomment if diskann needs to be tested. Currently we keep it this way, since |
| 149 | + # diskann installation requires additional steps in Dockerfile and will lead to the |
| 150 | + # increase in the time required to build the base integration tests image. |
| 151 | + # |
| 152 | + # """Test writing with DiskANN backend.""" |
| 153 | + # index_path = tmp_path / "test_diskann.leann" |
| 154 | + # |
| 155 | + # table = pw.debug.table_from_rows( |
| 156 | + # schema=DocumentSchema, |
| 157 | + # rows=[ |
| 158 | + # ("Document one for DiskANN test",), |
| 159 | + # ("Document two for DiskANN test",), |
| 160 | + # ], |
| 161 | + # ) |
| 162 | + # |
| 163 | + # pw.io.leann.write( |
| 164 | + # table, |
| 165 | + # index_path=str(index_path), |
| 166 | + # text_column=table.text, |
| 167 | + # backend_name="diskann", |
| 168 | + # ) |
| 169 | + # |
| 170 | + # pw.run(monitoring_level=pw.MonitoringLevel.NONE) |
| 171 | + # assert Path(str(index_path) + ".meta.json").exists() |
0 commit comments