1
Fork 0
simple-sql-parser/tools/Language/SQL/SimpleSQL/Tests.lhs

161 lines
5.5 KiB
Plaintext
Raw Normal View History

2013-12-13 11:39:26 +01:00
This is the main tests module which exposes the test data plus the
Test.Framework tests. It also contains the code which converts the
test data to the Test.Framework tests.
2013-12-14 19:42:01 +01:00
> module Language.SQL.SimpleSQL.Tests
> (testData
> ,tests
> ,TestItem(..)
> ) where
2013-12-13 11:39:26 +01:00
2015-07-31 11:45:51 +02:00
> import qualified Test.Tasty as T
> import qualified Test.Tasty.HUnit as H
2013-12-13 11:39:26 +01:00
> --import Language.SQL.SimpleSQL.Syntax
> import Language.SQL.SimpleSQL.Pretty
> import Language.SQL.SimpleSQL.Parse
> import Language.SQL.SimpleSQL.Lex
2013-12-16 09:03:46 +01:00
> import Language.SQL.SimpleSQL.TestTypes
> import Language.SQL.SimpleSQL.FullQueries
> import Language.SQL.SimpleSQL.GroupBy
> import Language.SQL.SimpleSQL.Postgres
2013-12-17 10:48:40 +01:00
> import Language.SQL.SimpleSQL.QueryExprComponents
> import Language.SQL.SimpleSQL.QueryExprs
> import Language.SQL.SimpleSQL.TableRefs
> import Language.SQL.SimpleSQL.ValueExprs
> import Language.SQL.SimpleSQL.Tpch
2015-07-31 23:04:18 +02:00
> import Language.SQL.SimpleSQL.LexerTests
> import Language.SQL.SimpleSQL.SQL2011Queries
> import Language.SQL.SimpleSQL.SQL2011AccessControl
> import Language.SQL.SimpleSQL.SQL2011Bits
> import Language.SQL.SimpleSQL.SQL2011DataManipulation
> import Language.SQL.SimpleSQL.SQL2011Schema
2013-12-13 11:39:26 +01:00
2014-06-27 11:19:15 +02:00
> import Language.SQL.SimpleSQL.MySQL
2013-12-17 10:48:40 +01:00
Order the tests to start from the simplest first. This is also the
order on the generated documentation.
2013-12-13 11:39:26 +01:00
> testData :: TestItem
> testData =
> Group "parserTest"
2015-07-31 23:04:18 +02:00
> [lexerTests
> ,valueExprTests
2013-12-17 10:48:40 +01:00
> ,queryExprComponentTests
> ,queryExprsTests
> ,tableRefTests
> ,groupByTests
2013-12-17 10:48:40 +01:00
> ,fullQueriesTests
> ,postgresTests
> ,tpchTests
> ,sql2011QueryTests
> ,sql2011DataManipulationTests
> ,sql2011SchemaTests
> ,sql2011AccessControlTests
> ,sql2011BitsTests
2014-06-27 11:19:15 +02:00
> ,mySQLTests
> ]
2015-07-31 11:45:51 +02:00
> tests :: T.TestTree
2013-12-14 19:42:01 +01:00
> tests = itemToTest testData
2013-12-14 19:42:01 +01:00
> --runTests :: IO ()
> --runTests = void $ H.runTestTT $ itemToTest testData
2015-07-31 11:45:51 +02:00
> itemToTest :: TestItem -> T.TestTree
> itemToTest (Group nm ts) =
2015-07-31 11:45:51 +02:00
> T.testGroup nm $ map itemToTest ts
2014-06-27 11:19:15 +02:00
> itemToTest (TestValueExpr d str expected) =
> toTest parseValueExpr prettyValueExpr d str expected
> itemToTest (TestQueryExpr d str expected) =
> toTest parseQueryExpr prettyQueryExpr d str expected
> itemToTest (TestStatement d str expected) =
> toTest parseStatement prettyStatement d str expected
> itemToTest (TestStatements d str expected) =
> toTest parseStatements prettyStatements d str expected
2014-06-27 11:19:15 +02:00
> itemToTest (ParseQueryExpr d str) =
> toPTest parseQueryExpr prettyQueryExpr d str
> itemToTest (ParseQueryExprFails d str) =
> toFTest parseQueryExpr prettyQueryExpr d str
> itemToTest (ParseValueExprFails d str) =
> toFTest parseValueExpr prettyValueExpr d str
> itemToTest (LexTest d s ts) = makeLexerTest d s ts
> itemToTest (LexFails d s) = makeLexingFailsTest d s
2015-07-31 23:04:18 +02:00
> makeLexerTest :: Dialect -> String -> [Token] -> T.TestTree
> makeLexerTest d s ts = H.testCase s $ do
> let lx = either (error . show) id $ lexSQL d "" Nothing s
2015-07-31 23:04:18 +02:00
> H.assertEqual "" ts $ map snd lx
> let s' = prettyTokens d $ map snd lx
> H.assertEqual "pretty print" s s'
> makeLexingFailsTest :: Dialect -> String -> T.TestTree
> makeLexingFailsTest d s = H.testCase s $ do
> case lexSQL d "" Nothing s of
> Right x -> H.assertFailure $ "lexing should have failed: " ++ s ++ "\ngot: " ++ show x
> Left _ -> return ()
2013-12-13 18:21:44 +01:00
> toTest :: (Eq a, Show a) =>
2014-06-27 11:19:15 +02:00
> (Dialect -> String -> Maybe (Int,Int) -> String -> Either ParseError a)
> -> (Dialect -> a -> String)
> -> Dialect
> -> String
> -> a
2015-07-31 11:45:51 +02:00
> -> T.TestTree
> toTest parser pp d str expected = H.testCase str $ do
2014-06-27 11:19:15 +02:00
> let egot = parser d "" Nothing str
> case egot of
2013-12-13 18:21:44 +01:00
> Left e -> H.assertFailure $ peFormattedError e
> Right got -> do
> H.assertEqual "" expected got
2014-06-27 11:19:15 +02:00
> let str' = pp d got
> let egot' = parser d "" Nothing str'
> case egot' of
> Left e' -> H.assertFailure $ "pp roundtrip"
> ++ "\n" ++ str'
2013-12-16 09:03:46 +01:00
> ++ peFormattedError e'
> Right got' -> H.assertEqual
> ("pp roundtrip" ++ "\n" ++ str')
> expected got'
2013-12-13 18:21:44 +01:00
> toPTest :: (Eq a, Show a) =>
2014-06-27 11:19:15 +02:00
> (Dialect -> String -> Maybe (Int,Int) -> String -> Either ParseError a)
> -> (Dialect -> a -> String)
> -> Dialect
> -> String
2015-07-31 11:45:51 +02:00
> -> T.TestTree
> toPTest parser pp d str = H.testCase str $ do
2014-06-27 11:19:15 +02:00
> let egot = parser d "" Nothing str
> case egot of
2013-12-13 18:21:44 +01:00
> Left e -> H.assertFailure $ peFormattedError e
> Right got -> do
2014-06-27 11:19:15 +02:00
> let str' = pp d got
> let egot' = parser d "" Nothing str'
> case egot' of
2013-12-16 09:03:46 +01:00
> Left e' -> H.assertFailure $ "pp roundtrip "
> ++ "\n" ++ str' ++ "\n"
2013-12-16 09:03:46 +01:00
> ++ peFormattedError e'
2013-12-13 18:21:44 +01:00
> Right _got' -> return ()
> toFTest :: (Eq a, Show a) =>
> (Dialect -> String -> Maybe (Int,Int) -> String -> Either ParseError a)
> -> (Dialect -> a -> String)
> -> Dialect
> -> String
2015-07-31 11:45:51 +02:00
> -> T.TestTree
2015-07-31 23:04:18 +02:00
> toFTest parser _pp d str = H.testCase str $ do
> let egot = parser d "" Nothing str
> case egot of
2015-07-31 23:04:18 +02:00
> Left _e -> return ()
> Right _got ->
> H.assertFailure $ "parse didn't fail: " ++ show d ++ "\n" ++ str