Skip to content

Commit ce1cafa

Browse files
authored
[SYNPY-1607] Enum conversion from String (#1210)
* Handle for enum conversion of `ColumnType` and `FacetType`
1 parent 7d6a06a commit ce1cafa

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

synapseclient/models/table_components.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class JsonSubColumn:
497497
def to_synapse_request(self) -> Dict[str, Any]:
498498
"""Converts the Column object into a dictionary that can be passed into the
499499
REST API."""
500+
if self.column_type and isinstance(self.column_type, str):
501+
self.column_type = ColumnType(self.column_type)
502+
503+
if self.facet_type and isinstance(self.facet_type, str):
504+
self.facet_type = FacetType(self.facet_type)
505+
500506
result = {
501507
"name": self.name,
502508
"columnType": self.column_type.value if self.column_type else None,
@@ -601,6 +607,11 @@ def _set_last_persistent_instance(self) -> None:
601607
def to_synapse_request(self) -> Dict[str, Any]:
602608
"""Converts the Column object into a dictionary that can be passed into the
603609
REST API."""
610+
if self.column_type and isinstance(self.column_type, str):
611+
self.column_type = ColumnType(self.column_type)
612+
613+
if self.facet_type and isinstance(self.facet_type, str):
614+
self.facet_type = FacetType(self.facet_type)
604615
result = {
605616
"concreteType": concrete_types.COLUMN_MODEL,
606617
"name": self.name,

tests/integration/synapseclient/models/async/test_table_async.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,46 @@ async def test_table_creation_with_data_sources(
275275
results["column_string"], csv_data["column_string"]
276276
)
277277

278+
async def test_create_table_with_string_column(
279+
self, project_model: Project
280+
) -> None:
281+
"""Test creating tables with string column configurations."""
282+
# GIVEN a table with columns
283+
table_name = str(uuid.uuid4())
284+
table_description = "Test table with columns"
285+
table = Table(
286+
name=table_name,
287+
parent_id=project_model.id,
288+
description=table_description,
289+
columns=[
290+
Column(name="test_column", column_type="STRING"),
291+
Column(name="test_column2", column_type="INTEGER"),
292+
],
293+
)
294+
295+
# WHEN I store the table
296+
table = await table.store_async(synapse_client=self.syn)
297+
self.schedule_for_cleanup(table.id)
298+
299+
# THEN the table should be created
300+
assert table.id is not None
301+
302+
# AND I can retrieve that table from Synapse
303+
new_table_instance = await Table(id=table.id).get_async(synapse_client=self.syn)
304+
assert new_table_instance is not None
305+
assert new_table_instance.name == table_name
306+
assert new_table_instance.id == table.id
307+
assert new_table_instance.description == table_description
308+
assert len(new_table_instance.columns) == 2
309+
assert new_table_instance.columns["test_column"].name == "test_column"
310+
assert (
311+
new_table_instance.columns["test_column"].column_type == ColumnType.STRING
312+
)
313+
assert new_table_instance.columns["test_column2"].name == "test_column2"
314+
assert (
315+
new_table_instance.columns["test_column2"].column_type == ColumnType.INTEGER
316+
)
317+
278318

279319
class TestRowStorage:
280320
@pytest.fixture(autouse=True, scope="function")

tests/integration/synapseclient/models/synchronous/test_table.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,46 @@ async def test_table_creation_with_data_sources(
265265
results["column_string"], csv_data["column_string"]
266266
)
267267

268+
async def test_create_table_with_string_column(
269+
self, project_model: Project
270+
) -> None:
271+
"""Test creating tables with string column configurations."""
272+
# GIVEN a table with columns
273+
table_name = str(uuid.uuid4())
274+
table_description = "Test table with columns"
275+
table = Table(
276+
name=table_name,
277+
parent_id=project_model.id,
278+
description=table_description,
279+
columns=[
280+
Column(name="test_column", column_type="STRING"),
281+
Column(name="test_column2", column_type="INTEGER"),
282+
],
283+
)
284+
285+
# WHEN I store the table
286+
table = table.store(synapse_client=self.syn)
287+
self.schedule_for_cleanup(table.id)
288+
289+
# THEN the table should be created
290+
assert table.id is not None
291+
292+
# AND I can retrieve that table from Synapse
293+
new_table_instance = Table(id=table.id).get(synapse_client=self.syn)
294+
assert new_table_instance is not None
295+
assert new_table_instance.name == table_name
296+
assert new_table_instance.id == table.id
297+
assert new_table_instance.description == table_description
298+
assert len(new_table_instance.columns) == 2
299+
assert new_table_instance.columns["test_column"].name == "test_column"
300+
assert (
301+
new_table_instance.columns["test_column"].column_type == ColumnType.STRING
302+
)
303+
assert new_table_instance.columns["test_column2"].name == "test_column2"
304+
assert (
305+
new_table_instance.columns["test_column2"].column_type == ColumnType.INTEGER
306+
)
307+
268308

269309
class TestRowStorage:
270310
@pytest.fixture(autouse=True, scope="function")

0 commit comments

Comments
 (0)