graphql-engine/server/src-test/Data/TrieSpec.hs
Antoine Leblanc 0e3beb028d Extract generic containers from the codebase
### Description

There were several places in the codebase where we would either implement a generic container, or express the need for one. This PR extracts / creates all relevant containers, and adapts the relevant parts of the code to make use of said new generic containers. More specifically, it introduces the following modules:
- `Data.Set.Extended`, for new functions on `Data.Set`
- `Data.HashMap.Strict.Multi`, for hash maps that accept multiple values
- `Data.HashMap.Strict.NonEmpty`, for hash maps that can never be constructed as empty
- `Data.Trie`, for a generic implementation of a prefix tree

This PR makes use of those new containers in the following parts of the code:
- `Hasura.GraphQL.Execute.RemoteJoin.Types`
- `Hasura.RQL.Types.Endpoint*`

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3828
GitOrigin-RevId: e6c1b971bcb3f5ab66bc91d0fa4d0e9df7a0c6c6
2022-03-01 16:04:22 +00:00

28 lines
954 B
Haskell

module Data.TrieSpec (spec) where
import Data.HashMap.Strict qualified as M
import Data.Trie qualified as T
import Hasura.Prelude
import Hasura.QuickCheck.Instances ()
import Test.Hspec
import Test.Hspec.QuickCheck
type TestTrie = T.Trie String [Int]
spec :: Spec
spec = describe "Trie" $ do
describe "insert" $ do
prop "insert a path into an empty trie" $ \p v -> do
let expected = T.singleton p v :: TestTrie
T.insert p v T.empty `shouldBe` expected
prop "insert a singleton path into an empty trie" $ \pc v -> do
let expected = T.Trie (M.singleton pc (T.Trie mempty (Just v))) Nothing :: TestTrie
T.insert [pc] v T.empty `shouldBe` expected
prop "insertion is idempotent" $ \p v (t :: TestTrie) -> do
T.insert p v (T.insert p v t) `shouldBe` T.insert p v t
prop "insertion with mappend monoid equivalence" $ \p v (t :: TestTrie) -> do
T.insertWith (<>) p v t `shouldBe` T.singleton p v <> t