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