-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathtest_export.py
More file actions
222 lines (179 loc) · 7.23 KB
/
test_export.py
File metadata and controls
222 lines (179 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import time
import uuid
from typing import Generator, List, Union
import pytest
from _pytest.fixtures import SubRequest
import weaviate
from weaviate.collections.classes.config import DataType, Property
from weaviate.exceptions import UnexpectedStatusCodeException
from weaviate.export.export import (
ExportFileFormat,
ExportStatus,
ExportStorage,
)
from .conftest import _sanitize_collection_name
pytestmark = pytest.mark.xdist_group(name="export")
BACKEND = ExportStorage.FILESYSTEM
COLLECTION_NAME = "ExportTestCollection"
OBJECT_PROPS = [{"title": f"object {i}", "count": i} for i in range(5)]
OBJECT_IDS = [
"fd34ccf4-1a2a-47ad-8446-231839366c3f",
"2653442b-05d8-4fa3-b46a-d4a152eb63bc",
"55374edb-17de-487f-86cb-9a9fbc30823f",
"124ff6aa-597f-44d0-8c13-62fbb1e66888",
"f787386e-7d1c-481f-b8c3-3dbfd8bbad85",
]
@pytest.fixture(scope="module")
def client() -> Generator[weaviate.WeaviateClient, None, None]:
client = weaviate.connect_to_local()
if client._connection._weaviate_version.is_lower_than(1, 37, 0):
client.close()
pytest.skip("Collection export is not supported in versions lower than 1.37.0")
client.collections.delete(COLLECTION_NAME)
col = client.collections.create(
name=COLLECTION_NAME,
properties=[
Property(name="title", data_type=DataType.TEXT),
Property(name="count", data_type=DataType.INT),
],
)
for i, props in enumerate(OBJECT_PROPS):
col.data.insert(properties=props, uuid=OBJECT_IDS[i])
yield client
client.collections.delete(COLLECTION_NAME)
client.close()
def unique_export_id(name: str) -> str:
"""Generate a unique export ID based on the test name."""
name = _sanitize_collection_name(name)
random_part = str(uuid.uuid4()).replace("-", "")[:12]
return name + random_part
def test_create_export_with_waiting(client: weaviate.WeaviateClient, request: SubRequest) -> None:
"""Create an export with wait_for_completion=True."""
export_id = unique_export_id(request.node.name)
resp = client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=[COLLECTION_NAME],
wait_for_completion=True,
)
assert resp.status == ExportStatus.SUCCESS
assert COLLECTION_NAME in resp.collections
def test_create_export_without_waiting(
client: weaviate.WeaviateClient, request: SubRequest
) -> None:
"""Create an export without waiting, then poll status."""
export_id = unique_export_id(request.node.name)
resp = client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=[COLLECTION_NAME],
)
assert resp.status in [ExportStatus.STARTED, ExportStatus.TRANSFERRING, ExportStatus.SUCCESS]
# poll until done
while True:
status = client.export.get_status(export_id=export_id, backend=BACKEND)
assert status.status in [
ExportStatus.STARTED,
ExportStatus.TRANSFERRING,
ExportStatus.SUCCESS,
]
if status.status == ExportStatus.SUCCESS:
break
time.sleep(0.1)
assert status.export_id.lower() == export_id.lower()
def test_get_export_status(client: weaviate.WeaviateClient, request: SubRequest) -> None:
"""Check status of a completed export."""
export_id = unique_export_id(request.node.name)
client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=[COLLECTION_NAME],
wait_for_completion=True,
)
status = client.export.get_status(export_id=export_id, backend=BACKEND)
assert status.status == ExportStatus.SUCCESS
assert status.export_id.lower() == export_id.lower()
assert status.backend == BACKEND.value
def test_create_export_with_parquet_format(
client: weaviate.WeaviateClient, request: SubRequest
) -> None:
"""Create an export explicitly specifying parquet format."""
export_id = unique_export_id(request.node.name)
resp = client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=[COLLECTION_NAME],
wait_for_completion=True,
)
assert resp.status == ExportStatus.SUCCESS
@pytest.mark.parametrize("include", [[COLLECTION_NAME], COLLECTION_NAME])
def test_create_export_include_as_str_and_list(
client: weaviate.WeaviateClient, include: Union[str, List[str]], request: SubRequest
) -> None:
"""Verify include_collections accepts both str and list."""
export_id = unique_export_id(request.node.name)
resp = client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=include,
wait_for_completion=True,
)
assert resp.status == ExportStatus.SUCCESS
assert COLLECTION_NAME in resp.collections
def test_cancel_export(client: weaviate.WeaviateClient, request: SubRequest) -> None:
"""Cancel a running export."""
export_id = unique_export_id(request.node.name)
resp = client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=[COLLECTION_NAME],
)
assert resp.status in [ExportStatus.STARTED, ExportStatus.TRANSFERRING, ExportStatus.SUCCESS]
client.export.cancel(export_id=export_id, backend=BACKEND)
# verify it's cancelled or already completed (race condition)
start = time.time()
while time.time() - start < 5:
status = client.export.get_status(export_id=export_id, backend=BACKEND)
if status.status in [ExportStatus.CANCELED, ExportStatus.SUCCESS]:
break
time.sleep(0.1)
assert status.status in [ExportStatus.CANCELED, ExportStatus.SUCCESS]
def test_fail_on_non_existing_collection(
client: weaviate.WeaviateClient, request: SubRequest
) -> None:
"""Fail export on non-existing collection."""
export_id = unique_export_id(request.node.name)
with pytest.raises(UnexpectedStatusCodeException):
client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=["NonExistingCollection"],
wait_for_completion=True,
)
def test_fail_on_both_include_and_exclude(
client: weaviate.WeaviateClient, request: SubRequest
) -> None:
"""Fail when both include and exclude collections are set."""
export_id = unique_export_id(request.node.name)
with pytest.raises(ValueError):
client.export.create(
export_id=export_id,
backend=BACKEND,
file_format=ExportFileFormat.PARQUET,
include_collections=COLLECTION_NAME,
exclude_collections="SomeOther",
)
def test_fail_status_for_non_existing_export(
client: weaviate.WeaviateClient, request: SubRequest
) -> None:
"""Fail checking status for non-existing export."""
export_id = unique_export_id(request.node.name)
with pytest.raises(UnexpectedStatusCodeException):
client.export.get_status(export_id=export_id, backend=BACKEND)