Esempio n. 1
0
    def get_metrics_mock(self):
        conditional_mock = ConditionalReturn()
        self.patch("foundations.projects._get_metrics_for_all_jobs",
                   conditional_mock)
        conditional_mock.return_when(self.metrics, self.project_name)

        return conditional_mock
 def set_up(self):
     self.bucket_connection.get_bucket = ConditionalReturn()
     self.bucket_connection.get_bucket.return_when(self.bucket,
                                                   self.bucket_name)
     self.bucket.blob = ConditionalReturn()
     self.bucket.blob.return_when(self.blob, self.upload_file_name)
     self.bucket.list_blobs = ConditionalReturn()
Esempio n. 3
0
    def get_logger_mock(self):
        from foundations_spec.helpers.conditional_return import ConditionalReturn

        mock = self.patch(
            "foundations_contrib.log_manager.LogManager.get_logger",
            ConditionalReturn())
        mock.return_when(Mock(), "foundations_events.consumers.annotate")
        mock.return_when(self.mock_logger, "foundations.utils")
        return mock
Esempio n. 4
0
 def set_up(self):
     self.mock_authorization = self.patch('foundations_gcp.authorized_storage_session.AuthorizedStorageSession', ConditionalReturn())
     self.mock_authorization.return_when(
         self.mock_authorization_instance, 
         pool_size=30,
         pool_block=True,
         max_retries=3
     )
     self.mock_connection = self.patch('google.cloud.storage.Client', ConditionalReturn())
     self.mock_connection.return_when(self.mock_connection_instance, _http=self.mock_authorization_instance)
 def _callback(self):
     from foundations_spec.helpers.conditional_return import ConditionalReturn
     return self.patch(name, ConditionalReturn())
    def setUp(self):
        from faker import Faker

        self.faker = Faker()
        self.conditional_return = ConditionalReturn()
class TestConditionalReturn(unittest.TestCase):
    def setUp(self):
        from faker import Faker

        self.faker = Faker()
        self.conditional_return = ConditionalReturn()

    def test_is_instance_of_mock(self):
        self.assertTrue(isinstance(self.conditional_return, Mock))

    def test_returns_value_when_no_params(self):
        mock = Mock()
        self.conditional_return.return_when(mock)
        self.assertEqual(mock, self.conditional_return())

    def test_fails_if_invalid_params_provided(self):
        fake_param = 'hello world'
        fake_param_2 = 5

        mock = Mock()
        self.conditional_return.return_when(mock, fake_param, fake_param_2)

        with self.assertRaises(AssertionError) as error_context:
            self.conditional_return()

        expected_message = """Mock called with unexpected arguments ((), {})
Supported arguments:
 (('hello world', 5), {})"""
        self.assertIn(expected_message, error_context.exception.args)

    def test_returns_value_if_params(self):
        fake_param = self.faker.sentence()
        fake_param_2 = self.faker.sentence()

        mock = Mock()
        self.conditional_return.return_when(mock, fake_param, fake_param_2)
        self.assertEqual(mock, self.conditional_return(fake_param,
                                                       fake_param_2))

    def test_mock_assertions_are_supported(self):
        mock = Mock()
        self.conditional_return.return_when(mock)
        self.conditional_return()
        self.conditional_return.assert_called()

    def test_mock_assertions_are_supported_with_params(self):
        fake_param = self.faker.sentence()

        mock = Mock()
        self.conditional_return.return_when(mock, fake_param)
        self.conditional_return(fake_param)
        self.conditional_return.assert_called_with(fake_param)

    def test_supports_specifying_value_param(self):
        fake_param = self.faker.sentence()

        mock = Mock()
        self.conditional_return.return_when(mock, value=fake_param)
        self.conditional_return(value=fake_param)
        self.conditional_return.assert_called_with(value=fake_param)

    def test_clear_params(self):
        fake_param = self.faker.sentence()

        mock = Mock()
        self.conditional_return.return_when(mock, fake_param)
        self.conditional_return.clear()

        with self.assertRaises(AssertionError):
            self.conditional_return(fake_param)
 def set_up(self):
     self.mock_slack_client = self.patch('slackclient.SlackClient',
                                         ConditionalReturn())
     self.mock_slack_client.return_when(self.mock_slack_client_instance,
                                        self.token)