packages feed

keiro-dsl-0.12.0.0: test/conformance-projection-catalog/Generated/CatalogDemo/OrderTotalsLookup/ReadModelHarness.hs

{-# LANGUAGE OverloadedRecordDot #-}
-- @generated by keiro-dsl 0.12.0.0 (language keiro-dsl 5) from readmodel order_totals_lookup; do not edit.
module Generated.CatalogDemo.OrderTotalsLookup.ReadModelHarness (readModelFacts, readModelFactResults, runReadModelFacts, catalogFactsAgainst) where

import Generated.CatalogDemo.OrderTotalsLookup.ReadModel (orderTotalsLookupReadModel)
import Data.Text qualified as T
import Data.List.NonEmpty qualified as NE
import Generated.CatalogDemo.ProjectionCatalog qualified as ProjectionCatalog
import Keiro.Projection.Catalog qualified as Catalog
import Keiro.ReadModel (ReadModel (..), readModelCursorAuthority, readModelDefaultFreshness)

-- | (fact, expected from notation, actual generated runtime value).
readModelFacts :: [(String, String, String)]
readModelFacts =
  [ ("registryName", "catalog-demo-order-totals-lookup", T.unpack orderTotalsLookupReadModel.name)
  , ("shapeHash", "fnv1a:768a23d719dcb4d4", T.unpack orderTotalsLookupReadModel.shapeHash)
  , ("freshness", "Immediate", show (readModelDefaultFreshness orderTotalsLookupReadModel))
  , ("cursorAuthority", "NoQueryCursor", show (readModelCursorAuthority orderTotalsLookupReadModel))
  ]
    <> catalogFactsAgainst ProjectionCatalog.projectionCatalogRegistrations ProjectionCatalog.projectionCatalogAsyncRegistrations ProjectionCatalog.projectionCatalogQuerySupplies

catalogFactsAgainst :: [Catalog.CatalogRegistration] -> [Catalog.AsyncProjectionRegistration] -> [Catalog.ResolvedQuerySupply] -> [(String, String, String)]
catalogFactsAgainst registrations _asyncRegistrations supplies =
  [ ("catalogRegistration", "catalog-demo-order-totals-lookup|1|fnv1a:768a23d719dcb4d4|reporting", renderRegistration [entry | entry <- registrations, Catalog.queryModelIdText entry.queryModelId == "order_totals_lookup"])
  , ("querySupply", "order_summary_writer|reporting|order_totals", renderSupply [entry | entry <- supplies, Catalog.queryModelIdText entry.resolvedQueryModelId == "order_totals_lookup"])
  , ("projectionDelivery", "inline", renderDelivery [entry | entry <- supplies, Catalog.queryModelIdText entry.resolvedQueryModelId == "order_totals_lookup"])
  ]

renderRegistration :: [Catalog.CatalogRegistration] -> String
renderRegistration [entry] = T.unpack entry.registryName <> "|" <> show entry.version <> "|" <> T.unpack entry.shapeHash <> "|" <> T.unpack (Catalog.rebuildGroupIdText entry.rebuildGroupId)
renderRegistration _ = "missing"

renderSupply :: [Catalog.ResolvedQuerySupply] -> String
renderSupply [entry] = T.unpack (Catalog.projectionIdText entry.resolvedProjectionId) <> "|" <> T.unpack (Catalog.rebuildGroupIdText entry.resolvedRebuildGroupId) <> "|" <> T.unpack (T.intercalate "," (map Catalog.targetIdText (NE.toList entry.resolvedObservedTargets)))
renderSupply _ = "missing"

renderDelivery :: [Catalog.ResolvedQuerySupply] -> String
renderDelivery [entry] = T.unpack (T.intercalate "," (map renderCapability (NE.toList entry.resolvedHandlerCapabilities)))
renderDelivery _ = "missing"

renderCapability :: Catalog.ProjectionHandlerCapability -> T.Text
renderCapability Catalog.InlineCapability {} = "inline"
renderCapability Catalog.SubscriptionCapability {} = "subscription"

readModelFactResults :: [(String, Bool)]
readModelFactResults =
  [(fact, expected == actual) | (fact, expected, actual) <- readModelFacts]

runReadModelFacts :: IO Bool
runReadModelFacts = do
  let failures = [(fact, expected, actual) | (fact, expected, actual) <- readModelFacts, expected /= actual]
  mapM_ (\(fact, expected, actual) -> putStrLn ("FAIL  " <> fact <> " expected=" <> show expected <> " actual=" <> show actual)) failures
  pure (null failures)