127
127
]
128
128
129
129
130
+ def _get_unique_suffix () -> str :
131
+ return str (uuid .uuid4 ())[:8 ]
132
+
133
+
130
134
@pytest .fixture (scope = "session" )
131
135
def cloudformation_outputs ():
132
136
return extract_cloudformation_outputs ()
@@ -249,7 +253,7 @@ def client(request, opensearch_1_0_client, elasticsearch_7_10_fgac_client, opens
249
253
250
254
251
255
def test_create_index (client ):
252
- index = "test_create_index "
256
+ index = f"test_create_index_ { _get_unique_suffix () } "
253
257
wr .opensearch .delete_index (client , index )
254
258
time .sleep (30 ) # let the cluster clean up
255
259
response = wr .opensearch .create_index (
@@ -259,56 +263,65 @@ def test_create_index(client):
259
263
settings = {"index" : {"number_of_shards" : 1 , "number_of_replicas" : 1 }},
260
264
)
261
265
assert response .get ("acknowledged" , False ) is True
266
+ wr .opensearch .delete_index (client , index )
262
267
263
268
264
269
def test_delete_index (client ):
265
- index = "test_delete_index "
270
+ index = f"test_create_index_ { _get_unique_suffix () } "
266
271
wr .opensearch .create_index (client , index = index )
267
272
response = wr .opensearch .delete_index (client , index = index )
268
273
assert response .get ("acknowledged" , False ) is True
269
274
270
275
271
276
def test_index_df (client ):
277
+ index = f"test_index_df_{ _get_unique_suffix ()} "
272
278
response = wr .opensearch .index_df (
273
279
client ,
274
280
df = pd .DataFrame ([{"_id" : "1" , "name" : "John" }, {"_id" : "2" , "name" : "George" }, {"_id" : "3" , "name" : "Julia" }]),
275
- index = "test_index_df1" ,
281
+ index = index ,
276
282
)
277
283
assert response .get ("success" , 0 ) == 3
284
+ wr .opensearch .delete_index (client , index )
278
285
279
286
280
287
def test_index_df_with_array (client ):
288
+ index = f"test_index_df_array_{ _get_unique_suffix ()} "
281
289
response = wr .opensearch .index_df (
282
290
client ,
283
291
df = pd .DataFrame (
284
292
[{"_id" : "1" , "name" : "John" , "tags" : ["foo" , "bar" ]}, {"_id" : "2" , "name" : "George" , "tags" : ["foo" ]}]
285
293
),
286
- index = "test_index_df1" ,
294
+ index = index ,
287
295
)
288
296
assert response .get ("success" , 0 ) == 2
297
+ wr .opensearch .delete_index (client , index )
289
298
290
299
291
300
def test_index_documents (client ):
301
+ index = f"test_index_documents_{ _get_unique_suffix ()} "
292
302
response = wr .opensearch .index_documents (
293
303
client ,
294
304
documents = [{"_id" : "1" , "name" : "John" }, {"_id" : "2" , "name" : "George" }, {"_id" : "3" , "name" : "Julia" }],
295
- index = "test_index_documents1" ,
305
+ index = index ,
296
306
)
297
307
assert response .get ("success" , 0 ) == 3
308
+ wr .opensearch .delete_index (client , index )
298
309
299
310
300
311
def test_index_documents_id_keys (client ):
301
- wr . opensearch . index_documents (
302
- client , documents = inspections_documents , index = "test_index_documents_id_keys" , id_keys = ["inspection_id" ]
303
- )
312
+ index = f"test_index_documents_id_keys_ { _get_unique_suffix () } "
313
+ wr . opensearch . index_documents ( client , documents = inspections_documents , index = index , id_keys = ["inspection_id" ])
314
+ wr . opensearch . delete_index ( client , index )
304
315
305
316
306
317
def test_index_documents_no_id_keys (client ):
307
- wr .opensearch .index_documents (client , documents = inspections_documents , index = "test_index_documents_no_id_keys" )
318
+ index = f"test_index_documents_no_id_keys_{ _get_unique_suffix ()} "
319
+ wr .opensearch .index_documents (client , documents = inspections_documents , index = index )
320
+ wr .opensearch .delete_index (client , index )
308
321
309
322
310
323
def test_search (client ):
311
- index = "test_search "
324
+ index = f"test_search_ { _get_unique_suffix () } "
312
325
kwargs = {} if _is_serverless (client ) else {"refresh" : "wait_for" }
313
326
wr .opensearch .index_documents (
314
327
client , documents = inspections_documents , index = index , id_keys = ["inspection_id" ], ** kwargs
@@ -330,11 +343,12 @@ def test_search(client):
330
343
search_body = {"query" : {"match" : {"business_name" : "message" }}},
331
344
)
332
345
assert df .shape == (0 , 0 )
346
+ wr .opensearch .delete_index (client , index )
333
347
334
348
335
349
@pytest .mark .parametrize ("filter_path" , [None , "hits.hits._source" , ["hits.hits._source" ]])
336
350
def test_search_filter_path (client , filter_path ):
337
- index = "test_search "
351
+ index = f"test_search_filter_ { _get_unique_suffix () } "
338
352
kwargs = {} if _is_serverless (client ) else {"refresh" : "wait_for" }
339
353
wr .opensearch .index_documents (
340
354
client , documents = inspections_documents , index = index , id_keys = ["inspection_id" ], ** kwargs
@@ -351,11 +365,12 @@ def test_search_filter_path(client, filter_path):
351
365
filter_path = filter_path ,
352
366
)
353
367
assert df .shape [0 ] == 3
368
+ wr .opensearch .delete_index (client , index )
354
369
355
370
356
371
@pytest .mark .xfail (raises = wr .exceptions .NotSupported , reason = "Scroll not available for OpenSearch Serverless." )
357
372
def test_search_scroll (client ):
358
- index = "test_search_scroll "
373
+ index = f"test_search_scroll_ { _get_unique_suffix () } "
359
374
kwargs = {} if _is_serverless (client ) else {"refresh" : "wait_for" }
360
375
wr .opensearch .index_documents (
361
376
client , documents = inspections_documents , index = index , id_keys = ["inspection_id" ], ** kwargs
@@ -364,32 +379,37 @@ def test_search_scroll(client):
364
379
client , index = index , is_scroll = True , _source = ["inspection_id" , "business_name" , "business_location" ]
365
380
)
366
381
assert df .shape [0 ] == 5
382
+ wr .opensearch .delete_index (client , index )
367
383
368
384
369
385
@pytest .mark .xfail (raises = wr .exceptions .NotSupported , reason = "SQL plugin not available for OpenSearch Serverless." )
370
386
@pytest .mark .parametrize ("fetch_size" , [None , 1000 , 10000 ])
371
387
@pytest .mark .parametrize ("fetch_size_param_name" , ["size" , "fetch_size" ])
372
388
def test_search_sql (client , fetch_size , fetch_size_param_name ):
373
- index = "test_search_sql "
389
+ index = f"test_search_sql_ { _get_unique_suffix () } "
374
390
kwargs = {} if _is_serverless (client ) else {"refresh" : "wait_for" }
375
391
wr .opensearch .index_documents (
376
392
client , documents = inspections_documents , index = index , id_keys = ["inspection_id" ], ** kwargs
377
393
)
378
394
search_kwargs = {fetch_size_param_name : fetch_size } if fetch_size else {}
379
395
df = wr .opensearch .search_by_sql (client , sql_query = f"select * from { index } " , ** search_kwargs )
380
396
assert df .shape [0 ] == 5
397
+ wr .opensearch .delete_index (client , index )
381
398
382
399
383
400
def test_index_json_local (client ):
401
+ index = f"test_index_json_local_{ _get_unique_suffix ()} "
384
402
file_path = f"{ tempfile .gettempdir ()} /inspections.json"
385
403
with open (file_path , "w" ) as filehandle :
386
404
for doc in inspections_documents :
387
405
filehandle .write ("%s\n " % json .dumps (doc ))
388
- response = wr .opensearch .index_json (client , index = "test_index_json_local" , path = file_path )
406
+ response = wr .opensearch .index_json (client , index = index , path = file_path )
389
407
assert response .get ("success" , 0 ) == 6
408
+ wr .opensearch .delete_index (client , index )
390
409
391
410
392
411
def test_index_json_s3 (client , path ):
412
+ index = f"test_index_json_s3_{ _get_unique_suffix ()} "
393
413
file_path = f"{ tempfile .gettempdir ()} /inspections.json"
394
414
with open (file_path , "w" ) as filehandle :
395
415
for doc in inspections_documents :
@@ -398,22 +418,24 @@ def test_index_json_s3(client, path):
398
418
path = f"{ path } opensearch/inspections.json"
399
419
bucket , key = wr ._utils .parse_path (path )
400
420
s3 .upload_file (file_path , bucket , key )
401
- response = wr .opensearch .index_json (client , index = "test_index_json_s3" , path = path )
421
+ response = wr .opensearch .index_json (client , index = index , path = path )
402
422
assert response .get ("success" , 0 ) == 6
423
+ wr .opensearch .delete_index (client , index )
403
424
404
425
405
426
def test_index_csv_local (client ):
406
427
file_path = f"{ tempfile .gettempdir ()} /inspections.csv"
407
- index = "test_index_csv_local "
428
+ index = f"test_index_csv_local_ { _get_unique_suffix () } "
408
429
df = pd .DataFrame (inspections_documents )
409
430
df .to_csv (file_path , index = False )
410
431
response = wr .opensearch .index_csv (client , path = file_path , index = index )
411
432
assert response .get ("success" , 0 ) == 6
433
+ wr .opensearch .delete_index (client , index )
412
434
413
435
414
436
def test_index_csv_s3 (client , path ):
415
437
file_path = f"{ tempfile .gettempdir ()} /inspections.csv"
416
- index = "test_index_csv_s3 "
438
+ index = f"test_index_csv_s3_ { _get_unique_suffix () } "
417
439
df = pd .DataFrame (inspections_documents )
418
440
df .to_csv (file_path , index = False )
419
441
s3 = boto3 .client ("s3" )
@@ -422,20 +444,24 @@ def test_index_csv_s3(client, path):
422
444
s3 .upload_file (file_path , bucket , key )
423
445
response = wr .opensearch .index_csv (client , path = path , index = index )
424
446
assert response .get ("success" , 0 ) == 6
447
+ wr .opensearch .delete_index (client , index )
425
448
426
449
427
450
@pytest .mark .skip (reason = "takes a long time (~5 mins) since testing against small clusters" )
428
451
def test_index_json_s3_large_file (client ):
452
+ index = f"test_index_json_s3_large_file_{ _get_unique_suffix ()} "
429
453
path = "s3://irs-form-990/index_2011.json"
430
454
response = wr .opensearch .index_json (
431
- client , index = "test_index_json_s3_large_file" , path = path , json_path = "Filings2011" , id_keys = ["EIN" ], bulk_size = 20
455
+ client , index = index , path = path , json_path = "Filings2011" , id_keys = ["EIN" ], bulk_size = 20
432
456
)
433
457
assert response .get ("success" , 0 ) > 0
458
+ wr .opensearch .delete_index (client , index )
434
459
435
460
461
+ @pytest .mark .skip (reason = "Temporary skip until collection cleanup issue is resolved" )
436
462
def test_opensearch_serverless_create_collection (opensearch_serverless_client ) -> None :
437
- collection_name : str = f"col-{ str ( uuid . uuid4 ())[: 8 ] } "
438
- client : boto3 . client = boto3 .client (service_name = "opensearchserverless" )
463
+ collection_name : str = f"col-{ _get_unique_suffix () } "
464
+ client = boto3 .client (service_name = "opensearchserverless" )
439
465
440
466
collection : Dict [str , Any ] = wr .opensearch .create_collection (
441
467
name = collection_name ,
0 commit comments