Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Run Python Tests

on:
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Run tests
run: |
python -m unittest discover
26 changes: 24 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from decimal import Decimal, ROUND_UP
from typing import Annotated

from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi import FastAPI, Request, Response, Form, Query
from fastapi.responses import HTMLResponse, JSONResponse, Response
from fastapi import FastAPI, Request, Form, Query, APIRouter, Body, HTTPException

from pint import UnitRegistry
from pint.errors import DimensionalityError, UndefinedUnitError
Expand Down Expand Up @@ -148,4 +148,26 @@ async def convert(
)


json = APIRouter(prefix="/json")


@json.post("/convert")
async def convert_json(
request: Request,
quantity: Annotated[float, Body()],
from_unit: Annotated[str, Body()],
to_unit: Annotated[str, Body()],
) -> JSONResponse:
try:
result = UnitRegistry().Quantity(quantity, from_unit).to(to_unit)
result = Decimal(str(result.magnitude))
result = result.quantize(Decimal("0.0001"), rounding=ROUND_UP)
except Exception as e:
raise HTTPException(
status_code=422, detail=f"Error while converting: {str(e)}"
) from e
return {"result": result}


app.mount("/hx", hx)
app.include_router(json)
21 changes: 18 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from main import hx
from main import hx, app
from unittest import TestCase

from fastapi.testclient import TestClient
Expand All @@ -16,8 +16,7 @@ def test_documentation(self):
self.assertEqual(response.status_code, 200)

def test_suggest_units(self):
response = self.client.get(
"/suggestions", headers={"HX-Request": "true"})
response = self.client.get("/suggestions", headers={"HX-Request": "true"})
self.assertEqual(response.status_code, 200)

def test_undefined_units(self):
Expand All @@ -35,3 +34,19 @@ def test_incompatible_conversions(self):
data={"quantity": 1, "from_unit": "second", "to_unit": "meter"},
)
self.assertEqual(response.status_code, 422)


class JsonApplicationTest(TestCase):
client = TestClient(app)
endpoint = "/json/convert"

def test_undefined_units(self):
from_unit = "foo"
to_unit = "bar"
json = {
"quantity": 1,
"from_unit": from_unit,
"to_unit": to_unit,
}
response = self.client.post(self.endpoint, json=json)
self.assertEqual(response.status_code, 422)