-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathKVDBSpec.hs
144 lines (128 loc) · 5.71 KB
/
KVDBSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module KVDB.KVDBSpec where
import Test.Hspec hiding (runIO)
import EulerHS.Interpreters
import qualified EulerHS.Language as L
import EulerHS.Prelude
import EulerHS.Runtime
import qualified EulerHS.Types as T
redisName = "eulerKVDB"
redisCfg = T.mkKVDBConfig redisName T.defaultKVDBConnConfig
spec :: Spec
spec =
around (withFlowRuntime Nothing) $
describe "EulerHS KVDB tests" $ do
it "Double connection initialization should fail" $ \rt -> do
eRes <- runFlow rt $ do
eConn1 <- L.initKVDBConnection redisCfg
eConn2 <- L.initKVDBConnection redisCfg
case (eConn1, eConn2) of
(Left err, _) -> pure $ Left $ "Failed to connect 1st time: " <> show err
(_, Left (T.KVDBError T.KVDBConnectionAlreadyExists msg)) -> pure $ Right ()
(_, Left err) -> pure $ Left $ "Unexpected error type on 2nd connect: " <> show err
eRes `shouldBe` Right ()
it "Get uninialized connection should fail" $ \rt -> do
eRes <- runFlow rt $ do
eConn <- L.getKVDBConnection redisCfg
case eConn of
Left (T.KVDBError T.KVDBConnectionDoesNotExist msg) -> pure $ Right ()
Left err -> pure $ Left $ "Unexpected error: " <> show err
Right _ -> pure $ Left "Unexpected connection success"
eRes `shouldBe` Right ()
it "Init and get connection should succeed" $ \rt -> do
eRes <- runFlow rt $ do
eConn1 <- L.initKVDBConnection redisCfg
eConn2 <- L.getKVDBConnection redisCfg
case (eConn1, eConn2) of
(Left err, _) -> pure $ Left $ "Failed to connect: " <> show err
(_, Left err) -> pure $ Left $ "Unexpected error on get connection: " <> show err
_ -> pure $ Right ()
eRes `shouldBe` Right ()
it "Init and double get connection should succeed" $ \rt -> do
eRes <- runFlow rt $ do
eConn1 <- L.initKVDBConnection redisCfg
eConn2 <- L.getKVDBConnection redisCfg
eConn3 <- L.getKVDBConnection redisCfg
case (eConn1, eConn2, eConn3) of
(Left err, _, _) -> pure $ Left $ "Failed to connect: " <> show err
(_, Left err, _) -> pure $ Left $ "Unexpected error on 1st get connection: " <> show err
(_, _, Left err) -> pure $ Left $ "Unexpected error on 2nd get connection: " <> show err
_ -> pure $ Right ()
eRes `shouldBe` Right ()
it "getOrInitKVDBConn should succeed" $ \rt -> do
eRes <- runFlow rt $ do
eConn <- L.getOrInitKVDBConn redisCfg
case eConn of
Left err -> pure $ Left $ "Failed to connect: " <> show err
_ -> pure $ Right ()
eRes `shouldBe` Right ()
it "Prepared connection should be available" $ \rt -> do
void $ runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
when (isLeft eConn) $ error "Failed to prepare connection."
void $ runFlow rt $ do
eConn <- L.getKVDBConnection redisCfg
when (isLeft eConn) $ error "Failed to get prepared connection."
it "Redis binary strings 1" $ \rt -> do
let key = "a\xfcज" :: ByteString
let value = "bbbex\xfc\xffकखगघङचछज" :: ByteString
result <- runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
case eConn of
Left err ->
error $ "Failed to get prepared connection: " <> show err
Right conn -> do
let hour = 60 * 60
L.runKVDB redisName $ do
L.setex key hour value
res <- L.get key
L.del [key]
pure res
result `shouldBe` Right (Just value)
it "Redis binary strings 2" $ \rt -> do
let key = "a\xfcज" :: ByteString
let value = "bbbex\xfc\xffकखगघङचछज" :: ByteString
result <- runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
case eConn of
Left err ->
error $ "Failed to get prepared connection: " <> show err
Right conn -> do
L.rSetB redisName key value
L.rGetB redisName key
result `shouldBe` Just value
it "Redis unicode" $ \rt -> do
let key = "a\xfcज" :: Text
let value = "bbbex\xfc\xffकखगघङचछज" :: Text
result <- runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
case eConn of
Left err ->
error $ "Failed to get prepared connection: " <> show err
Right conn -> do
L.rSetT redisName key value
L.rGetT redisName key
result `shouldBe` Just value
it "Redis unicode + json" $ \rt -> do
let key = "a\xfcज" :: Text
let value = "bbbex\xfc\xffकखगघङचछज" :: Text
result <- runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
case eConn of
Left err ->
error $ "Failed to get prepared connection: " <> show err
Right conn -> do
L.rSet redisName key value
L.rGet redisName key
result `shouldBe` Just value
it "Redis set functions" $ \rt -> do
let key = "abc" :: ByteString
let value = ["hello", "world"] :: [ByteString]
result <- runFlow rt $ do
eConn <- L.initKVDBConnection redisCfg
case eConn of
Left err ->
error $ "Failed to get prepared connection: " <> show err
Right conn -> do
void $ L.rSadd redisName key value
L.rSismember redisName key (head value)
result `shouldBe` (Right True)