added tests for math and aggregation functions

This commit is contained in:
Sam Anklesaria 2013-07-03 15:46:22 +09:00
parent 4377f2cffa
commit 3641f36326
4 changed files with 47 additions and 4 deletions

View File

@ -43,7 +43,7 @@ module Database.Esqueleto
, val, isNothing, just, nothing, countRows, count, not_
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
, (+.), (-.), (/.), (*.)
, random_, sum_, round_, ceil_, floor_, avg_, min_, max_
, random_, sum_, round_, ceiling_, floor_, avg_, min_, max_
, like, (%), concat_, (++.)
, subList_select, subList_selectDistinct, valList
, in_, notIn, exists, notExists

View File

@ -234,7 +234,7 @@ class (Functor query, Applicative query, Monad query) =>
random_ :: PersistField a => expr (Value a)
round_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
ceil_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
ceiling_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
floor_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
sum_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
min_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)

View File

@ -335,8 +335,8 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
random_ = unsafeSqlValue "RANDOM()"
sum_ = unsafeSqlFunction "SUM"
round_ = unsafeSqlFunction "ROUND_"
ceil_ = unsafeSqlFunction "CEILING"
round_ = unsafeSqlFunction "ROUND"
ceiling_ = unsafeSqlFunction "CEILING"
floor_ = unsafeSqlFunction "FLOOR"
avg_ = unsafeSqlFunction "AVG"
min_ = unsafeSqlFunction "MIN"

View File

@ -276,6 +276,49 @@ main = do
return $ sum_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value (36 + 17 + 17 :: Int) ]
it "works with sum_" $
run $ do
_ <- insert' p1
_ <- insert' p2
_ <- insert' p3
_ <- insert' p4
ret <- select $
from $ \p->
return $ avg_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value ((36 + 17 + 17) / 3 :: Double) ]
it "works with min_" $
run $ do
_ <- insert' p1
_ <- insert' p2
_ <- insert' p3
_ <- insert' p4
ret <- select $
from $ \p->
return $ min_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value (17 :: Int) ]
it "works with max_" $
run $ do
_ <- insert' p1
_ <- insert' p2
_ <- insert' p3
_ <- insert' p4
ret <- select $
from $ \p->
return $ max_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value (36 :: Int) ]
it "works with random_" $
run $ do
ret <- select $ return (random_ :: SqlExpr (Value Int))
return ()
it "works with round_" $
run $ do
ret <- select $ return $ round_ (val (16.2 :: Double))
liftIO $ ret `shouldBe` [ Value (16 :: Double) ]
it "works with isNothing" $
run $ do
_ <- insert' p1