Skip to content

Commit 6415b80

Browse files
committed
Add update feature flags tests
1 parent fb0126f commit 6415b80

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

feature-flipper-postgres.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ test-suite feature-flipper-postgres-test
6161
build-depends:
6262
base
6363
, bytestring
64+
, containers
6465
, hspec
6566
, monad-logger
6667
, mtl

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ tests:
5252
dependencies:
5353
- base
5454
- bytestring
55+
- containers
5556
- hspec
5657
- monad-logger
5758
- mtl

test/Control/Flipper/PostgresSpec.hs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Control.Flipper.PostgresSpec (main, spec) where
55
import Control.Monad (void)
66
import Control.Monad.Reader
77
import Control.Monad.State
8+
import Data.Map.Strict as Map
89
import Test.Hspec
910

1011
import Control.Flipper.Postgres as FP
@@ -23,6 +24,7 @@ newtype MyContext m a = MyContext { unContext :: StateT MyState (FlipperT m) a }
2324
, MonadState MyState
2425
, MonadReader Config
2526
, HasFeatureFlags
27+
, ModifiesFeatureFlags
2628
)
2729

2830
newtype MyState = MyState Int
@@ -36,28 +38,57 @@ runMyContext pool initialState f =
3638

3739
spec :: Spec
3840
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))
4346

44-
st `shouldBe` MyState 0
47+
st `shouldBe` MyState 0
4548

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
5053

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))
5356

54-
st `shouldBe` MyState 1
57+
st `shouldBe` MyState 1
5558

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
5993

60-
(_, st) <- runMyContext pool (MyState 0) $ do
61-
whenEnabled "some-feature" (void $ put (MyState 1))
6294

63-
st `shouldBe` MyState 0

0 commit comments

Comments
 (0)