|
| 1 | +import json |
| 2 | +import click |
| 3 | +import pytest |
| 4 | +import weaviate |
| 5 | +from weaviate_cli.managers.collection_manager import CollectionManager |
| 6 | +from weaviate_cli.managers.config_manager import ConfigManager |
| 7 | +from weaviate_cli.managers.data_manager import DataManager |
| 8 | +from weaviate_cli.managers.export_manager import ExportManager |
| 9 | + |
| 10 | + |
| 11 | +EXPORT_COLLECTION = "ExportTestCollection" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def client() -> weaviate.WeaviateClient: |
| 16 | + config = ConfigManager() |
| 17 | + return config.get_client() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def collection_manager(client: weaviate.WeaviateClient) -> CollectionManager: |
| 22 | + return CollectionManager(client) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def data_manager(client: weaviate.WeaviateClient) -> DataManager: |
| 27 | + return DataManager(client) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def export_manager(client: weaviate.WeaviateClient) -> ExportManager: |
| 32 | + return ExportManager(client) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def setup_collection(collection_manager, data_manager): |
| 37 | + """Create a collection with data for export tests.""" |
| 38 | + try: |
| 39 | + collection_manager.create_collection( |
| 40 | + collection=EXPORT_COLLECTION, |
| 41 | + replication_factor=1, |
| 42 | + vectorizer="none", |
| 43 | + force_auto_schema=True, |
| 44 | + ) |
| 45 | + data_manager.create_data( |
| 46 | + collection=EXPORT_COLLECTION, |
| 47 | + limit=100, |
| 48 | + randomize=True, |
| 49 | + consistency_level="one", |
| 50 | + ) |
| 51 | + yield |
| 52 | + finally: |
| 53 | + if collection_manager.client.collections.exists(EXPORT_COLLECTION): |
| 54 | + collection_manager.delete_collection(collection=EXPORT_COLLECTION) |
| 55 | + |
| 56 | + |
| 57 | +def test_create_export_and_get_status( |
| 58 | + export_manager: ExportManager, setup_collection, capsys |
| 59 | +): |
| 60 | + """Test creating an export and getting its status.""" |
| 61 | + export_manager.create_export( |
| 62 | + export_id="integration-test-export", |
| 63 | + backend="s3", |
| 64 | + file_format="parquet", |
| 65 | + include=EXPORT_COLLECTION, |
| 66 | + wait=True, |
| 67 | + json_output=False, |
| 68 | + ) |
| 69 | + |
| 70 | + out = capsys.readouterr().out |
| 71 | + assert "integration-test-export" in out |
| 72 | + assert "created successfully" in out |
| 73 | + |
| 74 | + export_manager.get_export_status( |
| 75 | + export_id="integration-test-export", |
| 76 | + backend="s3", |
| 77 | + json_output=True, |
| 78 | + ) |
| 79 | + |
| 80 | + out = capsys.readouterr().out |
| 81 | + data = json.loads(out) |
| 82 | + assert data["export_id"] == "integration-test-export" |
| 83 | + assert data["status"] == "SUCCESS" |
| 84 | + assert EXPORT_COLLECTION in data["collections"] |
| 85 | + assert "shard_status" in data |
| 86 | + |
| 87 | + |
| 88 | +def test_create_export_json_output( |
| 89 | + export_manager: ExportManager, setup_collection, capsys |
| 90 | +): |
| 91 | + """Test creating an export with JSON output.""" |
| 92 | + export_manager.create_export( |
| 93 | + export_id="integration-json-export", |
| 94 | + backend="s3", |
| 95 | + file_format="parquet", |
| 96 | + wait=True, |
| 97 | + json_output=True, |
| 98 | + ) |
| 99 | + |
| 100 | + out = capsys.readouterr().out |
| 101 | + data = json.loads(out) |
| 102 | + assert data["status"] == "SUCCESS" |
| 103 | + assert data["export_id"] == "integration-json-export" |
| 104 | + |
| 105 | + |
| 106 | +def test_create_export_with_exclude( |
| 107 | + export_manager: ExportManager, |
| 108 | + collection_manager: CollectionManager, |
| 109 | + data_manager: DataManager, |
| 110 | + setup_collection, |
| 111 | + capsys, |
| 112 | +): |
| 113 | + """Test creating an export with exclude filter. |
| 114 | +
|
| 115 | + Creates a second collection so that excluding it still leaves |
| 116 | + EXPORT_COLLECTION exportable (the server rejects an export with no |
| 117 | + exportable classes). |
| 118 | + """ |
| 119 | + second_collection = "ExportTestCollection_Excluded" |
| 120 | + try: |
| 121 | + collection_manager.create_collection( |
| 122 | + collection=second_collection, |
| 123 | + replication_factor=1, |
| 124 | + vectorizer="none", |
| 125 | + force_auto_schema=True, |
| 126 | + ) |
| 127 | + data_manager.create_data( |
| 128 | + collection=second_collection, |
| 129 | + limit=10, |
| 130 | + randomize=True, |
| 131 | + consistency_level="one", |
| 132 | + ) |
| 133 | + capsys.readouterr() # Clear setup output |
| 134 | + |
| 135 | + export_manager.create_export( |
| 136 | + export_id="integration-exclude-export", |
| 137 | + backend="s3", |
| 138 | + file_format="parquet", |
| 139 | + exclude=second_collection, |
| 140 | + wait=True, |
| 141 | + json_output=True, |
| 142 | + ) |
| 143 | + |
| 144 | + out = capsys.readouterr().out |
| 145 | + data = json.loads(out) |
| 146 | + assert data["status"] == "SUCCESS" |
| 147 | + assert second_collection not in data.get("collections", []) |
| 148 | + assert EXPORT_COLLECTION in data.get("collections", []) |
| 149 | + finally: |
| 150 | + if collection_manager.client.collections.exists(second_collection): |
| 151 | + collection_manager.delete_collection(collection=second_collection) |
| 152 | + |
| 153 | + |
| 154 | +def test_create_export_include_and_exclude_raises( |
| 155 | + export_manager: ExportManager, setup_collection |
| 156 | +): |
| 157 | + """Test that specifying both include and exclude raises an error.""" |
| 158 | + with pytest.raises(click.ClickException) as exc_info: |
| 159 | + export_manager.create_export( |
| 160 | + export_id="should-fail", |
| 161 | + backend="s3", |
| 162 | + file_format="parquet", |
| 163 | + include=EXPORT_COLLECTION, |
| 164 | + exclude="OtherCollection", |
| 165 | + ) |
| 166 | + assert "include" in str(exc_info.value).lower() |
| 167 | + assert "exclude" in str(exc_info.value).lower() |
| 168 | + |
| 169 | + |
| 170 | +def test_cancel_export(export_manager: ExportManager, setup_collection, capsys): |
| 171 | + """Test canceling an export.""" |
| 172 | + # Create export without waiting |
| 173 | + export_manager.create_export( |
| 174 | + export_id="integration-cancel-export", |
| 175 | + backend="s3", |
| 176 | + file_format="parquet", |
| 177 | + wait=False, |
| 178 | + ) |
| 179 | + capsys.readouterr() # Clear output |
| 180 | + |
| 181 | + # Try to cancel — may succeed or fail depending on timing. Only tolerate |
| 182 | + # the specific "could not be canceled" path (export already finished); |
| 183 | + # anything else is a real failure. |
| 184 | + try: |
| 185 | + export_manager.cancel_export( |
| 186 | + export_id="integration-cancel-export", |
| 187 | + backend="s3", |
| 188 | + json_output=True, |
| 189 | + ) |
| 190 | + except click.ClickException as e: |
| 191 | + assert "could not be canceled" in str(e) |
| 192 | + return |
| 193 | + |
| 194 | + out = capsys.readouterr().out |
| 195 | + data = json.loads(out) |
| 196 | + assert data["status"] == "success" |
0 commit comments