Skip to content

Commit

Permalink
Added unit tests for web server
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanHydrogen committed Aug 9, 2024
1 parent a70da08 commit 2e2f0a9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
12 changes: 6 additions & 6 deletions pyobs_cloudcover/web_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ def __init__(self, query_executor: CoverageQueryExecutor, url: str = "localhost"
self._url = url
self._port = port

app = web.Application()
app.add_routes([web.get("/query/point", self._point_query)])
app.add_routes([web.get("/query/area", self._area_query)])
self._runner = web.AppRunner(app)
self._app = web.Application()
self._app.add_routes([web.get("/query/point", self._point_query)])
self._app.add_routes([web.get("/query/area", self._area_query)])

def set_measurement(self, measurement: CloudCoverageInfo) -> None:
self._query_executor.set_measurement(measurement)

async def start(self) -> None:
await self._runner.setup()
site = web.TCPSite(self._runner, 'localhost', self._port)
runner = web.AppRunner(self._app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', self._port)
await site.start()

async def _point_query(self, request: web.Request) -> web.Response:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.1"
pytest-asyncio = "^0.21.1"
pytest-aiohttp = "^1.0.5"
mypy = "1.9.0"

[tool.mypy]
Expand Down Expand Up @@ -49,6 +50,7 @@ test = [
"pytest-cov>=4.1.0",
"pytest-mock>=3.11.1",
"pytest-asyncio>=0.21.1",
"pytest-aiohttp>=1.0.5",
"mypy>=1.9.0",
"testcontainers[influxdb]>=4.7.1",
"matplotlib"
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/web_api/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
from astroplan import Observer
import astropy.units as u


@pytest.fixture(scope='module')
def observer():
return Observer(latitude=51.559299 * u.deg, longitude=9.945472 * u.deg, elevation=201 * u.m)
6 changes: 0 additions & 6 deletions tests/unit/web_api/test_coverage_query_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
from pyobs_cloudcover.cloud_coverage_info import CloudCoverageInfo
from pyobs_cloudcover.web_api.coverage_query_executor import CoverageQueryExecutor


@pytest.fixture()
def observer():
return Observer(latitude=51.559299 * u.deg, longitude=9.945472 * u.deg, elevation=201 * u.m)


@pytest.fixture()
def obs_time():
return datetime.datetime(2024, 3, 9, 1, 48, 48, 297970)
Expand Down
120 changes: 120 additions & 0 deletions tests/unit/web_api/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from unittest.mock import Mock

import pytest
from astroplan import Observer

from pyobs_cloudcover.web_api.coverage_query_executor import CoverageQueryExecutor
from pyobs_cloudcover.web_api.server import Server


@pytest.mark.asyncio
async def test_point_query_radec(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.point_query_radec = Mock(return_value=False) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/point?ra=0.0&dec=0.0')

assert response.status == 200
data = await response.json()

assert data["value"] is False
assert data["obs_time"] == 0

@pytest.mark.asyncio
async def test_point_query_altaz(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.point_query_altaz = Mock(return_value=False) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/point?alt=0.0&az=0.0')

assert response.status == 200
data = await response.json()

assert data["value"] is False
assert data["obs_time"] == 0


@pytest.mark.asyncio
async def test_point_query_invalid(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.point_query_altaz = Mock(return_value=False) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/point?a=0.0&z=0.0')

assert response.status == 400


@pytest.mark.asyncio
async def test_area_query_radec(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.area_query_radec = Mock(return_value=0.0) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/area?ra=0.0&dec=0.0&radius=10.0')

assert response.status == 200
data = await response.json()

assert data["value"] == 0.0
assert data["obs_time"] == 0


@pytest.mark.asyncio
async def test_area_query_altaz(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.area_query_altaz = Mock(return_value=0.0) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/area?alt=0.0&az=0.0&radius=10.0')

assert response.status == 200
data = await response.json()

assert data["value"] == 0.0
assert data["obs_time"] == 0


@pytest.mark.asyncio
async def test_area_query_invalid_coords(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.area_query_altaz = Mock(return_value=0.0) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/area?a=0.0&z=0.0&radius=10.0')

assert response.status == 400


@pytest.mark.asyncio
async def test_area_query_invalid_coords(aiohttp_client, observer: Observer) -> None:
executor = CoverageQueryExecutor(observer=observer)
executor.area_query_altaz = Mock(return_value=0.0) # type: ignore
executor.get_obs_time = Mock(return_value=0)

server = Server(executor)

client = await aiohttp_client(server._app)
response = await client.get('/query/area?alt=0.0&az=0.0')

assert response.status == 400

0 comments on commit 2e2f0a9

Please sign in to comment.