diff --git a/beam-core/test/Database/Beam/Test/SQL.hs b/beam-core/test/Database/Beam/Test/SQL.hs index 0c8c8f4b..b8f3aaa3 100644 --- a/beam-core/test/Database/Beam/Test/SQL.hs +++ b/beam-core/test/Database/Beam/Test/SQL.hs @@ -12,7 +12,7 @@ import Database.Beam.Test.Schema hiding (tests) import Database.Beam import Database.Beam.Backend.SQL (MockSqlBackend) import Database.Beam.Backend.SQL.AST - +import Data.Kind (Type) import Data.Int import Data.Time.Clock import Data.Text (Text) @@ -24,6 +24,7 @@ tests :: TestTree tests = testGroup "SQL generation tests" [ simpleSelect , simpleWhere + , simpleWhereNoFrom , simpleJoin , selfJoin , leftJoin @@ -118,6 +119,32 @@ simpleWhere = selectWhere @?= Just (ExpressionBinOp "AND" salaryCond (ExpressionBinOp "AND" ageCond nameCond)) +-- | Simple select without FROM clause (#667) + +data EmptyDb (f :: Type -> Type) = EmptyDb + +simpleWhereNoFrom :: TestTree +simpleWhereNoFrom = + testCase "WHERE clause not dropped if there is no FROM" $ do + SqlSelect Select { selectTable = SelectTable { .. }, .. } <- pure $ selectMock simple + + selectGrouping @?= Nothing + selectOrdering @?= [] + selectLimit @?= Nothing + selectOffset @?= Nothing + selectHaving @?= Nothing + selectQuantifier @?= Nothing + -- Important point: no FROM clause, yet WHERE clause should still be here + selectFrom @?= Nothing + selectWhere @?= (Just (ExpressionValue (Value False))) + + where + simple :: Q (MockSqlBackend Command) EmptyDb s (QExpr (MockSqlBackend Command) s Bool) + simple = do + guard_ (val_ False) + pure (val_ True) + + -- | Ensure that multiple tables are correctly joined simpleJoin :: TestTree