-
Notifications
You must be signed in to change notification settings - Fork 75
[SYNPY-1777] Add fix_schema_name #1327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+193
−6
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
73f26f2
add fix_schema_name
andrewelamb 4a79d47
improved docstring description
andrewelamb c31b271
fix cli description
andrewelamb 5acdb31
remove assert statement
andrewelamb a47dd70
remove uneeded comments
andrewelamb f96349f
Linglings suggestions
andrewelamb 212d7c3
update docs
andrewelamb 6f049b6
fix doc list
andrewelamb aa2c5d2
handle merge conflict
andrewelamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
tests/unit/synapseclient/extensions/test_schema_management.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import json | ||
| from unittest.mock import AsyncMock, MagicMock, mock_open, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from synapseclient.extensions.curator import register_jsonschema_async | ||
| from synapseclient.extensions.curator.schema_management import fix_name | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_synapse_client(): | ||
| mock_client = MagicMock() | ||
| mock_client.logger = MagicMock() | ||
| return mock_client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_jsonschema(): | ||
| with patch("synapseclient.models.schema_organization.JSONSchema") as MockSchema: | ||
| instance = MockSchema.return_value | ||
| instance.store_async = AsyncMock() | ||
| instance.uri = "syn123.456" | ||
| yield MockSchema | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_register_jsonschema_async(mock_synapse_client, mock_jsonschema): | ||
| schema_path = "mock_path.json" | ||
| schema_content = {"$id": "test_schema", "type": "object"} | ||
| org_name = "test.org" | ||
| schema_name = "my-schema_name" | ||
| version = "1.0.0" | ||
|
|
||
| m_open = mock_open(read_data=json.dumps(schema_content)) | ||
|
|
||
| with patch("builtins.open", m_open), patch( | ||
| "synapseclient.Synapse.get_client", return_value=mock_synapse_client | ||
| ), patch("json.load", return_value=schema_content): | ||
| result = await register_jsonschema_async( | ||
| schema_path=schema_path, | ||
| organization_name=org_name, | ||
| schema_name=schema_name, | ||
| schema_version=version, | ||
| synapse_client=mock_synapse_client, | ||
| ) | ||
|
|
||
| # Verify the name was used as-is | ||
| mock_jsonschema.assert_called_once_with( | ||
| name=schema_name, organization_name=org_name | ||
| ) | ||
|
|
||
| result.store_async.assert_awaited_once_with( | ||
| schema_body=schema_content, | ||
| version=version, | ||
| synapse_client=mock_synapse_client, | ||
| ) | ||
|
|
||
| assert result.uri == "syn123.456" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_register_jsonschema_async_fix_schema_name( | ||
| mock_synapse_client, mock_jsonschema | ||
| ): | ||
| schema_path = "mock_path.json" | ||
| schema_content = {"$id": "test_schema", "type": "object"} | ||
| org_name = "test.org" | ||
| schema_name = "my-schema_name" | ||
| fixed_schema_name = "my.schema.name" | ||
| version = "1.0.0" | ||
|
|
||
| m_open = mock_open(read_data=json.dumps(schema_content)) | ||
|
|
||
| with patch("builtins.open", m_open), patch( | ||
| "synapseclient.Synapse.get_client", return_value=mock_synapse_client | ||
| ), patch("json.load", return_value=schema_content): | ||
| result = await register_jsonschema_async( | ||
| schema_path=schema_path, | ||
| organization_name=org_name, | ||
| schema_name=schema_name, | ||
| fix_schema_name=True, | ||
| schema_version=version, | ||
| synapse_client=mock_synapse_client, | ||
| ) | ||
|
|
||
| # Verify the name was fixed (dashes/underscores to dots) | ||
| mock_jsonschema.assert_called_once_with( | ||
| name=fixed_schema_name, organization_name=org_name | ||
| ) | ||
|
|
||
| result.store_async.assert_awaited_once_with( | ||
| schema_body=schema_content, | ||
| version=version, | ||
| synapse_client=mock_synapse_client, | ||
| ) | ||
|
|
||
| assert result.uri == "syn123.456" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "name, expected_fixed_name", | ||
| [ | ||
| ("name", "name"), | ||
| ("name.name", "name.name"), | ||
| ("name..name", "name.name"), | ||
| ("name-name", "name.name"), | ||
| ("name--name", "name.name"), | ||
| ("name_name", "name.name"), | ||
| ("name-_name", "name.name"), | ||
| ], | ||
| ids=[ | ||
| "No special characters", | ||
| "One period", | ||
| "Multiple periods", | ||
| "One dash", | ||
| "Multiple dashes", | ||
| "Underscore", | ||
| "Mixed special characters", | ||
| ], | ||
| ) | ||
| def test_fix_name(name, expected_fixed_name): | ||
| assert fix_name(name) == expected_fixed_name |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewelamb I just noticed there's a bug in the code. The log should only be printed if there's actually a name change. I am seeing such as below when there's no name changes:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, that's when fix_schema_names is turned on, but no change actually gets made. That's a quick fix.