Skip to content

Commit 1f4e140

Browse files
authored
refactor(smoke): use pytest.raises instead of custom solution (#12333)
1 parent 8ac35fa commit 1f4e140

File tree

1 file changed

+34
-111
lines changed

1 file changed

+34
-111
lines changed

smoke-test/tests/structured_properties/test_structured_properties.py

Lines changed: 34 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import logging
22
import os
3+
import re
34
import tempfile
45
from random import randint
56
from typing import Iterable, List, Optional, Union
67

8+
import pydantic
79
import pytest
8-
9-
# import tenacity
1010
from datahub.api.entities.dataset.dataset import Dataset
1111
from datahub.api.entities.structuredproperties.structuredproperties import (
1212
StructuredProperties,
1313
)
14+
from datahub.configuration.common import GraphError, OperationalError
1415
from datahub.emitter.mce_builder import make_dataset_urn, make_schema_field_urn
1516
from datahub.emitter.mcp import MetadataChangeProposalWrapper
1617
from datahub.ingestion.graph.client import DataHubGraph
@@ -173,10 +174,6 @@ def to_es_filter_name(
173174
return f"structuredProperties.{qualified_name}"
174175

175176

176-
# @tenacity.retry(
177-
# stop=tenacity.stop_after_attempt(sleep_times),
178-
# wait=tenacity.wait_fixed(sleep_sec),
179-
# )
180177
def test_structured_property_string(ingest_cleanup_data, graph_client):
181178
property_name = f"retention{randint(10, 10000)}Policy"
182179

@@ -186,24 +183,13 @@ def test_structured_property_string(ingest_cleanup_data, graph_client):
186183
dataset_urns[0], property_name, ["30d"], graph=graph_client
187184
)
188185

189-
try:
186+
with pytest.raises(OperationalError):
187+
# Cannot add a number to a string property.
190188
attach_property_to_entity(
191189
dataset_urns[0], property_name, 200030, graph=graph_client
192190
)
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
201191

202192

203-
# @tenacity.retry(
204-
# stop=tenacity.stop_after_attempt(sleep_times),
205-
# wait=tenacity.wait_fixed(sleep_sec),
206-
# )
207193
def test_structured_property_double(ingest_cleanup_data, graph_client):
208194
property_name = f"expiryTime{randint(10, 10000)}"
209195

@@ -213,35 +199,19 @@ def test_structured_property_double(ingest_cleanup_data, graph_client):
213199
dataset_urns[0], property_name, 2000034, graph=graph_client
214200
)
215201

216-
try:
202+
with pytest.raises(OperationalError):
203+
# Cannot add a string to a number property.
217204
attach_property_to_entity(
218205
dataset_urns[0], property_name, "30 days", graph=graph_client
219206
)
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
228207

229-
try:
208+
with pytest.raises(OperationalError):
209+
# Cannot add a list to a number property.
230210
attach_property_to_entity(
231211
dataset_urns[0], property_name, [2000034, 2000035], graph=graph_client
232212
)
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+
245215
def test_structured_property_double_multiple(ingest_cleanup_data, graph_client):
246216
property_name = f"versions{randint(10, 10000)}"
247217

@@ -254,10 +224,6 @@ def test_structured_property_double_multiple(ingest_cleanup_data, graph_client):
254224
)
255225

256226

257-
# @tenacity.retry(
258-
# stop=tenacity.stop_after_attempt(sleep_times),
259-
# wait=tenacity.wait_fixed(sleep_sec),
260-
# )
261227
def test_structured_property_string_allowed_values(ingest_cleanup_data, graph_client):
262228
property_name = f"enumProperty{randint(10, 10000)}"
263229

@@ -276,18 +242,13 @@ def test_structured_property_string_allowed_values(ingest_cleanup_data, graph_cl
276242
dataset_urns[0], property_name, ["foo", "bar"], graph=graph_client
277243
)
278244

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.
280249
attach_property_to_entity(
281250
dataset_urns[0], property_name, ["foo", "baz"], graph=graph_client
282251
)
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
291252

292253

293254
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
304265
],
305266
)
306267

307-
try:
268+
with pytest.raises(OperationalError):
269+
# Cannot change cardinality from MULTIPLE to SINGLE.
308270
create_property_definition(
309271
property_name,
310272
graph_client,
@@ -315,20 +277,8 @@ def test_structured_property_definition_evolution(ingest_cleanup_data, graph_cli
315277
PropertyValueClass(value="bar"),
316278
],
317279
)
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
326280

327281

328-
# @tenacity.retry(
329-
# stop=tenacity.stop_after_attempt(sleep_times),
330-
# wait=tenacity.wait_fixed(sleep_sec),
331-
# )
332282
def test_structured_property_schema_field(ingest_cleanup_data, graph_client):
333283
property_name = f"deprecationDate{randint(10, 10000)}"
334284

@@ -354,38 +304,28 @@ def test_structured_property_schema_field(ingest_cleanup_data, graph_client):
354304
graph=graph_client,
355305
) == ["2020-10-01"]
356306

357-
try:
307+
with pytest.raises(OperationalError):
308+
# Cannot add a number to a date property.
358309
attach_property_to_entity(
359310
schema_field_urns[0],
360311
property_name,
361312
200030,
362313
graph=graph_client,
363314
namespace="io.datahubproject.test",
364315
)
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
371316

372317

373318
def test_structured_properties_yaml_load_with_bad_entity_type(
374319
ingest_cleanup_data, graph_client
375320
):
376-
try:
321+
with pytest.raises(
322+
pydantic.ValidationError,
323+
match="urn:li:entityType:dataset is not a valid entity type urn",
324+
):
377325
StructuredProperties.create(
378326
"tests/structured_properties/bad_entity_type.yaml",
379327
graph=graph_client,
380328
)
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
389329

390330

391331
def test_dataset_yaml_loader(ingest_cleanup_data, graph_client):
@@ -617,35 +557,24 @@ def test_dataset_structured_property_soft_delete_validation(
617557
graph_client.soft_delete_entity(urn=property_urn)
618558

619559
# 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+
):
621564
create_property_definition(
622565
property_name=property_name,
623566
graph=graph_client,
624567
value_type=value_type,
625568
cardinality="SINGLE",
626569
)
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
635570

636571
# 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+
):
638575
attach_property_to_entity(
639576
dataset_urns[0], property_name, "test string", graph=graph_client
640577
)
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
649578

650579

651580
def test_dataset_structured_property_soft_delete_read_mutation(
@@ -685,7 +614,7 @@ def test_dataset_structured_property_soft_delete_read_mutation(
685614

686615

687616
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
689618
):
690619
# Create a test structured property
691620
dataset_property_name = f"softDeleteSearchFilter{randint(10, 10000)}"
@@ -722,7 +651,9 @@ def test_dataset_structured_property_soft_delete_search_filter_validation(
722651
wait_for_writes_to_sync()
723652

724653
# 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+
):
726657
list(
727658
graph_client.get_urns_by_filter(
728659
extraFilters=[
@@ -734,14 +665,6 @@ def test_dataset_structured_property_soft_delete_search_filter_validation(
734665
]
735666
)
736667
)
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
745668

746669

747670
def test_dataset_structured_property_delete(ingest_cleanup_data, graph_client, caplog):

0 commit comments

Comments
 (0)