Skip to content

Commit 1c7d86b

Browse files
committed
add version to delete methods
1 parent 259e0cb commit 1c7d86b

File tree

3 files changed

+152
-11
lines changed

3 files changed

+152
-11
lines changed

synapseclient/models/schema_organization.py

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -638,17 +638,23 @@ def store(
638638
"""
639639
return self
640640

641-
def delete(self) -> None:
641+
def delete(
642+
self, version: Optional[str] = None, synapse_client: Optional["Synapse"] = None
643+
) -> None:
642644
"""
643645
Deletes this JSONSchema
644646
645647
Arguments:
648+
version: Defaults to None.
649+
- If a version is supplied, that version will be deleted.
650+
- If no version is supplied the whole schema will be deleted.
646651
synapse_client: If not passed in and caching was not disabled by
647652
`Synapse.allow_client_caching(False)` this will use the last created
648653
instance from the Synapse class constructor
649654
650-
Example: Delete this JSONSchema from Synapse
651-
 
655+
Example: Delete from Synapse
656+
657+
Delete the entire schema
652658
653659
```python
654660
from synapseclient.models import JSONSchema
@@ -660,6 +666,19 @@ def delete(self) -> None:
660666
js = JSONSchema("my.schema.name", "my.org.name")
661667
js.delete()
662668
```
669+
670+
Delete a specific version from Synapse
671+
672+
```python
673+
from synapseclient.models import JSONSchema
674+
from synapseclient import Synapse
675+
676+
syn = Synapse()
677+
syn.login()
678+
679+
js = JSONSchema("my.schema.name", "my.org.name")
680+
js.delete(version="0.0.1")
681+
```
663682
"""
664683
return None
665684

@@ -750,7 +769,7 @@ class JSONSchema(JSONSchemaProtocol):
750769
id: The ID of the schema
751770
created_on: The date this schema was created
752771
created_by: The ID of the user that created this schema
753-
uri: The uri of this schema
772+
uri: The schema identifier in format: <organization_name>-<schema_name>
754773
"""
755774

756775
name: Optional[str] = None
@@ -772,7 +791,7 @@ class JSONSchema(JSONSchemaProtocol):
772791
"""The ID of the user that created this schema"""
773792

774793
uri: Optional[str] = field(init=False)
775-
"""The uri of this schema"""
794+
"""The schema identifier in format: <organization_name>-<schema_name>"""
776795

777796
def __post_init__(self) -> None:
778797
if self.name:
@@ -883,7 +902,20 @@ async def store_schema():
883902
syn.login()
884903
885904
schema = JSONSchema(organization_name="my.org", name="test.schema")
886-
await schema.store_async(schema_body = {})
905+
schema_body = {
906+
{
907+
"properties": {
908+
"Component": {
909+
"description": "TBD",
910+
"not": {
911+
"type": "null"
912+
},
913+
"title": "Component"
914+
}
915+
}
916+
}
917+
}
918+
await schema.store_async(schema_body = schema_body)
887919
888920
asyncio.run(store_schema())
889921
```
@@ -910,17 +942,24 @@ async def store_schema():
910942
self.created_on = new_version_info.created_on
911943
return self
912944

913-
async def delete_async(self, synapse_client: Optional["Synapse"] = None) -> None:
945+
async def delete_async(
946+
self, version: Optional[str] = None, synapse_client: Optional["Synapse"] = None
947+
) -> None:
914948
"""
915-
Deletes this JSONSchema from Synapse
949+
If a version is supplied the specific version is deleted from Synapse.
950+
Otherwise the entire schema is deleted from Synapse
916951
917952
Arguments:
953+
version: Defaults to None.
954+
- If a version is supplied, that version will be deleted.
955+
- If no version is supplied the whole schema will be deleted.
918956
synapse_client: If not passed in and caching was not disabled by
919957
`Synapse.allow_client_caching(False)` this will use the last created
920958
instance from the Synapse class constructor
921959
922960
Example: Delete an existing JSONSchema
923-
&nbsp;
961+
962+
Delete the whole schema
924963
925964
```python
926965
from synapseclient.models import JSONSchema
@@ -933,7 +972,25 @@ async def delete_schema():
933972
syn.login()
934973
935974
schema = JSONSchema(organization_name="my.org", name="test.schema")
936-
await schema.delete_async(schema_body = {})
975+
await schema.delete_async()
976+
977+
asyncio.run(delete_schema())
978+
```
979+
980+
Delete a specific version of the schema
981+
982+
```python
983+
from synapseclient.models import JSONSchema
984+
from synapseclient import Synapse
985+
import asyncio
986+
987+
async def delete_schema():
988+
989+
syn = Synapse()
990+
syn.login()
991+
992+
schema = JSONSchema(organization_name="my.org", name="test.schema")
993+
await schema.delete_async(version = "0.0.1")
937994
938995
asyncio.run(delete_schema())
939996
```
@@ -943,7 +1000,12 @@ async def delete_schema():
9431000
if not self.organization_name:
9441001
raise ValueError("JSONSchema must have a organization_name")
9451002

946-
await delete_json_schema(self.uri, synapse_client=synapse_client)
1003+
uri = self.uri
1004+
if version:
1005+
self._check_semantic_version(version)
1006+
uri = f"{uri}-{version}"
1007+
1008+
await delete_json_schema(uri, synapse_client=synapse_client)
9471009

9481010
@skip_async_to_sync
9491011
async def get_versions_async(

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from synapseclient.models.schema_organization import (
1313
SYNAPSE_SCHEMA_URL,
1414
CreateSchemaRequest,
15+
JSONSchemaVersionInfo,
1516
list_json_schema_organizations,
1617
)
1718

@@ -238,6 +239,50 @@ async def test_store_and_get(self, json_schema: JSONSchema) -> None:
238239
assert js2.created_by
239240
assert js2.created_on
240241

242+
async def test_delete(self, organization_with_schema: SchemaOrganization) -> None:
243+
# GIVEN an organization with 3 schema
244+
schemas: list[JSONSchema] = []
245+
async for item in organization_with_schema.get_json_schemas_async():
246+
schemas.append(item)
247+
assert len(schemas) == 3
248+
# WHEN deleting one of those schemas
249+
schema = schemas[0]
250+
await schema.delete_async()
251+
# THEN there should be only two left
252+
schemas2: list[JSONSchema] = []
253+
async for item in organization_with_schema.get_json_schemas_async():
254+
schemas2.append(item)
255+
assert len(schemas2) == 2
256+
257+
async def test_delete_version(self, json_schema: JSONSchema) -> None:
258+
# GIVEN an organization and a JSONSchema
259+
await json_schema.store_async(schema_body={}, version="0.0.1")
260+
# THEN that schema should have one version
261+
js_versions: list[JSONSchemaVersionInfo] = []
262+
async for item in json_schema.get_versions_async():
263+
js_versions.append(item)
264+
assert len(js_versions) == 1
265+
# WHEN storing a second version
266+
await json_schema.store_async(schema_body={}, version="0.0.2")
267+
# THEN that schema should have two versions
268+
js_versions = []
269+
async for item in json_schema.get_versions_async():
270+
js_versions.append(item)
271+
assert len(js_versions) == 2
272+
# AND they should be the ones stored
273+
versions = [js_version.semantic_version for js_version in js_versions]
274+
assert versions == ["0.0.1", "0.0.2"]
275+
# WHEN deleting the first schema version
276+
await json_schema.delete_async(version="0.0.1")
277+
# THEN there should only be one version left
278+
js_versions = []
279+
async for item in json_schema.get_versions_async():
280+
js_versions.append(item)
281+
assert len(js_versions) == 1
282+
# AND it should be the second version
283+
versions = [js_version.semantic_version for js_version in js_versions]
284+
assert versions == ["0.0.2"]
285+
241286
async def test_get_versions(self, json_schema: JSONSchema) -> None:
242287
# GIVEN an schema that hasn't been created
243288
# THEN get_versions should return an empty list

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,40 @@ async def test_store_and_get(self, json_schema: JSONSchema) -> None:
224224
assert js2.created_by
225225
assert js2.created_on
226226

227+
async def test_delete(self, organization_with_schema: SchemaOrganization) -> None:
228+
# GIVEN an organization with 3 schema
229+
schemas = list(organization_with_schema.get_json_schemas())
230+
assert len(schemas) == 3
231+
# WHEN deleting one of those schemas
232+
schema = schemas[0]
233+
schema.delete()
234+
# THEN there should be only two left
235+
schemas = list(organization_with_schema.get_json_schemas())
236+
assert len(schemas) == 2
237+
238+
async def test_delete_version(self, json_schema: JSONSchema) -> None:
239+
# GIVEN an organization and a JSONSchema
240+
json_schema.store(schema_body={}, version="0.0.1")
241+
# THEN that schema should have one version
242+
js_versions = list(json_schema.get_versions())
243+
assert len(js_versions) == 1
244+
# WHEN storing a second version
245+
json_schema.store(schema_body={}, version="0.0.2")
246+
# THEN that schema should have two versions
247+
js_versions = list(json_schema.get_versions())
248+
assert len(js_versions) == 2
249+
# AND they should be the ones stored
250+
versions = [js_version.semantic_version for js_version in js_versions]
251+
assert versions == ["0.0.1", "0.0.2"]
252+
# WHEN deleting the first schema version
253+
json_schema.delete(version="0.0.1")
254+
# THEN there should only be one version left
255+
js_versions = list(json_schema.get_versions())
256+
assert len(js_versions) == 1
257+
# AND it should be the second version
258+
versions = [js_version.semantic_version for js_version in js_versions]
259+
assert versions == ["0.0.2"]
260+
227261
async def test_get_versions(self, json_schema: JSONSchema) -> None:
228262
# GIVEN an schema that hasn't been created
229263
# THEN get_versions should return an empty list

0 commit comments

Comments
 (0)