def test_detect_mismatch_request_manual_mode():
    with tempfile.TemporaryDirectory() as d:
        pact = (
            Consumer("C")
            .has_pact_with(Provider("P"), pact_dir=d, file_write_mode="merge")
            .given("g")
            .upon_receiving("r")
            .with_request("get", "/foo")
            .will_respond_with(200)
        )
        with pact:
            requests.get(pact.uri + "/foo")

        # force a failure by specifying the same given/providerState but different request
        pact = (
            Consumer("C")
            .has_pact_with(Provider("P"), pact_dir=d, file_write_mode="merge")
            .given("g")
            .upon_receiving("r")
            .with_request("get", "/bar")
            .will_respond_with(200)
        )
        with pytest.raises(PactInteractionMismatch):
            with pact:
                requests.get(pact.uri + "/bar")
class ConsumerTestCase(TestCase):
    def setUp(self):
        self.mock_service = Mock(Pact)
        self.provider = Mock(Provider)
        self.consumer = Consumer('TestConsumer', service_cls=self.mock_service)

    def test_init(self):
        result = Consumer('TestConsumer')
        self.assertIsInstance(result, Consumer)
        self.assertEqual(result.name, 'TestConsumer')
        self.assertIs(result.service_cls, Pact)

    def test_has_pact_with(self):
        result = self.consumer.has_pact_with(self.provider)
        self.assertIs(result, self.mock_service.return_value)
        self.mock_service.assert_called_once_with(consumer=self.consumer,
                                                  provider=self.provider,
                                                  host_name='localhost',
                                                  port=None,
                                                  file_write_mode='overwrite',
                                                  log_dir=None,
                                                  ssl=False,
                                                  sslcert=None,
                                                  sslkey=None,
                                                  pact_dir=None,
                                                  version='2.0.0',
                                                  use_mocking_server=False)

    def test_has_pact_with_customer_all_options(self):
        result = self.consumer.has_pact_with(self.provider,
                                             host_name='example.com',
                                             port=1111,
                                             log_dir='/logs',
                                             ssl=True,
                                             sslcert='/ssl.cert',
                                             sslkey='ssl.pem',
                                             pact_dir='/pacts',
                                             version='3.0.0',
                                             use_mocking_server=False)

        self.assertIs(result, self.mock_service.return_value)
        self.mock_service.assert_called_once_with(consumer=self.consumer,
                                                  provider=self.provider,
                                                  host_name='example.com',
                                                  port=1111,
                                                  file_write_mode='overwrite',
                                                  log_dir='/logs',
                                                  ssl=True,
                                                  sslcert='/ssl.cert',
                                                  sslkey='ssl.pem',
                                                  pact_dir='/pacts',
                                                  version='3.0.0',
                                                  use_mocking_server=False)

    def test_has_pact_with_not_a_provider(self):
        with self.assertRaises(ValueError):
            self.consumer.has_pact_with(None)
Exemple #3
0
def test_multiple_pacts_dont_break_during_teardown():
    # ensure teardown is only done on when all pacts __exit__
    pact = Pact(Consumer('Consumer'), Provider('Provider'))
    p1 = pact.given('given').upon_receiving('when').with_request('GET', '/path').will_respond_with(201)
    p2 = pact.given('given2').upon_receiving('when2').with_request('GET', '/path2').will_respond_with(201)
    with p1, p2:
        requests.get(p1.uri + '/path')
Exemple #4
0
def test_multiple_pacts_dont_break_during_teardown():
    # ensure teardown is only done on when all pacts __exit__
    pact = Pact(Consumer("Consumer"), Provider("Provider"))
    p1 = (pact.given("given").upon_receiving("when").with_request(
        "GET", "/path").will_respond_with(201))
    p2 = (pact.given("given2").upon_receiving("when2").with_request(
        "GET", "/path2").will_respond_with(201))
    with p1, p2:
        requests.get(p1.uri + "/path")
 def f(file_write_mode=None, version='2.0.0'):
     monkeypatch.setattr(Pact, "allocate_port", Mock())
     monkeypatch.setattr(os, "remove", Mock())
     monkeypatch.setattr(os.path, "exists", Mock(return_value=True))
     log_dir = "/tmp/a"
     pact_dir = "/tmp/pact"
     return Pact(Consumer("CONSUMER"),
                 Provider("PROVIDER"),
                 log_dir=log_dir,
                 pact_dir=pact_dir,
                 version=version,
                 file_write_mode=file_write_mode)
def test_detect_mismatch_request_retained_relationship():
    with tempfile.TemporaryDirectory() as d:
        pact = Consumer('C').has_pact_with(Provider('P'), pact_dir=d)
        with pact.given("g").upon_receiving("r").with_request(
                "get", "/foo").will_respond_with(200):
            requests.get(pact.uri + '/foo')

        # force a failure by specifying the same given/providerState but different request
        with pytest.raises(PactInteractionMismatch):
            with pact.given("g").upon_receiving("r").with_request(
                    "get", "/bar").will_respond_with(200):
                requests.get(pact.uri + '/bar')
def test_pacts_written():
    with tempfile.TemporaryDirectory() as d:
        pact = Consumer('C').has_pact_with(Provider('P'), pact_dir=d)
        with pact.given("g").upon_receiving("r").with_request(
                "get", "/foo").will_respond_with(200):
            requests.get(pact.uri + '/foo')

        # force a failure
        with pytest.raises(AssertionError):
            with pact.given("g").upon_receiving("r2").with_request(
                    "get", "/bar").will_respond_with(200):
                requests.get(pact.uri + '/foo')

        # make sure mocking still works
        with pact.given("g").upon_receiving("r2").with_request(
                "get", "/bar").will_respond_with(200):
            requests.get(pact.uri + '/bar')

        # ensure two pacts written
        with open(pact.pact_json_filename) as f:
            content = json.load(f)
            assert len(content['interactions']) == 2
Exemple #8
0
 def setUp(self):
     self.mock_service = Mock(Pact)
     self.provider = Mock(Provider)
     self.consumer = Consumer('TestConsumer', service_cls=self.mock_service)
Exemple #9
0
 def test_init(self):
     result = Consumer('TestConsumer')
     self.assertIsInstance(result, Consumer)
     self.assertEqual(result.name, 'TestConsumer')
     self.assertIs(result.service_cls, Pact)
Exemple #10
0
 def setUp(self):
     self.consumer = Consumer('TestConsumer')
     self.provider = Provider('TestProvider')
Exemple #11
0
 def setUp(self):
     self.consumer = Consumer("TestConsumer")
     self.provider = Provider("TestProvider")
Exemple #12
0
class ConsumerTestCase(TestCase):
    def setUp(self):
        self.mock_service = Mock(Pact)
        self.provider = Mock(Provider)
        self.consumer = Consumer("TestConsumer", service_cls=self.mock_service)

    def test_init(self):
        result = Consumer("TestConsumer")
        self.assertIsInstance(result, Consumer)
        self.assertEqual(result.name, "TestConsumer")
        self.assertIs(result.service_cls, Pact)

    def test_has_pact_with(self):
        result = self.consumer.has_pact_with(self.provider)
        self.assertIs(result, self.mock_service.return_value)
        self.mock_service.assert_called_once_with(
            consumer=self.consumer,
            provider=self.provider,
            host_name="localhost",
            port=None,
            file_write_mode="overwrite",
            log_dir=None,
            ssl=False,
            sslcert=None,
            sslkey=None,
            pact_dir=None,
            version="2.0.0",
            use_mocking_server=False,
        )

    def test_has_pact_with_customer_all_options(self):
        result = self.consumer.has_pact_with(
            self.provider,
            host_name="example.com",
            port=1111,
            log_dir="/logs",
            ssl=True,
            sslcert="/ssl.cert",
            sslkey="ssl.pem",
            pact_dir="/pacts",
            version="3.0.0",
            use_mocking_server=False,
        )

        self.assertIs(result, self.mock_service.return_value)
        self.mock_service.assert_called_once_with(
            consumer=self.consumer,
            provider=self.provider,
            host_name="example.com",
            port=1111,
            file_write_mode="overwrite",
            log_dir="/logs",
            ssl=True,
            sslcert="/ssl.cert",
            sslkey="ssl.pem",
            pact_dir="/pacts",
            version="3.0.0",
            use_mocking_server=False,
        )

    def test_has_pact_with_not_a_provider(self):
        with self.assertRaises(ValueError):
            self.consumer.has_pact_with(None)