コード例 #1
0
    def test_definition_sparse(self):
        target = MessagePact(self.consumer, self.provider)
        (target.given('there is an alligator named John').expects_to_receive(
            'an alligator message').with_content({
                'name':
                'John',
                'document_name':
                'sample_document.doc'
            }).with_metadata({
                'contentType': 'application/json',
                'source': 'legacy_api'
            }))

        self.assertEqual(len(target._messages), 1)

        self.assertEqual(target._messages[0]['providerStates'],
                         [{
                             'name': 'there is an alligator named John'
                         }])

        self.assertEqual(target._messages[0]['description'],
                         'an alligator message')

        self.assertEqual(target._messages[0]['contents'], {
            'name': 'John',
            'document_name': 'sample_document.doc'
        })

        self.assertEqual(target._messages[0]['metaData'], {
            'contentType': 'application/json',
            'source': 'legacy_api'
        })
コード例 #2
0
    def test_call_pact_message_to_generate_pact_file(self):
        target = MessagePact(self.consumer,
                             self.provider,
                             pact_dir='/pacts',
                             version='3.0.0',
                             file_write_mode='merge',
                             publish_to_broker=True)

        (target.given('There is an alligator named John').expects_to_receive(
            'an alligator message').with_content({
                'name':
                'John',
                'document_name':
                'sample_document.doc'
            }).with_metadata({
                'contentType': 'application/json',
                'source': 'legacy_api'
            }))

        target.write_to_pact_file()

        self.mock_Popen.assert_called_once_with([
            MESSAGE_PATH,
            'update',
            json.dumps(target._messages[0]),
            '--pact-dir',
            '/pacts',
            '--pact-specification-version=3.0.0',
            '--consumer',
            'TestConsumer_message',
            '--provider',
            'TestProvider_message',
        ])
コード例 #3
0
    def test_successful(self):
        pact = MessagePact(
            self.consumer, self.provider, publish_to_broker=True,
            broker_base_url='http://localhost', broker_username='******', broker_password='******')

        with pact:
            pass

        self.write_to_pact_file.assert_called_once()
        self.mock_publish.assert_called_once()
コード例 #4
0
 def test_init_defaults(self):
     target = MessagePact(self.consumer, self.provider)
     self.assertIs(target.broker_base_url, None)
     self.assertIs(target.broker_username, None)
     self.assertIs(target.broker_password, None)
     self.assertIs(target.consumer, self.consumer)
     self.assertEqual(target.pact_dir, os.getcwd())
     self.assertIs(target.provider, self.provider)
     self.assertIs(target.publish_to_broker, False)
     self.assertEqual(target.version, '3.0.0')
     self.assertEqual(len(target._messages), 0)
コード例 #5
0
    def test_init_publish_to_broker(self):
        target = MessagePact(
            self.consumer, self.provider, publish_to_broker=True,
            broker_base_url='http://localhost', broker_username='******',
            broker_password='******', broker_token='token')

        self.assertEqual(target.broker_base_url, 'http://localhost')
        self.assertEqual(target.broker_username, 'username')
        self.assertEqual(target.broker_password, 'password')
        self.assertEqual(target.broker_token, 'token')
        self.assertIs(target.publish_to_broker, True)
コード例 #6
0
    def test_init_custom_mock_service(self):
        target = MessagePact(
            self.consumer, self.provider, pact_dir='/pacts',
            version='3.0.0', file_write_mode='merge')

        self.assertIs(target.consumer, self.consumer)
        self.assertEqual(target.pact_dir, '/pacts')
        self.assertIs(target.provider, self.provider)
        self.assertEqual(target.version, '3.0.0')
        self.assertEqual(target.file_write_mode, 'merge')
        self.assertEqual(len(target._messages), 0)
コード例 #7
0
    def test_context_raises_error(self):
        pact = MessagePact(
            self.consumer, self.provider, publish_to_broker=True,
            broker_base_url='http://localhost', broker_username='******', broker_password='******')

        with self.assertRaises(RuntimeError):
            with pact:
                raise RuntimeError

        self.write_to_pact_file.assert_not_called()
        self.mock_publish.assert_not_called()
コード例 #8
0
    def test_definition_without_given(self):
        target = MessagePact(self.consumer, self.provider)
        (target.expects_to_receive('an crocodile message').with_content({
            'name':
            'Mary'
        }).with_metadata({'source': 'legacy_api'}))

        self.assertEqual(len(target._messages), 1)

        self.assertIsNone(target._messages[0].get('providerStates'))

        self.assertEqual(target._messages[0]['description'],
                         'an crocodile message')

        self.assertEqual(target._messages[0]['contents'], {'name': 'Mary'})

        self.assertEqual(target._messages[0]['metaData'],
                         {'source': 'legacy_api'})
コード例 #9
0
    def test_insert_new_message_once_required_attributes_provided(self):
        target = MessagePact(self.consumer, self.provider)
        (target.given('there is an alligator named John').with_content({
            'name':
            'John',
            'document_name':
            'sample_document.doc'
        }).with_metadata({'contentType': 'application/json'}))
        self.assertEqual(len(target._messages), 1)

        (target.expects_to_receive('an alligator message'))
        self.assertEqual(len(target._messages), 1)
        self.assertEqual(target._messages[0]['description'],
                         'an alligator message')

        (target.expects_to_receive('a new message description'))
        self.assertEqual(len(target._messages), 2)
        self.assertEqual(target._messages[0]['description'],
                         'a new message description')