sqlite-easy-0.1.0.0: test/SqliteEasySpec.hs
{-# language OverloadedStrings #-}
{-# language ScopedTypeVariables #-}
module SqliteEasySpec (spec) where
import Test.Hspec
import Database.Sqlite.Easy
import Control.Exception (try)
spec :: Spec
spec = do
describe "sanity" $ do
it "create insert select" $ do
result <- withDb ":memory:" $ \db -> do
[] <- run "create table t(x int not null)" db
[] <- run "insert into t values (1),(2),(3)" db
run "select * from t" db
shouldBe
result
[[SQLInteger 1], [SQLInteger 2], [SQLInteger 3]]
it "select with params" $ do
result <- withDb ":memory:" $ \db -> do
[] <- run "create table t(x int not null)" db
[] <- run "insert into t values (1),(2),(3)" db
runWith "select * from t where x = ?" [SQLInteger 1] db
shouldBe
result
[[SQLInteger 1]]
describe "transactions" $ do
it "transaction is rolled back in case of error" $ do
result <- withDb ":memory:" $ \db -> do
[] <- run "create table t(x int not null)" db
Left SQLError{} <- try $ asTransaction db $ do
[] <- run "insert into t values (1)" db
[] <- run "insert into t values (2" db
pure ()
run "select * from t" db
shouldBe result []
it "transaction is commited" $ do
result <- withDb ":memory:" $ \db -> do
[] <- run "create table t(x int not null)" db
(Right () :: Either SQLError ()) <- try $ asTransaction db $ do
[] <- run "insert into t values (1)" db
[] <- run "insert into t values (2)" db
pure ()
run "select * from t" db
shouldBe result [[SQLInteger 1], [SQLInteger 2]]
it "transaction is cancelled" $ do
result <- withDb ":memory:" $ \db -> do
[] <- run "create table t(x int not null)" db
(Right transactionResult :: Either SQLError [[SQLData]]) <- try $ asTransaction db $ do
[] <- run "insert into t values (1)" db
cancelTransaction []
result <- run "select * from t" db
pure (transactionResult, result)
uncurry shouldBe result
describe "pool" $ do
it "create, use, and destroy" $ do
pool <- createSqlitePool ":memory:"
result <- withResource pool $ \db -> do
[] <- run "create table t(x int not null)" db
[] <- run "insert into t values (1)" db
run "select * from t" db
destroyAllResources pool
shouldBe result [[SQLInteger 1]]