{-# LANGUAGE OverloadedStrings #-}
import Control.Exception (bracket)
import Database.MySQL.Simple (ConnectInfo (..), defaultConnectInfo,
connect, close,
query_, Only (..))
import System.Exit (exitFailure)
import Test.Hspec
import TestOpts (dbtests)
-- This is how to connect to our test database
testConn :: ConnectInfo
testConn = defaultConnectInfo {
connectHost = "127.0.0.1",
connectUser = "test",
connectDatabase = "test"
}
advice :: String
advice =
"\n\
\Database test not run for mysql-simple\n\
\======================================\n\
\\n\
\To run the tests properly, you need to specify the flag 'dbtests' when\n\
\building the test, for example either of these commands will do it:\n\
\\n\
\ stack test --flag mysql-simple:dbtests\n\
\ cabal configure --enable-tests --flags=dbtests && cabal test\n\
\\n\
\You also need to set up a MySQL database which the test can use via\n\
\these connection parameters:\n\
\\n\
\ Host \"127.0.0.1\"\n\
\ Port 3306\n\
\ User \"test\"\n\
\ Password \"\"\n\
\ Database \"test\"\n\
\\n"
-- Only the most cursory test is done at the moment, simply showing that
-- things hang together sufficiently well that we can talk to the database
-- server.
--
main :: IO ()
main = if dbtests then bracket (connect testConn) close $ \conn -> hspec $ do
describe "Database" $ do
it "seems to be connected" $ do
result <- query_ conn "select 1 + 1"
result `shouldBe` [Only (2::Int)]
else do
putStr advice
exitFailure