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.3.tar.gz (3.9 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.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

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

Hashes for fluent_checks-0.1.3.tar.gz
Algorithm Hash digest
SHA256 da66b9f1f7832551b977e553d609f9fd6adab94e103f85523c8205bb2d35ebb2
MD5 b535c86af4b37e2887c33d67ad596c9d
BLAKE2b-256 5139d57c77d2570d89527a53818401c25261025851a9f23eb90bf87d2b2996c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fluent_checks-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8cc76e84e3db0b9f8045ce2032d0cbcd8c12bb6f15606104246323a2ce096fd9
MD5 0b8ecea23bcaf9d86947bd0cb55e8cf4
BLAKE2b-256 628311ae821fabc39836f52cb409de35a68161b95d2727a989cdeecd50669c91

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