jason kirtland avatar jason kirtland committed 50900a6

Added tests for Query.scalar(), .value() [ticket:1163]

Comments (0)

Files changed (2)

lib/sqlalchemy/orm/query.py

 
     def value(self, column):
         """Return a scalar result corresponding to the given column expression."""
-        
-        return self.values(column).next()[0]
-        
+        try:
+            return self.values(column).next()[0]
+        except StopIteration:
+            return None
+
     @_generative()
     def add_column(self, column):
         """Add a SQL ColumnElement to the list of result columns to be returned."""
           <Item>
           >>> session.query(Item.id).scalar()
           1
+          >>> session.query(Item.id).filter(Item.id < 0).scalar()
+          None
           >>> session.query(Item.id, Item.name).scalar()
           1
           >>> session.query(func.count(Parent.id)).scalar()
         This results in an execution of the underlying query.
 
         """
-        ret = list(self)[0]
-        if not isinstance(ret, tuple):
-            return ret
         try:
+            ret = list(self)[0]
+            if not isinstance(ret, tuple):
+                return ret
             return ret[0]
         except IndexError:
             return None

test/orm/query.py

         self.assertRaises(sa.orm.exc.MultipleResultsFound,
                           sess.query(User, Address).join(User.addresses).one)
 
-
     @testing.future
     def test_getslice(self):
         assert False
 
+    @testing.resolve_artifact_names
+    def test_scalar(self):
+        sess = create_session()
+
+        eq_(sess.query(User.id).filter_by(id=7).scalar(), 7)
+        eq_(sess.query(User.id, User.name).filter_by(id=7).scalar(), 7)
+        eq_(sess.query(User.id).filter_by(id=0).scalar(), None)
+        eq_(sess.query(User).filter_by(id=7).scalar(),
+            sess.query(User).filter_by(id=7).one())
+
+    @testing.resolve_artifact_names
+    def test_value(self):
+        sess = create_session()
+
+        eq_(sess.query(User).filter_by(id=7).value(User.id), 7)
+        eq_(sess.query(User.id, User.name).filter_by(id=7).value(User.id), 7)
+        eq_(sess.query(User).filter_by(id=0).value(User.id), None)
+
+        sess.bind = sa.testing.db
+        eq_(sess.query().value(sa.literal_column('1')), 1)
+
 
 class SelectFromTest(QueryTest):
     keep_mappers = False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.