{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_HADDOCK prune not-home #-}
module Test.KeyedVals.CheckTypedHandle (
spec,
setupFixture,
closeFixture,
) where
import qualified Data.Map.Strict as Map
import KeyedVals.Handle.Typed
import Test.KeyedVals.Prelude
spec :: SpecWith (Handle IO)
spec :: SpecWith (Handle IO)
spec = do
SpecWith (Handle IO)
checkFixedPathed
SpecWith (Handle IO)
checkVarPathed
checkFixedPathed :: SpecWith (Handle IO)
checkFixedPathed :: SpecWith (Handle IO)
checkFixedPathed = do
String -> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
context String
"with typed key-values stored in fixed paths" (SpecWith (Handle IO) -> SpecWith (Handle IO))
-> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a b. (a -> b) -> a -> b
$ do
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should load correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
Handle IO
-> TypedPath FixedDemo
-> IO (Either HandleErr (Map (KeyType FixedDemo) FixedDemo))
forall (m :: * -> *) a.
(Monad m, DecodeKV a, DecodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> m (Either HandleErr (TypedKVs a))
loadKVs Handle IO
h TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed IO (Either HandleErr (Map (KeyType FixedDemo) FixedDemo))
-> Map (KeyType FixedDemo) FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` Map (KeyType FixedDemo) FixedDemo
fixedKVs
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength @FixedDemo TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed Natural
2
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should update an indexed value correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let k :: TypedKey FixedDemo
k = KeyType FixedDemo -> TypedKey FixedDemo
forall v. PathOf v => KeyType v -> TypedKey v
key KeyType FixedDemo
FixedDemoKey
fixedK1
want :: FixedDemo
want = (Int, Text) -> FixedDemo
FixedDemo (Int
1, Text
"changed")
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedKey FixedDemo -> FixedDemo -> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, PathOf a) =>
Handle m -> TypedKey a -> a -> m (Either HandleErr ())
saveTo Handle IO
h TypedKey FixedDemo
k FixedDemo
want
Handle IO -> TypedKey FixedDemo -> IO (Either HandleErr FixedDemo)
forall a (m :: * -> *).
(Monad m, DecodeKV a) =>
Handle m -> TypedKey a -> m (Either HandleErr a)
loadFrom Handle IO
h TypedKey FixedDemo
k IO (Either HandleErr FixedDemo) -> FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` FixedDemo
want
Handle IO
-> TypedKey FixedDemo -> IO (Either HandleErr (Maybe FixedDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey FixedDemo
k IO (Either HandleErr (Maybe FixedDemo)) -> Maybe FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` FixedDemo -> Maybe FixedDemo
forall a. a -> Maybe a
Just FixedDemo
want
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength @FixedDemo TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed Natural
2
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should add an indexed value correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let added :: TypedKey FixedDemo
added = KeyType FixedDemo -> TypedKey FixedDemo
forall v. PathOf v => KeyType v -> TypedKey v
key KeyType FixedDemo
FixedDemoKey
fixedK3
want :: FixedDemo
want = (Int, Text) -> FixedDemo
FixedDemo (Int
3, Text
"added")
Handle IO
-> TypedKey FixedDemo -> IO (Either HandleErr (Maybe FixedDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey FixedDemo
added IO (Either HandleErr (Maybe FixedDemo)) -> Maybe FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` Maybe FixedDemo
forall a. Maybe a
Nothing
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedKey FixedDemo -> FixedDemo -> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, PathOf a) =>
Handle m -> TypedKey a -> a -> m (Either HandleErr ())
saveTo Handle IO
h TypedKey FixedDemo
added FixedDemo
want
Handle IO
-> TypedKey FixedDemo -> IO (Either HandleErr (Maybe FixedDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey FixedDemo
added IO (Either HandleErr (Maybe FixedDemo)) -> Maybe FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` FixedDemo -> Maybe FixedDemo
forall a. a -> Maybe a
Just FixedDemo
want
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength @FixedDemo TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed Natural
3
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should update the key-values correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedPath FixedDemo
-> Map (KeyType FixedDemo) FixedDemo
-> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, EncodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> TypedKVs a -> m (Either HandleErr ())
updateKVs Handle IO
h TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed Map (KeyType FixedDemo) FixedDemo
moreFixedKVs
Handle IO
-> TypedKey FixedDemo -> IO (Either HandleErr (Maybe FixedDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h (KeyType FixedDemo -> TypedKey FixedDemo
forall v. PathOf v => KeyType v -> TypedKey v
key KeyType FixedDemo
FixedDemoKey
fixedK1) IO (Either HandleErr (Maybe FixedDemo)) -> Maybe FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` FixedDemo -> Maybe FixedDemo
forall a. a -> Maybe a
Just FixedDemo
fixedV3
Handle IO
-> TypedKey FixedDemo -> IO (Either HandleErr (Maybe FixedDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h (KeyType FixedDemo -> TypedKey FixedDemo
forall v. PathOf v => KeyType v -> TypedKey v
key KeyType FixedDemo
FixedDemoKey
fixedK4) IO (Either HandleErr (Maybe FixedDemo)) -> Maybe FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` FixedDemo -> Maybe FixedDemo
forall a. a -> Maybe a
Just FixedDemo
fixedV4
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should fetch a subset of the key-values as a dict correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let selection :: NonEmpty FixedDemoKey
selection = FixedDemoKey
fixedK1 FixedDemoKey -> [FixedDemoKey] -> NonEmpty FixedDemoKey
forall a. a -> [a] -> NonEmpty a
:| [FixedDemoKey
fixedK4]
Handle IO
-> TypedPath FixedDemo
-> NonEmpty (KeyType FixedDemo)
-> IO (Either HandleErr (Map (KeyType FixedDemo) FixedDemo))
forall (m :: * -> *) a.
(Monad m, DecodeKV a, PathOf a, DecodeKV (KeyType a),
Ord (KeyType a)) =>
Handle m
-> TypedPath a
-> NonEmpty (KeyType a)
-> m (Either HandleErr (TypedKVs a))
loadSlice Handle IO
h TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed NonEmpty (KeyType FixedDemo)
NonEmpty FixedDemoKey
selection IO (Either HandleErr (Map (KeyType FixedDemo) FixedDemo))
-> Map (KeyType FixedDemo) FixedDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` Map (KeyType FixedDemo) FixedDemo
moreFixedKVs
checkVarPathed :: SpecWith (Handle IO)
checkVarPathed :: SpecWith (Handle IO)
checkVarPathed = do
String -> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
context String
"with typed key-values stored in variable paths" (SpecWith (Handle IO) -> SpecWith (Handle IO))
-> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a b. (a -> b) -> a -> b
$ do
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should load correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
Handle IO
-> TypedPath VarDemo -> IO (Either HandleErr (TypedKVs VarDemo))
forall (m :: * -> *) a.
(Monad m, DecodeKV a, DecodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> m (Either HandleErr (TypedKVs a))
loadKVs Handle IO
h TypedPath VarDemo
path1 IO (Either HandleErr (Map VarDemoKey VarDemo))
-> Map VarDemoKey VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` TypedKVs VarDemo
Map VarDemoKey VarDemo
varKVs
Handle IO
-> TypedPath VarDemo -> IO (Either HandleErr (TypedKVs VarDemo))
forall (m :: * -> *) a.
(Monad m, DecodeKV a, DecodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> m (Either HandleErr (TypedKVs a))
loadKVs Handle IO
h TypedPath VarDemo
path2 IO (Either HandleErr (Map VarDemoKey VarDemo))
-> Map VarDemoKey VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` TypedKVs VarDemo
Map VarDemoKey VarDemo
varKVs
TypedPath VarDemo -> Natural -> SpecWith (Handle IO)
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength TypedPath VarDemo
path1 Natural
2
TypedPath VarDemo -> Natural -> SpecWith (Handle IO)
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength TypedPath VarDemo
path2 Natural
2
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should update an indexed value correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let k :: TypedKey VarDemo
k = PathVar VarDemo
VarDemoID
id1 PathVar VarDemo -> KeyType VarDemo -> TypedKey VarDemo
forall v. VaryingPathOf v => PathVar v -> KeyType v -> TypedKey v
// KeyType VarDemo
VarDemoKey
varK1
want :: VarDemo
want = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Bool -> Either Text Bool
forall a b. b -> Either a b
Right Bool
False
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedKey VarDemo -> VarDemo -> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, PathOf a) =>
Handle m -> TypedKey a -> a -> m (Either HandleErr ())
saveTo Handle IO
h TypedKey VarDemo
k VarDemo
want
Handle IO -> TypedKey VarDemo -> IO (Either HandleErr VarDemo)
forall a (m :: * -> *).
(Monad m, DecodeKV a) =>
Handle m -> TypedKey a -> m (Either HandleErr a)
loadFrom Handle IO
h TypedKey VarDemo
k IO (Either HandleErr VarDemo) -> VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` VarDemo
want
Handle IO
-> TypedKey VarDemo -> IO (Either HandleErr (Maybe VarDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey VarDemo
k IO (Either HandleErr (Maybe VarDemo)) -> Maybe VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` VarDemo -> Maybe VarDemo
forall a. a -> Maybe a
Just VarDemo
want
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should add an indexed value correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let added :: TypedKey VarDemo
added = PathVar VarDemo
VarDemoID
id2 PathVar VarDemo -> KeyType VarDemo -> TypedKey VarDemo
forall v. VaryingPathOf v => PathVar v -> KeyType v -> TypedKey v
// KeyType VarDemo
VarDemoKey
varK3
want :: VarDemo
want = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Text -> Either Text Bool
forall a b. a -> Either a b
Left Text
"added"
Handle IO
-> TypedKey VarDemo -> IO (Either HandleErr (Maybe VarDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey VarDemo
added IO (Either HandleErr (Maybe VarDemo)) -> Maybe VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` Maybe VarDemo
forall a. Maybe a
Nothing
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedKey VarDemo -> VarDemo -> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, PathOf a) =>
Handle m -> TypedKey a -> a -> m (Either HandleErr ())
saveTo Handle IO
h TypedKey VarDemo
added VarDemo
want
Handle IO
-> TypedKey VarDemo -> IO (Either HandleErr (Maybe VarDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h TypedKey VarDemo
added IO (Either HandleErr (Maybe VarDemo)) -> Maybe VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` VarDemo -> Maybe VarDemo
forall a. a -> Maybe a
Just VarDemo
want
TypedPath VarDemo -> Natural -> SpecWith (Handle IO)
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength TypedPath VarDemo
path1 Natural
2
TypedPath VarDemo -> Natural -> SpecWith (Handle IO)
forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength TypedPath VarDemo
path2 Natural
3
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should update the key-values correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
IO (Either HandleErr ()) -> IO ()
forall a b. (Show a, Show b) => IO (Either a b) -> IO ()
endsRight_ (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedPath VarDemo
-> TypedKVs VarDemo
-> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, EncodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> TypedKVs a -> m (Either HandleErr ())
updateKVs Handle IO
h TypedPath VarDemo
path1 TypedKVs VarDemo
moreVarKVs
Handle IO
-> TypedKey VarDemo -> IO (Either HandleErr (Maybe VarDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h (PathVar VarDemo
VarDemoID
id1 PathVar VarDemo -> KeyType VarDemo -> TypedKey VarDemo
forall v. VaryingPathOf v => PathVar v -> KeyType v -> TypedKey v
// KeyType VarDemo
VarDemoKey
varK1) IO (Either HandleErr (Maybe VarDemo)) -> Maybe VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` VarDemo -> Maybe VarDemo
forall a. a -> Maybe a
Just VarDemo
varV3
Handle IO
-> TypedKey VarDemo -> IO (Either HandleErr (Maybe VarDemo))
forall a (m :: * -> *).
(Monad m, DecodeKV a, PathOf a) =>
Handle m -> TypedKey a -> m (Either HandleErr (Maybe a))
mayLoadFrom Handle IO
h (PathVar VarDemo
VarDemoID
id1 PathVar VarDemo -> KeyType VarDemo -> TypedKey VarDemo
forall v. VaryingPathOf v => PathVar v -> KeyType v -> TypedKey v
// KeyType VarDemo
VarDemoKey
varK4) IO (Either HandleErr (Maybe VarDemo)) -> Maybe VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` VarDemo -> Maybe VarDemo
forall a. a -> Maybe a
Just VarDemo
varV4
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should fetch a subset of the key-values correctly" ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
let selection :: NonEmpty VarDemoKey
selection = VarDemoKey
varK1 VarDemoKey -> [VarDemoKey] -> NonEmpty VarDemoKey
forall a. a -> [a] -> NonEmpty a
:| [VarDemoKey
varK4]
Handle IO
-> TypedPath VarDemo
-> NonEmpty (KeyType VarDemo)
-> IO (Either HandleErr (TypedKVs VarDemo))
forall (m :: * -> *) a.
(Monad m, DecodeKV a, PathOf a, DecodeKV (KeyType a),
Ord (KeyType a)) =>
Handle m
-> TypedPath a
-> NonEmpty (KeyType a)
-> m (Either HandleErr (TypedKVs a))
loadSlice Handle IO
h TypedPath VarDemo
path1 NonEmpty (KeyType VarDemo)
NonEmpty VarDemoKey
selection IO (Either HandleErr (Map VarDemoKey VarDemo))
-> Map VarDemoKey VarDemo -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` TypedKVs VarDemo
Map VarDemoKey VarDemo
moreVarKVs
setupFixture :: Handle IO -> IO (Handle IO)
setupFixture :: Handle IO -> IO (Handle IO)
setupFixture Handle IO
h = do
IO (Either HandleErr ()) -> IO ()
orThrowHandleErr (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedPath FixedDemo
-> Map (KeyType FixedDemo) FixedDemo
-> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, EncodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> TypedKVs a -> m (Either HandleErr ())
saveKVs Handle IO
h TypedPath FixedDemo
forall v. PathOf v => TypedPath v
Fixed Map (KeyType FixedDemo) FixedDemo
fixedKVs
IO (Either HandleErr ()) -> IO ()
orThrowHandleErr (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedPath VarDemo
-> TypedKVs VarDemo
-> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, EncodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> TypedKVs a -> m (Either HandleErr ())
saveKVs Handle IO
h TypedPath VarDemo
path1 TypedKVs VarDemo
varKVs
IO (Either HandleErr ()) -> IO ()
orThrowHandleErr (IO (Either HandleErr ()) -> IO ())
-> IO (Either HandleErr ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle IO
-> TypedPath VarDemo
-> TypedKVs VarDemo
-> IO (Either HandleErr ())
forall (m :: * -> *) a.
(Monad m, EncodeKV a, EncodeKV (KeyType a), Ord (KeyType a)) =>
Handle m -> TypedPath a -> TypedKVs a -> m (Either HandleErr ())
saveKVs Handle IO
h TypedPath VarDemo
path2 TypedKVs VarDemo
varKVs
Handle IO -> IO (Handle IO)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Handle IO
h
closeFixture :: Handle IO -> IO ()
closeFixture :: Handle IO -> IO ()
closeFixture = Handle IO -> IO ()
forall (m :: * -> *). Handle m -> m ()
close
fixedK1, fixedK2, fixedK3, fixedK4 :: FixedDemoKey
fixedK1 :: FixedDemoKey
fixedK1 = FixedDemoKey
25
fixedK2 :: FixedDemoKey
fixedK2 = FixedDemoKey
49
fixedK3 :: FixedDemoKey
fixedK3 = FixedDemoKey
81
fixedK4 :: FixedDemoKey
fixedK4 = FixedDemoKey
121
fixedV1, fixedV2, fixedV3, fixedV4 :: FixedDemo
fixedV1 :: FixedDemo
fixedV1 = (Int, Text) -> FixedDemo
FixedDemo (Int
1, Text
"one")
fixedV2 :: FixedDemo
fixedV2 = (Int, Text) -> FixedDemo
FixedDemo (Int
2, Text
"two")
fixedV3 :: FixedDemo
fixedV3 = (Int, Text) -> FixedDemo
FixedDemo (Int
1, Text
"un")
fixedV4 :: FixedDemo
fixedV4 = (Int, Text) -> FixedDemo
FixedDemo (Int
4, Text
"quatre")
fixedKVs, moreFixedKVs :: TypedKVs FixedDemo
fixedKVs :: Map (KeyType FixedDemo) FixedDemo
fixedKVs =
[(FixedDemoKey, FixedDemo)] -> Map FixedDemoKey FixedDemo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (FixedDemoKey
fixedK1, FixedDemo
fixedV1)
, (FixedDemoKey
fixedK2, FixedDemo
fixedV2)
]
moreFixedKVs :: Map (KeyType FixedDemo) FixedDemo
moreFixedKVs =
[(FixedDemoKey, FixedDemo)] -> Map FixedDemoKey FixedDemo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (FixedDemoKey
fixedK1, FixedDemo
fixedV3)
, (FixedDemoKey
fixedK4, FixedDemo
fixedV4)
]
varK1, varK2, varK3, varK4 :: VarDemoKey
varK1 :: VarDemoKey
varK1 = VarDemoKey
36
varK2 :: VarDemoKey
varK2 = VarDemoKey
64
varK3 :: VarDemoKey
varK3 = VarDemoKey
100
varK4 :: VarDemoKey
varK4 = VarDemoKey
144
path1, path2 :: TypedPath VarDemo
path1 :: TypedPath VarDemo
path1 = PathVar VarDemo -> TypedPath VarDemo
forall v. VaryingPathOf v => PathVar v -> TypedPath v
Variable PathVar VarDemo
VarDemoID
id1
path2 :: TypedPath VarDemo
path2 = PathVar VarDemo -> TypedPath VarDemo
forall v. VaryingPathOf v => PathVar v -> TypedPath v
Variable PathVar VarDemo
VarDemoID
id2
id1, id2 :: VarDemoID
id1 :: VarDemoID
id1 = VarDemoID
"id1"
id2 :: VarDemoID
id2 = VarDemoID
"id2"
varV1, varV2, varV3, varV4 :: VarDemo
varV1 :: VarDemo
varV1 = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Text -> Either Text Bool
forall a b. a -> Either a b
Left Text
"one"
varV2 :: VarDemo
varV2 = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Bool -> Either Text Bool
forall a b. b -> Either a b
Right Bool
False
varV3 :: VarDemo
varV3 = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Text -> Either Text Bool
forall a b. a -> Either a b
Left Text
"three"
varV4 :: VarDemo
varV4 = Either Text Bool -> VarDemo
VarDemo (Either Text Bool -> VarDemo) -> Either Text Bool -> VarDemo
forall a b. (a -> b) -> a -> b
$ Text -> Either Text Bool
forall a b. a -> Either a b
Left Text
"four"
varKVs, moreVarKVs :: TypedKVs VarDemo
varKVs :: TypedKVs VarDemo
varKVs =
[(VarDemoKey, VarDemo)] -> Map VarDemoKey VarDemo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (VarDemoKey
varK1, VarDemo
varV1)
, (VarDemoKey
varK2, VarDemo
varV2)
]
moreVarKVs :: TypedKVs VarDemo
moreVarKVs =
[(VarDemoKey, VarDemo)] -> Map VarDemoKey VarDemo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (VarDemoKey
varK1, VarDemo
varV3)
, (VarDemoKey
varK4, VarDemo
varV4)
]
checkLength ::
(Ord (KeyType v)) =>
TypedPath v ->
Natural ->
SpecWith (Handle IO)
checkLength :: forall v.
Ord (KeyType v) =>
TypedPath v -> Natural -> SpecWith (Handle IO)
checkLength TypedPath v
path Natural
n = String -> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
context String
"and the reported size" (SpecWith (Handle IO) -> SpecWith (Handle IO))
-> SpecWith (Handle IO) -> SpecWith (Handle IO)
forall a b. (a -> b) -> a -> b
$ do
String
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"should be correct " ((Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ())))
-> (Handle IO -> IO ()) -> SpecWith (Arg (Handle IO -> IO ()))
forall a b. (a -> b) -> a -> b
$ \Handle IO
h -> do
Handle IO -> TypedPath v -> IO (Either HandleErr Natural)
forall a (m :: * -> *).
(Monad m, Ord (KeyType a)) =>
Handle m -> TypedPath a -> m (Either HandleErr Natural)
countKVs Handle IO
h TypedPath v
path IO (Either HandleErr Natural) -> Natural -> IO ()
forall a b.
(HasCallStack, Show a, Eq a, Show b, Eq b) =>
IO (Either a b) -> b -> IO ()
`endsRight` Natural
n