Skip to content

Commit

Permalink
Merge pull request #183 from thatguysimon/feat/verifier-class-consume…
Browse files Browse the repository at this point in the history
…r-version-selectors

feat(verifier): Allow setting consumer_version_selectors on Verifier
  • Loading branch information
elliottmurray authored Oct 19, 2020
2 parents 482407d + 683a931 commit 4430681
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pact/verifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Classes and methods to verify Contracts."""
import json

from pact.verify_wrapper import VerifyWrapper, path_exists, expand_directories

class Verifier(object):
Expand Down Expand Up @@ -103,6 +105,9 @@ def extract_params(self, **kwargs):
verbose = kwargs.get('verbose', False)
publish_version = kwargs.get('publish_version', None)

raw_consumer_selectors = kwargs.get('consumer_version_selectors', [])
consumer_selectors = self._build_consumer_selectors(raw_consumer_selectors)

options = {
'log_dir': log_dir,
'log_level': log_level,
Expand All @@ -114,9 +119,19 @@ def extract_params(self, **kwargs):
'provider_states_setup_url': states_setup_url,
'verbose': verbose,
'publish_version': publish_version,
'consumer_selectors': consumer_selectors
}
return self.filter_empty_options(**options)

def _build_consumer_selectors(self, consumer_selectors):
"""
Build the consumer_selectors list.
Turn each dict in the consumer_selectors list into a string with a
json object, as expected by VerifyWrapper.
"""
return [json.dumps(selector) for selector in consumer_selectors]

def filter_empty_options(self, **kwargs):
"""Filter out empty options."""
kwargs = dict(filter(lambda item: item[1] is not None, kwargs.items()))
Expand Down
60 changes: 60 additions & 0 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import OrderedDict

from unittest import TestCase
import unittest
from mock import patch
Expand Down Expand Up @@ -42,6 +44,35 @@ def test_verifier_with_provider_and_files(self, mock_path_exists, mock_wrapper):
enable_pending=False,
include_wip_pacts_since=None)

@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
@patch('pact.verifier.path_exists', return_value=True)
def test_verifier_with_provider_and_files_passes_consumer_selctors(self, mock_path_exists, mock_wrapper):
mock_wrapper.return_value = (True, 'some logs')

output, _ = self.verifier.verify_pacts(
'path/to/pact1',
'path/to/pact2',
headers=['header1', 'header2'],
consumer_version_selectors=[
# Using OrderedDict for the sake of testing
OrderedDict([("tag", "main"), ("latest", True)]),
OrderedDict([("tag", "test"), ("latest", False)]),
]
)

assertVerifyCalled(mock_wrapper,
'path/to/pact1',
'path/to/pact2',
provider='test_provider',
custom_provider_headers=['header1', 'header2'],
provider_base_url='http://localhost:8888',
log_level='INFO',
verbose=False,
enable_pending=False,
include_wip_pacts_since=None,
consumer_selectors=['{"tag": "main", "latest": true}',
'{"tag": "test", "latest": false}'])

def test_validate_on_publish_results(self):
self.assertRaises(Exception, self.verifier.verify_pacts, 'path/to/pact1', publish=True)

Expand Down Expand Up @@ -140,6 +171,35 @@ def test_verifier_with_broker(self, mock_wrapper):
enable_pending=False,
include_wip_pacts_since=None)

@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
def test_verifier_with_broker_passes_consumer_selctors(self, mock_wrapper):

mock_wrapper.return_value = (True, 'some value')

output, _ = self.verifier.verify_with_broker(
consumer_version_selectors=[
# Using OrderedDict for the sake of testing
OrderedDict([("tag", "main"), ("latest", True)]),
OrderedDict([("tag", "test"), ("latest", False)]),
],
**self.default_opts
)

self.assertTrue(output)
assertVerifyCalled(mock_wrapper,
provider='test_provider',
provider_base_url='http://localhost:8888',
broker_password=self.broker_password,
broker_username=self.broker_username,
broker_token='token',
broker_url=self.broker_url,
log_level='INFO',
verbose=False,
enable_pending=False,
include_wip_pacts_since=None,
consumer_selectors=['{"tag": "main", "latest": true}',
'{"tag": "test", "latest": false}'])

@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
@patch('pact.verifier.path_exists', return_value=True)
def test_publish_on_success(self, mock_path_exists, mock_wrapper):
Expand Down

0 comments on commit 4430681

Please sign in to comment.