Skip to content

Commit

Permalink
WIP convert from Flask to Quart
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Nov 13, 2024
1 parent 7d1d312 commit 1f7a8fe
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 137 deletions.
148 changes: 147 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jsonschema = "^4.23.0"
requests = "^2.32.3"
filelock = "^3.15.4"
prometheus-client = "^0.20.0"
quart = "^0.19.8"
asyncio = "^3.4.3"

[tool.poetry.group.dev.dependencies]
Pygments = ">=2.10.0"
Expand Down
12 changes: 6 additions & 6 deletions src/dm_mac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import logging
from time import time

from flask import Flask
from flask import has_request_context
from flask import request
from flask.logging import default_handler
from quart import Quart
from quart import has_request_context
from quart import request
from quart.logging import default_handler

from dm_mac.models.machine import MachinesConfig
from dm_mac.models.users import UsersConfig
Expand Down Expand Up @@ -52,9 +52,9 @@ def format(self, record: logging.LogRecord) -> str:
api.register_blueprint(machineapi)


def create_app() -> Flask:
def create_app() -> Quart:
"""Factory to create the app."""
app: Flask = Flask("dm_mac")
app: Quart = Quart("dm_mac")
app.config.update({"MACHINES": MachinesConfig()})
app.config.update({"USERS": UsersConfig()})
app.config.update({"START_TIME": time()})
Expand Down
8 changes: 4 additions & 4 deletions src/dm_mac/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from logging import getLogger
from typing import Tuple

from flask import Blueprint
from flask import Response
from flask import current_app
from flask import jsonify
from quart import Blueprint
from quart import Response
from quart import current_app
from quart import jsonify

from dm_mac.models.users import UsersConfig

Expand Down
10 changes: 5 additions & 5 deletions src/dm_mac/views/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from typing import Tuple
from typing import cast

from flask import Blueprint
from flask import Response
from flask import current_app
from flask import jsonify
from flask import request
from quart import Blueprint
from quart import Response
from quart import current_app
from quart import jsonify
from quart import request

from dm_mac.models.machine import Machine
from dm_mac.models.machine import MachinesConfig
Expand Down
4 changes: 2 additions & 2 deletions src/dm_mac/views/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import Generator
from typing import Optional

from flask import Response
from flask import current_app
from quart import Response
from quart import current_app
from prometheus_client import CollectorRegistry
from prometheus_client import generate_latest
from prometheus_client.core import Metric
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"""Helpers for testing the Flask app."""
"""Helpers for testing the Quart app."""

import os
from pathlib import Path
from typing import Tuple
from unittest.mock import patch

from flask import Flask
from flask.testing import FlaskClient
from quart import Quart
from quart.testing import QuartClient

from dm_mac import create_app


def app_and_client(
tmp_path: Path, user_conf: str = "users.json", machine_conf: str = "machines.json"
) -> Tuple[Flask, FlaskClient]:
) -> Tuple[Quart, QuartClient]:
"""Test App - app instance configured for testing.
Doing this as a pytest fixture is a complete pain in the ass because I want
Expand Down
22 changes: 11 additions & 11 deletions tests/views/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
from shutil import copy
from unittest.mock import patch

from flask import Flask
from flask.testing import FlaskClient
from quart import Quart
from quart.testing import QuartClient
from freezegun import freeze_time
from werkzeug.test import TestResponse

from dm_mac.models.users import UsersConfig

from .flask_test_helpers import app_and_client
from .quart_test_helpers import app_and_client


class TestIndex:
"""Tests for API Index view."""

def test_index_response(self, tmp_path: Path) -> None:
"""Test for API index response."""
app: Flask
client: FlaskClient
app: Quart
client: QuartClient
app, client = app_and_client(tmp_path)
response: TestResponse = client.get("/api/")
assert response.status_code == 200
Expand All @@ -39,8 +39,8 @@ def test_no_change(self, tmp_path: Path, fixtures_path: str) -> None:
uconf: str = str(os.path.join(tmp_path, "users.json"))
copy(os.path.join(fixtures_path, "users.json"), uconf)
with patch.dict("os.environ", {"USERS_CONFIG": uconf}):
app: Flask
client: FlaskClient
app: Quart
client: QuartClient
app, client = app_and_client(tmp_path)
users: UsersConfig = app.config["USERS"]
users.load_time = 123456.0
Expand Down Expand Up @@ -68,8 +68,8 @@ def test_exception(self, tmp_path: Path, fixtures_path: str) -> None:
uconf: str = str(os.path.join(tmp_path, "users.json"))
copy(os.path.join(fixtures_path, "users.json"), uconf)
with patch.dict("os.environ", {"USERS_CONFIG": uconf}):
app: Flask
client: FlaskClient
app: Quart
client: QuartClient
app, client = app_and_client(tmp_path)
users: UsersConfig = app.config["USERS"]
users.load_time = 123456.0
Expand All @@ -90,8 +90,8 @@ def test_changed(self, tmp_path: Path, fixtures_path: str) -> None:
uconf: str = str(os.path.join(tmp_path, "users.json"))
copy(os.path.join(fixtures_path, "users.json"), uconf)
with patch.dict("os.environ", {"USERS_CONFIG": uconf}):
app: Flask
client: FlaskClient
app: Quart
client: QuartClient
app, client = app_and_client(tmp_path)
app.config["USERS"].load_time = 123456.0
copy(os.path.join(fixtures_path, "users-changed.json"), uconf)
Expand Down
Loading

0 comments on commit 1f7a8fe

Please sign in to comment.