basic-test.hs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. {-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
  2. {-# LANGUAGE TypeFamilies, EmptyDataDecls, GADTs #-}
  3. {-# LANGUAGE MultiParamTypeClasses #-}
  4. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  5. module Main where
  6. import Database.Zookeeper ()
  7. import Database.Persist
  8. import Database.Persist.Zookeeper
  9. import Database.Persist.Zookeeper.Internal
  10. import Database.Persist.TH
  11. import Language.Haskell.TH.Syntax ()
  12. import Data.Maybe
  13. import Data.Pool ()
  14. import Test.Hspec
  15. let zookeeperSettings = defaultZookeeperSettings
  16. in share [mkPersist zookeeperSettings] [persistLowerCase|
  17. Person
  18. name String
  19. age Int
  20. hoge Int Maybe
  21. PersonU name
  22. deriving Show
  23. deriving Eq
  24. |]
  25. zookeeperConf :: ZookeeperConf
  26. zookeeperConf = ZookeeperConf "localhost:2181" 10000 1 50 30
  27. main :: IO ()
  28. main =
  29. withZookeeperPool zookeeperConf $ \conn -> do
  30. hspec $ do
  31. let key = txtToKey "WyJzVGVzdC9ob2dlIl0="
  32. let val = Person "Test/hoge" 12 Nothing
  33. describe "PersistUnique test" $ do
  34. it "insertUnique" $ do
  35. v <- flip runZookeeperPool conn $ do
  36. deleteBy $ PersonU "Test/hoge"
  37. insertUnique val
  38. v `shouldBe` (Just key)
  39. it "getBy" $ do
  40. v <- flip runZookeeperPool conn $ do
  41. getBy $ PersonU "Test/hoge"
  42. (entityKey (fromJust v)) `shouldBe` key
  43. (entityVal (fromJust v)) `shouldBe` val
  44. v `shouldBe` (Just (Entity key val))
  45. it "deleteBy" $ do
  46. v <- flip runZookeeperPool conn $ do
  47. deleteBy $ PersonU "Test/hoge"
  48. getBy $ PersonU "Test/hoge"
  49. v `shouldBe` Nothing
  50. describe "PersistStore test" $ do
  51. it "StoreTest" $ do
  52. key' <- flip runZookeeperPool conn $ do
  53. insert val
  54. v <- flip runZookeeperPool conn $ do
  55. get key'
  56. print $ show key'
  57. v `shouldBe` (Just val)
  58. v' <- flip runZookeeperPool conn $ do
  59. delete key'
  60. get key'
  61. v' `shouldBe` Nothing
  62. describe "PersistQeuery test" $ do
  63. let check val' filter' expbool = case filterClause val' filter' of
  64. a@(bool,_,_) -> do
  65. print $ show a
  66. bool `shouldBe` expbool
  67. it "FilterTest Filter OR/AND" $ do
  68. check (Person "Test/hoge" 12 Nothing) [FilterOr[]] False
  69. check (Person "Test/hoge" 12 Nothing) [FilterAnd[]] True
  70. it "FilterTest Nothing" $ do
  71. check (Person "Test/hoge" 12 Nothing) [] True
  72. check (Person "Test/hoge" 12 (Just 3)) [] True
  73. check (Person "Test/hoge" 12 Nothing) [] True
  74. it "FilterTestEq" $ do
  75. check (Person "Test/hoge" 12 Nothing) [PersonName ==. ""] False
  76. check (Person "Test/hoge" 12 (Just 3)) [PersonHoge ==. Just 3] True
  77. check (Person "Test/hoge" 12 Nothing) [PersonAge ==. 12] True
  78. it "FilterTestNe" $ do
  79. check (Person "Test/hoge" 12 Nothing) [PersonAge !=. 12] False
  80. check (Person "Test/hoge" 12 Nothing) [PersonAge !=. 11] True
  81. check (Person "Test/hoge" 12 (Just 4)) [PersonHoge !=. Just 4] False
  82. check (Person "Test/hoge" 12 Nothing) [PersonHoge !=. Just 3] True
  83. check (Person "Test/hoge" 12 (Just 4)) [PersonHoge !=. Just 3] True
  84. it "FilterTestLt" $ do
  85. check (Person "Test/hoge" 12 (Just 4)) [PersonHoge <=. Just 3] False
  86. check (Person "Test/hoge" 12 (Just 2)) [PersonHoge <=. Just 3] True
  87. it "StoreTest" $ do
  88. va <- flip runZookeeperPool conn $ do
  89. deleteWhere [PersonName !=. ""]
  90. _ <- insert (Person "hoge0" 1 Nothing)
  91. _ <- insert (Person "hoge1" 2 Nothing)
  92. _ <- insert (Person "hoge2" 3 Nothing)
  93. _ <- insert (Person "hoge3" 4 Nothing)
  94. selectList [PersonAge ==. 2] []
  95. (entityVal (head va)) `shouldBe` (Person "hoge1" 2 Nothing)
  96. [Entity _k v] <- flip runZookeeperPool conn $ do
  97. selectList [PersonName ==. "hoge2"] []
  98. v `shouldBe` (Person "hoge2" 3 Nothing)
  99. [Entity _k v1] <- flip runZookeeperPool conn $ do
  100. updateWhere [PersonName ==. "hoge2"] [PersonAge =. 10]
  101. selectList [PersonName ==. "hoge2"] []
  102. v1 `shouldBe` (Person "hoge2" 10 Nothing)
  103. v2 <- flip runZookeeperPool conn $ do
  104. selectList [PersonName !=. ""] []
  105. length v2 `shouldBe` 4
  106. v3 <- flip runZookeeperPool conn $ do
  107. deleteWhere [PersonName !=. ""]
  108. selectList [PersonName !=. ""] []
  109. length v3 `shouldBe` 0