Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hanars committed Oct 27, 2024
1 parent 377ef55 commit 260bb4c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 22 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/vlm-unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: VLM Unit Tests

# Run the test suite on pushes (incl. merges) to master and dev
# Run the test suite when a PR is opened, pushed to, or reopened
on:
push:
branches:
- master
- dev
paths:
- 'vlm/**'
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'vlm/**'

jobs:
vlm:
runs-on: ubuntu-latest
container: hailgenetics/hail:0.2.128

steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip wheel
pip install -r vlm/requirements-test.txt
- name: Run coverage tests
run: |
coverage run --source="./vlm" --omit="./vlm/__main__.py" -m pytest vlm/
coverage report --fail-under=99
24 changes: 2 additions & 22 deletions vlm/__main__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
from aiohttp import web
import logging
import traceback

logger = logging.getLogger(__name__)


def _handle_exception(e, request):
logger.error(f'{request.headers.get("From")} "{e}"')
raise e


@web.middleware
async def error_middleware(request, handler):
try:
return await handler(request)
except web.HTTPError as e:
_handle_exception(e, request)
except Exception as e:
error_reason = f'{e}: {traceback.format_exc()}'
_handle_exception(web.HTTPInternalServerError(reason=error_reason), request)
from vlm.web_app import init_web_app


async def status(request: web.Request) -> web.Response:
Expand All @@ -27,10 +10,7 @@ async def status(request: web.Request) -> web.Response:

def run():
logging.basicConfig(level=logging.INFO)
app = web.Application(middlewares=[error_middleware], client_max_size=(1024 ** 2) * 10)
app.add_routes([
web.get('/status', status),
])
app = init_web_app()
web.run_app(
app,
host='0.0.0.0', # nosec
Expand Down
15 changes: 15 additions & 0 deletions vlm/test_vlm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from aiohttp.test_utils import AioHTTPTestCase

from vlm.web_app import init_web_app


class VlmTestCase(AioHTTPTestCase):

async def get_application(self):
return await init_web_app()

async def test_status(self):
async with self.client.request('GET', '/status') as resp:
self.assertEqual(resp.status, 200)
resp_json = await resp.json()
self.assertDictEqual(resp_json, {'success': True})
33 changes: 33 additions & 0 deletions vlm/web_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from aiohttp import web
import logging
import traceback

logger = logging.getLogger(__name__)


def _handle_exception(e, request):
logger.error(f'{request.headers.get("From")} "{e}"')
raise e


@web.middleware
async def error_middleware(request, handler):
try:
return await handler(request)
except web.HTTPError as e:
_handle_exception(e, request)
except Exception as e:
error_reason = f'{e}: {traceback.format_exc()}'
_handle_exception(web.HTTPInternalServerError(reason=error_reason), request)


async def status(request: web.Request) -> web.Response:
return web.json_response({'success': True})


async def init_web_app():
app = web.Application(middlewares=[error_middleware], client_max_size=(1024 ** 2) * 10)
app.add_routes([
web.get('/status', status),
])
return app

0 comments on commit 260bb4c

Please sign in to comment.