Skip to main content

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 is True or the timeout expires.
  • with_timeout(timeout: float) -> TimeoutCheck: Returns a new check that will raise a TimeoutException if it doesn't complete within the timeout.
  • with_deadline(deadline: datetime) -> DeadlineCheck: Similar to with_timeout but 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

fluent_checks-0.1.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fluent_checks-0.1.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fluent_checks-0.1.4.tar.gz.

File metadata

  • Download URL: fluent_checks-0.1.4.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for fluent_checks-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3ade32e246c571f69d42397001c54b6a45bd6f267a053aea8c481c63da21cf09
MD5 951d1b6d1a611dacca5ec19bed743463
BLAKE2b-256 6f16ceceeadc792cfb1eedcf5a14a091da8349410dd76bdf5924e30f82502b7d

See more details on using hashes here.

File details

Details for the file fluent_checks-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fluent_checks-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ba00051119d63eb8e9922736b814c76ec422c7bae872bbc8118b03882a790fd9
MD5 afb5e517f0a915ad9d28d7e8903d2d32
BLAKE2b-256 6db6198db6990e68986249a56456afcaf165c5e9f4495bad6ca95feae63c1a8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page