Skip to content

Commit 71a859e

Browse files
committed
separated check_name into org and schema functions
1 parent da9c068 commit 71a859e

2 files changed

Lines changed: 60 additions & 16 deletions

File tree

synapseclient/models/schema_organization.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class SchemaOrganization(SchemaOrganizationProtocol):
254254

255255
def __post_init__(self) -> None:
256256
if self.name:
257-
_check_name(self.name)
257+
_check_org_name(self.name)
258258

259259
async def get_async(
260260
self, *, synapse_client: Optional["Synapse"] = None
@@ -826,9 +826,9 @@ class JSONSchema(JSONSchemaProtocol):
826826

827827
def __post_init__(self) -> None:
828828
if self.name:
829-
_check_name(self.name)
829+
_check_schema_name(self.name)
830830
if self.organization_name:
831-
_check_name(self.organization_name)
831+
_check_org_name(self.organization_name)
832832
if self.name and self.organization_name:
833833
self.uri = f"{self.organization_name}-{self.name}"
834834
else:
@@ -1313,8 +1313,8 @@ class CreateSchemaRequest(AsynchronousCommunicator):
13131313

13141314
def __post_init__(self) -> None:
13151315
self.concrete_type = CREATE_SCHEMA_REQUEST
1316-
_check_name(self.name)
1317-
_check_name(self.organization_name)
1316+
_check_schema_name(self.name)
1317+
_check_org_name(self.organization_name)
13181318
uri = f"{self.organization_name}-{self.name}"
13191319
if self.version:
13201320
self._check_semantic_version(self.version)
@@ -1428,9 +1428,9 @@ def list_json_schema_organizations(
14281428
return all_orgs
14291429

14301430

1431-
def _check_name(name: str) -> None:
1431+
def _check_org_name(name: str) -> None:
14321432
"""
1433-
Checks that the input name is a valid Synapse Organization or JSONSchema name
1433+
Checks that the input name is a valid Synapse Organization
14341434
- Length requirement of 6 ≤ x ≤ 250
14351435
- Names do not contain the string sagebionetworks (case insensitive)
14361436
- May contain periods (each part is separated by periods)
@@ -1452,7 +1452,32 @@ def _check_name(name: str) -> None:
14521452
if not re.match(r"^([A-Za-z])([A-Za-z]|\d|)*$", part):
14531453
raise ValueError(
14541454
(
1455-
"Name may be separated by periods, "
1455+
"Organization name may be separated by periods, "
1456+
"but each part must start with a letter and contain "
1457+
f"only letters and numbers: {name}"
1458+
)
1459+
)
1460+
1461+
1462+
def _check_schema_name(name: str) -> None:
1463+
"""
1464+
Checks that the input name is a valid Synapse JSONSchema name
1465+
- May contain periods (each part is separated by periods)
1466+
- Each part must start with a letter
1467+
- Each part contains only letters and numbers
1468+
1469+
Arguments:
1470+
name: The name of the schema to be checked
1471+
1472+
Raises:
1473+
ValueError: When the name isn't valid
1474+
"""
1475+
parts = name.split(".")
1476+
for part in parts:
1477+
if not re.match(r"^([A-Za-z])([A-Za-z]|\d|)*$", part):
1478+
raise ValueError(
1479+
(
1480+
"Schema name may be separated by periods, "
14561481
"but each part must start with a letter and contain "
14571482
f"only letters and numbers: {name}"
14581483
)

tests/unit/synapseclient/models/async/unit_test_schema_organization_async.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import pytest
66

7+
from build.lib.synapseclient.models.schema_organization import _check_name
78
from synapseclient import Synapse
89
from synapseclient.models.mixins.json_schema import JSONSchemaVersionInfo
910
from synapseclient.models.schema_organization import (
1011
CreateSchemaRequest,
1112
JSONSchema,
1213
SchemaOrganization,
13-
_check_name,
14+
_check_org_name,
15+
_check_schema_name,
1416
)
1517

1618
ORG_NAME = "mytest.organization"
@@ -97,42 +99,59 @@ def _get_acl_response():
9799
}
98100

99101

100-
class TestCheckName:
101-
"""Tests for the _check_name validation function."""
102+
class TestCheckOrgName:
103+
"""Tests for the _check_org_name validation function."""
102104

103105
def test_valid_name(self) -> None:
104106
# GIVEN a valid name
105107
# WHEN I check the name
106108
# THEN no exception should be raised
107-
_check_name("mytest.organization")
109+
_check_org_name("mytest.organization")
108110

109111
def test_name_too_short(self) -> None:
110112
# GIVEN a name that is too short
111113
# WHEN I check the name
112114
# THEN it should raise ValueError
113115
with pytest.raises(ValueError, match="length 6 to 250"):
114-
_check_name("abc")
116+
_check_org_name("abc")
115117

116118
def test_name_too_long(self) -> None:
117119
# GIVEN a name that is too long
118120
# WHEN I check the name
119121
# THEN it should raise ValueError
120122
with pytest.raises(ValueError, match="length 6 to 250"):
121-
_check_name("a" * 251)
123+
_check_org_name("a" * 251)
122124

123125
def test_name_contains_sagebionetworks(self) -> None:
124126
# GIVEN a name containing 'sagebionetworks'
125127
# WHEN I check the name
126128
# THEN it should raise ValueError
127129
with pytest.raises(ValueError, match="sagebionetworks"):
128-
_check_name("my.sagebionetworks.test")
130+
_check_org_name("my.sagebionetworks.test")
129131

130132
def test_name_part_starts_with_number(self) -> None:
131133
# GIVEN a name where a part starts with a number
132134
# WHEN I check the name
133135
# THEN it should raise ValueError
134136
with pytest.raises(ValueError, match="must start with a letter"):
135-
_check_name("mytest.1invalid")
137+
_check_org_name("mytest.1invalid")
138+
139+
140+
class TestCheckSchemaName:
141+
"""Tests for the _check_schema_name validation function."""
142+
143+
def test_valid_name(self) -> None:
144+
# GIVEN a valid name
145+
# WHEN I check the name
146+
# THEN no exception should be raised
147+
_check_schema_name("mytest.schema")
148+
149+
def test_name_part_starts_with_number(self) -> None:
150+
# GIVEN a name where a part starts with a number
151+
# WHEN I check the name
152+
# THEN it should raise ValueError
153+
with pytest.raises(ValueError, match="must start with a letter"):
154+
_check_schema_name("mytest.1invalid")
136155

137156

138157
class TestSchemaOrganization:

0 commit comments

Comments
 (0)