Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalimaha committed Oct 19, 2017
1 parent da0e642 commit fc3771d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
23 changes: 22 additions & 1 deletion pact_test/runners/service_providers/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pact_test.constants import *
from pact_test.utils.logger import error
from pact_test.utils.logger import debug
from pact_test.models.response import PactResponse
from pact_test.servers.mock_server import MockServer


class ServiceProviderTestSuiteRunner(object):
Expand All @@ -21,13 +23,32 @@ def verify(self):
for test in tests.value:
test_verification = test.is_valid()
if type(test_verification) is Right:
pass
for decorated_method in test.decorated_methods:
mock_response = self.build_expected_response(decorated_method)
mock_server = MockServer(mock_response=mock_response, port=9999)
mock_server.start()
debug('Server is running')
decorated_method()
mock_server.shutdown()
report = mock_server.report()
if len(report) == 0:
error('Verify providers: EXIT WITH ERRORS:')
error(' No request made for ' + str(decorated_method.__name__))
return Left('No request made for ' + str(decorated_method.__name__))
error('Verify providers: EXIT WITH ERRORS:')
error(test_verification.value)
error('Verify providers: EXIT WITH ERRORS:')
error(tests.value)
return tests

@staticmethod
def build_expected_response(decorated_method):
return PactResponse(
body=decorated_method.will_respond_with.get('body'),
status=decorated_method.will_respond_with.get('status'),
headers=decorated_method.will_respond_with.get('headers')
)

def collect_tests(self):
root = self.config.provider_tests_path
debug(self.config)
Expand Down
9 changes: 8 additions & 1 deletion tests/resources/service_providers/simple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ class SimpleTest(ServiceProviderTest):

@given('a book exists')
@upon_receiving('a request for a book')
@with_request({'method': 'get', 'path': '/books/42'})
@with_request({'method': 'get', 'path': '/books/42/'})
@will_respond_with({'status': 200})
def test_get_book(self):
pass

@given('several books exist')
@upon_receiving('a request for a book')
@with_request({'method': 'get', 'path': '/books/'})
@will_respond_with({'status': 200})
def test_get_books(self):
pass
13 changes: 13 additions & 0 deletions tests/resources/service_providers/simple_test_without_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pact_test.models.service_provider_test import *


@service_consumer('Library App')
@has_pact_with('Books Service')
class SimpleTest(ServiceProviderTest):

@given('a book exists')
@upon_receiving('a request for a book')
@with_request({'method': 'get', 'path': '/books/42/'})
@will_respond_with({'status': 200})
def test_get_book(self):
pass
38 changes: 37 additions & 1 deletion tests/runners/service_providers/test_suite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import imp
from pact_test import *
from pact_test.either import Left
from pact_test.config.config_builder import Config
from pact_test.runners.service_providers.test_suite import ServiceProviderTestSuiteRunner # nopep8
Expand Down Expand Up @@ -29,4 +30,39 @@ def test_collect_tests():
t = ServiceProviderTestSuiteRunner(config)

tests = t.collect_tests().value
assert len(tests) == 1
assert len(tests) == 2


def test_build_expected_response():
mock_resp = {
'status': 418,
'body': {'spam': 'eggs'},
'headers': [{'Spam': 'eggs'}]
}

class SimpleTest(ServiceProviderTest):
@given('the breakfast menu is available')
@upon_receiving('a request for a breakfast')
@with_request('I don\'t like spam')
@will_respond_with(mock_resp)
def test_get_book(self):
pass

simple_test = SimpleTest()
method = next(simple_test.decorated_methods)
response = ServiceProviderTestSuiteRunner.build_expected_response(method)

assert response.status == 418
assert response.headers == [{'Spam': 'eggs'}]
assert response.body == {'spam': 'eggs'}


def test_mock_server():
config = Config()
config.provider_tests_path = os.path.join(os.getcwd(), 'tests',
'resources',
'service_providers')
t = ServiceProviderTestSuiteRunner(config)

test_result = t.verify()
assert type(test_result) is Left

0 comments on commit fc3771d

Please sign in to comment.