当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.filter方法代码示例

本文整理汇总了Python中mock.MagicMock.filter方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.filter方法的具体用法?Python MagicMock.filter怎么用?Python MagicMock.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mock.MagicMock的用法示例。


在下文中一共展示了MagicMock.filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_rss_creation

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_rss_creation(self, data, refresh, resp, created, expected_request, side_effect=None):

        # Include data to mock
        self.request.raw_post_data = json.dumps(data)

        # Mock ExpenditureManager
        self.views.ExpenditureManager = MagicMock()
        # Mock ModelManager
        self.views.ModelManager = MagicMock()

        # Create a mock method to manage the token refresh
        # if needed
        if refresh:
            exp_mock = ExpenditureMock()
            self.views.ExpenditureManager = exp_mock.ExpenditureManager
            # Create social auth mocks
            social_mock = MagicMock()
            filter_mock = MagicMock()
            object_mock = MagicMock()
            object_mock.extra_data = {
                'access_token': 'accesstoken',
                'refresh_token': 'refreshtoken'
            }
            filter_mock.return_value = [object_mock]
            social_mock.filter = filter_mock
            self.request.user.social_auth = social_mock

        # Create the corresponding side effect if needed
        if side_effect:
            side_effect(self)

        # Call the view
        collection = self.views.RSSCollection(permitted_methods=('GET', 'POST'))
        response = collection.create(self.request)

        # Check response
        val = json.loads(response.content)
        self.assertEquals(response.status_code, resp[0])
        self.assertEquals(val['message'], resp[1])
        self.assertEquals(val['result'], resp[2])

        # Check the result depending if the model should
        # have been created
        if created:
            # Check rss call
            model_info = None
            if 'models' in data:
                model_info = data['models']

            revenue_model = self._generate_models(model_info)
            self.views.RSS.objects.create.assert_called_with(name=data['name'], host=data['host'], expenditure_limits=expected_request, revenue_models=revenue_model)
            self.assertEquals(self.rss_object.access_token, self.user.userprofile.access_token)
        else:
            self.views.RSS.objects.delete.assert_called_once()
开发者ID:jartieda,项目名称:wstore,代码行数:56,代码来源:tests.py

示例2: test_get_logged_in_char

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_get_logged_in_char(self):
        """Tests if the get_logged_in_char behaves as expected.
        The test asserts if the returned value of the function is the return value
        of the mocked out database query. Also asserts if the query was called with
        the right parameters.
        """
        character = MagicMock()
        character.filter = MagicMock()
        character.filter.return_value = 'test'

        test = get_logged_in_char(character)
        character.filter.assert_called_with(is_logged=True)
        self.assertEqual(test, 'test')
开发者ID:dongliang3571,项目名称:Adventure-Game,代码行数:15,代码来源:test_queries.py

示例3: test_GIVEN_no_model_runs_WHEN_get_model_list_THEN_empty_list_returned

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_GIVEN_no_model_runs_WHEN_get_model_list_THEN_empty_list_returned(self):

        mock_query_result = MagicMock()
        mock_query_result.all = MagicMock(return_value=[])

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result
        self._mock_session.query.return_value = mock_query

        user = User()
        models = self.model_run_service.get_models_for_user(user)

        assert_that(len(models), is_(0), "There should be no model for the user")
开发者ID:NERC-CEH,项目名称:jules-jasmin,代码行数:16,代码来源:model_run_service_test.py

示例4: test_get_character_name

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
 def test_get_character_name(self):
     """Tests if the get_character_name function behaves as expected.
     A fake character is created and mocked out. The database query is
     mocked out and the test character is set as the return value of the
     query. The test then asserts if the returned character name is the
     same as the faked character.
     """
     characters = MagicMock()
     char = MagicMock()
     char.character_name = 'testname'
     characters.filter = MagicMock()
     characters.filter.return_value = [char]
     test = get_character_name(characters)
     characters.filter.assert_called_with(is_logged=True)
     self.assertEquals(test, 'testname')
开发者ID:dongliang3571,项目名称:Adventure-Game,代码行数:17,代码来源:test_queries.py

示例5: test_delete_private_analysis

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_delete_private_analysis(self):

        mock_query_result = MagicMock()
        mock_query_result.one = MagicMock(return_value=self.sample_analysis)

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result

        self._mock_session.query = MagicMock()
        self._mock_session.query.return_value = mock_query

        analysis_service = AnalysisService(self._mock_session)
        analysis_service.delete_private_analysis(self.sample_analysis.id)

        self.assertEqual(self.sample_analysis.deleted, True)
开发者ID:NERC-CEH,项目名称:ecomaps,代码行数:18,代码来源:analysis_test.py

示例6: test_publish_analysis

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_publish_analysis(self):

        mock_query_result = MagicMock()
        mock_query_result.one = MagicMock(return_value=self.sample_analysis)

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result

        self._mock_session.query = MagicMock()
        self._mock_session.query.return_value = mock_query

        analysis_service = AnalysisService(self._mock_session)
        analysis_service.publish_analysis(self.sample_analysis.id)

        self.assertEqual(self.sample_analysis.viewable_by, None)
        self.assertEqual(self.sample_analysis.result_dataset.viewable_by_user_id, None)
开发者ID:NERC-CEH,项目名称:ecomaps,代码行数:19,代码来源:analysis_test.py

示例7: test_get_datasets_for_user

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_get_datasets_for_user(self):

        mock_query_result = MagicMock()
        mock_query_result.all = MagicMock()

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result

        self._mock_session.query = MagicMock()
        self._mock_session.query.return_value = mock_query

        service = DatasetService(self._mock_session)
        service.get_datasets_for_user(1234, dataset_type_id=1)

        self._mock_session.query.assert_called_once_with(Dataset)
        mock_query.filter.assert_called_once_with(ANY, ANY)
        mock_query_result.all.assert_called_once_with()
开发者ID:NERC-CEH,项目名称:ecomaps,代码行数:20,代码来源:dataset_test.py

示例8: test_get_model_by_id

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_get_model_by_id(self):
        """ Test if model can be found by id. Test just checks that the sqlalchemy is called.
        """

        mock_query_result = MagicMock()
        mock_query_result.one = MagicMock(return_value=self.sample_model)

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result

        self._mock_session.query = MagicMock()
        self._mock_session.query.return_value = mock_query

        model_service = ModelService(self._mock_session)
        model = model_service.get_model_by_id(1)

        self.assertEqual(model.id, 1)
        self.assertEqual(model.name, 'Test Model')
开发者ID:NERC-CEH,项目名称:ecomaps,代码行数:21,代码来源:model_test.py

示例9: test_get_user_by_username

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_get_user_by_username(self):

        mock_query_result = MagicMock()
        mock_query_result.one = MagicMock()

        mock_query = MagicMock()
        mock_query.filter = MagicMock()
        mock_query.filter.return_value = mock_query_result

        self._mock_session.query = MagicMock()
        self._mock_session.query.return_value = mock_query

        username = "testuser"

        user_service = UserService(self._mock_session)
        user_service.get_user_by_username(username)

        self._mock_session.query.assert_called_once_with(User)
        mock_query.filter.assert_called_once_with(ANY)
        mock_query_result.one.assert_called_once_with()
开发者ID:NERC-CEH,项目名称:ecomaps,代码行数:22,代码来源:user_test.py

示例10: test_walk_ToNode

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_walk_ToNode(self):
        nw = GraphDSLNodeWalker(self.graphmgr)

        node = MagicMock()
        node.alias.name = 'alias'
        node.filter.query = 'filter'

        nw.walk_ToNode(node, [])

        self.assertEqual(node.alias, 'alias')
        self.assertEqual(node.filter, 'filter')

        node = MagicMock()
        node.alias.name = 'alias'
        node.filter = None

        nw.walk_ToNode(node, [])

        self.assertEqual(node.alias, 'alias')
        self.assertIsNone(node.filter)
开发者ID:linkdd,项目名称:link.graph,代码行数:22,代码来源:core.py

示例11: test_auth_resource_show

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_auth_resource_show(self, exist_pkg=True, authorized_pkg=True):
        #Recover the exception
        auth.tk.ObjectNotFound = self._tk.ObjectNotFound

        # Mock the calls
        package = MagicMock()
        package.id = '1'

        final_query = MagicMock()
        final_query.first = MagicMock(return_value=package if exist_pkg else None)

        second_join = MagicMock()
        second_join.filter = MagicMock(return_value=final_query)

        first_join = MagicMock()
        first_join.join = MagicMock(return_value=second_join)

        query = MagicMock()
        query.join = MagicMock(return_value=first_join)

        model = MagicMock()
        session = MagicMock()
        session.query = MagicMock(return_value=query)
        model.Session = session

        # Create the context
        context = {}
        context['model'] = model

        # Mock the package_show function
        self._package_show = auth.package_show
        success = True if authorized_pkg else False
        auth.package_show = MagicMock(return_value={'success': success})

        if not exist_pkg:
            self.assertRaises(self._tk.ObjectNotFound, auth.resource_show, context, {})
        else:
            result = auth.resource_show(context, {})
            self.assertEquals(authorized_pkg, result['success'])
开发者ID:pgalves,项目名称:ckanext-privatedatasets,代码行数:41,代码来源:test_auth.py

示例12: test_rss_creation

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_rss_creation(self, data, refresh, resp, created, expected_request, side_effect=None):

        # Include data to mock
        self.request.body = json.dumps(data)

        # Mock ExpenditureManager
        self.views.ExpenditureManager = MagicMock()
        # Mock ModelManager
        self.views.ModelManager = MagicMock()

        # Create a mock method to manage the token refresh
        # if needed
        if refresh:
            self._exp_mock = ExpenditureMock()
            self.views.ExpenditureManager = self._exp_mock.ExpenditureManager
            # Create social auth mocks
            social_mock = MagicMock()
            filter_mock = MagicMock()
            object_mock = MagicMock()
            object_mock.extra_data = {
                'access_token': 'accesstoken',
                'refresh_token': 'refreshtoken'
            }
            filter_mock.return_value = [object_mock]
            social_mock.filter = filter_mock
            self.request.user.social_auth = social_mock

        # Create the corresponding side effect if needed
        if side_effect:
            side_effect(self)

        # Call the view
        collection = self.views.RSSCollection(permitted_methods=('GET', 'POST'))
        response = collection.create(self.request)

        # Check response
        val = json.loads(response.content)
        self.assertEquals(response.status_code, resp[0])
        self.assertEquals(val['message'], resp[1])
        self.assertEquals(val['result'], resp[2])

        # Check the result depending if the model should
        # have been created
        if created:
            # Check rss call
            model_info = None
            if 'models' in data:
                model_info = data['models']

            revenue_model = self._generate_models(model_info)
            self.views.RSS.objects.create.assert_called_with(
                name=data['name'],
                host=data['host'],
                api_version=data['api_version'],
                expenditure_limits=expected_request,
                revenue_models=revenue_model,
                aggregator_id=self.user.email
            )

            self.assertEquals(self.rss_object.access_token, self.user.userprofile.access_token)

            views.RSSManagerFactory.assert_called_once_with(self.rss_object)
            self.fact_mock.get_expenditure_manager.assert_called_once_with(self.user.userprofile.access_token)

            if not refresh:
                self._exp_mock.set_provider_limit.assert_called_once_with()

            if data['api_version'] == 2:
                self.fact_mock.get_provider_manager.assert_called_once_with(self.user.userprofile.access_token)

                prov_data = {
                    'provider_id': settings.STORE_NAME.lower() + '-provider',
                    'provider_name': settings.STORE_NAME + '-Provider'
                }

                self._prov_mock.register_provider.assert_called_once_with(prov_data)

        else:
            self.views.RSS.objects.delete.assert_called_once()
开发者ID:tmforum,项目名称:tmffiware,代码行数:81,代码来源:tests.py

示例13: test_walk_depth_backward

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_walk_depth_backward(self):
        self.expected_nodes = {
            'fromfilter': [
                {'_id': 'buzz'}
            ],
            'targets_set:("andy:buzz")': [
                {'_id': 'woody'},
                {'_id': 'rex'}
            ],
            'targets_set:("andy:woody")': [
                {'_id': 'sarge'}
            ],
            'targets_set:("andy:rex")': [
                {'_id': 'sid'}
            ],
            'targets_set:("andy:sarge")': [],
            'targets_set:("andy:sid")': []
        }
        self.expected_rels = {
            'throughfilter': [
                {'_id': 'andy'}
            ]
        }

        from_ = MagicMock()
        from_.set_ = 'NODES'
        from_.alias = 'elt0'
        from_.filter = 'fromfilter'

        statement = MagicMock()
        statement.froms = [from_]

        path = MagicMock()

        through = MagicMock()
        through.set_ = 'RELS'
        through.alias = 'rel0'
        through.wmode.type = 'DEPTH'
        through.wmode.begin = 1
        through.wmode.end = 2
        through.wmode.direction = 'BACKWARD'
        through.filter = 'throughfilter'
        path.through = [through]

        to = MagicMock()
        to.alias = 'elt1'
        to.filter = '_id:sarge'

        path.to = [to]

        statement.path = [path]

        result = self.walk([statement])

        self.assertIn('elt0', result)
        self.assertIn('elt1', result)
        self.assertIn('rel0', result)

        self.assertEqual(result['elt0']['type'], 'nodes')
        self.assertEqual(len(result['elt0']['dataset']), 1)
        self.assertIn({'_id': 'buzz'}, result['elt0']['dataset'])

        self.assertEqual(result['elt1']['type'], 'nodes')
        self.assertEqual(len(result['elt1']['dataset']), 1)
        self.assertIn({'_id': 'sarge'}, result['elt1']['dataset'])

        self.assertEqual(result['rel0']['type'], 'relationships')
        self.assertEqual(len(result['rel0']['dataset']), 1)
        self.assertIn({'_id': 'andy'}, result['rel0']['dataset'])
开发者ID:linkdd,项目名称:link.graph,代码行数:71,代码来源:walkthrough.py

示例14: test_complex_walkthrough

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_complex_walkthrough(self):
        self.expected_nodes = {
            'fromfilter': [
                {
                    '_id': 'buzz',
                    'targets_set': [
                        'andy:woody',
                        'andy:rex'
                    ]
                },
                {
                    '_id': 'lenny',
                    'targets_set': []
                }
            ],
            '_id:(woody OR rex)': [
                {
                    '_id': 'woody',
                    'targets_set': [
                        'andy:sarge'
                    ]
                },
                {
                    '_id': 'rex',
                    'targets_set': [
                        'andy:sid'
                    ]
                }
            ],
            '_id:(sarge OR sid)': [
                {
                    '_id': 'sarge',
                    'targets_set': []
                },
                {
                    '_id': 'sid',
                    'targets_set': []
                }
            ],
            'targets_set:("molly:sarge")': [
                {
                    '_id': 'squeeze',
                    'targets_set': []
                }
            ],
            'targets_set:("molly:sid")': [
                {
                    '_id': 'squeeze',
                    'targets_set': []
                }
            ],
            'targets_set:("molly:squeeze")': [
                {
                    '_id': 'hamm',
                    'targets_set': []
                }
            ],
            'targets_set:("andy:sarge" OR "molly:sarge")': [
                {
                    '_id': 'squeeze',
                    'targets_set': []
                }
            ],
            'targets_set:("andy:sid" OR "molly:sid")': [
                {
                    '_id': 'squeeze',
                    'targets_set': []
                }
            ],
            'targets_set:("andy:squeeze" OR "molly:squeeze")': [
                {
                    '_id': 'hamm',
                    'targets_set': []
                }
            ],
            'targets_set:("molly:hamm")': [
                {
                    '_id': 'bopeep',
                    'targets_set': []
                }
            ],
            'targets_set:("molly:bopeep")': [
                {
                    '_id': 'mrpotato',
                    'targets_set': []
                }
            ]
        }
        self.expected_rels = {
            'through0filter': [
                {'_id': 'andy'},
                {'_id': 'molly'}
            ]
        }

        from0 = MagicMock()
        from0.set_ = 'NODES'
        from0.alias = 'elt0'
        from0.filter = 'fromfilter'

#.........这里部分代码省略.........
开发者ID:linkdd,项目名称:link.graph,代码行数:103,代码来源:walkthrough.py

示例15: test_walk_breadth_forward_noend

# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import filter [as 别名]
    def test_walk_breadth_forward_noend(self):
        self.expected_nodes = {
            'fromfilter': [
                {
                    '_id': 'buzz',
                    'targets_set': [
                        'andy:woody',
                        'andy:rex'
                    ]
                }
            ],
            '_id:(woody OR rex)': [
                {
                    '_id': 'woody',
                    'targets_set': [
                        'andy:sarge'
                    ]
                },
                {
                    '_id': 'rex',
                    'targets_set': [
                        'andy:sid'
                    ]
                }
            ],
            '_id:(sarge OR sid)': [
                {
                    '_id': 'sarge',
                    'targets_set': []
                },
                {
                    '_id': 'sid',
                    'targets_set': []
                }
            ]
        }
        self.expected_rels = {
            'throughfilter': [
                {'_id': 'andy'}
            ]
        }

        from_ = MagicMock()
        from_.set_ = 'NODES'
        from_.alias = 'elt0'
        from_.filter = 'fromfilter'

        statement = MagicMock()
        statement.froms = [from_]

        path = MagicMock()

        through = MagicMock()
        through.set_ = 'RELS'
        through.alias = 'rel0'
        through.wmode.type = 'BREADTH'
        through.wmode.begin = 1
        through.wmode.end = None
        through.wmode.direction = 'FORWARD'
        through.filter = 'throughfilter'
        path.through = [through]

        to = MagicMock()
        to.alias = 'elt1'
        to.filter = '_id:sarge'

        path.to = [to]

        statement.path = [path]

        result = self.walk([statement])

        self.assertIn('elt0', result)
        self.assertIn('elt1', result)
        self.assertIn('rel0', result)

        self.assertEqual(result['elt0']['type'], 'nodes')
        self.assertEqual(len(result['elt0']['dataset']), 1)
        self.assertIn(
            {
                '_id': 'buzz',
                'targets_set': ['andy:woody', 'andy:rex']
            },
            result['elt0']['dataset']
        )

        self.assertEqual(result['elt1']['type'], 'nodes')
        self.assertEqual(len(result['elt1']['dataset']), 1)
        self.assertIn(
            {
                '_id': 'sarge',
                'targets_set': []
            },
            result['elt1']['dataset']
        )

        self.assertEqual(result['rel0']['type'], 'relationships')
        self.assertEqual(len(result['rel0']['dataset']), 1)
        self.assertIn({'_id': 'andy'}, result['rel0']['dataset'])
开发者ID:linkdd,项目名称:link.graph,代码行数:101,代码来源:walkthrough.py


注:本文中的mock.MagicMock.filter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。