forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TP: Minor refactoring, added more unit tests
Signed-off-by: Chris Helma <chelma+github@amazon.com>
- Loading branch information
Showing
6 changed files
with
213 additions
and
67 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
TransformationPlayground/tp_backend/transform_api/tests/test_views.py
This file contains 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,157 @@ | ||
from unittest.mock import patch, MagicMock | ||
from django.test import TestCase | ||
from rest_framework.test import APIClient | ||
from rest_framework import status | ||
|
||
from transform_expert.validation import TestTargetInnaccessibleError | ||
|
||
|
||
class TransformsIndexViewTestCase(TestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.url = "/transforms/index/" | ||
|
||
self.valid_request_body = { | ||
"transform_language": "Python", | ||
"source_version": "Elasticsearch 6.8", | ||
"target_version": "OpenSearch 2.17", | ||
"input_shape": { | ||
"index_name": "test-index", | ||
"index_json": { | ||
"settings": { | ||
"index": { | ||
"number_of_shards": 1, | ||
"number_of_replicas": 0 | ||
} | ||
}, | ||
"mappings": { | ||
"type1": { | ||
"properties": { | ||
"title": {"type": "text"} | ||
} | ||
}, | ||
"type2": { | ||
"properties": { | ||
"contents": {"type": "text"} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"test_target_url": "http://localhost:29200" | ||
} | ||
|
||
self.valid_response_body = { | ||
"output_shape": [ | ||
{ | ||
"index_name": "test-index-type1", | ||
"index_json": { | ||
"settings": { | ||
"index": { | ||
"number_of_shards": 1, | ||
"number_of_replicas": 0 | ||
} | ||
}, | ||
"mappings": { | ||
"properties": { | ||
"title": {"type": "text"} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"index_name": "test-index-type2", | ||
"index_json": { | ||
"settings": { | ||
"index": { | ||
"number_of_shards": 1, | ||
"number_of_replicas": 0 | ||
} | ||
}, | ||
"mappings": { | ||
"properties": { | ||
"contents": {"type": "text"} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"transform_logic": "Generated Python transformation logic" | ||
} | ||
|
||
@patch("transform_api.views.TransformsIndexView._perform_transformation") | ||
def test_post_happy_path(self, mock_perform_transformation): | ||
# Mock the transformation result | ||
mock_transform_report = MagicMock() | ||
mock_transform_report.task.output = self.valid_response_body["output_shape"] | ||
mock_transform_report.task.transform.to_file_format.return_value = self.valid_response_body["transform_logic"] | ||
mock_perform_transformation.return_value = mock_transform_report | ||
|
||
# Make the request | ||
response = self.client.post(self.url, self.valid_request_body, format="json") | ||
|
||
# Assertions | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.json(), self.valid_response_body) | ||
mock_perform_transformation.assert_called_once() | ||
|
||
def test_post_invalid_request_body(self): | ||
# Incomplete request body | ||
invalid_request_body = {"transform_language": "Python"} | ||
|
||
# Make the request | ||
response = self.client.post(self.url, invalid_request_body, format="json") | ||
|
||
# Assertions | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("input_shape", response.json()) | ||
|
||
@patch("transform_api.views.TransformsIndexView._perform_transformation") | ||
def test_post_inaccessible_target_cluster(self, mock_perform_transformation): | ||
# Mock the `_perform_transformation` method to raise `TestTargetInnaccessibleError` | ||
mock_perform_transformation.side_effect = TestTargetInnaccessibleError("Cluster not accessible") | ||
|
||
# Make the request | ||
response = self.client.post(self.url, self.valid_request_body, format="json") | ||
|
||
# Assertions | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(response.json(), {"error": "Cluster not accessible"}) | ||
mock_perform_transformation.assert_called_once() | ||
|
||
@patch("transform_api.views.TransformsIndexView._perform_transformation") | ||
def test_post_general_transformation_failure(self, mock_perform_transformation): | ||
# Mock the `_perform_transformation` method to raise a general exception | ||
mock_perform_transformation.side_effect = RuntimeError("General failure") | ||
|
||
# Make the request | ||
response = self.client.post(self.url, self.valid_request_body, format="json") | ||
|
||
# Assertions | ||
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
self.assertEqual(response.json(), {"error": "General failure"}) | ||
mock_perform_transformation.assert_called_once() | ||
|
||
@patch("transform_api.views.TransformsIndexCreateResponseSerializer") | ||
@patch("transform_api.views.TransformsIndexView._perform_transformation") | ||
def test_post_invalid_response(self, mock_perform_transformation, mock_response_serializer): | ||
# Mock the transformation result | ||
mock_transform_report = MagicMock() | ||
mock_transform_report.task.output = self.valid_response_body["output_shape"] | ||
mock_transform_report.task.transform.to_file_format.return_value = self.valid_response_body["transform_logic"] | ||
mock_perform_transformation.return_value = mock_transform_report | ||
|
||
# Mock the serializer behavior | ||
mock_serializer_instance = mock_response_serializer.return_value | ||
mock_serializer_instance.is_valid.return_value = False | ||
mock_serializer_instance.errors = {"transform_logic": ["Invalid format"]} | ||
|
||
# Make the request | ||
response = self.client.post(self.url, self.valid_request_body, format="json") | ||
|
||
# Assertions | ||
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
mock_perform_transformation.assert_called_once() | ||
mock_serializer_instance.is_valid.assert_called_once() | ||
self.assertEqual(mock_serializer_instance.errors, {"transform_logic": ["Invalid format"]}) | ||
|
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
TransformationPlayground/tp_backend/transform_expert/tests/utils/test_opensearch_client.py
This file contains 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
45 changes: 0 additions & 45 deletions
45
TransformationPlayground/tp_backend/transform_expert/utils/inference.py
This file was deleted.
Oops, something went wrong.
This file contains 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