-
Notifications
You must be signed in to change notification settings - Fork 1
feat(assets): add asset history API methods #79
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
Open
briansumma
wants to merge
2
commits into
NorthShoreAutomation:main
Choose a base branch
from
briansumma:feat/asset-history-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # pythonik/tests/test_assets_history.py | ||
| import uuid | ||
| import pytest | ||
| import requests_mock | ||
|
|
||
| from pythonik.client import PythonikClient | ||
| from pythonik.models.base import PaginatedResponse | ||
| from pythonik.specs.assets import AssetSpec | ||
|
|
||
|
|
||
| def test_fetch(): | ||
| """Test fetching a list of assets.""" | ||
| with requests_mock.Mocker() as m: | ||
| app_id = str(uuid.uuid4()) | ||
| auth_token = str(uuid.uuid4()) | ||
|
|
||
| # Mock response data structure | ||
| response_data = { | ||
| "objects": [ | ||
| {"id": str(uuid.uuid4()), "title": "Test Asset 1", "status": "ACTIVE"}, | ||
| {"id": str(uuid.uuid4()), "title": "Test Asset 2", "status": "ACTIVE"}, | ||
| ], | ||
| "page": 1, | ||
| "pages": 1, | ||
| "per_page": 2, | ||
| "total": 2, | ||
| } | ||
|
|
||
| # Setup mock endpoint | ||
| mock_address = AssetSpec.gen_url("assets/") | ||
| m.get(mock_address, json=response_data) | ||
|
|
||
| # Create client and call the method | ||
| client = PythonikClient(app_id=app_id, auth_token=auth_token, timeout=3) | ||
| result = client.assets().list_all() | ||
|
|
||
| # Verify response | ||
| assert result.response.ok | ||
| assert isinstance(result.data, PaginatedResponse) | ||
| assert len(result.data.objects) == 2 | ||
| assert result.data.page == 1 | ||
| assert result.data.pages == 1 | ||
| assert result.data.total == 2 | ||
|
|
||
|
|
||
| def test_fetch_with_params(): | ||
| """Test fetching a list of assets with query parameters.""" | ||
| with requests_mock.Mocker() as m: | ||
| app_id = str(uuid.uuid4()) | ||
| auth_token = str(uuid.uuid4()) | ||
|
|
||
| # Mock response data | ||
| response_data = { | ||
| "objects": [ | ||
| {"id": str(uuid.uuid4()), "title": "Test Asset 1", "status": "ACTIVE"} | ||
| ], | ||
| "page": 1, | ||
| "pages": 1, | ||
| "per_page": 1, | ||
| "total": 1, | ||
| } | ||
|
|
||
| # Setup mock endpoint with params matcher | ||
| mock_address = AssetSpec.gen_url("assets/") | ||
| m.get( | ||
| mock_address, | ||
| json=response_data, | ||
| # Add request matcher to ensure params are passed correctly | ||
| additional_matcher=lambda req: req.qs == {"page": ["1"], "per_page": ["1"]}, | ||
| ) | ||
|
|
||
| # Create client and call method with params | ||
| client = PythonikClient(app_id=app_id, auth_token=auth_token, timeout=3) | ||
| params = {"page": 1, "per_page": 1} | ||
| result = client.assets().list_all(params=params) | ||
|
|
||
| # Verify response | ||
| assert result.response.ok | ||
| assert isinstance(result.data, PaginatedResponse) | ||
| assert len(result.data.objects) == 1 | ||
|
|
||
|
|
||
| def test_fetch_asset_history_entities(): | ||
| """Test fetching history entities for an asset.""" | ||
| with requests_mock.Mocker() as m: | ||
| app_id = str(uuid.uuid4()) | ||
| auth_token = str(uuid.uuid4()) | ||
| asset_id = str(uuid.uuid4()) | ||
|
|
||
| # Mock response data | ||
| response_data = { | ||
| "objects": [ | ||
| { | ||
| "id": str(uuid.uuid4()), | ||
| "operation_type": "METADATA", | ||
| "operation_description": "Updated metadata", | ||
| "date_created": "2025-05-13T10:00:00Z", | ||
| "created_by_user": "user123", | ||
| }, | ||
| { | ||
| "id": str(uuid.uuid4()), | ||
| "operation_type": "VERSION_CREATE", | ||
| "operation_description": "Created new version", | ||
| "date_created": "2025-05-12T15:30:00Z", | ||
| "created_by_user": "user123", | ||
| }, | ||
| ], | ||
| "page": 1, | ||
| "pages": 1, | ||
| "per_page": 10, | ||
| "total": 2, | ||
| } | ||
|
|
||
| # Setup mock endpoint | ||
| mock_address = AssetSpec.gen_url(f"assets/{asset_id}/history/") | ||
| m.get(mock_address, json=response_data) | ||
|
|
||
| # Create client and call the method | ||
| client = PythonikClient(app_id=app_id, auth_token=auth_token, timeout=3) | ||
| result = client.assets().list_asset_history_entities(asset_id) | ||
|
|
||
| # Verify response | ||
| assert result.response.ok | ||
| assert isinstance(result.data, PaginatedResponse) | ||
| assert len(result.data.objects) == 2 | ||
| assert result.data.objects[0]["operation_type"] == "METADATA" | ||
| assert result.data.objects[1]["operation_type"] == "VERSION_CREATE" | ||
|
|
||
|
|
||
| def test_create_history_entity(): | ||
| """Test creating a history entity for an asset.""" | ||
| with requests_mock.Mocker() as m: | ||
| app_id = str(uuid.uuid4()) | ||
| auth_token = str(uuid.uuid4()) | ||
| asset_id = str(uuid.uuid4()) | ||
| operation_description = "Test history entry" | ||
| operation_type = "CUSTOM" | ||
|
|
||
| # Setup mock endpoint | ||
| mock_address = AssetSpec.gen_url(f"assets/{asset_id}/history/") | ||
| m.post(mock_address, status_code=201) | ||
|
|
||
| # Create client and call the method | ||
| client = PythonikClient(app_id=app_id, auth_token=auth_token, timeout=3) | ||
| result = client.assets().create_history_entity( | ||
| asset_id=asset_id, | ||
| operation_description=operation_description, | ||
| operation_type=operation_type, | ||
| ) | ||
|
|
||
| # Verify response | ||
| assert result.response.ok | ||
| assert result.response.status_code == 201 | ||
|
|
||
| # Verify the correct request body was sent | ||
| assert m.last_request.json() == { | ||
| "operation_description": operation_description, | ||
| "operation_type": operation_type, | ||
| } | ||
|
|
||
|
|
||
| def test_create_history_entity_with_invalid_operation_type(): | ||
| """Test creating a history entity with an invalid operation type.""" | ||
| app_id = str(uuid.uuid4()) | ||
| auth_token = str(uuid.uuid4()) | ||
| asset_id = str(uuid.uuid4()) | ||
| operation_description = "Test history entry" | ||
| operation_type = "INVALID_TYPE" | ||
|
|
||
| # Create client | ||
| client = PythonikClient(app_id=app_id, auth_token=auth_token, timeout=3) | ||
|
|
||
| # Check that an error is raised for invalid operation type | ||
| with pytest.raises(ValueError) as excinfo: | ||
| client.assets().create_history_entity( | ||
| asset_id=asset_id, | ||
| operation_description=operation_description, | ||
| operation_type=operation_type, | ||
| ) | ||
|
|
||
| # Verify the error message | ||
| assert "operation_type must be one of:" in str(excinfo.value) | ||
| assert "EXPORT" in str(excinfo.value) | ||
| assert "CUSTOM" in str(excinfo.value) | ||
| assert "VERSION_CREATE" in str(excinfo.value) |
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.
This might be better as an Enum and we can set the operation_type as the Enum, so then the developer/user can use the Enum when sending, also we may not want to restrict them too much here, but just recommend passing the enum or a string, allowing any value but the Enum will help them to see what is expected (this way if the api changes they can still use it)