python类PropertyMock()的实例源码

test_interest.py 文件源码 项目:biweeklybudget 作者: jantman 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_find_payments_total_too_low(self):
        cls = HighestInterestRateFirstMethod(Decimal('3.00'))
        s1 = Mock(spec_set=CCStatement)
        type(s1).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s1).principal = PropertyMock(return_value=Decimal('10.00'))
        s2 = Mock(spec_set=CCStatement)
        type(s2).minimum_payment = PropertyMock(return_value=Decimal('5.00'))
        type(s2).principal = PropertyMock(return_value=Decimal('25.00'))
        s3 = Mock(spec_set=CCStatement)
        type(s3).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s3).principal = PropertyMock(return_value=Decimal('1234.56'))
        s4 = Mock(spec_set=CCStatement)
        type(s4).minimum_payment = PropertyMock(return_value=Decimal('7.00'))
        type(s4).principal = PropertyMock(return_value=Decimal('3.00'))
        with pytest.raises(TypeError):
            cls.find_payments([s1, s2, s3, s4])
test_interest.py 文件源码 项目:biweeklybudget 作者: jantman 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_find_payments(self):
        cls = LowestInterestRateFirstMethod(Decimal('100.00'))
        s1 = Mock(spec_set=CCStatement)
        type(s1).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s1).apr = PropertyMock(return_value=Decimal('0.0100'))
        type(s1).principal = PropertyMock(return_value=Decimal('10.00'))
        s2 = Mock(spec_set=CCStatement)
        type(s2).minimum_payment = PropertyMock(return_value=Decimal('5.00'))
        type(s2).apr = PropertyMock(return_value=Decimal('0.0200'))
        type(s2).principal = PropertyMock(return_value=Decimal('25.00'))
        s3 = Mock(spec_set=CCStatement)
        type(s3).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s3).apr = PropertyMock(return_value=Decimal('0.0800'))
        type(s3).principal = PropertyMock(return_value=Decimal('1234.56'))
        s4 = Mock(spec_set=CCStatement)
        type(s4).minimum_payment = PropertyMock(return_value=Decimal('7.00'))
        type(s4).apr = PropertyMock(return_value=Decimal('0.0300'))
        type(s4).principal = PropertyMock(return_value=Decimal('3.00'))
        assert cls.find_payments([s1, s2, s3, s4]) == [
            Decimal('86.00'),
            Decimal('5.00'),
            Decimal('2.00'),
            Decimal('7.00')
        ]
test_interest.py 文件源码 项目:biweeklybudget 作者: jantman 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_find_payments_total_too_low(self):
        cls = LowestInterestRateFirstMethod(Decimal('3.00'))
        s1 = Mock(spec_set=CCStatement)
        type(s1).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s1).principal = PropertyMock(return_value=Decimal('10.00'))
        s2 = Mock(spec_set=CCStatement)
        type(s2).minimum_payment = PropertyMock(return_value=Decimal('5.00'))
        type(s2).principal = PropertyMock(return_value=Decimal('25.00'))
        s3 = Mock(spec_set=CCStatement)
        type(s3).minimum_payment = PropertyMock(return_value=Decimal('2.00'))
        type(s3).principal = PropertyMock(return_value=Decimal('1234.56'))
        s4 = Mock(spec_set=CCStatement)
        type(s4).minimum_payment = PropertyMock(return_value=Decimal('7.00'))
        type(s4).principal = PropertyMock(return_value=Decimal('3.00'))
        with pytest.raises(TypeError):
            cls.find_payments([s1, s2, s3, s4])
test_interest.py 文件源码 项目:biweeklybudget 作者: jantman 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_next_with_transactions(self):
        b = Mock(spec_set=_BillingPeriod)
        b_next = Mock(spec_set=_BillingPeriod)
        type(b).next_period = PropertyMock(return_value=b_next)
        i = Mock(spec_set=_InterestCalculation)
        p = Mock(spec_set=_MinPaymentFormula)
        cls = CCStatement(
            i, Decimal('1.23'), p, b,
            end_balance=Decimal('1.50'), interest_amt=Decimal('0.27')
        )
        mock_stmt = Mock(spec_set=CCStatement)
        with patch('biweeklybudget.interest.CCStatement', autospec=True) as m:
            m.return_value = mock_stmt
            res = cls.next_with_transactions({'foo': 'bar'})
        assert res == mock_stmt
        assert m.mock_calls == [
            call(
                i, Decimal('1.50'), p, b_next, transactions={'foo': 'bar'}
            )
        ]
test_interest.py 文件源码 项目:biweeklybudget 作者: jantman 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_pay(self):
        b = Mock(spec_set=_BillingPeriod)
        b_next = Mock(spec_set=_BillingPeriod)
        type(b_next).payment_date = PropertyMock(return_value=date(2017, 1, 15))
        type(b).next_period = PropertyMock(return_value=b_next)
        i = Mock(spec_set=_InterestCalculation)
        p = Mock(spec_set=_MinPaymentFormula)
        cls = CCStatement(
            i, Decimal('1.23'), p, b,
            end_balance=Decimal('1.50'), interest_amt=Decimal('0.27')
        )
        mock_stmt = Mock(spec_set=CCStatement)
        with patch(
            'biweeklybudget.interest.CCStatement.next_with_transactions',
            autospec=True
        ) as m:
            m.return_value = mock_stmt
            res = cls.pay(Decimal('98.76'))
        assert res == mock_stmt
        assert m.mock_calls == [
            call(cls, {date(2017, 1, 15): Decimal('98.76')})
        ]
runtime_test.py 文件源码 项目:treadmill 作者: Morgan-Stanley 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def test__finish_no_info(self):
        """Tests docker runtime finish when not aborted or exited."""
        # Access to a protected member
        # pylint: disable=W0212
        treadmill.runtime.save_app(self.manifest, self.data_dir)

        client = mock.MagicMock()
        docker.from_env.return_value = client

        container = mock.MagicMock()
        client.containers.get.return_value = container

        type(container).attrs = mock.PropertyMock(return_value={
            'State': None
        })

        docker_runtime = runtime.DockerRuntime(self.tm_env, self.container_dir)

        # Should not throw any exception
        docker_runtime._finish()
test_lib_charm_openstack_aodh.py 文件源码 项目:charm-aodh 作者: openstack 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_setup_endpoint(self):
        self.patch_object(aodh.AodhCharm, 'service_name',
                          new_callable=mock.PropertyMock)
        self.patch_object(aodh.AodhCharm, 'region',
                          new_callable=mock.PropertyMock)
        self.patch_object(aodh.AodhCharm, 'public_url',
                          new_callable=mock.PropertyMock)
        self.patch_object(aodh.AodhCharm, 'internal_url',
                          new_callable=mock.PropertyMock)
        self.patch_object(aodh.AodhCharm, 'admin_url',
                          new_callable=mock.PropertyMock)
        self.service_name.return_value = 'type1'
        self.region.return_value = 'region1'
        self.public_url.return_value = 'public_url'
        self.internal_url.return_value = 'internal_url'
        self.admin_url.return_value = 'admin_url'
        keystone = mock.MagicMock()
        aodh.setup_endpoint(keystone)
        keystone.register_endpoints.assert_called_once_with(
            'type1', 'region1', 'public_url', 'internal_url', 'admin_url')
test_backends.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_vimeo_get_default_transcripts(self):
        """
        Test Vimeo's default transcripts fetching (positive scenario).
        """
        # Arrange
        test_json_data = {"data": [{"test_key": "test_value"}]}
        success_message = _('Default transcripts successfully fetched from a video platform.')

        with patch.object(self.vimeo_player, 'api_client') as api_client_mock, \
                patch.object(self.vimeo_player, 'parse_vimeo_texttracks') as parse_texttracks_mock:
            type(api_client_mock).access_token = PropertyMock(return_value="test_token")
            api_client_mock.get.return_value = test_json_data
            parse_texttracks_mock.return_value = test_json_data["data"]

            # Act
            transcripts, message = self.vimeo_player.get_default_transcripts(video_id="test_video_id")

            # Assert
            api_client_mock.get.assert_called_with('https://api.vimeo.com/videos/test_video_id/texttracks')
            parse_texttracks_mock.assert_called_with(test_json_data["data"])

            self.assertIsInstance(transcripts, list)
            self.assertEqual(message, success_message)
test_backends.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_vimeo_get_default_transcripts_no_token(self):
        """
        Test Vimeo's default transcripts fetching without provided API token.
        """
        # Arrange
        failure_message = _('No API credentials provided.')

        with patch.object(self.vimeo_player, 'api_client') as api_client_mock:
            type(api_client_mock).access_token = PropertyMock(return_value=None)

            # Act
            with self.assertRaises(vimeo.VimeoApiClientError) as raised:
                self.vimeo_player.get_default_transcripts()

                # Assert
                self.assertEqual(str(raised.exception), failure_message)
test_backends.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_vimeo_get_default_transcripts_no_data(self):
        """
        Test Vimeo's default transcripts fetching with no data returned.
        """
        # Arrange
        test_json_data = []
        success_message = _('There are no default transcripts for the video on the video platform.')

        with patch.object(self.vimeo_player, 'api_client') as api_client_mock:
            type(api_client_mock).access_token = PropertyMock(return_value="test_token")
            api_client_mock.get.return_value = test_json_data

            # Act
            transcripts, message = self.vimeo_player.get_default_transcripts(video_id="test_video_id")

            # Assert
            self.assertEqual(transcripts, [])
            self.assertEqual(message, success_message)
test_backends.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_vimeo_get_default_transcripts_parsing_failure(self):
        """
        Test Vimeo's default transcripts fetching with data parsing failure.
        """
        # Arrange
        test_json_data = {"data": [{"test_key": "test_value"}]}
        failure_message = "test_message"

        with patch.object(self.vimeo_player, 'api_client') as api_client_mock, \
                patch.object(self.vimeo_player, 'parse_vimeo_texttracks') as parse_texttracks_mock:
            type(api_client_mock).access_token = PropertyMock(return_value="test_token")
            api_client_mock.get.return_value = test_json_data
            parse_texttracks_mock.side_effect = vimeo.VimeoApiClientError(failure_message)

            # Act
            transcripts, message = self.vimeo_player.get_default_transcripts(video_id="test_video_id")

            # Assert
            self.assertEqual(transcripts, [])
            self.assertEqual(message, failure_message)
test_mixins.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def test_course_default_language(self):
        """
        Test xBlock's `course_default_language` property works properly.
        """
        with patch.object(self.xblock, 'runtime') as runtime_mock:
            service_mock = runtime_mock.service
            lang_mock = type(service_mock.return_value.get_course.return_value).language = PropertyMock(
                return_value='test_lang'
            )
            lang_mock.return_value = 'test_lang'
            self.xblock.course_id = course_id_mock = PropertyMock()

            self.assertEqual(self.xblock.course_default_language, 'test_lang')
            service_mock.assert_called_once_with(self.xblock, 'modulestore')
            lang_mock.assert_called_once()
            course_id_mock.assert_not_called()
test_mixins.py 文件源码 项目:xblock-video 作者: appsembler 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_srt_to_vtt(self, convert_caps_to_vtt_mock, requests_mock):
        """
        Test xBlock's srt-to-vtt convertation works properly.
        """
        # Arrange
        request_mock = MagicMock()
        convert_caps_to_vtt_mock.return_value = 'vtt transcripts'
        requests_mock.get.return_value.text = text_mock = PropertyMock()
        text_mock.return_value = 'vtt transcripts'

        # Act
        vtt_response = self.xblock.srt_to_vtt(request_mock, 'unused suffix')

        # Assert
        self.assertIsInstance(vtt_response, Response)
        self.assertEqual(vtt_response.text, 'vtt transcripts')
        convert_caps_to_vtt_mock.assert_called_once_with(text_mock)
test_jira.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_dispatch_issue_empty_comment(self, post_mock, get_mock, log_mock):
        """JiraOutput - Dispatch Success, Empty Comment"""
        # setup the request to find an existing issue
        get_mock.return_value.status_code = 200
        existing_issues = {'issues': [{'fields': {'summary': 'Bogus'}, 'id': '5000'}]}
        get_mock.return_value.json.return_value = existing_issues
        # setup the auth and successful creation responses
        auth_resp = {'session': {'name': 'cookie_name', 'value': 'cookie_value'}}
        type(post_mock.return_value).status_code = PropertyMock(side_effect=[200, 200, 200])
        post_mock.return_value.json.side_effect = [auth_resp, {}, {'id': 5000}]

        assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                              rule_name='rule_name',
                                              alert=get_alert()))

        log_mock.assert_called_with('Successfully sent alert to %s', self.SERVICE)
test_jira.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_issue_creation_failure(self, post_mock, get_mock, log_mock):
        """JiraOutput - Issue Creation, Failure"""
        # setup the successful search response - no results
        get_mock.return_value.status_code = 200
        get_mock.return_value.json.return_value = {'issues': []}
        # setup successful auth response and failed issue creation
        type(post_mock.return_value).status_code = PropertyMock(side_effect=[200, 400])
        auth_resp = {'session': {'name': 'cookie_name', 'value': 'cookie_value'}}
        post_mock.return_value.content = 'some bad content'
        post_mock.return_value.json.side_effect = [auth_resp, dict()]

        assert_false(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                               rule_name='rule_name',
                                               alert=get_alert()))

        log_mock.assert_called_with('Failed to send alert to %s', self.SERVICE)
test_jira.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_issue_creation_empty_search(self, post_mock, get_mock, log_mock):
        """JiraOutput - Issue Creation, Failure Empty Search"""
        # setup the successful search response - empty response
        get_mock.return_value.status_code = 200
        get_mock.return_value.json.return_value = {}
        # setup successful auth response and failed issue creation
        type(post_mock.return_value).status_code = PropertyMock(side_effect=[200, 400])
        auth_resp = {'session': {'name': 'cookie_name', 'value': 'cookie_value'}}
        post_mock.return_value.content = 'some bad content'
        post_mock.return_value.json.side_effect = [auth_resp, dict()]

        assert_false(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                               rule_name='rule_name',
                                               alert=get_alert()))

        log_mock.assert_called_with('Failed to send alert to %s', self.SERVICE)
test_jira.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_comment_creation_failure(self, post_mock, get_mock, log_mock):
        """JiraOutput - Comment Creation, Failure"""
        # setup successful search response
        get_mock.return_value.status_code = 200
        existing_issues = {'issues': [{'fields': {'summary': 'Bogus'}, 'id': '5000'}]}
        get_mock.return_value.json.return_value = existing_issues
        # setup successful auth, failed comment creation, and successful issue creation
        type(post_mock.return_value).status_code = PropertyMock(side_effect=[200, 400, 200])
        auth_resp = {'session': {'name': 'cookie_name', 'value': 'cookie_value'}}
        post_mock.return_value.json.side_effect = [auth_resp, {'id': 6000}]

        assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                              rule_name='rule_name',
                                              alert=get_alert()))

        log_mock.assert_called_with('Encountered an error when adding alert to existing Jira '
                                    'issue %s. Attempting to create new Jira issue.', 5000)
test_pagerduty.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_dispatch_success_good_user(self, get_mock, post_mock, log_mock):
        """PagerDutyIncidentOutput - Dispatch Success, Good User"""
        # /users, /users, /services
        type(get_mock.return_value).status_code = PropertyMock(side_effect=[200, 200, 200])
        json_user = {'users': [{'id': 'valid_user_id'}]}
        json_service = {'services': [{'id': 'service_id'}]}
        get_mock.return_value.json.side_effect = [json_user, json_user, json_service]

        # /incidents
        post_mock.return_value.status_code = 200

        ctx = {'pagerduty-incident': {'assigned_user': 'valid_user'}}

        assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                              rule_name='rule_name',
                                              alert=get_alert(context=ctx)))

        log_mock.assert_called_with('Successfully sent alert to %s', self.SERVICE)
test_pagerduty.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_dispatch_success_good_policy(self, get_mock, post_mock, log_mock):
        """PagerDutyIncidentOutput - Dispatch Success, Good Policy"""
         # /users, /escalation_policies, /services
        type(get_mock.return_value).status_code = PropertyMock(side_effect=[200, 200, 200])
        json_user = {'users': [{'id': 'user_id'}]}
        json_policy = {'escalation_policies': [{'id': 'policy_id'}]}
        json_service = {'services': [{'id': 'service_id'}]}
        get_mock.return_value.json.side_effect = [json_user, json_policy, json_service]

        # /incidents
        post_mock.return_value.status_code = 200

        ctx = {'pagerduty-incident': {'assigned_policy': 'valid_policy'}}

        assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                              rule_name='rule_name',
                                              alert=get_alert(context=ctx)))

        log_mock.assert_called_with('Successfully sent alert to %s', self.SERVICE)
test_pagerduty.py 文件源码 项目:streamalert 作者: airbnb 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_dispatch_success_no_context(self, get_mock, post_mock, log_mock):
        """PagerDutyIncidentOutput - Dispatch Success, No Context"""
        # /users, /escalation_policies, /services
        type(get_mock.return_value).status_code = PropertyMock(side_effect=[200, 200, 200])
        json_user = {'users': [{'id': 'user_id'}]}
        json_policy = {'escalation_policies': [{'id': 'policy_id'}]}
        json_service = {'services': [{'id': 'service_id'}]}
        get_mock.return_value.json.side_effect = [json_user, json_policy, json_service]

        # /incidents
        post_mock.return_value.status_code = 200

        assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                              rule_name='rule_name',
                                              alert=get_alert()))

        log_mock.assert_called_with('Successfully sent alert to %s', self.SERVICE)


问题


面经


文章

微信
公众号

扫码关注公众号