Skip to content

Commit

Permalink
add: unittesting for the inscriptis web service.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertWeichselbraun committed Feb 17, 2024
1 parent 1fe8da7 commit 16ba471
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/inscriptis/service/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
@app.get("/")
def index():
"""Print a short status message for the Web service's base URL."""
return "Inscriptis text to HTML Web service."
return PlainTextResponse("Inscriptis text to HTML Web service.")


@app.post("/get_text", response_class=PlainTextResponse)
async def get_text_call(request: Request):
"""Return the text representation of the given HTML content."""
content_type = request.headers.get("Content-type")
if "; encoding=" in content_type:
encoding = content_type.split("; encoding=")[1]
if "; charset=" in content_type:
encoding = content_type.split("; charset=")[1]
else:
encoding = "UTF-8"
html_content = await request.body()
Expand Down
47 changes: 47 additions & 0 deletions tests/test_web_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
from fastapi.testclient import TestClient
from inscriptis.service.web import app
from inscriptis.metadata import __version__

# Replace "your_module" with the actual module name where your FastAPI app is defined.


@pytest.fixture
def client():
return TestClient(app)


def test_index(client):
response = client.get("/")
assert response.status_code == 200
assert response.text == "Inscriptis text to HTML Web service."


def test_get_text_call_with_content_type(client):
html_content = "<html><body>Österliche Freuden!</body></html>"
response = client.post(
"/get_text",
content=html_content,
headers={"Content-type": "text/html; charset=UTF-8"},
)
assert response.status_code == 200
assert response.text == "Österliche Freuden!"


def test_get_text_call_without_content_type(client):
html_content = "<html><body>Hello World!</body></html>"
response = client.post(
"/get_text",
content=html_content,
headers={"Content-type": "text/html"},
)
assert response.status_code == 200
assert response.text == "Hello World!"


def test_get_version_call(client):
response = client.get("/version")
assert response.status_code == 200
assert (
response.text == __version__
) # Assuming your ParserConfig has a version attribute
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ envlist = pytest, pyroma, flake8
[testenv:pytest]
deps = pytest ~= 7.4.4
pytest-cov ~= 4.1.0
fastapi ~= 0.109.2
httpx ~= 0.26.0
commands = pytest --cov-config=.coveragerc --cov=inscriptis ./tests

# python packaging best practices
Expand Down

0 comments on commit 16ba471

Please sign in to comment.