Skip to content

Commit

Permalink
Add API Tests (#91)
Browse files Browse the repository at this point in the history
* Add API Tests

* Update action
  • Loading branch information
wjiuhe committed Jun 3, 2022
1 parent ae30fba commit 1edce32
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/api-test.yml
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 added tests/api_tests/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/api_tests/app.py
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")
26 changes: 26 additions & 0 deletions tests/api_tests/conftest.py
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()
8 changes: 8 additions & 0 deletions tests/api_tests/test_list.py
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
10 changes: 10 additions & 0 deletions tests/api_tests/test_predict.py
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"

0 comments on commit 1edce32

Please sign in to comment.