2022-04-03 18:41:59 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
2022-04-04 06:26:58 +00:00
|
|
|
def test_api_translate(client):
|
2022-04-03 18:41:59 +00:00
|
|
|
response = client.post("/translate", data={
|
|
|
|
"q": "Hello",
|
|
|
|
"source": "en",
|
|
|
|
"target": "es",
|
|
|
|
"format": "text"
|
|
|
|
})
|
|
|
|
|
|
|
|
response_json = json.loads(response.data)
|
|
|
|
|
|
|
|
assert "translatedText" in response_json
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
2022-04-05 15:12:15 +00:00
|
|
|
|
|
|
|
def test_api_translate_batch(client):
|
|
|
|
|
|
|
|
response = client.post("/translate", json={
|
|
|
|
"q": ["Hello", "World"],
|
|
|
|
"source": "en",
|
|
|
|
"target": "es",
|
|
|
|
"format": "text"
|
|
|
|
})
|
|
|
|
|
|
|
|
response_json = json.loads(response.data)
|
|
|
|
|
|
|
|
assert "translatedText" in response_json
|
|
|
|
assert isinstance(response_json["translatedText"], list)
|
|
|
|
assert len(response_json["translatedText"]) == 2
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_api_translate_unsupported_language(client):
|
|
|
|
response = client.post("/translate", data={
|
|
|
|
"q": "Hello",
|
|
|
|
"source": "en",
|
|
|
|
"target": "zz",
|
|
|
|
"format": "text"
|
|
|
|
})
|
|
|
|
|
|
|
|
response_json = json.loads(response.data)
|
|
|
|
|
|
|
|
assert "error" in response_json
|
2023-07-09 10:29:11 +00:00
|
|
|
assert response_json["error"] == "zz is not supported"
|
2022-04-05 15:12:15 +00:00
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_api_translate_missing_parameter(client):
|
|
|
|
response = client.post("/translate", data={
|
|
|
|
"source": "en",
|
|
|
|
"target": "es",
|
|
|
|
"format": "text"
|
|
|
|
})
|
|
|
|
|
|
|
|
response_json = json.loads(response.data)
|
|
|
|
|
|
|
|
assert "error" in response_json
|
2023-07-09 10:29:11 +00:00
|
|
|
assert response_json["error"] == "Invalid request: missing q parameter"
|
2022-04-05 15:12:15 +00:00
|
|
|
assert response.status_code == 400
|