Skip to content

Commit

Permalink
New unit test #46
Browse files Browse the repository at this point in the history
  • Loading branch information
heming-langrenn committed Jul 14, 2024
1 parent b1299ee commit 46b2dd3
Show file tree
Hide file tree
Showing 8 changed files with 935 additions and 981 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Look to the [openAPI specification](./specification.yaml) for the details.
Start the server locally:

```Shell
% poetry run adev runserver -p 8080 --aux-port 8089 photo_service
poetry run adev runserver -p 8080 --aux-port 8089 photo_service
docker-compose up user-service mongodb
```

## Running the API in a wsgi-server (gunicorn)
Expand Down
2 changes: 2 additions & 0 deletions photo_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Ping,
Ready,
StatusView,
UnitTestView,
)


Expand Down Expand Up @@ -55,6 +56,7 @@ async def create_app() -> web.Application:
web.view("/photos", PhotosView),
web.view("/photos/{photoId}", PhotoView),
web.view("/status", StatusView),
web.view("/unit_test", UnitTestView),
]
)

Expand Down
10 changes: 9 additions & 1 deletion photo_service/services/config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigService:

@classmethod
async def get_all_configs(
cls: Any, db: Any, event_id: Optional[str]
cls: Any, db: Any, event_id: Optional[str] = None
) -> List[Config]:
"""Get all config function."""
config: List[Config] = []
Expand Down Expand Up @@ -56,6 +56,14 @@ async def create_config(cls: Any, db: Any, config: Config) -> Optional[str]:
# Validation:
if config.id:
raise IllegalValueException("Cannot create config with input id.") from None
old_config = await ConfigAdapter.get_config_by_key(
db, config.event_id, config.key
)
if old_config:
raise IllegalValueException(
f"Config with key {config.key} already exists on event {config.event_id}"
) from None

# create id
id = create_id()
config.id = id
Expand Down
1 change: 1 addition & 0 deletions photo_service/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .liveness import Ping, Ready
from .photos import PhotosView, PhotoView
from .status import StatusView
from .unit_test import UnitTestView
137 changes: 137 additions & 0 deletions photo_service/views/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""Resource module for configs resources."""
import os
from typing import Any

from aiohttp.web import (
Response,
View,
)
from dotenv import load_dotenv

from photo_service.models import Config, Status
from photo_service.services import (
ConfigService,
StatusService,
)

load_dotenv()
HOST_SERVER = os.getenv("HOST_SERVER", "localhost")
HOST_PORT = os.getenv("HOST_PORT", "8080")
BASE_URL = f"http://{HOST_SERVER}:{HOST_PORT}"


class UnitTestView(View):
"""Class representing unit test resource."""

async def get(self) -> Response:
"""Get route function."""
db = self.request.app["db"]
try:
domain = self.request.rel_url.query["domain"]
action = int(self.request.rel_url.query["action"])
except Exception as e:
body = f"Usage - Use url params action and domain: {e}"
return Response(status=400, body=body)

if domain == "config":
try:
body = await test_config(db, action)
return Response(status=201, body=body)
except Exception as e:
body = f"ERROR Config: {e}"
return Response(status=500, body=body)
elif domain == "status":
try:
body = await test_status(db, action)
return Response(status=201, body=body)
except Exception as e:
body = f"ERROR Status: {e}"
return Response(status=500, body=body)

body = "Test domains: config, status"
return Response(status=200, body=body)


async def test_config(db: Any, action: int) -> str:
"""Class representing unit test for config resource."""
body = ""
config_dict = {
"event_id": "1e95458c-e000-4d8b-beda-f860c77fd758",
"key": "photo_location",
"value": "2024 Ragde-sprinten",
}
config = Config.from_dict(config_dict)

if action == 0:
body = "Config: 1-create,2-get-all,3-get-all-by-event,4-update,5-delete,6-get-by-key"
elif action == 1:
# create config
result = await ConfigService.create_config(db, config)
body = f"Config created: {result}"
elif action == 2:
# get all config
result = await ConfigService.get_all_configs(db) # type: ignore
body = f"Configs: {result}"
elif action == 3:
# get all config by event
event_id = config_dict["event_id"]
result = await ConfigService.get_all_configs(db, event_id) # type: ignore
body = f"Configs by event: {result}"
elif action == 4:
# update config
configs = await ConfigService.get_all_configs(db)
new_config = configs[0]
new_config.value = "DENNE ER OPPDATERT!"
result = await ConfigService.update_config(db, new_config) # type: ignore
body = f"Config updated: {result} - {new_config}"
elif action == 5:
# delete config
configs = await ConfigService.get_all_configs(db)
new_config = configs[0]
result = await ConfigService.delete_config(db, new_config.id) # type: ignore
body = f"Config deleted: {result} - {new_config}"
elif action == 6:
# get config by key
event_id = config_dict["event_id"]
key = config_dict["key"]
result = await ConfigService.get_config_by_key(db, event_id, key) # type: ignore
body = f"Config: {result}"
return body


async def test_status(db: Any, action: int) -> str:
"""Class representing unit test for status resource."""
body = ""
status_dict = {
"event_id": "1e95458c-e000-4d8b-beda-f860c77fd758",
"time": "2022-09-25T16:41:52",
"type": "video_status",
"message": "2022 Ragde-sprinten",
}
status = Status.from_dict(status_dict)

if action == 0:
body = "Status: 1-create,2-get-all,3-get-all-by-type,5-delete"
elif action == 1:
# create status
result = await StatusService.create_status(db, status)
body = f"Status created: {result}"
elif action == 2:
# get all status
event_id = status_dict["event_id"]
result = await StatusService.get_all_status(db, event_id, 25) # type: ignore
body = f"Statuses: {result}"
elif action == 3:
# get all status by type
event_id = status_dict["event_id"]
type = status_dict["type"]
result = await StatusService.get_all_status_by_type(db, event_id, type, 25) # type: ignore
body = f"Statuses by type: {result}"
elif action == 5:
# delete status
event_id = status_dict["event_id"]
statuss = await StatusService.get_all_status(db, event_id, 1)
new_status = statuss[0]
result = await StatusService.delete_status(db, new_status.id) # type: ignore
body = f"Status deleted: {result} - {new_status}"
return body
Loading

0 comments on commit 46b2dd3

Please sign in to comment.