forked from mirrors/LibreTranslate
Merge pull request #237 from dingedi/main
Run test for pull requests, add some functional tests
This commit is contained in:
commit
9eb1bff261
13 changed files with 169 additions and 14 deletions
2
.github/workflows/publish-package.yml
vendored
2
.github/workflows/publish-package.yml
vendored
|
@ -12,7 +12,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
python-version: [3.6, 3.7, 3.8, 3.9]
|
python-version: [3.7, 3.8, 3.9]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
22
.github/workflows/run-tests.yml
vendored
22
.github/workflows/run-tests.yml
vendored
|
@ -2,9 +2,12 @@ name: Run tests
|
||||||
# Run test at each push to main, if changes to package or tests files
|
# Run test at each push to main, if changes to package or tests files
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
push:
|
push:
|
||||||
branches: [ main ]
|
branches: [ main ]
|
||||||
paths:
|
paths:
|
||||||
|
- '*.py'
|
||||||
- 'requirements.txt'
|
- 'requirements.txt'
|
||||||
- 'app/**'
|
- 'app/**'
|
||||||
- 'tests/**'
|
- 'tests/**'
|
||||||
|
@ -12,11 +15,11 @@ on:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
tests:
|
tests_python:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
python-version: [3.6, 3.7, 3.8, 3.9]
|
python-version: [3.7, 3.8, 3.9]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
@ -33,12 +36,23 @@ jobs:
|
||||||
|
|
||||||
- name: Check code style with flake8 (lint)
|
- name: Check code style with flake8 (lint)
|
||||||
run: |
|
run: |
|
||||||
# warnings if there are Python syntax errors or undefined names
|
# warnings if there are Python syntax errors or undefined names
|
||||||
# (remove --exit-zero to fail when syntax error)
|
# (remove --exit-zero to fail when syntax error)
|
||||||
flake8 . --count --exit-zero --select=E9,F63,F7,F82 --show-source --statistics
|
flake8 . --count --exit-zero --select=E9,F63,F7,F82 --show-source --statistics
|
||||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
||||||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||||
|
|
||||||
- name: Test with pytest
|
- name: Test with pytest
|
||||||
run: pytest
|
run: pytest -v
|
||||||
|
|
||||||
|
|
||||||
|
test_docker_build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Docker build
|
||||||
|
run: docker build -t libretranslate .
|
||||||
|
|
||||||
|
- name: Docker build with some models
|
||||||
|
run: docker build -t libretranslate --build-arg models=en,es .
|
||||||
|
|
|
@ -239,7 +239,7 @@ def create_app(args):
|
||||||
|
|
||||||
return render_template("javascript-licenses.html")
|
return render_template("javascript-licenses.html")
|
||||||
|
|
||||||
@app.route("/languages", methods=["GET", "POST"])
|
@app.get("/languages")
|
||||||
@limiter.exempt
|
@limiter.exempt
|
||||||
def langs():
|
def langs():
|
||||||
"""
|
"""
|
||||||
|
|
11
app/main.py
11
app/main.py
|
@ -6,7 +6,7 @@ from app.app import create_app
|
||||||
from app.default_values import DEFAULT_ARGUMENTS as DEFARGS
|
from app.default_values import DEFAULT_ARGUMENTS as DEFARGS
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def get_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="LibreTranslate - Free and Open Source Translation API"
|
description="LibreTranslate - Free and Open Source Translation API"
|
||||||
)
|
)
|
||||||
|
@ -106,13 +106,18 @@ def main():
|
||||||
"--suggestions", default=DEFARGS['SUGGESTIONS'], action="store_true", help="Allow user suggestions"
|
"--suggestions", default=DEFARGS['SUGGESTIONS'], action="store_true", help="Allow user suggestions"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--disable-files-translation", default=DEFARGS['DISABLE_FILES_TRANSLATION'], action="store_true", help="Disable files translation"
|
"--disable-files-translation", default=DEFARGS['DISABLE_FILES_TRANSLATION'], action="store_true",
|
||||||
|
help="Disable files translation"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--disable-web-ui", default=DEFARGS['DISABLE_WEB_UI'], action="store_true", help="Disable web ui"
|
"--disable-web-ui", default=DEFARGS['DISABLE_WEB_UI'], action="store_true", help="Disable web ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = get_args()
|
||||||
app = create_app(args)
|
app = create_app(args)
|
||||||
|
|
||||||
if sys.argv[0] == '--wsgi':
|
if sys.argv[0] == '--wsgi':
|
||||||
|
|
5
setup.py
5
setup.py
|
@ -22,16 +22,15 @@ setup(
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
python_requires='>=3.6.0',
|
python_requires='>=3.7.0',
|
||||||
long_description=open('README.md').read(),
|
long_description=open('README.md').read(),
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
install_requires=open("requirements.txt", "r").readlines(),
|
install_requires=open("requirements.txt", "r").readlines(),
|
||||||
tests_require=['pytest==5.2.0'],
|
tests_require=['pytest==7.1.1'],
|
||||||
setup_requires=['pytest-runner'],
|
setup_requires=['pytest-runner'],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"License :: OSI Approved :: GNU Affero General Public License v3 ",
|
"License :: OSI Approved :: GNU Affero General Public License v3 ",
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
"Programming Language :: Python :: 3.6",
|
|
||||||
"Programming Language :: Python :: 3.7",
|
"Programming Language :: Python :: 3.7",
|
||||||
"Programming Language :: Python :: 3.8",
|
"Programming Language :: Python :: 3.8",
|
||||||
"Programming Language :: Python :: 3.9"
|
"Programming Language :: Python :: 3.9"
|
||||||
|
|
0
tests/test_api/__init__.py
Normal file
0
tests/test_api/__init__.py
Normal file
20
tests/test_api/conftest.py
Normal file
20
tests/test_api/conftest.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import sys
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.app import create_app
|
||||||
|
from app.default_values import DEFAULT_ARGUMENTS
|
||||||
|
from app.main import get_args
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def app():
|
||||||
|
sys.argv = ['']
|
||||||
|
DEFAULT_ARGUMENTS['LOAD_ONLY'] = "en,es"
|
||||||
|
app = create_app(get_args())
|
||||||
|
|
||||||
|
yield app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def client(app):
|
||||||
|
return app.test_client()
|
26
tests/test_api/test_api_detect_language.py
Normal file
26
tests/test_api/test_api_detect_language.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_detect_language(client):
|
||||||
|
response = client.post("/detect", data={
|
||||||
|
"q": "Hello"
|
||||||
|
})
|
||||||
|
response_json = json.loads(response.data)
|
||||||
|
|
||||||
|
assert "confidence" in response_json[0] and "language" in response_json[0]
|
||||||
|
assert len(response_json) >= 1
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_detect_language_must_fail_without_parameters(client):
|
||||||
|
response = client.post("/detect")
|
||||||
|
response_json = json.loads(response.data)
|
||||||
|
|
||||||
|
assert "error" in response_json
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_detect_language_must_fail_bad_request_type(client):
|
||||||
|
response = client.get("/detect")
|
||||||
|
|
||||||
|
assert response.status_code == 405
|
4
tests/test_api/test_api_frontend_settings.py
Normal file
4
tests/test_api/test_api_frontend_settings.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
def test_api_get_frontend_settings(client):
|
||||||
|
response = client.get("/frontend/settings")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
16
tests/test_api/test_api_get_languages.py
Normal file
16
tests/test_api/test_api_get_languages.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_get_languages(client):
|
||||||
|
response = client.get("/languages")
|
||||||
|
response_json = json.loads(response.data)
|
||||||
|
|
||||||
|
assert "code" in response_json[0] and "name" in response_json[0]
|
||||||
|
assert len(response_json) >= 1
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_get_languages_must_fail_bad_request_type(client):
|
||||||
|
response = client.post("/languages")
|
||||||
|
|
||||||
|
assert response.status_code == 405
|
10
tests/test_api/test_api_spec.py
Normal file
10
tests/test_api/test_api_spec.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
def test_api_get_spec(client):
|
||||||
|
response = client.get("/spec")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_get_spec_must_fail_bad_request_type(client):
|
||||||
|
response = client.post("/spec")
|
||||||
|
|
||||||
|
assert response.status_code == 405
|
61
tests/test_api/test_api_translate.py
Normal file
61
tests/test_api/test_api_translate.py
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_translate(client):
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
assert "zz is not supported" == response_json["error"]
|
||||||
|
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
|
||||||
|
assert "Invalid request: missing q parameter" == response_json["error"]
|
||||||
|
assert response.status_code == 400
|
|
@ -4,6 +4,6 @@ from argostranslate import package
|
||||||
|
|
||||||
def test_boot_argos():
|
def test_boot_argos():
|
||||||
"""Test Argos translate models initialization"""
|
"""Test Argos translate models initialization"""
|
||||||
boot()
|
boot(["en", "es"])
|
||||||
|
|
||||||
assert len(package.get_installed_packages()) > 2
|
assert len(package.get_installed_packages()) >= 2
|
||||||
|
|
Loading…
Reference in a new issue