graphql-engine/server/src-test/Hasura/SQL/BackendMapSpec.hs
Jesse Hallett bd9f93eaef server: codecs for backend configs
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8269
GitOrigin-RevId: 34330f383ca82fb159842a171a763c178b462788
2023-03-30 15:53:55 +00:00

56 lines
2.0 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Hasura.SQL.BackendMapSpec (spec) where
import Autodocodec (parseJSONViaCodec, toJSONViaCodec)
import Data.Aeson.QQ (aesonQQ)
import Data.Aeson.Types (parseEither)
import Data.Either.Combinators (fromRight')
import Data.Map.Strict qualified as Map
import Data.Maybe (fromJust)
import Hasura.Backends.DataConnector.Adapter.Types (DataConnectorOptions (..), mkDataConnectorName)
import Hasura.Prelude
import Hasura.RQL.Types.Metadata.Common (BackendConfigWrapper (BackendConfigWrapper))
import Hasura.SQL.Backend (BackendType (..))
import Hasura.SQL.BackendMap (BackendMap)
import Hasura.SQL.BackendMap qualified as BM
import Language.GraphQL.Draft.Syntax qualified as GQL
import Servant.Client qualified as S
import Test.Hspec
import Test.Hspec.Expectations.Json (shouldBeJson)
spec :: Spec
spec = describe "BackendMap" do
it "serializes via Autodocodec" do
let mysqlConfig = BM.singleton @'MySQL @BackendConfigWrapper (BackendConfigWrapper ())
let dataconnectorConfig =
BM.singleton @'DataConnector @BackendConfigWrapper
( BackendConfigWrapper $
Map.singleton
(fromRight' $ mkDataConnectorName $ fromJust $ GQL.mkName "MyConnector")
( DataConnectorOptions
{ _dcoUri = fromRight' $ S.parseBaseUrl "https://somehost.org/",
_dcoDisplayName = Just "My Connector"
}
)
)
let configs = mysqlConfig <> dataconnectorConfig
let expected =
[aesonQQ|
{
"mysql": [],
"dataconnector": {
"MyConnector": {
"uri": "https://somehost.org",
"display_name": "My Connector"
}
}
}
|]
let json = toJSONViaCodec configs
json `shouldBeJson` expected
let decoded = parseEither (parseJSONViaCodec @(BackendMap BackendConfigWrapper)) json
decoded `shouldBe` Right configs