Code Monkey home page Code Monkey logo

pydantic_hypothesis's Introduction

Consider the following pydantic dataclass (see tests/contract.py for full code)

@dataclass
class Contract:
    name: str
    description: str
    contractors: List[str]
    start_date: date
    end_date: date

    @validator("name", "description")
    def name_must_not_be_empty(cls, name):
        pass

    @validator("contractors")
    def must_have_at_least_one_contractor(cls, contractors):
        pass

    @validator("end_date")
    def end_date_must_not_before_start_date(cls, end_date, values):
        pass

One can not easily use hypothesis.from_type(Contractor) for it given the three validations.

One solution candidate is to code up a customized hypothesis strategies builder for this Contractor type, something like this:

def get_contract():
    return st.builds(
        Contract,
        name=st.from_regex(".* .*"),
        description=st.from_regex(".* .*"),
        contractors=st.lists(st.text(), min_size=1),
        start_date=st.dates(max_value=date(2010, 12, 31)),
        end_date=st.dates(min_value=date(2011, 1, 1)),
    )

@given(contract=get_contract())
def test_with_explicit_registeration(contract):
    pass

Run it in tests/test_with_explicit_builder to see

Another solution will be to just use from_type as it is, but add an extra layer so that if one generation fails the validation, try again. e.g.

def from_pydantic_type(type_name):
    x = strategies.from_type(type_name)
    old_do_draw = x.do_draw

    def do_draw(data):
        try:
            return old_do_draw(data)
        except ValueError as e:
            print(e)
            raise UnsatisfiedAssumption()

    x.do_draw = do_draw

    return x

@given(contract=from_pydantic_type(Contract))
def test_with_pydantic_type_syntax(contract):
    pass

pydantic_hypothesis's People

Stargazers

Francis T. O'Donovan avatar

Watchers

Chihang Wang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.