def test_get_account(pact): expected = { 'name': Like('A'), 'account_number': 'XQR9MUP', 'amount': Like(4000) } # Usage of Like: # Like(123) # Matches if the value is an integer # Like('hello world') # Matches if the value is a string # Like(3.14) # Matches if the value is a float (pact.given('Account A exists and has balance and activity fields'). upon_receiving('a request for Account A').with_request( 'get', '/accounts/num/XQR9MUP').will_respond_with(200, body=expected)) (pact.given('Active accounts exists').upon_receiving( 'Request for Active accounts, return list').with_request( 'get', '/accounts/active').will_respond_with(200, body=EachLike(expected))) # pact.setup() with pact: acc = getAccount('XQR9MUP') activeAccs = getActiveAccounts() # print(acc) assert acc['name'] == 'A' assert activeAccs[0]['name'] == 'A'
def test_get_addresses(pact): expected = EachLike({ 'ID': Like('py'), 'ZipCode': Like(''), 'Street': Like('') }) headers = {'Content-Type': 'application/json'} (pact.given('test').upon_receiving('a request for addresses').with_request( 'get', '/').will_respond_with(200, headers=headers, body=expected)) with pact: result = get_addresses(pact.uri + '/') assert result == get_generated_values(expected) pact.verify()
def test_get_user_non_admin(pact, consumer): # Define the Matcher; the expected structure and content of the response expected = { "name": "UserA", "id": Format().uuid, "created_on": Term(r"\d+-\d+-\d+T\d+:\d+:\d+", "2016-12-15T20:16:01"), "ip_address": Format().ip_address, "admin": False, } # Define the expected behaviour of the Provider. This determines how the # Pact mock provider will behave. In this case, we expect a body which is # "Like" the structure defined above. This means the mock provider will # return the EXACT content where defined, e.g. UserA for name, and SOME # appropriate content e.g. for ip_address. (pact.given("UserA exists and is not an administrator").upon_receiving( "a request for UserA").with_request( "get", "/users/UserA").will_respond_with(200, body=Like(expected))) with pact: # Perform the actual request user = consumer.get_user("UserA") # In this case the mock Provider will have returned a valid response assert user.name == "UserA" # Make sure that all interactions defined occurred pact.verify()
def test_get_product(pact, consumer): expected = {'id': "27", 'name': 'burger', 'type': 'food'} (pact.given('a product with ID 27 exists').upon_receiving( 'a request to get a product').with_request( 'GET', '/product').will_respond_with(200, body=Like(expected))) with pact: user = consumer.get_product('27') assert user.name == 'burger'
def test_get_user_non_admin(pact, client): expected = { 'name': 'UserA', 'id': Term(r'[a-f0-9]+', '1234567'), 'created_on': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), 'admin': False } (pact.given('UserA exists and is not an administrator').upon_receiving( 'a request for UserA').with_request( 'get', '/users/UserA').will_respond_with(200, body=Like(expected))) with pact: result = client.get_user('UserA')
def test_get_user(): pact = Consumer("UserClient").has_pact_with(Provider("UserProvider")) # pact.start_service expected_body = {"email": "admin@localhost", "id": 42, "username": "******"} (pact.given("admin user exists").upon_receiving( "a request for admin user").with_request( "get", "users/admin").will_respond_with(200, Like(expected_body))) with pact: user = consumer.get_user_by_name("admin") assert user.username == "admin" pact.stop_service
def test_get_user_non_admin(pact, consumer): expected = { 'name': 'UserA', 'id': Format().uuid, 'created_on': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), 'ip_address': Format().ip_address, 'admin': False } (pact.given('UserA exists and is not an administrator').upon_receiving( 'a request for UserA').with_request( 'get', '/users/UserA').will_respond_with(200, body=Like(expected))) with pact: user = consumer.get_user('UserA') assert user.name == 'UserA'
def test_get_existing_location(pact, consumer): expected = { 'id': Format().uuid, 'zip_code': '90210', 'country': 'United States', 'place_name': 'Beverly Hills', 'state': 'California', 'active': True } (pact.given('Location exists in database').upon_receiving( 'a request for 90210').with_request('get', '/90210').will_respond_with( 200, body=Like(expected))) with pact: location = consumer.get_location('90210') assert location.place_name == 'Beverly Hills'
def test_get_user_non_admin(broker, pact, consumer): expected = { 'name': 'UserA', 'id': Format().uuid, 'created_on': Format().timestamp, 'ip_address': Format().ip_address, 'admin': False } (pact.given('UserA exists and is not an administrator').upon_receiving( 'a request for UserA').with_request( 'get', '/users/UserA').will_respond_with(200, body=Like(expected))) with pact: user = consumer.get_user('UserA') assert user.name == 'UserA' assert user.created_on == datetime.strptime('2000-2-1T12:30:00', '%Y-%m-%dT%H:%M:%S')
def test_get_status(pact, client): #expected = { # 'name': 'UserA', # 'id': Term( # r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', # '00000000-0000-4000-a000-000000000000' # ), # 'created_on': Term( # r'\d+-\d+-\d+T\d+:\d+:\d+', # '2016-12-15T20:16:01' # ), # 'admin': False #} expected = { 'buildTime': Term( r'\d+-\d+-\d+T\d+:\d+:\d+Z', '2018-09-10T11:08:26Z' ), 'commit': Term( r'^[a-f0-9]+-dirty', '164762f67a3a7634fa4ee1e8bb55c458281803c7-dirty' ), 'startTime': Term( r'\d+-\d+-\d+T\d+:\d+:\d+Z', '2018-09-10T11:08:26Z' ) } (pact .given('status exists') .upon_receiving('a request for status') .with_request('get', '/status') .will_respond_with(200, body=Like(expected))) with pact: result = client.get_status('status')
'port': 1111, 'interactions': [{ 'GIVEN': 'Provided we have the correct project_id and data to edit', 'UPON_RECEIVING': 'the correct paper URL should be provided', 'REQUEST': { 'method': 'get', 'path': '/' }, 'RESPONSE': { 'status': 200, 'body': EachLike({ "id": Like(1), "name": Like("Pants"), "quantity": Like(15) }) } }] }, 'Shipping': { 'port': 2222, 'interactions': [{ 'GIVEN': 'Provided we have the correct project_id and data to edit', 'UPON_RECEIVING': 'the correct paper URL should be provided', 'REQUEST': { 'method': 'get',
# 'REQUEST' - the expected request to the API (see pact.io for details), # 'RESPONSE' the expected response from the API (see pact.io for the details) SERVICES = { 'python-data-aggregator': { 'port': 1111, 'interactions': [{ 'GIVEN': 'Provided we have the correct project_id and data to edit', 'UPON_RECEIVING': 'the correct paper URL should be provided', 'REQUEST': { 'method': 'get', 'path': '/buy/1/1/15' }, 'RESPONSE': { 'status': 200, 'body': { "status": Like("Transaction successful"), "transaction": Like({ "account_name": Like("John Smith"), "product_name": Like("Socks"), "quantity": Like(5) }) } } }] } }