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


Python QueryResultProxy.first方法代码示例

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


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

示例1: QueryResultProxyTest

# 需要导入模块: from mediadrop.lib.auth.query_result_proxy import QueryResultProxy [as 别名]
# 或者: from mediadrop.lib.auth.query_result_proxy.QueryResultProxy import first [as 别名]
class QueryResultProxyTest(PythonicTestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        self.session = self._create_session()
        self._populate_database()
        self.query = self.session.query(User).order_by(asc(User.id))
        self.proxy = QueryResultProxy(self.query)
    
    def _tearDown(self):
        Base.metadata.drop_all(self.engine)
    
    def _create_session(self):
        Base.metadata.create_all(self.engine)
        Session = sessionmaker(bind=self.engine)
        return Session()
    
    def _populate_database(self):
        for i, name in enumerate(('foo', 'bar', 'baz', 'quux', 'quuux')):
            self.session.add(User(name, i))
        self.session.commit()
    
    def _next_name(self):
        items = self.proxy.fetch(n=1)
        return items[0].name
    
    def _next_names(self, n=1):
        return self._names(self.proxy.fetch(n=n))
    
    def _names(self, results):
        return [item.name for item in results]
    
    def _name(self, result):
        return result.name
    
    def test_can_fetch_next_item(self):
        assert_equals('foo', self._next_name())
        assert_equals('bar', self._next_name())
        assert_equals('baz', self._next_name())
    
    def test_can_fetch_single_item(self):
        filter_ = lambda item: item.activity >= 4
        self.proxy = QueryResultProxy(self.query, filter_=filter_)
        assert_equals('quuux', self._name(self.proxy.first()))
        assert_equals(None, self.proxy.first())
    
    def test_can_fetch_multiple_items_at_once(self):
        assert_equals(['foo', 'bar'], self._next_names(n=2))
        assert_equals(['baz', 'quux'], self._next_names(n=2))
        assert_equals(['quuux'], self._next_names(n=2))
        assert_equals([], self._next_names(n=2))
    
    def test_regression_do_not_skipped_items_because_of_prefetching(self):
        assert_equals('foo', self._next_name())
        assert_equals(['bar', 'baz'], self._next_names(n=2))
        assert_equals(['quux', 'quuux'], self._next_names(n=2))
    
    def test_can_tell_if_more_items_are_available_even_before_explicit_fetching(self):
        assert_true(self.proxy.more_available())
        assert_equals('foo', self._next_name())
    
    def test_can_tell_if_no_more_items_are_available(self):
        assert_equals(['foo', 'bar', 'baz', 'quux'], self._next_names(n=4))
        assert_true(self.proxy.more_available())
        
        assert_equals('quuux', self._next_name())
        assert_false(self.proxy.more_available())
    
    def test_does_not_omit_prefetched_items_after_asking_if_more_are_available(self):
        assert_true(self.proxy.more_available())
        assert_equals(['foo', 'bar'], self._next_names(n=2))
    
    def test_does_not_omit_prefetched_items_if_many_prefetched_items_were_available(self):
        assert_true(self.proxy.more_available())
        # more_available() should have fetched more than one item so we have 
        # actually 2+ items prefetched.
        assert_not_equals(0, len(self.proxy._prefetched_items))
        # .next() consumes only one item so there should be one left
        # (._prefetched_items were overwritten in this step)
        assert_equals(['foo'], self._names([self.proxy.next()]))
        assert_not_equals(0, len(self.proxy._prefetched_items))
        
        assert_equals(['bar', 'baz'], self._next_names(n=2))
    
    def test_can_initialize_proxy_with_offset(self):
        self.proxy = QueryResultProxy(self.query, start=2)
        assert_equals(['baz', 'quux'], self._next_names(n=2))
    
    def test_can_specify_filter_callable(self):
        filter_ = lambda item: item.activity % 2 == 1
        self.proxy = QueryResultProxy(self.query, filter_=filter_)
        assert_equals(['bar', 'quux'], self._next_names(n=5))
        assert_false(self.proxy.more_available())
    
    def test_proxy_returns_always_specified_number_of_items_if_possible(self):
        filter_ = lambda item: item.activity >= 2
        self.proxy = QueryResultProxy(self.query, filter_=filter_)
        assert_equals(['baz', 'quux', 'quuux'], self._next_names(n=3))
        assert_false(self.proxy.more_available())
    
    # --- limit ----------------------------------------------------------------
#.........这里部分代码省略.........
开发者ID:GitReadysoft,项目名称:mediadrop,代码行数:103,代码来源:query_result_proxy_test.py


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