♻️(backend) rename convert_markdown to convert (#1114)
Renamed the `convert_markdown` method to `convert` to prepare for an all-purpose conversion endpoint, enabling support for multiple formats and simplifying future extension. Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
This commit is contained in:
@@ -408,9 +408,7 @@ class ServerCreateDocumentSerializer(serializers.Serializer):
|
|||||||
language = user.language or language
|
language = user.language or language
|
||||||
|
|
||||||
try:
|
try:
|
||||||
document_content = YdocConverter().convert_markdown(
|
document_content = YdocConverter().convert(validated_data["content"])
|
||||||
validated_data["content"]
|
|
||||||
)
|
|
||||||
except ConversionError as err:
|
except ConversionError as err:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
{"content": ["Could not convert content"]}
|
{"content": ["Could not convert content"]}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class YdocConverter:
|
|||||||
# Note: Yprovider microservice accepts only raw token, which is not recommended
|
# Note: Yprovider microservice accepts only raw token, which is not recommended
|
||||||
return settings.Y_PROVIDER_API_KEY
|
return settings.Y_PROVIDER_API_KEY
|
||||||
|
|
||||||
def convert_markdown(self, text):
|
def convert(self, text):
|
||||||
"""Convert a Markdown text into our internal format using an external microservice."""
|
"""Convert a Markdown text into our internal format using an external microservice."""
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ pytestmark = pytest.mark.django_db
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_convert_md():
|
def mock_convert_md():
|
||||||
"""Mock YdocConverter.convert_markdown to return a converted content."""
|
"""Mock YdocConverter.convert to return a converted content."""
|
||||||
with patch.object(
|
with patch.object(
|
||||||
YdocConverter,
|
YdocConverter,
|
||||||
"convert_markdown",
|
"convert",
|
||||||
return_value="Converted document content",
|
return_value="Converted document content",
|
||||||
) as mock:
|
) as mock:
|
||||||
yield mock
|
yield mock
|
||||||
|
|||||||
@@ -21,15 +21,15 @@ def test_auth_header(settings):
|
|||||||
assert converter.auth_header == "test-key"
|
assert converter.auth_header == "test-key"
|
||||||
|
|
||||||
|
|
||||||
def test_convert_markdown_empty_text():
|
def test_convert_empty_text():
|
||||||
"""Should raise ValidationError when text is empty."""
|
"""Should raise ValidationError when text is empty."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
with pytest.raises(ValidationError, match="Input text cannot be empty"):
|
with pytest.raises(ValidationError, match="Input text cannot be empty"):
|
||||||
converter.convert_markdown("")
|
converter.convert("")
|
||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_service_unavailable(mock_post):
|
def test_convert_service_unavailable(mock_post):
|
||||||
"""Should raise ServiceUnavailableError when service is unavailable."""
|
"""Should raise ServiceUnavailableError when service is unavailable."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
|
|
||||||
@@ -39,11 +39,11 @@ def test_convert_markdown_service_unavailable(mock_post):
|
|||||||
ServiceUnavailableError,
|
ServiceUnavailableError,
|
||||||
match="Failed to connect to conversion service",
|
match="Failed to connect to conversion service",
|
||||||
):
|
):
|
||||||
converter.convert_markdown("test text")
|
converter.convert("test text")
|
||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_http_error(mock_post):
|
def test_convert_http_error(mock_post):
|
||||||
"""Should raise ServiceUnavailableError when HTTP error occurs."""
|
"""Should raise ServiceUnavailableError when HTTP error occurs."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
|
|
||||||
@@ -55,11 +55,11 @@ def test_convert_markdown_http_error(mock_post):
|
|||||||
ServiceUnavailableError,
|
ServiceUnavailableError,
|
||||||
match="Failed to connect to conversion service",
|
match="Failed to connect to conversion service",
|
||||||
):
|
):
|
||||||
converter.convert_markdown("test text")
|
converter.convert("test text")
|
||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_invalid_json_response(mock_post):
|
def test_convert_invalid_json_response(mock_post):
|
||||||
"""Should raise InvalidResponseError when response is not valid JSON."""
|
"""Should raise InvalidResponseError when response is not valid JSON."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
|
|
||||||
@@ -71,11 +71,11 @@ def test_convert_markdown_invalid_json_response(mock_post):
|
|||||||
InvalidResponseError,
|
InvalidResponseError,
|
||||||
match="Could not parse conversion service response",
|
match="Could not parse conversion service response",
|
||||||
):
|
):
|
||||||
converter.convert_markdown("test text")
|
converter.convert("test text")
|
||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_missing_content_field(mock_post, settings):
|
def test_convert_missing_content_field(mock_post, settings):
|
||||||
"""Should raise MissingContentError when response is missing required field."""
|
"""Should raise MissingContentError when response is missing required field."""
|
||||||
|
|
||||||
settings.CONVERSION_API_CONTENT_FIELD = "expected_field"
|
settings.CONVERSION_API_CONTENT_FIELD = "expected_field"
|
||||||
@@ -90,11 +90,11 @@ def test_convert_markdown_missing_content_field(mock_post, settings):
|
|||||||
MissingContentError,
|
MissingContentError,
|
||||||
match="Response missing required field: expected_field",
|
match="Response missing required field: expected_field",
|
||||||
):
|
):
|
||||||
converter.convert_markdown("test text")
|
converter.convert("test text")
|
||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_full_integration(mock_post, settings):
|
def test_convert_full_integration(mock_post, settings):
|
||||||
"""Test full integration with all settings."""
|
"""Test full integration with all settings."""
|
||||||
|
|
||||||
settings.Y_PROVIDER_API_BASE_URL = "http://test.com/"
|
settings.Y_PROVIDER_API_BASE_URL = "http://test.com/"
|
||||||
@@ -110,7 +110,7 @@ def test_convert_markdown_full_integration(mock_post, settings):
|
|||||||
mock_response.json.return_value = {"content": expected_content}
|
mock_response.json.return_value = {"content": expected_content}
|
||||||
mock_post.return_value = mock_response
|
mock_post.return_value = mock_response
|
||||||
|
|
||||||
result = converter.convert_markdown("test markdown")
|
result = converter.convert("test markdown")
|
||||||
|
|
||||||
assert result == expected_content
|
assert result == expected_content
|
||||||
mock_post.assert_called_once_with(
|
mock_post.assert_called_once_with(
|
||||||
@@ -126,7 +126,7 @@ def test_convert_markdown_full_integration(mock_post, settings):
|
|||||||
|
|
||||||
|
|
||||||
@patch("requests.post")
|
@patch("requests.post")
|
||||||
def test_convert_markdown_timeout(mock_post):
|
def test_convert_timeout(mock_post):
|
||||||
"""Should raise ServiceUnavailableError when request times out."""
|
"""Should raise ServiceUnavailableError when request times out."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
|
|
||||||
@@ -136,12 +136,12 @@ def test_convert_markdown_timeout(mock_post):
|
|||||||
ServiceUnavailableError,
|
ServiceUnavailableError,
|
||||||
match="Failed to connect to conversion service",
|
match="Failed to connect to conversion service",
|
||||||
):
|
):
|
||||||
converter.convert_markdown("test text")
|
converter.convert("test text")
|
||||||
|
|
||||||
|
|
||||||
def test_convert_markdown_none_input():
|
def test_convert_none_input():
|
||||||
"""Should raise ValidationError when input is None."""
|
"""Should raise ValidationError when input is None."""
|
||||||
converter = YdocConverter()
|
converter = YdocConverter()
|
||||||
|
|
||||||
with pytest.raises(ValidationError, match="Input text cannot be empty"):
|
with pytest.raises(ValidationError, match="Input text cannot be empty"):
|
||||||
converter.convert_markdown(None)
|
converter.convert(None)
|
||||||
|
|||||||
Reference in New Issue
Block a user