Skip to content

Commit

Permalink
Add initial test suite (#21)
Browse files Browse the repository at this point in the history
* adding deps

Signed-off-by: Matthias Wessendorf <[email protected]>

* Adding simple test... that works w/ 'poetry run pytest'

Signed-off-by: Matthias Wessendorf <[email protected]>

* some try...

Signed-off-by: Matthias Wessendorf <[email protected]>

* explicitly choose function scope

* add full server test for static functions

* add full server test for instanced http functions

* update README with test suite instructions

* lockfile

* cleanup

---------

Signed-off-by: Matthias Wessendorf <[email protected]>
Co-authored-by: Luke Kingland <[email protected]>
  • Loading branch information
matzew and lkingland authored Dec 11, 2024
1 parent 12894a1 commit f359836
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 7 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@ Python as a network service.
```
.
├── cmd
│   └── fhttp - Example a function using the http middleware
│   └── fhttp - Example a Function using the http middleware
├── src/func_python
│   ├── http.py - HTTP Middleware
└── README.md - This Readme
│   ├── http.py - HTTP Middleware
├── tests
│   ├── http_test.py - HTTP Middleware tests
└── README.md - This Readme
```

## Development

Minimal example of running the test Function:
## Tests

Run suite:
`poetry run pytest`

To enable more granular log levels:
`poetry run pytest --log-cli-level=INFO`

## Example Main

Minimal example of running the test command, which shows how this
library is used when integrate with Functions, and can be useful during dev.

- install `poetry` via `pipx`
- install dependencies with `poetry install`
Expand All @@ -27,6 +40,7 @@ Minimal example of running the test Function:
- run the example via `poetry run python cmd/fhttp/main.py`
- deactivate the virtual environment with `exit`

## Suggested Flow

A nice method of development using git worktrees:

Expand Down
185 changes: 183 additions & 2 deletions poetry.lock

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

7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ repository = "https://github.com/knative-extensions/func-python"
python = "^3.12"
hypercorn = "^0.17.3"

[tool.pytest.ini_options]
pythonpath = ["src"]

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.4"
httpx = "^0.28.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
6 changes: 5 additions & 1 deletion src/func_python/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def serve(f):
if f.__name__ == 'new':
return ASGIApplication(f()).serve()
elif f.__name__ == 'handle':
return ASGIApplication(DefaultFunction(f)).serve()
try:
return ASGIApplication(DefaultFunction(f)).serve()
except Exception as e:
logging.error(f"Server failed to start: {e}")
raise
else:
raise ValueError("function must be either be a constructor 'new' or a "
"handler 'handle'.")
Expand Down
Loading

0 comments on commit f359836

Please sign in to comment.