def test_post_duration(self): expected_response = {'id': 42, 'duration_min': 192} expected_pact = Like(expected_response) pact.given('provider allows duration creation').upon_receiving( 'a request for create a duration').with_request( 'POST', '/duration', body={ 'duration_min': Like(192) }).will_respond_with(201, body=expected_pact) with pact: result = post_duration(192) self.assertEqual(result, expected_response)
def test_like_v3(): pact = ( Consumer("consumer") .has_pact_with(Provider("provider"), version="3.0.0") .given("the condition exists") .upon_receiving("a request") .with_request("GET", "/path", query=dict(fields=Like(["first,second"]))) .will_respond_with(200, body="ok") ) result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": {"name": "consumer"}, "provider": {"name": "provider"}, "interactions": [ { "description": "a request", "providerStates": [{"name": "the condition exists", "params": {}}], "request": dict( method="GET", path="/path", query=dict(fields=["first,second"]), matchingRules={"query": {"fields": {"matchers": [{"match": "type"}]}}}, ), "response": dict(status=200, body="ok"), } ], "metadata": dict(pactSpecification=dict(version="3.0.0")), }
def test_like_v2(): pact = Consumer('consumer').has_pact_with(Provider('provider'), version='2.0.0') pact.given("the condition exists").upon_receiving("a request") \ .with_request("GET", "/path", query=Like("fields=first,second")).will_respond_with(200, body='ok') result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'providerState': 'the condition exists', 'request': dict(method='GET', path='/path', query='fields=first,second', matchingRules={'$.query': { 'match': 'type' }}), 'response': dict(status=200, body='ok'), }], 'metadata': dict(pactSpecification=dict(version='2.0.0')) }
def test_matcher_in_query(): target = Request('GET', '/test-path', query={ 'q': [Like('spam')], 'l': [Term(r'\d+', '10')] }) assert target.json('3.0.0') == { 'method': 'GET', 'path': '/test-path', 'query': { 'q': ['spam'], 'l': ['10'] }, 'matchingRules': { 'query': { 'q': { 'matchers': [ { 'match': 'type' }, ] }, 'l': { 'matchers': [{ 'match': 'regex', 'regex': r'\d+', }] }, } } }
def test_mock_usage_fail_validation(): pact = Consumer('C').has_pact_with(Provider('P'), version='3.0.0') \ .given("g").upon_receiving("r").with_request("post", "/foo", body=Like({"a": "spam", "b": Equals("bee")})) \ .will_respond_with(200) with pytest.raises(AssertionError), pact: requests.post(pact.uri + '/foo', json={"a": "ham", "b": "wasp"})
def test_matcher_in_query(): target = Request("GET", "/test-path", query={ "q": [Like("spam")], "l": [Term(r"\d+", "10")] }) assert target.json("3.0.0") == { "method": "GET", "path": "/test-path", "query": { "q": ["spam"], "l": ["10"] }, "matchingRules": { "query": { "q": { "matchers": [{ "match": "type" }] }, "l": { "matchers": [{ "match": "regex", "regex": r"\d+" }] }, } }, }
def test_mock_usage_pass_validation(): pact = Consumer('C').has_pact_with(Provider('P'), version='3.0.0') \ .given("g").upon_receiving("r").with_request("post", "/foo", body=Like({"a": "spam", "b": Includes("bee", 'been')})) \ .will_respond_with(200) with pact: requests.post(pact.uri + '/foo', json={"a": "ham", "b": "has bee in it"})
def test_get_user(pact, client): expected = {'id': 123, 'name': 'Test User'} (pact.given('User with id 3 exists').upon_receiving( 'a request for existing user').with_request( 'get', '/user/3').will_respond_with(200, body=Like(expected))) with pact: result = client.get_user(3) assert result == expected
def test_mock_usage_pass_validation(): pact = (Consumer("C").has_pact_with( Provider("P"), version="3.0.0").given("g").upon_receiving("r").with_request( "post", "/foo", body=Like({ "a": "spam", "b": Equals("bee") })).will_respond_with(200)) with pact: requests.post(pact.uri + "/foo", json={"a": "ham", "b": "bee"})
def test_mock_usage_fail_validation(): pact = (Consumer("C").has_pact_with( Provider("P"), version="3.0.0").given("g").upon_receiving("r").with_request( "post", "/foo", body=Like({ "a": "spam", "b": Includes("bee", "been") })).will_respond_with(200)) with pytest.raises(AssertionError), pact: requests.post(pact.uri + "/foo", json={"a": "ham", "b": "wasp"})
def test_nested_matchers(): matcher = EachLike({"username": Term("[a-z]+", "user"), "id": Like(123)}) assert matcher.ruby_protocol() == { "json_class": "Pact::ArrayLike", "contents": { "username": { "json_class": "Pact::Term", "data": { "matcher": {"json_class": "Regexp", "s": "[a-z]+", "o": 0}, "generate": "user", }, }, "id": {"json_class": "Pact::SomethingLike", "contents": 123}, }, "min": 1, }
def test_get_duration(self): expected = { "id": 42, "duration_min": 192, } expected_pact = Like(expected) pact.given('Given a movie exists').upon_receiving( 'a request for movies-api').with_request( 'GET', '/duration/42').will_respond_with(200, body=expected_pact) with pact: result = get_duration(42) self.assertEqual(result, expected)
def test_nested_matchers(): matcher = EachLike({'username': Term('[a-z]+', 'user'), 'id': Like(123)}) assert matcher.ruby_protocol() == { 'json_class': 'Pact::ArrayLike', 'contents': { 'username': { 'json_class': 'Pact::Term', 'data': { 'matcher': { 'json_class': 'Regexp', 's': '[a-z]+', 'o': 0}, 'generate': 'user'}}, 'id': { 'json_class': 'Pact::SomethingLike', 'contents': 123}}, 'min': 1 }