1
1
import logging
2
2
import os
3
+ import re
3
4
import tempfile
4
5
from random import randint
5
6
from typing import Iterable , List , Optional , Union
6
7
8
+ import pydantic
7
9
import pytest
8
-
9
- # import tenacity
10
10
from datahub .api .entities .dataset .dataset import Dataset
11
11
from datahub .api .entities .structuredproperties .structuredproperties import (
12
12
StructuredProperties ,
13
13
)
14
+ from datahub .configuration .common import GraphError , OperationalError
14
15
from datahub .emitter .mce_builder import make_dataset_urn , make_schema_field_urn
15
16
from datahub .emitter .mcp import MetadataChangeProposalWrapper
16
17
from datahub .ingestion .graph .client import DataHubGraph
@@ -173,10 +174,6 @@ def to_es_filter_name(
173
174
return f"structuredProperties.{ qualified_name } "
174
175
175
176
176
- # @tenacity.retry(
177
- # stop=tenacity.stop_after_attempt(sleep_times),
178
- # wait=tenacity.wait_fixed(sleep_sec),
179
- # )
180
177
def test_structured_property_string (ingest_cleanup_data , graph_client ):
181
178
property_name = f"retention{ randint (10 , 10000 )} Policy"
182
179
@@ -186,24 +183,13 @@ def test_structured_property_string(ingest_cleanup_data, graph_client):
186
183
dataset_urns [0 ], property_name , ["30d" ], graph = graph_client
187
184
)
188
185
189
- try :
186
+ with pytest .raises (OperationalError ):
187
+ # Cannot add a number to a string property.
190
188
attach_property_to_entity (
191
189
dataset_urns [0 ], property_name , 200030 , graph = graph_client
192
190
)
193
- raise AssertionError (
194
- "Should not be able to attach a number to a string property"
195
- )
196
- except Exception as e :
197
- if not isinstance (e , AssertionError ):
198
- pass
199
- else :
200
- raise e
201
191
202
192
203
- # @tenacity.retry(
204
- # stop=tenacity.stop_after_attempt(sleep_times),
205
- # wait=tenacity.wait_fixed(sleep_sec),
206
- # )
207
193
def test_structured_property_double (ingest_cleanup_data , graph_client ):
208
194
property_name = f"expiryTime{ randint (10 , 10000 )} "
209
195
@@ -213,35 +199,19 @@ def test_structured_property_double(ingest_cleanup_data, graph_client):
213
199
dataset_urns [0 ], property_name , 2000034 , graph = graph_client
214
200
)
215
201
216
- try :
202
+ with pytest .raises (OperationalError ):
203
+ # Cannot add a string to a number property.
217
204
attach_property_to_entity (
218
205
dataset_urns [0 ], property_name , "30 days" , graph = graph_client
219
206
)
220
- raise AssertionError (
221
- "Should not be able to attach a string to a number property"
222
- )
223
- except Exception as e :
224
- if not isinstance (e , AssertionError ):
225
- pass
226
- else :
227
- raise e
228
207
229
- try :
208
+ with pytest .raises (OperationalError ):
209
+ # Cannot add a list to a number property.
230
210
attach_property_to_entity (
231
211
dataset_urns [0 ], property_name , [2000034 , 2000035 ], graph = graph_client
232
212
)
233
- raise AssertionError ("Should not be able to attach a list to a number property" )
234
- except Exception as e :
235
- if not isinstance (e , AssertionError ):
236
- pass
237
- else :
238
- raise e
239
-
240
-
241
- # @tenacity.retry(
242
- # stop=tenacity.stop_after_attempt(sleep_times),
243
- # wait=tenacity.wait_fixed(sleep_sec),
244
- # )
213
+
214
+
245
215
def test_structured_property_double_multiple (ingest_cleanup_data , graph_client ):
246
216
property_name = f"versions{ randint (10 , 10000 )} "
247
217
@@ -254,10 +224,6 @@ def test_structured_property_double_multiple(ingest_cleanup_data, graph_client):
254
224
)
255
225
256
226
257
- # @tenacity.retry(
258
- # stop=tenacity.stop_after_attempt(sleep_times),
259
- # wait=tenacity.wait_fixed(sleep_sec),
260
- # )
261
227
def test_structured_property_string_allowed_values (ingest_cleanup_data , graph_client ):
262
228
property_name = f"enumProperty{ randint (10 , 10000 )} "
263
229
@@ -276,18 +242,13 @@ def test_structured_property_string_allowed_values(ingest_cleanup_data, graph_cl
276
242
dataset_urns [0 ], property_name , ["foo" , "bar" ], graph = graph_client
277
243
)
278
244
279
- try :
245
+ with pytest .raises (
246
+ OperationalError , match = re .escape ("value: {string=baz} should be one of [" )
247
+ ):
248
+ # Cannot add a value that isn't in the allowed values list.
280
249
attach_property_to_entity (
281
250
dataset_urns [0 ], property_name , ["foo" , "baz" ], graph = graph_client
282
251
)
283
- raise AssertionError (
284
- "Should not be able to attach a value not in allowed values"
285
- )
286
- except Exception as e :
287
- if "value: {string=baz} should be one of [" in str (e ):
288
- pass
289
- else :
290
- raise e
291
252
292
253
293
254
def test_structured_property_definition_evolution (ingest_cleanup_data , graph_client ):
@@ -304,7 +265,8 @@ def test_structured_property_definition_evolution(ingest_cleanup_data, graph_cli
304
265
],
305
266
)
306
267
307
- try :
268
+ with pytest .raises (OperationalError ):
269
+ # Cannot change cardinality from MULTIPLE to SINGLE.
308
270
create_property_definition (
309
271
property_name ,
310
272
graph_client ,
@@ -315,20 +277,8 @@ def test_structured_property_definition_evolution(ingest_cleanup_data, graph_cli
315
277
PropertyValueClass (value = "bar" ),
316
278
],
317
279
)
318
- raise AssertionError (
319
- "Should not be able to change cardinality from MULTIPLE to SINGLE"
320
- )
321
- except Exception as e :
322
- if isinstance (e , AssertionError ):
323
- raise e
324
- else :
325
- pass
326
280
327
281
328
- # @tenacity.retry(
329
- # stop=tenacity.stop_after_attempt(sleep_times),
330
- # wait=tenacity.wait_fixed(sleep_sec),
331
- # )
332
282
def test_structured_property_schema_field (ingest_cleanup_data , graph_client ):
333
283
property_name = f"deprecationDate{ randint (10 , 10000 )} "
334
284
@@ -354,38 +304,28 @@ def test_structured_property_schema_field(ingest_cleanup_data, graph_client):
354
304
graph = graph_client ,
355
305
) == ["2020-10-01" ]
356
306
357
- try :
307
+ with pytest .raises (OperationalError ):
308
+ # Cannot add a number to a date property.
358
309
attach_property_to_entity (
359
310
schema_field_urns [0 ],
360
311
property_name ,
361
312
200030 ,
362
313
graph = graph_client ,
363
314
namespace = "io.datahubproject.test" ,
364
315
)
365
- raise AssertionError ("Should not be able to attach a number to a DATE property" )
366
- except Exception as e :
367
- if not isinstance (e , AssertionError ):
368
- pass
369
- else :
370
- raise e
371
316
372
317
373
318
def test_structured_properties_yaml_load_with_bad_entity_type (
374
319
ingest_cleanup_data , graph_client
375
320
):
376
- try :
321
+ with pytest .raises (
322
+ pydantic .ValidationError ,
323
+ match = "urn:li:entityType:dataset is not a valid entity type urn" ,
324
+ ):
377
325
StructuredProperties .create (
378
326
"tests/structured_properties/bad_entity_type.yaml" ,
379
327
graph = graph_client ,
380
328
)
381
- raise AssertionError (
382
- "Should not be able to create structured properties with bad entity type"
383
- )
384
- except Exception as e :
385
- if "urn:li:entityType:dataset is not a valid entity type urn" in str (e ):
386
- pass
387
- else :
388
- raise e
389
329
390
330
391
331
def test_dataset_yaml_loader (ingest_cleanup_data , graph_client ):
@@ -617,35 +557,24 @@ def test_dataset_structured_property_soft_delete_validation(
617
557
graph_client .soft_delete_entity (urn = property_urn )
618
558
619
559
# Attempt to modify soft deleted definition
620
- try :
560
+ with pytest .raises (
561
+ OperationalError ,
562
+ match = "Cannot mutate a soft deleted Structured Property Definition" ,
563
+ ):
621
564
create_property_definition (
622
565
property_name = property_name ,
623
566
graph = graph_client ,
624
567
value_type = value_type ,
625
568
cardinality = "SINGLE" ,
626
569
)
627
- raise AssertionError (
628
- "Should not be able to modify soft deleted structured property"
629
- )
630
- except Exception as e :
631
- if "Cannot mutate a soft deleted Structured Property Definition" in str (e ):
632
- pass
633
- else :
634
- raise e
635
570
636
571
# Attempt to add soft deleted structured property to entity
637
- try :
572
+ with pytest .raises (
573
+ OperationalError , match = "Cannot apply a soft deleted Structured Property value"
574
+ ):
638
575
attach_property_to_entity (
639
576
dataset_urns [0 ], property_name , "test string" , graph = graph_client
640
577
)
641
- raise AssertionError (
642
- "Should not be able to apply a soft deleted structured property to another entity"
643
- )
644
- except Exception as e :
645
- if "Cannot apply a soft deleted Structured Property value" in str (e ):
646
- pass
647
- else :
648
- raise e
649
578
650
579
651
580
def test_dataset_structured_property_soft_delete_read_mutation (
@@ -685,7 +614,7 @@ def test_dataset_structured_property_soft_delete_read_mutation(
685
614
686
615
687
616
def test_dataset_structured_property_soft_delete_search_filter_validation (
688
- ingest_cleanup_data , graph_client , caplog
617
+ ingest_cleanup_data , graph_client : DataHubGraph , caplog : pytest . LogCaptureFixture
689
618
):
690
619
# Create a test structured property
691
620
dataset_property_name = f"softDeleteSearchFilter{ randint (10 , 10000 )} "
@@ -722,7 +651,9 @@ def test_dataset_structured_property_soft_delete_search_filter_validation(
722
651
wait_for_writes_to_sync ()
723
652
724
653
# Perform search, make sure it validates filter and rejects as invalid request
725
- try :
654
+ with pytest .raises (
655
+ GraphError , match = "Cannot filter on deleted Structured Property"
656
+ ):
726
657
list (
727
658
graph_client .get_urns_by_filter (
728
659
extraFilters = [
@@ -734,14 +665,6 @@ def test_dataset_structured_property_soft_delete_search_filter_validation(
734
665
]
735
666
)
736
667
)
737
- raise AssertionError (
738
- "Should not be able to filter by soft deleted structured property"
739
- )
740
- except Exception as e :
741
- if "Cannot filter on deleted Structured Property" in str (e ):
742
- pass
743
- else :
744
- raise e
745
668
746
669
747
670
def test_dataset_structured_property_delete (ingest_cleanup_data , graph_client , caplog ):
0 commit comments