@@ -57,13 +57,14 @@ spanForFailingTask task =
5757 Prelude. fail " Expected task to fail"
5858
5959tests :: TestHandlers -> Test. Test
60- tests TestHandlers {handler, autoExtendExpireHandler} =
60+ tests TestHandlers {handler, autoExtendExpireHandler, noNamespaceHandler } =
6161 Test. describe
6262 " Redis Library"
6363 [ Test. describe " query tests using handler" (queryTests handler),
6464 Test. describe " query tests using auto extend expire handler" (queryTests autoExtendExpireHandler),
6565 Test. describe " observability tests" (observabilityTests handler),
66- Test. describe " ttl tests" (ttlTests handler autoExtendExpireHandler)
66+ Test. describe " ttl tests" (ttlTests handler autoExtendExpireHandler),
67+ Test. describe " no-namespace handler tests" (noNamespaceTests noNamespaceHandler handler)
6768 ]
6869
6970-- We want to test all of our potential makeApi alternatives because it's easy
@@ -486,9 +487,57 @@ ttlTests handler autoExtendExpireHandler =
486487 Expect. equal result Redis. TTLKeyNotFound
487488 ]
488489
490+ -- | Tests that pin down the contract of `handlerWithoutNamespace`: keys are
491+ -- neither prefixed on the way in nor stripped on the way out. The
492+ -- `nsHandler` argument is the regular namespaced `handler` (namespace
493+ -- `"tests"`); we use it to confirm prefixing happens on the namespaced side
494+ -- but not on the no-namespace side.
495+ noNamespaceTests :: Redis. Handler -> Redis. Handler -> List Test. Test
496+ noNamespaceTests noNs nsHandler =
497+ [ Test. test " set via no-namespace handler stores the key under its literal name" <| \ () -> do
498+ -- Write at literal key "noNs::literalKey" using the no-namespace handler.
499+ Redis. set api " noNs::literalKey" " value-no-ns" |> Redis. query noNs |> Expect. succeeds
500+ -- The namespaced handler would look at "tests:noNs::literalKey", which
501+ -- nothing has written to, so it should see Nothing.
502+ result <- Redis. get api " noNs::literalKey" |> Redis. query nsHandler |> Expect. succeeds
503+ Expect. equal result Nothing ,
504+ Test. test " get via no-namespace handler reads the literal key (no prefix added)" <| \ () -> do
505+ -- Namespaced handler writes "noNs::roundTrip" → redis sees "tests:noNs::roundTrip".
506+ Redis. set api " noNs::roundTrip" " via-namespace" |> Redis. query nsHandler |> Expect. succeeds
507+ -- The no-namespace handler can reach that same value by spelling out
508+ -- the full prefixed key, since it adds no prefix of its own.
509+ result <- Redis. get api " tests:noNs::roundTrip" |> Redis. query noNs |> Expect. succeeds
510+ Expect. equal result (Just " via-namespace" ),
511+ Test. test " eval via no-namespace handler does not prefix script keys" <| \ () -> do
512+ let script = [Redis. script |return ${Redis.Key "noNs::evalKey"}|]
513+ (result :: Text ) <- Redis. eval noNs script |> Expect. succeeds
514+ Expect. equal result " noNs::evalKey" ,
515+ Test. test " foldWithScan via no-namespace handler returns keys verbatim" <| \ () -> do
516+ let scanPrefix = " noNs::scanTest::"
517+ let firstKey = scanPrefix ++ " k1"
518+ let nonEmptyDict =
519+ NonEmptyDict. init firstKey " v1"
520+ <| Dict. fromList [(scanPrefix ++ " k2" , " v2" )]
521+ let expectedKeys =
522+ NonEmptyDict. toDict nonEmptyDict
523+ |> Dict. keys
524+ Redis. mset api nonEmptyDict |> Redis. query noNs |> Expect. succeeds
525+ let processBatch = \ batchKeys acc ->
526+ Task. succeed (List. foldl Set. insert acc batchKeys)
527+ keySet <-
528+ Redis. foldWithScan noNs (Just (scanPrefix ++ " *" )) (Just 10 ) processBatch Set. empty
529+ |> Expect. succeeds
530+ keySet
531+ |> Set. toList
532+ |> Expect. equal expectedKeys
533+ ]
534+
489535addNamespace :: Text -> Redis. Handler' x -> Redis. Handler' x
490536addNamespace namespace handler' =
491- handler' {Internal. namespace = Internal. namespace handler' ++ " :" ++ namespace}
537+ let combined = case Internal. namespace handler' of
538+ Just existing -> existing ++ " :" ++ namespace
539+ Nothing -> namespace
540+ in handler' {Internal. namespace = Just combined}
492541
493542api :: Redis. Api Text Text
494543api = Redis. textApi identity
0 commit comments