Skip to main content

Partial matching of any object

Project description

GitHub Actions Contributors Forks Stargazers Issues MIT License


Expyct

Partial matching of any Python object.
Explore the docs »

Report Bug · Request Feature

Table of Contents

  1. Expyct
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

Expyct

Partial matching of any Python object.

import expyct as exp


def test_my_function():
    result = my_function()

    assert result == exp.Float(optional=True, close_to=0.076, error=0.01)

Using Expyct is a good idea when you need to assert something in a test case but there is some non-determinism.

For example, rounding errors prevent you from comparing a float exactly. Or a timestamp is created on-the-fly, and therefore changes every test run.

In these cases, you need to be able to set specific constraints on the expected value. That is what Expyct is for!

The constraints can be provided as constructor arguments. For example n == Number(min=3, max=5) is only true when n is between 3 and 5.

Some other examples of classes are Float, String, Any and DateTime. As you can see, they closely match the built-in Python types.

The library also comes with many commonly used data validators like ANY_UUID which matches any UUID string. And TODAY which matches any datetime occurring on the current day.

Checking nested data structures is easy as well.

See Usage examples

Getting Started

Prerequisites

Supported and tested for:

  • Python 3.6
  • Python 3.7
  • Python 3.8
  • Python 3.9

Installation

pip install expyct

Or install using any Python package manager like conda, pipenv or poetry.

Usage

See below examples of how to use Expyct with pytest.

Simple example:

import expyct as exp
from myclass import MyClass


def test_my_function():
    result = my_function()

    assert result == exp.AnyValue(instance_of=MyClass, vars={"property": "value"})

More complicated nested example:

import expyct as exp
from datetime import datetime


def test_my_function():
    result = my_function()

    assert result == {
        "first_name": exp.String(regex="(mary)|(peter)", ignore_case=True),
        "last_name": "Johnson",
        "signup_date": exp.DateTime(after=datetime(2020, 1, 2), before=datetime(2020, 3, 5)),
        "details": {
            "number": exp.Int(min=2),
            "amount": exp.Float(close_to=2.3, error=0.001),
            "purchases": exp.List(exp.Dict(keys={"id", "product", "category"}), non_empty=True),
        },
        "time_of_purchase": exp.OneOf([exp.TODAY, exp.THIS_HOUR]),
        "type": exp.AnyType(subclass_of=str),
        "item_ids": exp.Set(subset_of=[1, 2, 3]),
        "metadata": exp.Dict(keys_any=exp.Collection(superset_of=["a", "b"])),
        "context": exp.ANY,
    }

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Bump the version in expyct/__version__.py following SemVer
  5. Push the Branch (git push origin feature/AmazingFeature)
  6. Open a Pull Request

Before starting to contribute to Expyct, please install pre-commit to make sure your changes get checked for style and standards before committing them to repository:

$ pre-commit install

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Please file an issue on Github.

Acknowledgements

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

expyct-1.1.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

expyct-1.1.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file expyct-1.1.0.tar.gz.

File metadata

  • Download URL: expyct-1.1.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.12.0 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for expyct-1.1.0.tar.gz
Algorithm Hash digest
SHA256 db089ddbedf01b60548bbbb5d1ab0e342562e3f197b634a7194da1e201dcecd7
MD5 188b75ba84b3065517eb7070d42958a7
BLAKE2b-256 e82d32d0695b9802606da6bb049c5ce4ff8106f45d65b23bac8ec1ebb40639f7

See more details on using hashes here.

File details

Details for the file expyct-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: expyct-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.12.0 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for expyct-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b5b664577edc5442514568774905a9654803febbd9c8632d453ad24212d2bb1
MD5 cc15e81b099beeb2138daae9ae1553a3
BLAKE2b-256 4c86304b38a8be0a8bfe9f7c157047bf1cbccc7c24aa64124bad5ce97d1258ed

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