def test_query_article_inequality_explicit(testbed): Article(tags=['perl']).put() Article(tags=['perl', 'python']).put() query = snippets.query_article_inequality_explicit() articles = query.fetch() assert len(articles) == 1
def test_print_query_keys(testbed, capsys): for i in range(3): Article(title='title {}'.format(i)).put() snippets.print_query_keys(Article.query()) stdout, _ = capsys.readouterr() assert "Key('Article'" in stdout
def test_query_properties_named_by_string_using_getattr(testbed): Article(title='from').put() Article(title='to').put() query = snippets.query_properties_named_by_string_using_getattr( 'title', 'from') articles = query.fetch() assert len(articles) == 1
def test_query_article_inequality_explicit(testbed): Article(tags=['perl']).put() Article(tags=['perl', 'python']).put() query = snippets.query_article_inequality_explicit() articles = query.fetch() if len(articles) != 1: raise AssertionError
def test_query_properties_named_by_string_for_defined_properties(testbed): Article(title='from').put() Article(title='to').put() query = snippets.query_properties_named_by_string_for_defined_properties( 'title', 'from') articles = query.fetch() if len(articles) != 1: raise AssertionError
def test_order_query_results_by_property(testbed): Article(title='2').put() Article(title='1').put() FlexEmployee(location=2).put() FlexEmployee(location=1).put() expando_query, property_query = snippets.order_query_results_by_property( 'title') assert expando_query.fetch()[0].location == 1 assert property_query.fetch()[0].title == '1'
def test_query_article_in(testbed): Article(tags=['perl']).put() Article(tags=['perl', 'python']).put() Article(tags=['ruby']).put() Article(tags=['php']).put() query = snippets.query_article_in() articles = query.fetch() assert len(articles) == 3
def test_query_article_in_equivalent(testbed): Article(tags=['perl']).put() Article(tags=['perl', 'python']).put() Article(tags=['ruby']).put() Article(tags=['php']).put() query = snippets.query_article_in_equivalent() articles = query.fetch() if len(articles) != 3: raise AssertionError
def test_order_query_results_by_property(testbed): Article(title='2').put() Article(title='1').put() FlexEmployee(location=2).put() FlexEmployee(location=1).put() expando_query, property_query = snippets.order_query_results_by_property( 'title') if expando_query.fetch()[0].location != 1: raise AssertionError if property_query.fetch()[0].title != '1': raise AssertionError
def query_article_nested(): query = Article.query( ndb.AND( Article.tags == 'python', ndb.OR(Article.tags.IN(['ruby', 'jruby']), ndb.AND(Article.tags == 'php', Article.tags != 'perl')))) return query
def test_fetch_good_articles_using_gql_with_inlined_bind(testbed): for i in range(1, 6): Article(stars=i).put() query = snippets.fetch_good_articles_using_gql_with_inlined_bind() articles = query.fetch() assert len(articles) == 2 assert all(a.stars > 3 for a in articles)
def test_fetch_good_articles_using_gql_with_inlined_bind(testbed): for i in range(1, 6): Article(stars=i).put() query = snippets.fetch_good_articles_using_gql_with_inlined_bind() articles = query.fetch() if len(articles) != 2: raise AssertionError if not all(a.stars > 3 for a in articles): raise AssertionError
def test_query_article_nested(testbed): Article(tags=['python']).put() # excluded - no non-python Article(tags=['ruby']).put() # excluded - no python Article(tags=['python', 'ruby']).put() # included Article(tags=['python', 'jruby']).put() # included Article(tags=['python', 'ruby', 'jruby']).put() # included Article(tags=['python', 'php']).put() # included Article(tags=['python', 'perl']).put() # excluded query = snippets.query_article_nested() articles = query.fetch() assert len(articles) == 4
def query_article_nested(): query = Article.query(ndb.AND(Article.tags == 'python', ndb.OR(Article.tags.IN(['ruby', 'jruby']), ndb.AND(Article.tags == 'php', Article.tags != 'perl')))) return query
def query_article_in(): query = Article.query(Article.tags.IN(['python', 'ruby', 'php'])) return query
def query_article_in_equivalent(): query = Article.query(ndb.OR(Article.tags == 'python', Article.tags == 'ruby', Article.tags == 'php')) return query
def query_article_inequality(): query = Article.query(Article.tags != 'perl') return query
def query_article_inequality_explicit(): query = Article.query(ndb.OR(Article.tags < 'perl', Article.tags > 'perl')) return query
def query_properties_named_by_string_using_getattr(keyword, value): query = Article.query(getattr(Article, keyword) == value) return query
def order_query_results_by_property(keyword): expando_query = FlexEmployee.query().order(ndb.GenericProperty('location')) property_query = Article.query().order(Article._properties[keyword]) return expando_query, property_query
def query_properties_named_by_string_for_defined_properties(keyword, value): query = Article.query(Article._properties[keyword] == value) return query
def articles_with_tags_example(): # [START included_in_inequality] Article(title='Perl + Python = Parrot', stars=5, tags=['python', 'perl']) # [END included_in_inequality] # [START excluded_from_inequality] Article(title='Introduction to Perl', stars=3, tags=['perl'])
def query_article_in_equivalent(): query = Article.query( ndb.OR(Article.tags == 'python', Article.tags == 'ruby', Article.tags == 'php')) return query