Skip to content

Commit 0ef971f

Browse files
tuan-phamwilliaminfante
authored andcommitted
fix: improve test coverage
1 parent e543f04 commit 0ef971f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

tests/test_message_pact.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,42 @@ def test_definition_without_given(self):
103103
target._messages[0]['metaData'],
104104
{'source': 'legacy_api'})
105105

106+
class MessagePactContextManagerTestCase(MessagePactTestCase):
107+
def setUp(self):
108+
super(MessagePactContextManagerTestCase, self).setUp()
109+
self.addCleanup(patch.stopall)
110+
111+
self.write_to_pact_file = patch.object(
112+
message_pact.MessagePact, 'write_to_pact_file', autospec=True).start()
113+
self.write_to_pact_file.return_value.returncode = 0
114+
115+
self.mock_publish = patch.object(
116+
message_pact.MessagePact, 'publish', autospec=True).start()
117+
self.mock_publish.return_value.returncode = 0
118+
119+
def test_successful(self):
120+
pact = MessagePact(
121+
self.consumer, self.provider, publish_to_broker=True,
122+
broker_base_url='http://localhost', broker_username='username', broker_password='password')
123+
124+
with pact:
125+
pass
126+
127+
self.write_to_pact_file.assert_called_once()
128+
self.mock_publish.assert_called_once()
129+
130+
def test_context_raises_error(self):
131+
pact = MessagePact(
132+
self.consumer, self.provider, publish_to_broker=True,
133+
broker_base_url='http://localhost', broker_username='username', broker_password='password')
134+
135+
with self.assertRaises(RuntimeError):
136+
with pact:
137+
raise RuntimeError
138+
139+
self.write_to_pact_file.assert_not_called()
140+
self.mock_publish.assert_not_called()
141+
106142

107143
class PactGeneratePactFileTestCase(TestCase):
108144
def setUp(self):
@@ -117,7 +153,7 @@ def setUp(self):
117153
def test_call_pact_message_to_generate_pact_file(self):
118154
target = MessagePact(
119155
self.consumer, self.provider, pact_dir='/pacts',
120-
version='3.0.0', file_write_mode='merge')
156+
version='3.0.0', file_write_mode='merge', publish_to_broker=True)
121157

122158
(target
123159
.given('There is an alligator named John')

tests/test_pact.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from mock import patch, call, Mock
66
from psutil import Process
77

8+
from pact.broker import Broker
89
from pact.consumer import Consumer, Provider
910
from pact.matchers import Term
1011
from pact.constants import MOCK_SERVICE_PATH
@@ -294,6 +295,8 @@ def setUp(self):
294295
pact.Pact, '_wait_for_server_start', autospec=True).start()
295296
self.mock_Pid_exists = patch.object(
296297
pact.psutil, 'pid_exists', autospec=True).start()
298+
self.mock_publish = patch.object(
299+
Broker, 'publish', autospec=True).start()
297300

298301
def test_start_fails(self):
299302
self.mock_Popen.return_value.returncode = 1
@@ -352,21 +355,23 @@ def test_start_with_ssl(self):
352355
'--sslkey', '/ssl.key'])
353356

354357
def test_stop_posix(self):
358+
self.mock_publish.return_value.returncode = 0
355359
self.mock_platform.return_value = 'Linux'
356360
pact = Pact(Consumer('consumer'), Provider('provider'))
357361
pact._process = Mock(spec=Popen, pid=999, returncode=0)
358362
pact.stop_service()
359363

360364
pact._process.terminate.assert_called_once_with()
361365
pact._process.communicate.assert_called_once_with()
366+
self.mock_publish.assert_not_called()
362367
self.assertFalse(self.mock_Process.called)
363368

364369
def test_stop_windows(self):
365370
self.mock_platform.return_value = 'Windows'
366371
ruby_exe = Mock(spec=Process)
367372
self.mock_Process.return_value.children.return_value = [ruby_exe]
368373
self.mock_Pid_exists.return_value = False
369-
pact = Pact(Consumer('consumer'), Provider('provider'))
374+
pact = Pact(Consumer('consumer'), Provider('provider'), publish_to_broker=True)
370375
pact._process = Mock(spec=Popen, pid=999)
371376
pact.stop_service()
372377

@@ -378,6 +383,7 @@ def test_stop_windows(self):
378383
ruby_exe.terminate.assert_called_once_with()
379384
self.mock_Process.return_value.wait.assert_called_once_with()
380385
self.mock_Pid_exists.assert_called_once_with(999)
386+
self.mock_publish.assert_called_once()
381387

382388
def test_stop_fails_posix(self):
383389
self.mock_platform.return_value = 'Linux'
@@ -389,6 +395,7 @@ def test_stop_fails_posix(self):
389395

390396
pact._process.terminate.assert_called_once_with()
391397
pact._process.communicate.assert_called_once_with()
398+
self.mock_publish.assert_not_called()
392399

393400
def test_stop_fails_windows(self):
394401
self.mock_platform.return_value = 'Windows'
@@ -407,6 +414,7 @@ def test_stop_fails_windows(self):
407414
recursive=True)
408415
self.mock_Process.return_value.wait.assert_called_once_with()
409416
self.mock_Pid_exists.assert_called_once_with(999)
417+
self.mock_publish.assert_not_called()
410418

411419

412420
class PactWaitForServerStartTestCase(TestCase):

0 commit comments

Comments
 (0)