def test_count_and_filter(self): f1 = dict(method='count', field='authors') f2 = dict(method='filter', kwargs=dict(authors__count=3)) entries = traverse(self.blogs, Blog.entry_set, filters=[f1, f2]) assertQueryResultsEqual(self, entries, []) f1 = dict(method='count', field='authors') f2 = dict(method='filter', kwargs=dict(authors__count=2)) entries = traverse(self.blogs, Blog.entry_set, filters=[f1, f2]) assertQueryResultsEqual(self, entries, [(self.entries[0], self.blogs[0].pk), (self.entries[1], self.blogs[0].pk), (self.entries[2], self.blogs[0].pk)])
def test_exclude(self): f = dict(method='exclude', kwargs=dict(name__icontains='Smith')) authors = traverse(self.entries, Entry.authors, filters=[f]) assertQueryResultsEqual(self, authors, [(self.authors[1], self.entries[1].pk), (self.authors[2], self.entries[1].pk), (self.authors[2], self.entries[2].pk), (self.authors[1], self.entries[0].pk)])
def test_single_query_for_M2M_with_filter(self): connection.use_debug_cursor = True connection.queries = [] f = dict(method='filter', kwargs=dict(name__icontains='Smith')) authors = traverse(self.entries, Entry.authors, filters=[f]) self.assertEquals(len(authors), 2*TestQueryCount.N) self.assertEquals(len(connection.queries), 1)
def test_can_traverse_from_M2M_objects_with_exclusions(self): f = dict(method='exclude', kwargs=dict(headline__icontains='Graph')) entries = traverse(self.authors, Author.entry_set, filters=[f]) assertQueryResultsEqual(self, entries, [(self.entries[0], self.authors[0].pk), (self.entries[0], self.authors[1].pk), (self.entries[1], self.authors[1].pk), (self.entries[1], self.authors[2].pk)])
def test_can_traverse_to_M2M_objects_with_exclusions(self): f = dict(method='exclude', kwargs=dict(name__icontains='Smith')) authors = traverse(self.entries, Entry.authors, filters=[f]) assertQueryResultsEqual(self, authors, [(self.authors[1], self.entries[0].pk), (self.authors[1], self.entries[1].pk), (self.authors[2], self.entries[1].pk), (self.authors[2], self.entries[2].pk)])
def test_can_traverse_from_M2M_objects_and_returns_traversed_pair(self): entries = traverse(self.authors, Author.entry_set) assertQueryResultsEqual(self, entries, [(self.entries[0], self.authors[0].pk), (self.entries[0], self.authors[1].pk), (self.entries[1], self.authors[1].pk), (self.entries[1], self.authors[2].pk), (self.entries[2], self.authors[2].pk), (self.entries[2], self.authors[0].pk)])
def test_can_traverse_to_M2M_objects_and_returns_traversed_pair(self): authors = traverse(self.entries, Entry.authors) assertQueryResultsEqual(self, authors, [(self.authors[0], self.entries[0].pk), (self.authors[1], self.entries[0].pk), (self.authors[1], self.entries[1].pk), (self.authors[2], self.entries[1].pk), (self.authors[2], self.entries[2].pk), (self.authors[0], self.entries[2].pk)])
def test_can_traverse_from_fk_objects_and_returns_traversed_pair(self): entries = traverse(self.blogs, Blog.entry_set) assertQueryResultsEqual( self, entries, [ (self.entries[0], self.blogs[0].pk), (self.entries[1], self.blogs[0].pk), (self.entries[2], self.blogs[0].pk), (self.entries[3], self.blogs[1].pk), ], )
def test_can_traverse_to_fk_objects_with_exclusions(self): f = dict(method="exclude", kwargs=dict(name__icontains="graph")) blogs = traverse(self.entries, Entry.blog, filters=[f]) assertQueryResultsEqual( self, blogs, [ (self.blogs[0], self.entries[0].pk), (self.blogs[0], self.entries[1].pk), (self.blogs[0], self.entries[2].pk), ], )
def test_can_traverse_to_fk_objects_and_returns_traversed_pair(self): blogs = traverse(self.entries, Entry.blog) assertQueryResultsEqual( self, blogs, [ (self.blogs[0], self.entries[0].pk), (self.blogs[0], self.entries[1].pk), (self.blogs[0], self.entries[2].pk), (self.blogs[1], self.entries[3].pk), ], )
def _graph_step(obj_src, model, step_f, filters, tree=None): """ Traverse one step on the graph. Takes in and returns arrays of output, input object tuples. The input objects in the tuples are from start of the query, not start of this step. """ # check if type matches existing object type if len(obj_src): t = type(obj_src[0][0]) if hasattr(t, '_deferred') and t._deferred: t = t.__base__ if t != model_registry.get_manager(model).model_class: raise Exception('Type mismatch when executing query: expecting "%s", got "%s"' % (model, type(obj_src[0][0]))) next_obj_src = traverse([obj for obj, src in obj_src], step_f, filters) if tree is not None: tree.extend((t[0].id, t[1]) for t in next_obj_src) return Query._extend_result(obj_src, next_obj_src)
def _graph_step(obj_src, model, step_f, filters, tree=None): """ Traverse one step on the graph. Takes in and returns arrays of output, input object tuples. The input objects in the tuples are from start of the query, not start of this step. """ # check if type matches existing object type if len(obj_src): t = type(obj_src[0][0]) if hasattr(t, '_deferred') and t._deferred: t = t.__base__ if t != model_registry.get_manager(model).model_class: raise Exception( 'Type mismatch when executing query: expecting "%s", got "%s"' % (model, type(obj_src[0][0]))) next_obj_src = traverse([obj for obj, src in obj_src], step_f, filters) if tree is not None: tree.extend((t[0].id, t[1]) for t in next_obj_src) return Query._extend_result(obj_src, next_obj_src)
def test_single_query_for_reverse_M2M(self): connection.use_debug_cursor = True connection.queries = [] entries = traverse(self.authors, Author.entry_set) self.assertEquals(len(entries), 2*TestQueryCount.N) self.assertEquals(len(connection.queries), 1)
def test_can_traverse_to_fk_objects_with_filter(self): f = dict(method="filter", kwargs=dict(name__icontains="graph")) blogs = traverse(self.entries, Entry.blog, filters=[f]) assertQueryResultsEqual(self, blogs, [(self.blogs[1], self.entries[3].pk)])
def test_can_traverse_from_fk_objects_with_filter(self): f = dict(method='filter', kwargs=dict(headline__icontains='graph')) entries = traverse(self.blogs, Blog.entry_set, filters=[f]) assertQueryResultsEqual(self, entries, [(self.entries[2], self.blogs[0].pk), (self.entries[3], self.blogs[1].pk)])
def test_can_traverse_from_fk_objects_and_returns_traversed_pair(self): entries = traverse(self.blogs, Blog.entry_set) assertQueryResultsEqual(self, entries, [(self.entries[0], self.blogs[0].pk), (self.entries[1], self.blogs[0].pk), (self.entries[2], self.blogs[0].pk), (self.entries[3], self.blogs[1].pk)])
def test_can_traverse_to_fk_objects_with_exclusions(self): f = dict(method='exclude', kwargs=dict(name__icontains='graph')) blogs = traverse(self.entries, Entry.blog, filters=[f]) assertQueryResultsEqual(self, blogs, [(self.blogs[0], self.entries[0].pk), (self.blogs[0], self.entries[1].pk), (self.blogs[0], self.entries[2].pk)])
def test_can_traverse_via_function_with_exclusions(self): f = dict(method='exclude', kwargs=dict(name__icontains='Smith')) authors = traverse(self.blogs, Blog.authors, filters=[f]) assertQueryResultsEqual(self, authors, [x for x in Blog.authors(self.blogs) if 'Smith' not in x[0].name])
def test_max_and_filter(self): f1 = dict(method='max', field='authors__age') f2 = dict(method='filter', kwargs=dict(authors__age__max__lt=35)) entries = traverse(self.blogs, Blog.entry_set, filters=[f1, f2]) assertQueryResultsEqual(self, entries, [(self.entries[0], self.blogs[0].pk)])
def test_can_traverse_from_fk_objects_with_exclusions(self): f = dict(method="exclude", kwargs=dict(headline__icontains="graph")) entries = traverse(self.blogs, Blog.entry_set, filters=[f]) assertQueryResultsEqual( self, entries, [(self.entries[0], self.blogs[0].pk), (self.entries[1], self.blogs[0].pk)] )
def test_can_traverse_via_function_and_returns_traversed_pair(self): authors = traverse(self.blogs, Blog.authors) assertQueryResultsEqual(self, authors, Blog.authors(self.blogs))
def test_single_query_for_M2M(self): connection.use_debug_cursor = True connection.queries = [] authors = traverse(self.entries, Entry.authors) self.assertEquals(len(authors), 2*TestQueryCount.N) self.assertEquals(len(connection.queries), 1)
def test_can_traverse_via_function_with_exclusions(self): f = dict(method='exclude', kwargs=dict(name__icontains='Smith')) authors = traverse(self.blogs, Blog.authors, filters=[f]) assertQueryResultsEqual( self, authors, [x for x in Blog.authors(self.blogs) if 'Smith' not in x[0].name])
def test_double_filter(self): f1 = dict(method='filter', kwargs=dict(name__icontains='Smith')) f2 = dict(method='filter', kwargs=dict(name__icontains='Joe')) authors = traverse(self.entries, Entry.authors, filters=[f1, f2]) assertQueryResultsEqual(self, authors, [])
def test_single_query_for_FK(self): connection.use_debug_cursor = True connection.queries = [] blogs = traverse(self.entries, Entry.blog) self.assertEquals(len(blogs), TestQueryCount.N) self.assertEquals(len(connection.queries), 1)
def test_single_query_for_reverse_FK(self): connection.use_debug_cursor = True connection.queries = [] entries = traverse(self.blogs, Blog.entry_set) self.assertEquals(len(entries), TestQueryCount.N) self.assertEquals(len(connection.queries), 1)
def test_can_traverse_to_fk_objects_and_returns_traversed_pair(self): blogs = traverse(self.entries, Entry.blog) assertQueryResultsEqual(self, blogs, [(self.blogs[0], self.entries[0].pk), (self.blogs[0], self.entries[1].pk), (self.blogs[0], self.entries[2].pk), (self.blogs[1], self.entries[3].pk)])
def test_sum_and_filter(self): f1 = dict(method='sum', field='authors__age') f2 = dict(method='filter', kwargs=dict(authors__age__sum__gt=51)) entries = traverse(self.blogs, Blog.entry_set, filters=[f1, f2]) assertQueryResultsEqual(self, entries, [(self.entries[1], self.blogs[0].pk), (self.entries[2], self.blogs[0].pk)])