A library for creating fluent, readable, and composable checks for your tests or application logic.
Project description
Fluent Checks
A Python library for creating fluent, readable, and composable checks for your tests or application logic.
Installation
Install the package using pip:
pip install fluent-checks
Core Concepts
The core of the library is the Check class. A Check is a simple wrapper around a function that returns a boolean (a Condition).
from fluent_checks import Check
# Create a check from a lambda
is_even = Check(lambda: 2 % 2 == 0)
# Evaluate the check
if is_even:
print("It's even!")
# Or more explicitly
assert is_even.as_bool() is True
Usage
Combining Checks
Checks can be combined using logical operators to create more complex conditions.
a = Check(lambda: True)
b = Check(lambda: False)
assert (a & b).as_bool() is False # And
assert (a | b).as_bool() is True # Or
assert (~b).as_bool() is True # Not
Waiting for Conditions
You can wait for a condition to become true.
import time
start_time = time.time()
flaky_check = Check(lambda: time.time() - start_time > 2)
# wait_for will block until the check is true, or the timeout is reached.
assert flaky_check.wait_for(timeout=3) is True
assert flaky_check.wait_for(timeout=1) is False
Timeouts and Deadlines
You can enforce time limits on checks.
from datetime import datetime, timedelta
# This check will raise a TimeoutException if it takes longer than 1 second
slow_check = Check(lambda: time.sleep(2) or True)
failing_check = slow_check.with_timeout(1)
try:
failing_check.as_bool()
except TimeoutException:
print("Caught expected timeout!")
# You can also use a specific deadline
deadline = datetime.now() + timedelta(seconds=1)
check_with_deadline = slow_check.with_deadline(deadline)
Repeating Checks
You can verify that a condition holds true multiple times.
# succeeds_within: True if the check passes at least once in 5 attempts
flaky_check = Check(lambda: random.random() > 0.5)
assert flaky_check.succeeds_within(5)
# is_consistent_for: True if the check passes 5 times in a row
stable_check = Check(lambda: True)
assert stable_check.is_consistent_for(5)
Checking for Exceptions
You can check that a piece of code raises a specific exception.
def might_fail():
raise ValueError("Something went wrong")
check = Check(might_fail).raises(ValueError)
assert check.as_bool() is True
API Overview
Check(condition: Callable[[], bool]): The base class for all checks.&,|,~: Operators for AND, OR, and NOT logic.as_bool() -> bool: Evaluates the check and returns the boolean result.wait_for(timeout: float) -> bool: Blocks until the check isTrueor the timeout expires.with_timeout(timeout: float) -> TimeoutCheck: Returns a new check that will raise aTimeoutExceptionif it doesn't complete within the timeout.with_deadline(deadline: datetime) -> DeadlineCheck: Similar towith_timeoutbut uses an absolute deadline.succeeds_within(times: int) -> RepeatingOrCheck: Checks if the condition is met at least once within a number of tries.is_consistent_for(times: int) -> RepeatingAndCheck: Checks if the condition is met consecutively for a number of tries.raises(exception: type[Exception]) -> RaisesCheck: Checks if the condition raises a specific exception.
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fluent_checks-0.1.3.tar.gz.
File metadata
- Download URL: fluent_checks-0.1.3.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da66b9f1f7832551b977e553d609f9fd6adab94e103f85523c8205bb2d35ebb2
|
|
| MD5 |
b535c86af4b37e2887c33d67ad596c9d
|
|
| BLAKE2b-256 |
5139d57c77d2570d89527a53818401c25261025851a9f23eb90bf87d2b2996c4
|
File details
Details for the file fluent_checks-0.1.3-py3-none-any.whl.
File metadata
- Download URL: fluent_checks-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cc76e84e3db0b9f8045ce2032d0cbcd8c12bb6f15606104246323a2ce096fd9
|
|
| MD5 |
0b8ecea23bcaf9d86947bd0cb55e8cf4
|
|
| BLAKE2b-256 |
628311ae821fabc39836f52cb409de35a68161b95d2727a989cdeecd50669c91
|