Skip to content

Commit f2ef8a7

Browse files
Lingling PengLingling Peng
authored andcommitted
update teardown method
1 parent d67c381 commit f2ef8a7

File tree

1 file changed

+71
-62
lines changed

1 file changed

+71
-62
lines changed

tests/unit/synapseclient/extensions/unit_test_curator.py

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@
5454
from synapseclient.models.mixins.json_schema import JSONSchemaVersionInfo
5555

5656

57-
@pytest.fixture(name="test_directory", scope="function")
58-
def fixture_test_directory(tmp_path) -> str:
59-
"""Returns a directory for creating test files in.
60-
61-
pytest automatically handles cleanup of the temporary directory.
62-
"""
63-
return str(tmp_path)
64-
return str(tmp_path)
65-
66-
6757
class TestCreateFileBasedMetadataTask(unittest.TestCase):
6858
"""Test cases for create_file_based_metadata_task function."""
6959

@@ -1743,6 +1733,23 @@ def setUp(self):
17431733
"schema_files",
17441734
"data_models/example.model.csv",
17451735
)
1736+
self.test_directory = tempfile.mkdtemp()
1737+
1738+
def tearDown(self):
1739+
"""Clean up test directory and any leaked files after each test."""
1740+
# Clean up temp directory
1741+
if hasattr(self, "test_directory") and os.path.exists(self.test_directory):
1742+
shutil.rmtree(self.test_directory)
1743+
1744+
# Clean up any files created alongside the source CSV
1745+
# (e.g., when output_jsonld=None is used)
1746+
if hasattr(self, "test_schema_path"):
1747+
schema_dir = os.path.dirname(self.test_schema_path)
1748+
for file in os.listdir(schema_dir):
1749+
if file.startswith("example.model") and file.endswith(".jsonld"):
1750+
leaked_file = os.path.join(schema_dir, file)
1751+
if os.path.exists(leaked_file):
1752+
os.remove(leaked_file)
17461753

17471754
def test_generate_jsonld_with_default_output_path(self):
17481755
"""Test generate_jsonld with default output path (None)."""
@@ -1768,10 +1775,10 @@ def test_generate_jsonld_with_default_output_path(self):
17681775
if os.path.exists(expected_output):
17691776
os.remove(expected_output)
17701777

1771-
def test_generate_jsonld_with_custom_output_path(self, test_directory):
1778+
def test_generate_jsonld_with_custom_output_path(self):
17721779
"""Test generate_jsonld with custom output path."""
17731780
# GIVEN a CSV schema file and a custom output path
1774-
custom_output = os.path.join(test_directory, "custom_output.jsonld")
1781+
custom_output = os.path.join(self.test_directory, "custom_output.jsonld")
17751782

17761783
# WHEN I generate JSONLD
17771784
result = generate_jsonld(
@@ -1794,10 +1801,10 @@ def test_generate_jsonld_with_custom_output_path(self, test_directory):
17941801
file_content = json.load(f)
17951802
assert file_content == result
17961803

1797-
def test_generate_jsonld_with_display_label(self, test_directory):
1804+
def test_generate_jsonld_with_display_label(self):
17981805
"""Test generate_jsonld with display_label format."""
17991806
# GIVEN a CSV schema file
1800-
output_path = os.path.join(test_directory, "display_label.jsonld")
1807+
output_path = os.path.join(self.test_directory, "display_label.jsonld")
18011808

18021809
# WHEN I generate JSONLD with display_label
18031810
result = generate_jsonld(
@@ -1819,10 +1826,10 @@ def test_generate_jsonld_with_display_label(self, test_directory):
18191826
labels = [entry.get("rdfs:label") for entry in graph if "rdfs:label" in entry]
18201827
assert len(labels) > 0
18211828

1822-
def test_generate_jsonld_validates_data_model(self, test_directory):
1829+
def test_generate_jsonld_validates_data_model(self):
18231830
"""Test that generate_jsonld performs validation checks."""
18241831
# GIVEN a CSV schema file
1825-
output_path = os.path.join(test_directory, "validated.jsonld")
1832+
output_path = os.path.join(self.test_directory, "validated.jsonld")
18261833

18271834
# WHEN I generate JSONLD
18281835
result = generate_jsonld(
@@ -1837,10 +1844,10 @@ def test_generate_jsonld_validates_data_model(self, test_directory):
18371844
assert isinstance(result, dict)
18381845
assert os.path.exists(output_path)
18391846

1840-
def test_generate_jsonld_contains_expected_components(self, test_directory):
1847+
def test_generate_jsonld_contains_expected_components(self):
18411848
"""Test that generated JSONLD contains expected components from CSV."""
18421849
# GIVEN a CSV schema file with known components
1843-
output_path = os.path.join(test_directory, "components.jsonld")
1850+
output_path = os.path.join(self.test_directory, "components.jsonld")
18441851

18451852
# WHEN I generate JSONLD
18461853
result = generate_jsonld(
@@ -1859,10 +1866,10 @@ def test_generate_jsonld_contains_expected_components(self, test_directory):
18591866
for component in expected_components:
18601867
assert component in labels, f"Expected component {component} not found"
18611868

1862-
def test_generate_jsonld_preserves_relationships(self, test_directory):
1869+
def test_generate_jsonld_preserves_relationships(self):
18631870
"""Test that JSONLD preserves relationships from the CSV."""
18641871
# GIVEN a CSV schema file with relationships
1865-
output_path = os.path.join(test_directory, "relationships.jsonld")
1872+
output_path = os.path.join(self.test_directory, "relationships.jsonld")
18661873

18671874
# WHEN I generate JSONLD
18681875
result = generate_jsonld(
@@ -1885,10 +1892,10 @@ def test_generate_jsonld_preserves_relationships(self, test_directory):
18851892
# Patient should have requiresDependency field
18861893
assert "sms:requiresDependency" in patient, "Patient should have dependencies"
18871894

1888-
def test_generate_jsonld_includes_validation_rules(self, test_directory):
1895+
def test_generate_jsonld_includes_validation_rules(self):
18891896
"""Test that JSONLD includes validation rules from CSV."""
18901897
# GIVEN a CSV schema file with validation rules
1891-
output_path = os.path.join(test_directory, "validation_rules.jsonld")
1898+
output_path = os.path.join(self.test_directory, "validation_rules.jsonld")
18921899

18931900
# WHEN I generate JSONLD
18941901
result = generate_jsonld(
@@ -1927,14 +1934,32 @@ def setUp(self):
19271934
"schema_files",
19281935
"data_models/example.model.csv",
19291936
)
1930-
1931-
def test_generate_jsonschema_from_csv(self, test_directory):
1937+
self.test_directory = tempfile.mkdtemp()
1938+
1939+
def tearDown(self):
1940+
"""Clean up test directory and any leaked files after each test."""
1941+
# Clean up temp directory
1942+
if hasattr(self, "test_directory") and os.path.exists(self.test_directory):
1943+
shutil.rmtree(self.test_directory)
1944+
1945+
# Clean up any schema files created alongside the source CSV
1946+
if hasattr(self, "test_schema_path"):
1947+
schema_dir = os.path.dirname(self.test_schema_path)
1948+
for file in os.listdir(schema_dir):
1949+
if file.endswith(".schema.json") or (
1950+
file.startswith("example.model") and file.endswith(".jsonld")
1951+
):
1952+
leaked_file = os.path.join(schema_dir, file)
1953+
if os.path.exists(leaked_file):
1954+
os.remove(leaked_file)
1955+
1956+
def test_generate_jsonschema_from_csv(self):
19321957
"""Test generate_jsonschema from CSV file."""
19331958
# GIVEN a CSV schema file
19341959
# WHEN I generate JSON schemas
19351960
schemas, file_paths = generate_jsonschema(
19361961
data_model_source=self.test_schema_path,
1937-
output_directory=test_directory,
1962+
output_directory=self.test_directory,
19381963
data_type=None,
19391964
data_model_labels="class_label",
19401965
synapse_client=self.syn,
@@ -1956,10 +1981,10 @@ def test_generate_jsonschema_from_csv(self, test_directory):
19561981
assert "$schema" in schema
19571982
assert "properties" in schema
19581983

1959-
def test_generate_jsonschema_from_jsonld(self, test_directory):
1984+
def test_generate_jsonschema_from_jsonld(self):
19601985
"""Test generate_jsonschema from JSONLD file."""
19611986
# GIVEN a JSONLD file (first generate it from CSV)
1962-
jsonld_path = os.path.join(test_directory, "test.jsonld")
1987+
jsonld_path = os.path.join(self.test_directory, "test.jsonld")
19631988
generate_jsonld(
19641989
schema=self.test_schema_path,
19651990
data_model_labels="class_label",
@@ -1970,7 +1995,7 @@ def test_generate_jsonschema_from_jsonld(self, test_directory):
19701995
# WHEN I generate JSON schemas from the JSONLD
19711996
schemas, file_paths = generate_jsonschema(
19721997
data_model_source=jsonld_path,
1973-
output_directory=test_directory,
1998+
output_directory=self.test_directory,
19741999
data_type=None,
19752000
data_model_labels="class_label",
19762001
synapse_client=self.syn,
@@ -1984,16 +2009,15 @@ def test_generate_jsonschema_from_jsonld(self, test_directory):
19842009
for file_path in file_paths:
19852010
assert os.path.exists(file_path)
19862011

1987-
def test_generate_jsonschema_specific_components(self, tmp_path):
2012+
def test_generate_jsonschema_specific_components(self):
19882013
"""Test generate_jsonschema for specific components only."""
19892014
# GIVEN a CSV schema file and specific components
1990-
test_directory = str(tmp_path) # pytest automatically handles cleanup
19912015
target_components = ["Patient", "Biospecimen"]
19922016

19932017
# WHEN I generate JSON schemas for specific components
19942018
schemas, file_paths = generate_jsonschema(
19952019
data_model_source=self.test_schema_path,
1996-
output_directory=test_directory,
2020+
output_directory=self.test_directory,
19972021
data_type=target_components,
19982022
data_model_labels="class_label",
19992023
synapse_client=self.syn,
@@ -2008,19 +2032,13 @@ def test_generate_jsonschema_specific_components(self, tmp_path):
20082032
matching_files = [fp for fp in file_paths if component in fp]
20092033
assert len(matching_files) > 0, f"Expected file for {component}"
20102034

2011-
# AND all files should be in the temp directory
2012-
for file_path in file_paths:
2013-
assert file_path.startswith(
2014-
test_directory
2015-
), f"File {file_path} was not created in temp directory {test_directory}"
2016-
2017-
def test_generate_jsonschema_with_display_label(self, test_directory):
2035+
def test_generate_jsonschema_with_display_label(self):
20182036
"""Test generate_jsonschema with display_label format."""
20192037
# GIVEN a CSV schema file
20202038
# WHEN I generate schemas with display_label
20212039
schemas, file_paths = generate_jsonschema(
20222040
data_model_source=self.test_schema_path,
2023-
output_directory=test_directory,
2041+
output_directory=self.test_directory,
20242042
data_type=["Patient"],
20252043
data_model_labels="display_label",
20262044
synapse_client=self.syn,
@@ -2034,13 +2052,13 @@ def test_generate_jsonschema_with_display_label(self, test_directory):
20342052
patient_schema = schemas[0]
20352053
assert "properties" in patient_schema
20362054

2037-
def test_generate_jsonschema_validates_required_fields(self, test_directory):
2055+
def test_generate_jsonschema_validates_required_fields(self):
20382056
"""Test that generated schemas include required field constraints."""
20392057
# GIVEN a CSV schema file
20402058
# WHEN I generate schemas
20412059
schemas, _ = generate_jsonschema(
20422060
data_model_source=self.test_schema_path,
2043-
output_directory=test_directory,
2061+
output_directory=self.test_directory,
20442062
data_type=["Patient"],
20452063
data_model_labels="class_label",
20462064
synapse_client=self.syn,
@@ -2057,13 +2075,13 @@ def test_generate_jsonschema_validates_required_fields(self, test_directory):
20572075
# Component is required for Patient
20582076
assert "Component" in required_fields
20592077

2060-
def test_generate_jsonschema_includes_enums(self, test_directory):
2078+
def test_generate_jsonschema_includes_enums(self):
20612079
"""Test that generated schemas include enum constraints."""
20622080
# GIVEN a CSV schema file with enum values
20632081
# WHEN I generate schemas
20642082
schemas, _ = generate_jsonschema(
20652083
data_model_source=self.test_schema_path,
2066-
output_directory=test_directory,
2084+
output_directory=self.test_directory,
20672085
data_type=["Patient"],
20682086
data_model_labels="class_label",
20692087
synapse_client=self.syn,
@@ -2091,13 +2109,13 @@ def test_generate_jsonschema_includes_enums(self, test_directory):
20912109

20922110
assert has_enum, "Expected enum constraint for Sex field"
20932111

2094-
def test_generate_jsonschema_includes_validation_rules(self, test_directory):
2112+
def test_generate_jsonschema_includes_validation_rules(self):
20952113
"""Test that schemas include validation rules like inRange, regex, etc."""
20962114
# GIVEN a CSV with validation rules
20972115
# WHEN I generate schemas for MockComponent (has many validation rules)
20982116
schemas, _ = generate_jsonschema(
20992117
data_model_source=self.test_schema_path,
2100-
output_directory=test_directory,
2118+
output_directory=self.test_directory,
21012119
data_type=["MockComponent"],
21022120
data_model_labels="class_label",
21032121
synapse_client=self.syn,
@@ -2135,16 +2153,14 @@ def test_generate_jsonschema_includes_validation_rules(self, test_directory):
21352153
break
21362154
assert has_uri_format, "Expected URI format for URL field"
21372155

2138-
def test_generate_jsonschema_includes_conditional_dependencies(
2139-
self, test_directory
2140-
):
2156+
def test_generate_jsonschema_includes_conditional_dependencies(self):
21412157
"""Test that schemas include conditional dependencies (if/then)."""
21422158
# GIVEN a CSV with conditional dependencies
21432159
# Patient -> Diagnosis=Cancer -> requires CancerType and FamilyHistory
21442160
# WHEN I generate schemas
21452161
schemas, _ = generate_jsonschema(
21462162
data_model_source=self.test_schema_path,
2147-
output_directory=test_directory,
2163+
output_directory=self.test_directory,
21482164
data_type=["Patient"],
21492165
data_model_labels="class_label",
21502166
synapse_client=self.syn,
@@ -2178,13 +2194,13 @@ def test_generate_jsonschema_includes_conditional_dependencies(
21782194

21792195
assert has_cancer_dependency, "Expected conditional dependency for Cancer"
21802196

2181-
def test_generate_jsonschema_handles_array_types(self, test_directory):
2197+
def test_generate_jsonschema_handles_array_types(self):
21822198
"""Test that schemas handle array/list types correctly."""
21832199
# GIVEN a CSV with list validation rules
21842200
# WHEN I generate schemas for MockComponent (has list rules)
21852201
schemas, _ = generate_jsonschema(
21862202
data_model_source=self.test_schema_path,
2187-
output_directory=test_directory,
2203+
output_directory=self.test_directory,
21882204
data_type=["MockComponent"],
21892205
data_model_labels="class_label",
21902206
synapse_client=self.syn,
@@ -2209,13 +2225,13 @@ def test_generate_jsonschema_handles_array_types(self, test_directory):
22092225

22102226
assert has_array_type, "Expected array type for list field"
22112227

2212-
def test_generate_jsonschema_file_content_matches_schema_dict(self, test_directory):
2228+
def test_generate_jsonschema_file_content_matches_schema_dict(self):
22132229
"""Test that saved files match the returned schema dictionaries."""
22142230
# GIVEN a CSV schema file
22152231
# WHEN I generate schemas
22162232
schemas, file_paths = generate_jsonschema(
22172233
data_model_source=self.test_schema_path,
2218-
output_directory=test_directory,
2234+
output_directory=self.test_directory,
22192235
data_type=["Patient"],
22202236
data_model_labels="class_label",
22212237
synapse_client=self.syn,
@@ -2227,15 +2243,14 @@ def test_generate_jsonschema_file_content_matches_schema_dict(self, test_directo
22272243
file_content = json.load(f)
22282244
assert file_content == schema
22292245

2230-
def test_generate_jsonschema_creates_valid_json_schema_structure(self, tmp_path):
2246+
def test_generate_jsonschema_creates_valid_json_schema_structure(self):
22312247
"""Test that generated schemas follow JSON Schema specification."""
22322248
# GIVEN a CSV schema file and a temporary directory
2233-
test_directory = str(tmp_path) # pytest automatically handles cleanup
22342249

22352250
# WHEN I generate schemas
22362251
schemas, file_paths = generate_jsonschema(
22372252
data_model_source=self.test_schema_path,
2238-
output_directory=test_directory,
2253+
output_directory=self.test_directory,
22392254
data_type=None,
22402255
data_model_labels="class_label",
22412256
synapse_client=self.syn,
@@ -2258,9 +2273,3 @@ def test_generate_jsonschema_creates_valid_json_schema_structure(self, tmp_path)
22582273
assert isinstance(
22592274
prop_value, dict
22602275
), f"Property {prop_name} is not a dict"
2261-
2262-
# AND all files should be in the temp directory
2263-
for file_path in file_paths:
2264-
assert file_path.startswith(
2265-
test_directory
2266-
), f"File {file_path} was not created in temp directory {test_directory}"

0 commit comments

Comments
 (0)