Add the validation for empty texts.

main
KKlochko 1 month ago
parent 59caa6a0ab
commit 0f9546462e
Signed by: KKlochko
GPG Key ID: 572ECCD219BBA91B

@ -27,18 +27,45 @@ def validate_single_or_batch(text: Optional[str], texts: Optional[List[str]]):
}
)
if text == "" and texts is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": "'text' cannot be empty or only whitespace",
"details": {}
}
}
)
if texts is not None and len(texts) == 0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": "texts list cannot be empty",
"message": "Texts list cannot be empty",
"details": {}
}
}
)
if texts is not None and len(texts) != 0:
# Check for empty strings within the list
for i, t in enumerate(texts):
if not isinstance(t, str) or not t.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": f"Text item at index {i} in 'texts' cannot be empty or only whitespace",
"details": {}
}
}
)
return text if text is not None else texts

@ -82,6 +82,53 @@ class TestGeneralEndpoint:
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_empty_text_provided(self, api_client):
"""Test error when the text is empty"""
payload = {
"text": "",
"entities": ["organization"]
}
response = api_client.post(f"{BASE_URL}/general", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_value(self, api_client):
"""Test error when texts has an empty value"""
payload = {
"texts": [
"Apple Inc. was founded by Steve Jobs in California.",
"",
"Elon Musk leads Tesla and SpaceX."
],
"entities": ["organization"]
}
response = api_client.post(f"{BASE_URL}/general", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_values(self, api_client):
"""Test error when texts has empty values"""
payload = {
"texts": [
"",
"",
""
],
"entities": ["organization"]
}
response = api_client.post(f"{BASE_URL}/general", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_empty_entities_list(self, api_client):
"""Test error with an empty entities list"""
payload = {

@ -85,3 +85,63 @@ class TestRelationExtractionEndpoint:
response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload)
assert response.status_code == 400
def test_error_no_text_provided(self, api_client):
"""Test error when neither text nor texts is provided"""
payload = {
"text": "",
"relations": ["founded"]
}
response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_empty_text_provided(self, api_client):
"""Test error when the text is empty"""
payload = {
"relations": ["founded"]
}
response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_value(self, api_client):
"""Test error when texts has an empty value"""
payload = {
"texts": [
"Bill Gates founded Microsoft.",
"",
"Apple is based in California."
],
"relations": ["founded", "founded_by", "based_in"],
"threshold": 0.3
}
response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_values(self, api_client):
"""Test error when texts has empty values"""
payload = {
"texts": [
"",
"",
""
],
"relations": ["founded", "founded_by", "based_in"],
"threshold": 0.3
}
response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"

@ -57,3 +57,54 @@ class TestSummarizationEndpoint:
}
response = api_client.post(f"{BASE_URL}/summarization", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_empty_text(self, api_client):
"""Test error when the text is empty"""
payload = {
"text": "",
"threshold": 0.5
}
response = api_client.post(f"{BASE_URL}/summarization", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_value(self, api_client):
"""Test error when texts has an empty value"""
payload = {
"texts": [
"Python is a high-level programming language. It's known for its simplicity and readability.",
"",
"Rust is a systems programming language. It focuses on safety and performance."
],
"threshold": 0.3
}
response = api_client.post(f"{BASE_URL}/summarization", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"
def test_error_texts_has_empty_values(self, api_client):
"""Test error when texts has empty values"""
payload = {
"texts": [
"",
"",
""
],
"threshold": 0.3
}
response = api_client.post(f"{BASE_URL}/summarization", json=payload)
assert response.status_code == 400
error = response.json()
assert "error" in error
assert error["error"]["code"] == "INVALID_INPUT"

Loading…
Cancel
Save