|
| 1 | +import json |
| 2 | +from unittest.mock import AsyncMock, MagicMock, mock_open, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from synapseclient.extensions.curator import register_jsonschema_async |
| 7 | +from synapseclient.extensions.curator.schema_management import fix_name |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def mock_synapse_client(): |
| 12 | + mock_client = MagicMock() |
| 13 | + mock_client.logger = MagicMock() |
| 14 | + return mock_client |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_jsonschema(): |
| 19 | + with patch("synapseclient.models.schema_organization.JSONSchema") as MockSchema: |
| 20 | + instance = MockSchema.return_value |
| 21 | + instance.store_async = AsyncMock() |
| 22 | + instance.uri = "syn123.456" |
| 23 | + yield MockSchema |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_register_jsonschema_async(mock_synapse_client, mock_jsonschema): |
| 28 | + schema_path = "mock_path.json" |
| 29 | + schema_content = {"$id": "test_schema", "type": "object"} |
| 30 | + org_name = "test.org" |
| 31 | + schema_name = "my-schema_name" |
| 32 | + version = "1.0.0" |
| 33 | + |
| 34 | + m_open = mock_open(read_data=json.dumps(schema_content)) |
| 35 | + |
| 36 | + with patch("builtins.open", m_open), patch( |
| 37 | + "synapseclient.Synapse.get_client", return_value=mock_synapse_client |
| 38 | + ), patch("json.load", return_value=schema_content): |
| 39 | + result = await register_jsonschema_async( |
| 40 | + schema_path=schema_path, |
| 41 | + organization_name=org_name, |
| 42 | + schema_name=schema_name, |
| 43 | + schema_version=version, |
| 44 | + synapse_client=mock_synapse_client, |
| 45 | + ) |
| 46 | + |
| 47 | + # Verify the name was used as-is |
| 48 | + mock_jsonschema.assert_called_once_with( |
| 49 | + name=schema_name, organization_name=org_name |
| 50 | + ) |
| 51 | + |
| 52 | + result.store_async.assert_awaited_once_with( |
| 53 | + schema_body=schema_content, |
| 54 | + version=version, |
| 55 | + synapse_client=mock_synapse_client, |
| 56 | + ) |
| 57 | + |
| 58 | + assert result.uri == "syn123.456" |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_register_jsonschema_async_fix_schema_name( |
| 63 | + mock_synapse_client, mock_jsonschema |
| 64 | +): |
| 65 | + schema_path = "mock_path.json" |
| 66 | + schema_content = {"$id": "test_schema", "type": "object"} |
| 67 | + org_name = "test.org" |
| 68 | + schema_name = "my-schema_name" |
| 69 | + fixed_schema_name = "my.schema.name" |
| 70 | + version = "1.0.0" |
| 71 | + |
| 72 | + m_open = mock_open(read_data=json.dumps(schema_content)) |
| 73 | + |
| 74 | + with patch("builtins.open", m_open), patch( |
| 75 | + "synapseclient.Synapse.get_client", return_value=mock_synapse_client |
| 76 | + ), patch("json.load", return_value=schema_content): |
| 77 | + result = await register_jsonschema_async( |
| 78 | + schema_path=schema_path, |
| 79 | + organization_name=org_name, |
| 80 | + schema_name=schema_name, |
| 81 | + fix_schema_name=True, |
| 82 | + schema_version=version, |
| 83 | + synapse_client=mock_synapse_client, |
| 84 | + ) |
| 85 | + |
| 86 | + # Verify the name was fixed (dashes/underscores to dots) |
| 87 | + mock_jsonschema.assert_called_once_with( |
| 88 | + name=fixed_schema_name, organization_name=org_name |
| 89 | + ) |
| 90 | + |
| 91 | + result.store_async.assert_awaited_once_with( |
| 92 | + schema_body=schema_content, |
| 93 | + version=version, |
| 94 | + synapse_client=mock_synapse_client, |
| 95 | + ) |
| 96 | + |
| 97 | + assert result.uri == "syn123.456" |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "name, expected_fixed_name", |
| 102 | + [ |
| 103 | + ("name", "name"), |
| 104 | + ("name.name", "name.name"), |
| 105 | + ("name..name", "name.name"), |
| 106 | + ("name-name", "name.name"), |
| 107 | + ("name--name", "name.name"), |
| 108 | + ("name_name", "name.name"), |
| 109 | + ("name-_name", "name.name"), |
| 110 | + ], |
| 111 | + ids=[ |
| 112 | + "No special characters", |
| 113 | + "One period", |
| 114 | + "Multiple periods", |
| 115 | + "One dash", |
| 116 | + "Multiple dashes", |
| 117 | + "Underscore", |
| 118 | + "Mixed special characters", |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_fix_name(name, expected_fixed_name): |
| 122 | + assert fix_name(name) == expected_fixed_name |
0 commit comments