Skip to content

Commit

Permalink
Merge pull request #85 from leozqin/unit-test
Browse files Browse the repository at this point in the history
Add Unit Tests and Async-ify about and health check endpoints
  • Loading branch information
leozqin authored Dec 7, 2024
2 parents 1ff7112 + 576182e commit d3854b1
Show file tree
Hide file tree
Showing 22 changed files with 510 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
source=app
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Integration Test
name: Full Test
on:
workflow_call:
inputs:
Expand All @@ -14,16 +14,16 @@ on:

env:
IMAGE_NAME: precis
#

jobs:
integration_test:
test:
runs-on: ubuntu-latest
permissions:
contents: read
env:
PRECIS_STORAGE_HANDLER: ${{ inputs.storage_handler }}
CONFIG_DIR: ${{ github.workspace }}/tests/integration/config
DATA_DIR: ${{ github.workspace }}
DATA_DIR: ${{ github.workspace }}/data
RSS_BASE_URL: http://localhost:8000
steps:
- name: checkout
Expand All @@ -33,7 +33,12 @@ jobs:
pip install uv && uv venv
source .venv/bin/activate
make install-ci
- name: test
- name: unit_test
run: |
source .venv/bin/activate
pytest -vvv -cov
make clean
- name: integration_test
run: |
source .venv/bin/activate
mv tests/integration/config/settings-${{ inputs.content_retrieval_handler }}-${{ inputs.llm_handler}}.yml tests/integration/config/settings.yml
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,73 @@ on:

jobs:
integration-test-tinydb-playwright-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: tinydb
content_retrieval_handler: playwright
llm_handler: "null"
integration-test-lmdb-playwright-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: lmdb
content_retrieval_handler: playwright
llm_handler: "null"
integration-test-hybrid-playwright-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: hybrid
content_retrieval_handler: playwright
llm_handler: "null"
integration-test-tinydb-requests-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: tinydb
content_retrieval_handler: requests
llm_handler: "null"
integration-test-lmdb-requests-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: lmdb
content_retrieval_handler: requests
llm_handler: "null"
integration-test-hybrid-requests-null:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: hybrid
content_retrieval_handler: requests
llm_handler: "null"
integration-test-tinydb-playwright-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: tinydb
content_retrieval_handler: playwright
llm_handler: dummy
integration-test-lmdb-playwright-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: lmdb
content_retrieval_handler: playwright
llm_handler: dummy
integration-test-hybrid-playwright-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: hybrid
content_retrieval_handler: playwright
llm_handler: dummy
integration-test-tinydb-requests-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: tinydb
content_retrieval_handler: requests
llm_handler: dummy
integration-test-lmdb-requests-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: lmdb
content_retrieval_handler: requests
llm_handler: dummy
integration-test-hybrid-requests-dummy:
uses: ./.github/workflows/integration_test.yml
uses: ./.github/workflows/full_test.yml
with:
storage_handler: hybrid
content_retrieval_handler: requests
Expand Down
35 changes: 23 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ repos:
- id: isort
name: isort (python)
args: ["--profile", "black"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: detect-private-key
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: detect-private-key
- repo: local
hooks:
# Run the linter.
- id: ruff
name: lint
language: python
types_or: [python, pyi, jupyter]
entry: ruff check --force-exclude
args: [ --fix ]
# Run the formatter.
- id: ruff-format
name: format
language: python
types_or: [python, pyi, jupyter]
entry: ruff format --force-exclude
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ build:

.PHONY: clean
clean:
rm *.mdb db.json
rm -r ${DATA_DIR}

.PHONY: test
test:
Expand Down
22 changes: 2 additions & 20 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ async def favicon():

@app.get("/", response_class=HTMLResponse)
async def root(request: Request):

settings = await bk.get_settings()

if settings.get("finished_onboarding"):

return templates.TemplateResponse(
"index.html",
{
Expand All @@ -87,7 +85,6 @@ async def root(request: Request):
)

else:

return RedirectResponse("/onboarding/")


Expand All @@ -99,7 +96,7 @@ async def about(
"settings": await bk.get_settings(),
"update_status": update_status,
"update_exception": update_exception,
**bk.about(),
**await bk.about(),
}

if request.headers.get("accept") == JSON:
Expand All @@ -112,8 +109,7 @@ async def about(

@app.get("/health", response_model=HealthCheck)
async def health_check(request: Request) -> HealthCheck:

return bk.health_check()
return await bk.health_check()


@app.get("/onboarding/")
Expand Down Expand Up @@ -235,7 +231,6 @@ async def handler_settings(
async def update_handler(
handler: Annotated[str, Form()], config: Annotated[str, Form()], request: Request
):

try:
await bk.update_handler(handler=handler, config=config)

Expand All @@ -246,7 +241,6 @@ async def update_handler(
status_code=status.HTTP_303_SEE_OTHER,
)
except Exception as e:

return RedirectResponse(
request.url_for("handler_settings", handler=handler).include_query_params(
update_exception=e
Expand All @@ -257,7 +251,6 @@ async def update_handler(

@app.get("/api/refresh_feed/{feed_id}", status_code=status.HTTP_200_OK)
async def refresh_feed(feed_id: str, request: Request):

await rss.check_feed_by_id(id=feed_id)

return RedirectResponse(
Expand All @@ -270,7 +263,6 @@ async def refresh_feed(feed_id: str, request: Request):

@app.get("/api/delete_feed/{feed_id}", status_code=status.HTTP_200_OK)
async def delete_feed(feed_id: str, request: Request):

await bk.delete_feed(feed_id=feed_id)

return RedirectResponse(
Expand Down Expand Up @@ -315,7 +307,6 @@ async def update_settings(
status_code=status.HTTP_303_SEE_OTHER,
)
except Exception as e:

return RedirectResponse(
request.url_for("settings").include_query_params(update_exception=e),
status_code=status.HTTP_303_SEE_OTHER,
Expand Down Expand Up @@ -371,23 +362,20 @@ async def update_feed(

@app.get("/api/export_opml/", status_code=status.HTTP_200_OK)
async def export_opml(request: Request):

write_path, file_name = await rss.feeds_to_opml()

return FileResponse(path=write_path, filename=file_name)


@app.get("/api/backup/", status_code=status.HTTP_200_OK)
async def backup(request: Request):

write_path, file_name = await rss.backup()

return FileResponse(path=write_path, filename=file_name)


@app.post("/api/restore/", status_code=status.HTTP_200_OK)
async def restore(request: Request, file: UploadFile):

try:
await rss.restore(file=file.file)

Expand All @@ -404,7 +392,6 @@ async def restore(request: Request, file: UploadFile):

@app.post("/api/import_opml/", status_code=status.HTTP_200_OK)
async def import_opml(request: Request, file: UploadFile):

try:
await rss.opml_to_feeds(file=file.file)

Expand All @@ -413,7 +400,6 @@ async def import_opml(request: Request, file: UploadFile):
status_code=status.HTTP_303_SEE_OTHER,
)
except Exception as e:

return RedirectResponse(
request.url_for("feeds").include_query_params(update_exception=e),
status_code=status.HTTP_303_SEE_OTHER,
Expand All @@ -424,7 +410,6 @@ async def import_opml(request: Request, file: UploadFile):
async def feeds(
request: Request, update_status: bool = False, update_exception: str = None
):

return templates.TemplateResponse(
"feeds.html",
{
Expand Down Expand Up @@ -477,13 +462,11 @@ async def new_feed(request: Request, update_exception: str = None):

@app.get("/util/list-feeds", status_code=status.HTTP_200_OK)
async def list_feeds(request: Request) -> Sequence[Mapping]:

return bk.list_feeds()


@app.get("/util/list-feed-entries", status_code=status.HTTP_200_OK)
async def list_feed_entries(request: Request) -> Sequence[Mapping]:

all_feeds = bk.list_feeds()

entries = [list(bk.list_entries(feed["id"])) for feed in all_feeds]
Expand All @@ -493,7 +476,6 @@ async def list_feed_entries(request: Request) -> Sequence[Mapping]:

@app.get("/util/list-handlers", status_code=status.HTTP_200_OK)
async def list_handlers(request: Request) -> Sequence[Mapping]:

handlers = bk.get_handlers()

# config might have secrets so we only return if its configured
Expand Down
Loading

0 comments on commit d3854b1

Please sign in to comment.