如何使用QuickCheck测试数据库相关的功能?

Sau*_*nda 5 haskell persistent quickcheck

我需要测试许多访问数据库的函数(通过Persistent).虽然我可以使用它monadicIO,withSqlitePool但会导致测试效率低下.每个测试,而不是属性,但测试,将创建和销毁数据库池.我该如何防止这种情况?

重要提示:忘记效率或优雅.我甚至无法制作QuickCheck和Persistent类型甚至构成.

instance (Monad a) => MonadThrow (PropertyM a)

instance (MonadThrow a) => MonadCatch (PropertyM a)

type NwApp = SqlPersistT IO

prop_childCreation :: PropertyM NwApp Bool
prop_childCreation = do
  uid <- pick $ UserKey <$> arbitrary
  lid <- pick $ LogKey <$> arbitrary
  gid <- pick $ Aria2Gid <$> arbitrary
  let createDownload_  = createDownload gid lid uid []
  (Entity pid _) <- run $ createDownload_ Nothing
  dstatus <- pick arbitrary
  parent <- run $ updateGet pid [DownloadStatus =. dstatus]

  let test = do 
        (Entity cid child) <- run $ createDownload_ (Just pid)
        case (parent ^. status, child ^. status) of
          (DownloadComplete ChildrenComplete, DownloadComplete ChildrenNone) -> return True
          (DownloadComplete ChildrenIncomplete, DownloadIncomplete) -> return True
          _ -> return False

  test `catches` [
    Handler (\ (e :: SanityException) -> return True),
    Handler (\ (e :: SomeException) -> return False)
    ]

-- How do I write this function?
runTests = monadicIO $ runSqlite ":memory:" $ do 
 -- whatever I do, this function fails to typecheck
Run Code Online (Sandbox Code Playgroud)

ben*_*ofs 4

为了避免创建和销毁数据库池并仅设置数据库一次,您需要withSqliteConn在main外部函数中使用,然后转换每个属性以使用该连接,如以下代码所示:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show Eq
|]

type SqlT m = SqlPersistT (NoLoggingT (ResourceT m))

prop_insert_person :: PropertyM (SqlT IO) ()
prop_insert_person = do
  personName <- pick arbitrary
  personAge  <- pick arbitrary
  let person = Person personName personAge

  -- This assertion will fail right now on the second iteration
  -- since I have not implemented the cleanup code
  numEntries <- run $ count ([] :: [Filter Person])
  assert (numEntries == 0)

  personId <- run $ insert person
  result <- run $ get personId
  assert (result == Just person)

main :: IO ()
main = runNoLoggingT $ withSqliteConn ":memory:" $ \connection -> lift $ do
  let 
    -- Run a SqlT action using our connection
    runSql :: SqlT IO a -> IO a
    runSql =  flip runSqlPersistM connection

    runSqlProperty :: SqlT IO Property -> Property
    runSqlProperty action = ioProperty . runSql $ do
        prop <- action
        liftIO $ putStrLn "\nDB reset code (per test) goes here\n"
        return prop

    quickCheckSql :: PropertyM (SqlT IO) () -> IO ()
    quickCheckSql = quickCheck . monadic runSqlProperty

  -- Initial DB setup code
  runSql $ runMigration migrateAll

  -- Test as many quickcheck properties as you like
  quickCheckSql prop_insert_person
Run Code Online (Sandbox Code Playgroud)

包括导入和扩展的完整代码可以在这个要点中找到。

请注意,我没有实现在测试之间清理数据库的功能,因为我不知道如何使用持久性方法来执行此操作,您必须自己实现(替换现在仅打印消息的占位符清理操作) 。


您也不应该需要MonadCatch/ MonadThrowfor的实例PropertyM。相反,您应该捕获 monad NwApp。所以代替这个:

let test = do
  run a
  ...
  run b
test `catch` \exc -> ...
Run Code Online (Sandbox Code Playgroud)

您应该使用以下代码:

let test = do
  a
  b
  return ...whether or not the test was successfull...
let testCaught = test `catch` \exc -> ..handler code...
ok <- test
assert ok
Run Code Online (Sandbox Code Playgroud)