1
0
forked from clan/clan-core

Merge pull request 'Flake: Added python package deal as dependency' (#476) from Qubasa-main into main

This commit is contained in:
clan-bot 2023-11-08 19:16:58 +00:00
commit 4f39abd1de
10 changed files with 248 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
.direnv
***/.hypothesis
.coverage.*
**/qubeclan
**/testdir

View File

@ -1,6 +1,6 @@
{ ... }: {
perSystem = { pkgs, lib, ... }: {
packages = {
packages = rec {
# a script that executes all other checks
impure-checks = pkgs.writeShellScriptBin "impure-checks" ''
#!${pkgs.bash}/bin/bash
@ -15,6 +15,52 @@
cd "$ROOT/pkgs/clan-cli"
nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp python -m pytest -m impure -s ./tests'
'';
runMockApi = pkgs.writeShellScriptBin "run-mock-api" ''
#!${pkgs.bash}/bin/bash
set -euo pipefail
export PATH="${lib.makeBinPath [
pkgs.gitMinimal
pkgs.nix
pkgs.rsync # needed to have rsync installed on the dummy ssh server
pkgs.coreutils
pkgs.procps
]}"
ROOT=$(git rev-parse --show-toplevel)
cd "$ROOT/pkgs/clan-cli"
nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp python -m clan_cli webui --no-open --port 2979'
'';
runSchemaTests = pkgs.writeShellScriptBin "runSchemaTests" ''
#!${pkgs.bash}/bin/bash
set -euo pipefail
${runMockApi}/bin/run-mock-api &
MOCK_API_PID=$!
echo "Started mock api with pid $MOCK_API_PID"
function cleanup {
echo "Stopping server..."
pkill -9 -f "python -m clan_cli webui"
}
trap cleanup EXIT
export PATH="${lib.makeBinPath [
pkgs.gitMinimal
pkgs.nix
pkgs.rsync # needed to have rsync installed on the dummy ssh server
pkgs.procps
pkgs.coreutils
]}"
sleep 3
ROOT=$(git rev-parse --show-toplevel)
cd "$ROOT/pkgs/clan-cli"
nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp st auth login RHtr8nLtz77tqRP8yUGyf-Flv_9SLI'
nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp st run http://localhost:2979/openapi.json --experimental=openapi-3.1 --report --workers 8 --max-response-time=50 --request-timeout=1000 -M GET'
'';
};
};
}

View File

@ -172,6 +172,14 @@ nix build .#checks.x86_64-linux.clan-pytest --rebuild
This command will run all pure test functions.
### Running schemathesis fuzzer on GET requests
```bash
nix run .#runSchemaTests
```
If you want to test more request types edit the file `checks/impure/flake-module.nix`
### Inspecting the Nix Sandbox
If you need to inspect the Nix sandbox while running tests, follow these steps:
@ -192,6 +200,8 @@ If you need to inspect the Nix sandbox while running tests, follow these steps:
These debugging and testing methods will help you identify and fix issues in your backend code efficiently, ensuring the reliability and robustness of your application.
For more information on testing read [property and contract based testing](testing.md)
# Using this Template
To make the most of this template:

131
docs/testing.md Normal file
View File

@ -0,0 +1,131 @@
# Property vs Contract based testing
In this section, we'll explore the importance of testing the backend of your FastAPI application, specifically focusing on the advantages of using contract-based testing with property-based testing frameworks.
## Why Use Property-Based Testing?
Property-based testing is a powerful approach to test your APIs, offering several key benefits:
### 1. Scope
Instead of having to write numerous test cases for various input arguments, property-based testing enables you to test a range of arguments for each parameter using a single test. This approach significantly enhances the robustness of your test suite while reducing redundancy in your testing code. In short, your test code becomes cleaner, more DRY (Don't Repeat Yourself), and more efficient. It also becomes more effective as you can easily test numerous edge cases.
### 2. Reproducibility
Property-based testing tools retain test cases and their results, allowing you to reproduce and replay tests in case of failure. This feature is invaluable for debugging and ensuring the stability of your application over time.
## Frameworks for Property-Based Testing
To implement property-based testing in FastAPI, you can use the following framework:
- [Hypothesis: Property-Based Testing](https://hypothesis.readthedocs.io/en/latest/quickstart.html)
- [Schemathesis](https://schemathesis.readthedocs.io/en/stable/#id2)
## Example
Running schemathesis fuzzer on GET requests
```bash
nix run .#runSchemaTests
```
If you want to test more request types edit the file [flake-module.nix](../checks/impure/flake-module.nix)
After a run it will upload the results to `schemathesis.io` and give you a link to the report.
The credentials to the account are `Username: schemathesis@qube.email` and `Password:6tv4eP96WXsarF`
## Why Schemas Are Not Contracts
A schema is a description of the data structure of your API, whereas a contract defines not only the structure but also the expected behavior and constraints. The following resource explains why schemas are not contracts in more detail:
- [Why Schemas Are Not Contracts](https://pactflow.io/blog/schemas-are-not-contracts/)
In a nutshell, schemas may define the data structure but often fail to capture complex constraints and the expected interactions between different API endpoints. Contracts fill this gap by specifying both the structure and behavior of your API.
## Why Use Contract-Driven Testing?
Contract-driven testing combines the benefits of type annotations and property-based testing, providing a robust approach to ensuring the correctness of your APIs.
- Contracts become an integral part of the function signature and can be checked statically, ensuring that the API adheres to the defined contract.
- Contracts, like property-based tests, allow you to specify conditions and constraints, with the testing framework automatically generating test cases and verifying call results.
### Frameworks for Contract-Driven Testing
To implement contract-driven testing in FastAPI, consider the following framework and extension:
- [Deal: Contract Driven Development](https://deal.readthedocs.io/)
By adopting contract-driven testing, you can ensure that your FastAPI application not only has a well-defined structure but also behaves correctly, making it more robust and reliable.
- [Whitepaper: Python by contract](https://users.ece.utexas.edu/~gligoric/papers/ZhangETAL22PythonByContractDataset.pdf) This paper goes more into detail how it works
## Examples
You can annotate functions with `@deal.raises(ClanError)` to say that they can _only_ raise a ClanError Exception.
```python
import deal
@deal.raises(ClanError)
def get_task(uuid: UUID) -> BaseTask:
global POOL
return POOL[uuid]
```
To say that it can raise multiple exceptions just add after one another separated with a `,`
```python
import deal
@deal.raises(ClanError, IndexError, ZeroDivisionError)
def get_task(uuid: UUID) -> BaseTask:
global POOL
return POOL[uuid]
```
### Adding deal annotated functions to pytest
```python
from clan_cli.task_manager import get_task
import deal
@deal.cases(get_task) # <--- Add function get_task to testing corpus
def test_get_task(case: deal.TestCase) -> None:
case() # <--- Call testing framework with function
```
### Combining hypothesis with deal
You can combine hypothesis annotations with deal annotations to add example inputs to the function so that the verifier can reach deeper parts of the function.
```python
import hypothesis
import deal
@hypothesis.example(["8c3041e0-4512-4b30-aa8e-7be4a75b8b45", "5c2061e0-4512-4b30-aa8e-7be4a75b8b45"])
@deal.raises(ClanError)
def get_task(uuid: UUID) -> BaseTask:
global POOL
if "206" in str(uuid):
raise ValueError("206 should not be in here")
return POOL[uuid]
```
You can also annotate what kind of information a value can have by adding a hypothesis "strategy". Which kind of data generation hypothesis supports you can see here:
[Hypothesis Value Properties](https://hypothesis.readthedocs.io/en/latest/data.html)
The example above doesn't really need a hypothesis annotation because deal can infer from the UUID type a strategy.
But as an example how it might look like:
```python
from hypothesis.strategies import uuids
@hypothesis.given(uuids)
@deal.raises(ClanError)
def get_task(uuid: UUID) -> BaseTask:
global POOL
if "206" in str(uuid):
raise ValueError("206 should not be in here")
return POOL[uuid]
```
For a complex example for an [HTTP API look here](https://hypothesis.readthedocs.io/en/latest/examples.html#fuzzing-an-http-api). Just note we are using schemathesis for this already.
You can also add `pre` and `post` conditions. A `pre` condition must be true before the function is executed. A `post` condition must be true after the function was executed. For more information read the [Writing Contracts Section](https://deal.readthedocs.io/basic/values.html).

View File

@ -60,6 +60,22 @@
"type": "github"
}
},
"luispkgs": {
"locked": {
"lastModified": 1699470058,
"narHash": "sha256-//c1SEENoNFEDtp8x5lokNxsU9lZjyNkEf5k3OJADTs=",
"owner": "Luis-Hebendanz",
"repo": "nixpkgs",
"rev": "842a157b727ad9712d41a80d1e1564e4e6bbe697",
"type": "github"
},
"original": {
"owner": "Luis-Hebendanz",
"ref": "fix_python_deal",
"repo": "nixpkgs",
"type": "github"
}
},
"nixlib": {
"locked": {
"lastModified": 1693701915,
@ -117,6 +133,7 @@
"disko": "disko",
"flake-parts": "flake-parts",
"floco": "floco",
"luispkgs": "luispkgs",
"nixos-generators": "nixos-generators",
"nixpkgs": "nixpkgs",
"sops-nix": "sops-nix",

View File

@ -6,6 +6,10 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
#nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
# https://github.com/NixOS/nixpkgs/pull/265872
luispkgs.url = "github:Luis-Hebendanz/nixpkgs/fix_python_deal";
floco.url = "github:aakropotkin/floco";
floco.inputs.nixpkgs.follows = "nixpkgs";
disko.url = "github:nix-community/disko";
@ -29,6 +33,7 @@
"aarch64-darwin"
];
imports = [
./checks/flake-module.nix
./devShell.nix
./formatter.nix

View File

@ -12,6 +12,8 @@ from pathlib import Path
from typing import Any, Iterator, Optional, Type, TypeVar
from uuid import UUID, uuid4
import deal
from .custom_logger import ThreadFormatter, get_caller
from .errors import ClanError
@ -161,6 +163,8 @@ class TaskPool:
def __getitem__(self, uuid: UUID) -> BaseTask:
with self.lock:
if uuid not in self.pool:
raise ClanError(f"Task with uuid {uuid} does not exist")
return self.pool[uuid]
def __setitem__(self, uuid: UUID, task: BaseTask) -> None:
@ -175,6 +179,7 @@ class TaskPool:
POOL: TaskPool = TaskPool()
@deal.raises(ClanError)
def get_task(uuid: UUID) -> BaseTask:
global POOL
return POOL[uuid]

View File

@ -34,7 +34,8 @@
, gnupg
, e2fsprogs
, mypy
, cntr
, deal
, schemathesis
}:
let
@ -42,6 +43,8 @@ let
argcomplete # optional dependency: if not enabled, shell completion will not work
fastapi
uvicorn # optional dependencies: if not enabled, webui subcommand will not work
deal
schemathesis
];
pytestDependencies = runtimeDependencies ++ dependencies ++ [

View File

@ -1,19 +1,25 @@
{ inputs, ... }:
{
perSystem = { self', pkgs, ... }: {
devShells.clan-cli = pkgs.callPackage ./shell.nix {
inherit (self'.packages) clan-cli ui-assets nix-unit;
};
packages = {
clan-cli = pkgs.python3.pkgs.callPackage ./default.nix {
inherit (self'.packages) ui-assets;
inherit (inputs) nixpkgs;
perSystem = { self', pkgs, system, ... }:
let
luisPythonPkgs = inputs.luispkgs.legacyPackages.${system}.python3Packages;
in
{
devShells.clan-cli = pkgs.callPackage ./shell.nix {
inherit (self'.packages) clan-cli ui-assets nix-unit;
};
packages = {
clan-cli = pkgs.python3.pkgs.callPackage ./default.nix {
inherit (self'.packages) ui-assets;
inherit (inputs) nixpkgs;
deal = luisPythonPkgs.deal;
schemathesis = luisPythonPkgs.schemathesis;
};
inherit (self'.packages.clan-cli) clan-openapi;
default = self'.packages.clan-cli;
};
inherit (self'.packages.clan-cli) clan-openapi;
default = self'.packages.clan-cli;
};
checks = self'.packages.clan-cli.tests;
};
checks = self'.packages.clan-cli.tests;
};
}

View File

@ -0,0 +1,9 @@
import deal
from clan_cli.task_manager import get_task
# type annotations below are optional
@deal.cases(get_task)
def test_get_task(case: deal.TestCase) -> None:
case()