How to use the pactman.mock.matchers.Matcher function in pactman

To help you get started, we’ve selected a few pactman examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github reecetech / pactman / pactman / mock / matchers.py View on Github external
class Matcher(object):
    """Base class for defining complex contract expectations."""

    def ruby_protocol(self):  # pragma: no cover
        """
        Serialise this Matcher for the Ruby mocking server.

        :rtype: any
        """
        raise NotImplementedError

    def generate_matching_rule_v3(self):  # pragma: no cover
        raise NotImplementedError


class EachLike(Matcher):
    """
    Expect the data to be a list of similar objects.

    Example:

    >>> from pactman import Consumer, Provider
    >>> pact = Consumer('consumer').has_pact_with(Provider('provider'))
    >>> (pact.given('there are three comments')
    ...  .upon_receiving('a request for the most recent 2 comments')
    ...  .with_request('get', '/comment', query={'limit': 2})
    ...  .will_respond_with(200, body={
    ...    'comments': EachLike(
    ...        {'name': Like('bob'), 'text': Like('Hello!')},
    ...        minimum=2)
    ...  }))
github reecetech / pactman / pactman / mock / matchers.py View on Github external
the request/response should be, and what should match for the requests.
        :rtype: dict
        """
        return {
            "json_class": "Pact::Term",
            "data": {
                "generate": self.generate,
                "matcher": {"json_class": "Regexp", "o": 0, "s": self.matcher},
            },
        }

    def generate_matching_rule_v3(self):
        return {"matchers": [{"match": "regex", "regex": self.matcher}]}


class Equals(Matcher):
    """
    Expect the value to be the same as matcher.

    Example:

    >>> from pactman import Consumer, Provider
    >>> pact = Consumer('consumer').has_pact_with(Provider('provider'))
    >>> (pact
    ...  .given('there is a random number generator')
    ...  .upon_receiving('a request for a random number')
    ...  .with_request('get', '/generate-number')
    ...  .will_respond_with(200, body={
    ...    'number': Equals(1111222233334444)
    ...  }))

    Would expect the response body to be a JSON object, containing the key
github reecetech / pactman / pactman / mock / matchers.py View on Github external
Parse the provided term into the JSON for the Ruby mock server.

    :param term: The term to be parsed.
    :type term: None, list, dict, int, float, str, unicode, Matcher
    :return: The JSON representation for this term.
    :rtype: dict, list, str
    """
    if term is None:
        return term
    elif isinstance(term, (str, int, float)):
        return term
    elif isinstance(term, dict):
        return {k: generate_ruby_protocol(v) for k, v in term.items()}
    elif isinstance(term, list):
        return [generate_ruby_protocol(t) for i, t in enumerate(term)]
    elif issubclass(term.__class__, (Matcher,)):
        return term.ruby_protocol()
    else:
        raise ValueError("Unknown type: %s" % type(term))
github reecetech / pactman / pactman / mock / matchers.py View on Github external
will return this value. When verified against the provider, the
            value will be asserted.
        :type matcher: None, list, dict, int, float, str
        """
        valid_types = (type(None), list, dict, int, float, str)
        assert isinstance(
            matcher, valid_types
        ), f"matcher must be one of '{valid_types}', got '{type(matcher)}'"

        self.matcher = matcher

    def generate_matching_rule_v3(self):
        return {"matchers": [{"match": "equality"}]}


class Includes(Matcher):
    """
    Expect the string value to contain the matcher.

    Example:

    >>> from pactman import Consumer, Provider
    >>> pact = Consumer('consumer').has_pact_with(Provider('provider'))
    >>> (pact
    ...  .given('there is a random number generator')
    ...  .upon_receiving('a request for a random number')
    ...  .with_request('get', '/generate-number')
    ...  .will_respond_with(200, body={
    ...    'content': Includes('spam', 'Some example spamming content')
    ...  }))

    Would expect the response body to be a JSON object, containing the key
github reecetech / pactman / pactman / mock / matchers.py View on Github external
:return: A dict containing the information about the contents of the
            list and the provided minimum number of items for that list.
        :rtype: dict
        """
        return {
            "json_class": "Pact::ArrayLike",
            "contents": generate_ruby_protocol(self.matcher),
            "min": self.minimum,
        }

    def generate_matching_rule_v3(self):
        return {"matchers": [{"match": "type", "min": self.minimum}]}


class Like(Matcher):
    """
    Expect the type of the value to be the same as matcher.

    Example:

    >>> from pactman import Consumer, Provider
    >>> pact = Consumer('consumer').has_pact_with(Provider('provider'))
    >>> (pact
    ...  .given('there is a random number generator')
    ...  .upon_receiving('a request for a random number')
    ...  .with_request('get', '/generate-number')
    ...  .will_respond_with(200, body={
    ...    'number': Like(1111222233334444)
    ...  }))

    Would expect the response body to be a JSON object, containing the key
github reecetech / pactman / pactman / mock / matchers.py View on Github external
:rtype: dict
        """
        return {
            "json_class": "Pact::SomethingLike",
            "contents": generate_ruby_protocol(self.matcher),
        }

    def generate_matching_rule_v3(self):
        return {"matchers": [{"match": "type"}]}


# Remove SomethingLike in major version 1.0.0
SomethingLike = Like


class Term(Matcher):
    """
    Expect the response to match a specified regular expression.

    Example:

    >>> from pactman import Consumer, Provider
    >>> pact = Consumer('consumer').has_pact_with(Provider('provider'))
    >>> (pact.given('the current user is logged in as `tester`')
    ...  .upon_receiving('a request for the user profile')
    ...  .with_request('get', '/profile')
    ...  .will_respond_with(200, body={
    ...    'name': 'tester',
    ...    'theme': Term('light|dark|legacy', 'dark')
    ...  }))

    Would expect the response body to be a JSON object, containing the key