@@ -5,6 +5,7 @@ module Control.Flipper.PostgresSpec (main, spec) where
5
5
import Control.Monad (void )
6
6
import Control.Monad.Reader
7
7
import Control.Monad.State
8
+ import Data.Map.Strict as Map
8
9
import Test.Hspec
9
10
10
11
import Control.Flipper.Postgres as FP
@@ -23,6 +24,7 @@ newtype MyContext m a = MyContext { unContext :: StateT MyState (FlipperT m) a }
23
24
, MonadState MyState
24
25
, MonadReader Config
25
26
, HasFeatureFlags
27
+ , ModifiesFeatureFlags
26
28
)
27
29
28
30
newtype MyState = MyState Int
@@ -36,28 +38,57 @@ runMyContext pool initialState f =
36
38
37
39
spec :: Spec
38
40
spec = around Cfg. withConfig $ do
39
- describe " a non-existant feature" $ do
40
- it " is disabled by default" $ \ (Config pool _) -> do
41
- (_, st) <- runMyContext pool (MyState 0 ) $ do
42
- whenEnabled " non-existant feature" (void $ put (MyState 1 ))
41
+ describe " control flow with feature flags" $ do
42
+ describe " a non-existant feature" $ do
43
+ it " is disabled by default" $ \ (Config pool _) -> do
44
+ (_, st) <- runMyContext pool (MyState 0 ) $ do
45
+ whenEnabled " non-existant feature" (void $ put (MyState 1 ))
43
46
44
- st `shouldBe` MyState 0
47
+ st `shouldBe` MyState 0
45
48
46
- describe " a persisted feature" $ do
47
- it " runs features when it is enabled" $ \ (Config pool dbAccess) -> do
48
- f <- Q. mkFeature (FP. FeatureName " some -feature" ) True
49
- void $ Q. addFeature f dbAccess
49
+ describe " a persisted feature" $ do
50
+ it " runs features when it is enabled" $ \ (Config pool dbAccess) -> do
51
+ f <- Q. mkFeature (FP. FeatureName " enabled -feature" ) True
52
+ void $ Q. addFeature f dbAccess
50
53
51
- (_, st) <- runMyContext pool (MyState 0 ) $ do
52
- whenEnabled " some -feature" (void $ put (MyState 1 ))
54
+ (_, st) <- runMyContext pool (MyState 0 ) $ do
55
+ whenEnabled " enabled -feature" (void $ put (MyState 1 ))
53
56
54
- st `shouldBe` MyState 1
57
+ st `shouldBe` MyState 1
55
58
56
- it " does not run features it is are disabled" $ \ (Config pool dbAccess) -> do
57
- f <- Q. mkFeature (FP. FeatureName " some-feature" ) False
58
- void $ Q. addFeature f dbAccess
59
+ it " does not run features it is are disabled" $ \ (Config pool dbAccess) -> do
60
+ f <- Q. mkFeature (FP. FeatureName " disabled-feature" ) False
61
+ void $ Q. addFeature f dbAccess
62
+
63
+ (_, st) <- runMyContext pool (MyState 0 ) $ do
64
+ whenEnabled " disabled-feature" (void $ put (MyState 1 ))
65
+
66
+ st `shouldBe` MyState 0
67
+
68
+ describe " modifying feature flags" $ do
69
+ describe " new feature flags" $ do
70
+ it " creates new records" $ \ (Config pool dbAccess) -> do
71
+ featureCount dbAccess `shouldReturn` 0
72
+
73
+ (_, _) <- runMyContext pool (MyState 0 ) $ do
74
+ let fs = Features $ Map. fromList [ (" my-new-feature" , True ), (" some-other-feature" , True ) ]
75
+ updateFeatures fs
76
+
77
+ featureCount dbAccess `shouldReturn` 2
78
+
79
+ it " updates existing records" $ \ (Config pool dbAccess) -> do
80
+ featureCount dbAccess `shouldReturn` 0
81
+
82
+ void $ runMyContext pool (MyState 0 ) $ do
83
+ let fs = Features $ Map. fromList [ (" my-new-feature" , True ), (" some-other-feature" , True ) ]
84
+ updateFeatures fs
85
+ liftIO $ featureCount dbAccess `shouldReturn` 2
86
+
87
+ let fs' = Features $ Map. fromList [ (" my-new-feature" , False ), (" some-other-feature" , False ), (" hi-there" , False ) ]
88
+ updateFeatures fs'
89
+ liftIO $ featureCount dbAccess `shouldReturn` 3
90
+
91
+ fs'' <- FP. getFeatures
92
+ liftIO $ all (== False ) (Map. elems (unFeatures fs'')) `shouldBe` True
59
93
60
- (_, st) <- runMyContext pool (MyState 0 ) $ do
61
- whenEnabled " some-feature" (void $ put (MyState 1 ))
62
94
63
- st `shouldBe` MyState 0
0 commit comments