def test_supports_relationship_attributes(self): articles = self.session.query(self.Article).all() batch_fetch(articles, 'author') query_count = self.connection.query_count assert articles[0].author == self.users[0] # no lazy load should occur assert articles[1].author == self.users[1] # no lazy load should occur assert articles[2].author is None # no lazy load should occur assert self.connection.query_count == query_count
def test_deep_relationships(self): articles = (self.session.query(self.Article).order_by( self.Article.id1).all()) batch_fetch(articles, 'tags') query_count = self.connection.query_count assert articles[0].tags articles[1].tags assert articles[3].tags assert self.connection.query_count == query_count
def test_many_to_one_relationships(self): articles = ( self.session.query(self.Article) .filter_by(name=u'Article 1') .all() ) batch_fetch( articles, 'author' )
def test_multiple_relationships(self): categories = self.session.query(self.Category).all() batch_fetch(categories, 'articles', 'blog_posts') query_count = self.connection.query_count categories[0].articles[1] categories[0].blog_posts[0] assert self.connection.query_count == query_count categories[1].articles[1] categories[1].blog_posts[0] assert self.connection.query_count == query_count
def test_supports_relationship_attributes(self): articles = self.session.query(self.Article).all() batch_fetch( articles, 'author' ) query_count = self.connection.query_count assert articles[0].author == self.users[0] # no lazy load should occur assert articles[1].author == self.users[1] # no lazy load should occur assert articles[2].author is None # no lazy load should occur assert self.connection.query_count == query_count
def test_supports_empty_related_entities(self): category = self.Category(name=u'Category #1') self.session.add(category) self.session.commit() categories = self.session.query(self.Category).all() batch_fetch( categories, 'articles', 'articles.tags' ) query_count = self.connection.query_count assert not categories[0].articles assert self.connection.query_count == query_count
def test_deep_relationships(self): self.init_data() categories = self.session.query(self.Category).all() batch_fetch( categories, 'articles', 'articles.tags' ) query_count = self.connection.query_count categories[0].articles[0].tags assert self.connection.query_count == query_count categories[1].articles[1].tags assert self.connection.query_count == query_count
def test_multiple_relationships(self): categories = self.session.query(self.Category).all() batch_fetch( categories, 'articles', 'blog_posts' ) query_count = self.connection.query_count categories[0].articles[1] categories[0].blog_posts[0] assert self.connection.query_count == query_count categories[1].articles[1] categories[1].blog_posts[0] assert self.connection.query_count == query_count
def test_deep_relationships(self): articles = ( self.session.query(self.Article) .order_by(self.Article.id1).all() ) batch_fetch( articles, 'tags' ) query_count = self.connection.query_count assert articles[0].tags articles[1].tags assert articles[3].tags assert self.connection.query_count == query_count
def test_many_to_many_backref_population(self): articles = (self.session.query(self.Article).order_by( self.Article.id1).all()) batch_fetch( articles, with_backrefs('tags'), ) query_count = self.connection.query_count tags = articles[0].tags tags2 = articles[3].tags tags[0].articles tags2[0].articles names = [article.name for article in tags[0].articles] assert u'Article 1' in names assert self.connection.query_count == query_count
def test_batch_fetch(self): user = self.User() self.session.add(user) self.session.commit() event = self.Event(object=user) self.session.add(event) self.session.commit() events = self.session.query(self.Event).all() batch_fetch(events, 'object') query_count = self.connection.query_count events[0].object assert self.connection.query_count == query_count
def test_many_to_many_backref_population(self): self.init_data() categories = self.session.query(self.Category).all() batch_fetch( categories, 'articles', with_backrefs('articles.tags'), ) query_count = self.connection.query_count tags = categories[0].articles[0].tags tags2 = categories[1].articles[1].tags tags[0].articles tags2[0].articles names = [article.name for article in tags[0].articles] assert u'Article 1' in names assert self.connection.query_count == query_count
def test_many_to_many_backref_population(self): articles = ( self.session.query(self.Article) .order_by(self.Article.id1).all() ) batch_fetch( articles, with_backrefs('tags'), ) query_count = self.connection.query_count tags = articles[0].tags tags2 = articles[3].tags tags[0].articles tags2[0].articles names = [article.name for article in tags[0].articles] assert u'Article 1' in names assert self.connection.query_count == query_count
def test_supports_relationship_attributes(self): categories = self.session.query(self.Category).all() batch_fetch(categories, self.Category.articles) query_count = self.connection.query_count articles = categories[0].articles # no lazy load should occur assert len(articles) == 2 article_names = [article.name for article in articles] assert 'Article 1' in article_names assert 'Article 2' in article_names articles = categories[1].articles # no lazy load should occur assert len(articles) == 3 article_names = [article.name for article in articles] assert 'Article 3' in article_names assert 'Article 4' in article_names assert 'Article 5' in article_names assert self.connection.query_count == query_count
def test_compound_fetching(self): buildings = self.session.query(self.Building).all() batch_fetch( buildings, 'business_premises', ( 'equipment', 'business_premises.equipment' ) ) query_count = self.connection.query_count assert buildings[0].equipment.name == 'E 1' assert buildings[1].equipment.name == 'E 2' assert not buildings[2].equipment assert not buildings[1].business_premises assert buildings[2].business_premises[0].equipment.name == 'E 2' assert self.connection.query_count == query_count
def test_compound_fetching(self): buildings = self.session.query(self.Building).all() batch_fetch( buildings, 'business_premises', CompositePath( 'equipment', 'business_premises.equipment' ) ) query_count = self.connection.query_count assert buildings[0].equipment.name == 'E 1' assert buildings[1].equipment.name == 'E 2' assert not buildings[2].equipment assert not buildings[1].business_premises assert buildings[2].business_premises[0].equipment.name == 'E 2' assert self.connection.query_count == query_count
def paginate(self, search_content, content_type, offset, limit): if search_content: search_content = '%' + search_content + '%' filters = [ sqla.or_( Article.title.like(search_content), Article.location.has( sqla.or_(Location.name.like(search_content), Location.province.like(search_content), Location.city.like(search_content)))) ] else: filters = [] if content_type: filters.append(Article.content_type == content_type) count, articles = self.paginate_by(filters=filters, orders=[Article.id.desc()], offset=offset, limit=limit) if articles: sqla_utils.batch_fetch(articles, Article.location, Article.category) articles = [ article.asdict(include=Article.__dictfields__, follow={ "location": { "only": Location.__dictfields__ }, "category": { "only": Category.__dictfields__ } }) for article in articles ] return count, articles
def test_raises_error_if_relationship_not_found(self): categories = self.session.query(self.Category).all() with raises(AttributeError): batch_fetch(categories, 'unknown_relation')