add test coverage for count(), group_by(), having() in conjunction with select_from()

Issue #898 resolved
Mike Bayer repo owner created an issue

No description provided.

Comments (7)

  1. Former user Account Deleted

    Could this be related to the fact that

    len(Item.query().from_statement(compound).all())
    

    returns correct number and

    Item.query().from_statement(compound).count()
    

    ignores the compound part and simply counts all Items?

    If not, maybe you could give me a clue what could be wrong (because the first statement seems logical to me)?

  2. Mike Bayer reporter

    yes, count() has fallen out of maintenance and does not interact well with some of the newer options.

    your best bet for now is:

    select([func.count(1)](func.count(1))).select_from(compound).execute().scalar()
    
  3. Mike Bayer reporter

    0.5's Query works fairly well with select_from() and count(), might need more test coverage for group_by() but this should be generally working now.

  4. Mike Bayer reporter
    [classic@photon2 sqlalchemy]$ git diff
    diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
    index 2fae362..a9926b7 100644
    --- a/lib/sqlalchemy/orm/query.py
    +++ b/lib/sqlalchemy/orm/query.py
    @@ -1601,6 +1601,8 @@ class Query(object):
                in the same way as ORDER BY.
    
             """
    +        if self._from_obj:
    +            raise Exception("group by coverered")
    
             if len(criterion) == 1:
                 if criterion[0] is None:
    @@ -1632,6 +1634,8 @@ class Query(object):
                             having(func.count(Address.id) > 2)
    
             """
    +        if self._from_obj:
    +            raise Exception("having coverered")
    
             criterion = expression._expression_literal_as_text(criterion)
    
    @@ -3061,6 +3065,8 @@ class Query(object):
                 session.query(func.count(distinct(User.name)))
    
             """
    +        if self._from_obj:
    +            raise Exception("count coverered")
             col = sql.func.count(sql.literal_column('*'))
             return self.from_self(col).scalar()
    
    #!
    
    _
    Traceback (most recent call last):
      File "/home/classic/dev/sqlalchemy/test/orm/test_query.py", line 2792, in test_having
        assert [User(name='ed', id=8)] == \
      File "<string>", line 2, in having
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/base.py", line 201, in generate
        fn(self, *args[1:], **kw)
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/query.py", line 1638, in having
        raise Exception("having coverered")
    Exception: having coverered
    _______________________________________________________ CountTest.test_multiple_entity _______________________________________________________
    Traceback (most recent call last):
      File "/home/classic/dev/sqlalchemy/test/orm/test_query.py", line 2882, in test_multiple_entity
        eq_(q.count(), 5)
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/query.py", line 3069, in count
        raise Exception("count coverered")
    Exception: count coverered
    ___________________________________________________________ CountTest.test_nested ____________________________________________________________
    Traceback (most recent call last):
      File "/home/classic/dev/sqlalchemy/test/orm/test_query.py", line 2895, in test_nested
        eq_(q.count(), 5)
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/query.py", line 3069, in count
        raise Exception("count coverered")
    Exception: count coverered
    ___________________________________________________________ ExpressionTest.test_in ___________________________________________________________
    Traceback (most recent call last):
      File "/home/classic/dev/sqlalchemy/test/orm/test_query.py", line 1564, in test_in
        s = session.query(User.id).join(User.addresses).group_by(User.id).\
      File "<string>", line 2, in group_by
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/base.py", line 201, in generate
        fn(self, *args[1:], **kw)
      File "/home/classic/dev/sqlalchemy/test/../lib/sqlalchemy/orm/query.py", line 1605, in group_by
        raise Exception("group by coverered")
    Exception: group by coverered
    ==================================================== 4 failed, 245 passed in 3.66 seconds ====================================================
    
  5. Log in to comment