Skip to content

Commit e833345

Browse files
committed
remove Text from SyncInsertConfig
1 parent dcddc92 commit e833345

File tree

4 files changed

+53
-43
lines changed

4 files changed

+53
-43
lines changed

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Cardano.DbSync.Config.Types (
2222
SyncNodeConfig (..),
2323
SyncPreConfig (..),
2424
SyncInsertConfig (..),
25+
SyncInsertPreset (..),
2526
SyncInsertOptions (..),
2627
TxCBORConfig (..),
2728
PoolStatsConfig (..),
@@ -156,18 +157,17 @@ data SyncPreConfig = SyncPreConfig
156157
deriving (Show)
157158

158159
data SyncInsertConfig = SyncInsertConfig
159-
{ sicPreset :: Maybe Text
160+
{ sicPreset :: Maybe SyncInsertPreset
160161
, sicOptions :: SyncInsertOptions
161162
}
162163
deriving (Eq, Show)
163164

164-
-- data SyncInsertConfig
165-
-- = FullInsertOptions
166-
-- | OnlyUTxOInsertOptions
167-
-- | OnlyGovInsertOptions
168-
-- | DisableAllInsertOptions
169-
-- | SyncInsertConfig SyncInsertOptions
170-
-- deriving (Eq, Show)
165+
data SyncInsertPreset
166+
= FullInsertPreset
167+
| OnlyUTxOInsertPreset
168+
| OnlyGovInsertPreset
169+
| DisableAllInsertPreset
170+
deriving (Eq, Show)
171171

172172
data SyncInsertOptions = SyncInsertOptions
173173
{ sioTxCBOR :: TxCBORConfig
@@ -397,15 +397,28 @@ instance FromJSON SyncProtocol where
397397
String "Cardano" -> pure SyncProtocolCardano
398398
x -> typeMismatch "Protocol" x
399399

400+
instance FromJSON SyncInsertPreset where
401+
parseJSON = Aeson.withText "SyncInsertPreset" $ \case
402+
"full" -> pure FullInsertPreset
403+
"only_utxo" -> pure OnlyUTxOInsertPreset
404+
"only_governance" -> pure OnlyGovInsertPreset
405+
"disable_all" -> pure DisableAllInsertPreset
406+
other -> fail $ "unexpected preset: " <> show other
407+
408+
instance ToJSON SyncInsertPreset where
409+
toJSON FullInsertPreset = "full"
410+
toJSON OnlyUTxOInsertPreset = "only_utxo"
411+
toJSON OnlyGovInsertPreset = "only_governance"
412+
toJSON DisableAllInsertPreset = "disable_all"
413+
400414
instance FromJSON SyncInsertConfig where
401415
parseJSON = Aeson.withObject "SyncInsertConfig" $ \obj -> do
402416
preset <- obj .:? "preset"
403417
baseOptions <- case preset of
404-
Just "full" -> pure fullInsertOptions
405-
Just "only_utxo" -> pure onlyUTxOInsertOptions
406-
Just "only_gov" -> pure onlyGovInsertOptions
407-
Just "disable_all" -> pure disableAllInsertOptions
408-
Just other -> fail $ "unexpected preset: " <> show other
418+
Just FullInsertPreset -> pure fullInsertOptions
419+
Just OnlyUTxOInsertPreset -> pure onlyUTxOInsertOptions
420+
Just OnlyGovInsertPreset -> pure onlyGovInsertOptions
421+
Just DisableAllInsertPreset -> pure disableAllInsertOptions
409422
Nothing -> pure def -- Default options
410423
options <- parseOverrides obj baseOptions
411424
pure $ SyncInsertConfig preset options
@@ -738,6 +751,7 @@ onlyGovInsertOptions =
738751
disableAllInsertOptions
739752
{ sioLedger = LedgerEnable
740753
, sioGovernance = GovernanceConfig True
754+
, sioPoolStats = PoolStatsConfig True
741755
}
742756

743757
disableAllInsertOptions :: SyncInsertOptions

cardano-db-sync/test/Cardano/DbSync/ApiTest.hs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Cardano.DbSync.Api
77
import Cardano.DbSync.Config.Types
88
import qualified Cardano.DbSync.Gen as Gen
99
import Cardano.Prelude
10-
import Control.Monad (MonadFail (..))
1110
import Hedgehog
1211

1312
tests :: IO Bool
@@ -27,17 +26,16 @@ prop_extractInsertOptions = property $ do
2726
coverInsertCfg insertCfg
2827

2928
case insertCfg of
30-
SyncInsertConfig (Just "full") _ ->
29+
SyncInsertConfig (Just FullInsertPreset) _ ->
3130
extractInsertOptions cfg === fullInsertOptions
32-
SyncInsertConfig (Just "only_utxo") _ ->
31+
SyncInsertConfig (Just OnlyUTxOInsertPreset) _ ->
3332
extractInsertOptions cfg === onlyUTxOInsertOptions
34-
SyncInsertConfig (Just "only_gov") _ ->
33+
SyncInsertConfig (Just OnlyGovInsertPreset) _ ->
3534
extractInsertOptions cfg === onlyGovInsertOptions
36-
SyncInsertConfig (Just "disable_all") _ ->
35+
SyncInsertConfig (Just DisableAllInsertPreset) _ ->
3736
extractInsertOptions cfg === disableAllInsertOptions
3837
SyncInsertConfig Nothing opts ->
3938
extractInsertOptions cfg === opts
40-
_other -> fail "Unexpected SyncInsertConfig" -- This case should not happen if all presets are covered
4139

4240
prop_extractInsertOptionsRewards :: Property
4341
prop_extractInsertOptionsRewards = property $ do
@@ -49,24 +47,22 @@ prop_extractInsertOptionsRewards = property $ do
4947
let areRewardsEnabled' = areRewardsEnabled $ sioRewards (extractInsertOptions cfg)
5048

5149
case insertCfg of
52-
SyncInsertConfig (Just "only_gov") _ ->
50+
SyncInsertConfig (Just OnlyGovInsertPreset) _ ->
5351
assert $ not areRewardsEnabled'
54-
SyncInsertConfig (Just "disable_all") _ ->
52+
SyncInsertConfig (Just DisableAllInsertPreset) _ ->
5553
assert $ not areRewardsEnabled'
5654
_other -> assert areRewardsEnabled'
5755

58-
coverInsertCfg :: SyncInsertConfig -> PropertyT IO ()
59-
coverInsertCfg cfg = do
60-
cover 5 "full" $ isPreset "full" cfg
61-
cover 5 "only utxo" $ isPreset "only_utxo" cfg
62-
cover 5 "only gov" $ isPreset "only_gov" cfg
63-
cover 5 "disable all" $ isPreset "disable_all" cfg
64-
cover 5 "custom config" $ isCustomConfig cfg
56+
coverInsertCfg :: MonadTest m => SyncInsertConfig -> m ()
57+
coverInsertCfg insertOpts = do
58+
let preset = sicPreset insertOpts
59+
cover 5 "full" (preset == Just FullInsertPreset)
60+
cover 5 "only utxo" (preset == Just OnlyUTxOInsertPreset)
61+
cover 5 "only gov" (preset == Just OnlyGovInsertPreset)
62+
cover 5 "disable all" (preset == Just DisableAllInsertPreset)
63+
cover 5 "config" isSyncInsertConfig
6564
where
66-
isPreset :: Text -> SyncInsertConfig -> Bool
67-
isPreset preset (SyncInsertConfig (Just p) _) = p == preset
68-
isPreset _ _ = False
69-
70-
isCustomConfig :: SyncInsertConfig -> Bool
71-
isCustomConfig (SyncInsertConfig Nothing _) = True
72-
isCustomConfig _ = False
65+
isSyncInsertConfig =
66+
case insertOpts of
67+
(SyncInsertConfig Nothing _) -> True
68+
_other -> False

cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ prop_syncInsertConfigRoundtrip = property $ do
4747
SyncInsertConfig Nothing _ -> True
4848
_other -> False
4949

50-
cover 5 "full" (isPreset "full")
51-
cover 5 "only utxo" (isPreset "only_utxo")
52-
cover 5 "only gov" (isPreset "only_gov")
53-
cover 5 "disable all" (isPreset "disable_all")
50+
cover 5 "full" (isPreset FullInsertPreset)
51+
cover 5 "only utxo" (isPreset OnlyUTxOInsertPreset)
52+
cover 5 "only gov" (isPreset OnlyGovInsertPreset)
53+
cover 5 "disable all" (isPreset DisableAllInsertPreset)
5454
cover 5 "custom config" isCustomConfig
5555

5656
tripping cfg Aeson.encode Aeson.decode

cardano-db-sync/test/Cardano/DbSync/Gen.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ syncNodeConfig loggingCfg =
107107
syncInsertConfig :: Gen SyncInsertConfig
108108
syncInsertConfig =
109109
Gen.choice
110-
[ pure $ SyncInsertConfig (Just "full") fullInsertOptions
111-
, pure $ SyncInsertConfig (Just "only_utxo") onlyUTxOInsertOptions
112-
, pure $ SyncInsertConfig (Just "only_gov") onlyGovInsertOptions
113-
, pure $ SyncInsertConfig (Just "disable_all") disableAllInsertOptions
110+
[ pure $ SyncInsertConfig (Just FullInsertPreset) fullInsertOptions
111+
, pure $ SyncInsertConfig (Just OnlyUTxOInsertPreset) onlyUTxOInsertOptions
112+
, pure $ SyncInsertConfig (Just OnlyGovInsertPreset) onlyGovInsertOptions
113+
, pure $ SyncInsertConfig (Just DisableAllInsertPreset) disableAllInsertOptions
114114
, SyncInsertConfig Nothing <$> syncInsertOptions
115115
]
116116

0 commit comments

Comments
 (0)