-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add API Tests * Update action
- Loading branch information
Showing
6 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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,41 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a single version of Python | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: API Test | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
test: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
- name: Install dependencies | ||
run: | | ||
sudo apt-get update | ||
python -m pip install --upgrade pip | ||
pip install poetry | ||
poetry install | ||
poetry build | ||
mkdir -p /opt/workspace/tests | ||
cp -r tests/api_tests /opt/workspace/tests/ | ||
cp -r pytest.ini dist /opt/workspace/ | ||
cd /opt/workspace | ||
pip install dist/*.whl | ||
pip install pytest uvicorn | ||
- name: Test with pytest | ||
run: | | ||
cd /opt/workspace && echo $(pwd) && pytest -vvv tests/api_tests |
Empty file.
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,10 @@ | ||
from pinferencia import Server | ||
|
||
|
||
def predict(data: str) -> str: | ||
return data | ||
|
||
|
||
service = Server() | ||
service.register(model_name="test", model=predict) | ||
service.register(model_name="test", model=predict, version_name="v1") |
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,26 @@ | ||
import shlex | ||
import time | ||
from subprocess import Popen | ||
|
||
import pytest | ||
import requests | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def backend_port(): | ||
return 9999 | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="session") | ||
def backend(backend_port): | ||
args = shlex.split( | ||
f"uvicorn --port {backend_port} tests.api_tests.app:service" | ||
) | ||
p = Popen(args) | ||
for _ in range(60): | ||
try: | ||
requests.get(f"http://127.0.0.1:{backend_port}") | ||
except Exception: | ||
time.sleep(1) | ||
yield | ||
p.kill() |
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,8 @@ | ||
import requests | ||
|
||
|
||
def test(backend_port): | ||
response = requests.get(f"http://127.0.0.1:{backend_port}/v1/models") | ||
models = response.json() | ||
assert len(models) == 1 | ||
assert len(models[0]["versions"]) == 2 |
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,10 @@ | ||
import requests | ||
|
||
|
||
def test(backend_port): | ||
response = requests.post( | ||
f"http://127.0.0.1:{backend_port}/v1/models/test/predict", | ||
json={"data": "abc"}, | ||
) | ||
prediction = response.json() | ||
assert prediction["data"] == "abc" |