exchangealgebra-0.7.0.1: test/Algebra/ProjWildcardSpec.hs
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
-- | The @proj@ family matches wildcards one way: only the query's wildcards
-- act as a pattern, and a wildcard stored in a ledger base is an ordinary
-- value. The result must not depend on the shape of the ledger (a single
-- element or a 'Liner').
module Algebra.ProjWildcardSpec (runTests) where
import qualified Data.Binary as Binary
import qualified Data.Binary.Put as Binary
import qualified Data.HashMap.Strict as HashMap
import Data.Time ( Day(ModifiedJulianDay)
, TimeOfDay(..)
, fromGregorian
, midnight
)
import Data.Typeable (Typeable)
import ExchangeAlgebra.Accounting.Account
import ExchangeAlgebra.Accounting.Exchange
import Control.Monad (unless)
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe, listToMaybe)
import Data.Text (Text)
import System.Exit (exitFailure)
import Test.QuickCheck hiding (label)
import ExchangeAlgebra.Algebra hiding (map, filter)
import qualified ExchangeAlgebra.Algebra as Algebra
import ExchangeAlgebra.Algebra.Element (matchesQuery)
import qualified ExchangeAlgebra.Algebra.Internal as Internal
import qualified ExchangeAlgebra.Algebra.Transfer as Transfer
import ExchangeAlgebra.Algebra.Value (MoneyDecimal)
import qualified ExchangeAlgebra.Journal as Journal
import Support (assertEqual)
-- | Account and unit coordinates; either axis may hold a ledger wildcard.
type TestBase = HatBase (AccountTitles, CountUnit)
type TestAlg = Alg Double TestBase
-- | Generated postings (ledger wildcards allowed) and a query set. Values are
-- small whole numbers, so every 'Double' sum below is exact.
data ProjCase = ProjCase [(Double, TestBase)] [TestBase]
deriving (Show)
genTitle :: Gen AccountTitles
genTitle = frequency [(1, pure wildcard), (4, elements [Cash, Products, Sales])]
genUnit :: Gen CountUnit
genUnit = frequency [(1, pure wildcard), (4, elements [Yen, Amount, Dollar])]
genEntryBase :: Gen TestBase
genEntryBase = (:<) <$> elements [Hat, Not] <*> ((,) <$> genTitle <*> genUnit)
genQueryBase :: Gen TestBase
genQueryBase = (:<) <$> elements [Hat, Not, HatNot] <*> ((,) <$> genTitle <*> genUnit)
genProjCase :: Gen ProjCase
genProjCase = do
count <- chooseInt (0, 12)
entries <- vectorOf count ((,) <$> (fromIntegral <$> chooseInt (1, 1000)) <*> genEntryBase)
queryCount <- chooseInt (1, 4)
ProjCase entries <$> vectorOf queryCount genQueryBase
-- | The same generator restricted to concrete ledger bases.
genConcreteCase :: Gen ProjCase
genConcreteCase = do
ProjCase entries queries <- genProjCase
pure (ProjCase (filter (not . haveWildcard . snd) entries) queries)
-- | Reference predicate, written per axis and independent of the library's
-- 'ignoreWildcard': a query axis matches when it is a wildcard or equals the
-- entry axis. A ledger wildcard is matched only by a query wildcard.
oneWay :: TestBase -> TestBase -> Bool
oneWay (queryHat :< (queryTitle, queryUnit)) (entryHat :< (entryTitle, entryUnit)) =
axis queryHat entryHat && axis queryTitle entryTitle && axis queryUnit entryUnit
where
axis :: Element a => a -> a -> Bool
axis query entry = isWildcard query || query == entry
-- | The old symmetric predicate, for the concrete-ledger regression.
symmetric :: TestBase -> TestBase -> Bool
symmetric = (.==)
build :: [(Double, TestBase)] -> TestAlg
build entries = fromList [value .@ postingBase | (value, postingBase) <- entries]
-- | Raw postings keyed by the rendered base. 'HatBase' orders wildcards as
-- equal to anything, so the base itself cannot be the key here.
observe :: TestAlg -> Map.Map String [Double]
observe = Map.map List.sort
. foldEntries (\result value postingBase ->
Map.insertWith (++) (show postingBase) [value] result) Map.empty
selected :: (TestBase -> TestBase -> Bool) -> ProjCase -> [(Double, TestBase)]
selected match (ProjCase entries queries) =
[entry | entry@(_, postingBase) <- entries, any (`match` postingBase) queries]
-- | Bar-netted norm of the selected entries, per base.
netNorm :: [(Double, TestBase)] -> Double
netNorm entries = sum (Map.elems (Map.map abs perBase))
where
perBase = Map.fromListWith (+)
[ (show (base postingBase), if hat postingBase == Hat then negate value else value)
| (value, postingBase) <- entries ]
propLedger :: ProjCase -> Bool
propLedger testCase@(ProjCase entries queries) =
observe (proj queries (build entries)) == observe (build (selected oneWay testCase))
propSingle :: ProjCase -> Bool
propSingle (ProjCase entries queries) = all agrees entries
where
agrees entry@(value, postingBase) =
observe (proj queries (value .@ postingBase))
== observe (build (selected oneWay (ProjCase [entry] queries)))
propNetNorm :: ProjCase -> Bool
propNetNorm testCase@(ProjCase entries queries) =
projNetNorm queries (build entries) == netNorm (selected oneWay testCase)
&& all single entries
where
single entry@(value, postingBase) =
projNetNorm queries (value .@ postingBase)
== netNorm (selected oneWay (ProjCase [entry] queries))
propConcreteRegression :: ProjCase -> Bool
propConcreteRegression testCase =
selected oneWay testCase == selected symmetric testCase && propLedger testCase
assertTest :: String -> Bool -> IO ()
assertTest label success = do
unless success $ do
putStrLn ("[FAIL] " ++ label)
exitFailure
putStrLn ("[PASS] " ++ label)
quickProperty :: Testable property => String -> property -> IO ()
quickProperty label proposition = do
result <- quickCheckWithResult stdArgs { maxSuccess = 500, chatty = False } proposition
unless (isSuccess result) $ do
putStrLn ("[FAIL] " ++ label ++ ": " ++ output result)
exitFailure
putStrLn ("[PASS] " ++ label)
-- | The reported table: cash carries no unit axis in the ledger.
testReportedTable :: IO ()
testReportedTable = do
let cash = 10 .@ Not :< (Cash, wildcard) :: TestAlg
both = cash .+ 3 .@ Not :< (Products, Yen)
anyTitleYen = [Not :< (wildcard, Yen)]
cashYen = [Not :< (Cash, Yen)]
cashAny = [Not :< (Cash, wildcard)]
products = 3 .@ Not :< (Products, Yen) :: TestAlg
assertTest "proj: wildcard-title query skips ledger-wildcard unit (single)"
(Algebra.isZero (proj anyTitleYen cash))
assertTest "proj: wildcard-title query skips ledger-wildcard unit (Liner)"
(observe (proj anyTitleYen both) == observe products)
assertTest "proj: concrete query does not match a ledger wildcard (single)"
(Algebra.isZero (proj cashYen cash))
assertTest "proj: concrete query does not match a ledger wildcard (Liner)"
(Algebra.isZero (proj cashYen both))
assertTest "proj: query wildcard picks up the ledger wildcard (single)"
(observe (proj cashAny cash) == observe cash)
assertTest "proj: query wildcard picks up the ledger wildcard (Liner)"
(observe (proj cashAny both) == observe cash)
assertTest "projNetNorm: single element and Liner agree on a ledger wildcard"
(projNetNorm cashYen cash == 0 && projNetNorm cashYen both == 0
&& projNetNorm cashAny cash == 10 && projNetNorm cashAny both == 10)
assertTest "proj: projecting a projection is stable"
(observe (proj cashYen (proj cashAny both)) == Map.empty)
-- | 'projByAccountTitle' follows the same one-way rule on the title axis.
testProjByAccountTitle :: IO ()
testProjByAccountTitle = do
let noTitle = 7 .@ Not :< wildcard :: Alg Double (HatBase AccountTitles)
cash = 10 .@ Not :< Cash
ledger = noTitle .+ cash
postings = List.sort . foldEntries (\result value postingBase ->
(show postingBase, value) : result) []
assertTest "projByAccountTitle: concrete title skips a ledger wildcard (single)"
(Algebra.isZero (projByAccountTitle Cash noTitle))
assertTest "projByAccountTitle: concrete title skips a ledger wildcard (Liner)"
(postings (projByAccountTitle Cash ledger) == postings cash)
assertTest "projByAccountTitle: wildcard title selects every entry"
(postings (projByAccountTitle wildcard ledger) == postings ledger)
-- | 'Algebra.map' keeps a rewrite between a wildcard and a concrete coordinate
-- in both directions and in both ledger shapes.
testMapRewritesWildcardAxis :: IO ()
testMapRewritesWildcardAxis = do
let cash = 10 .@ Not :< (Cash, wildcard) :: TestAlg
cashYen = 10 .@ Not :< (Cash, Yen) :: TestAlg
products = 3 .@ Not :< (Products, Amount) :: TestAlg
setCashUnit unit entry@(value :@ (h :< (title, _)))
| title == Cash = value .@ (h :< (title, unit))
| otherwise = entry
setCashUnit _ entry = entry
assertTest "map: wildcard axis rewritten to a concrete value (single)"
(observe (Algebra.map (setCashUnit Yen) cash) == observe cashYen)
assertTest "map: wildcard axis rewritten to a concrete value (Liner)"
(observe (Algebra.map (setCashUnit Yen) (cash .+ products))
== observe (cashYen .+ products))
assertTest "map: concrete axis rewritten to the wildcard (Liner)"
(observe (Algebra.map (setCashUnit wildcard) (cashYen .+ products))
== observe (cash .+ products))
-- | Flattening Text coordinates preserves projection norms, net norms,
-- collapse postings, and symmetric matching. Small non-negative whole values
-- make the Double observations exact; either inner query axis may be a wildcard.
propNestedFlattening :: (Text, Text, Text) -> (Double, Double, Double) -> Bool
propNestedFlattening (queryFirst, querySecond, queryThird) (notValue, hatValue, otherValue) =
norm (proj [nestedQuery] nestedLedger) == norm (proj [flatQuery] flatLedger)
&& projNetNorm [nestedQuery] nestedLedger == projNetNorm [flatQuery] flatLedger
&& List.sort nestedCollapsed == List.sort flatCollapsed
&& (nestedStored .== nestedCoordinates) == (flatStored .== flatCoordinates)
&& ((Not :< nestedStored) .== (Not :< nestedCoordinates))
== ((Not :< flatStored) .== (Not :< flatCoordinates))
where
nestedStored = (("a", "b"), "c") :: ((Text, Text), Text)
flatStored = ("a", "b", "c") :: (Text, Text, Text)
nestedCoordinates = ((queryFirst, querySecond), queryThird)
flatCoordinates = (queryFirst, querySecond, queryThird)
nestedQuery = HatNot :< nestedCoordinates
flatQuery = HatNot :< flatCoordinates
nestedLedger :: Alg Double (HatBase ((Text, Text), Text))
nestedLedger = notValue .@ Not :< nestedStored
.+ hatValue .@ Hat :< nestedStored
.+ otherValue .@ Not :< (("x", "y"), "z")
flatLedger :: Alg Double (HatBase (Text, Text, Text))
flatLedger = notValue .@ Not :< flatStored
.+ hatValue .@ Hat :< flatStored
.+ otherValue .@ Not :< ("x", "y", "z")
nestedCollapsed :: [(Double, Hat, (Text, Text, Text))]
nestedCollapsed = foldEntries
(\result value (side :< ((first, second), third)) ->
(value, side, (first, second, third)) : result) []
(Transfer.collapseEntries [nestedQuery] (const (("a", "b"), "d")) nestedLedger)
flatCollapsed :: [(Double, Hat, (Text, Text, Text))]
flatCollapsed = foldEntries
(\result value (side :< coordinates) -> (value, side, coordinates) : result) []
(Transfer.collapseEntries [flatQuery] (const ("a", "b", "d")) flatLedger)
-- | The reported inner-only wildcard selects gross 13 and net 7, and
-- 'Transfer.collapseEntries' adds reversal and replacement postings with total norm 26.
testNestedWildcard :: IO ()
testNestedWildcard = do
let ledger = 10 .@ Not :< (("a", "b"), "c")
.+ 3 .@ Hat :< (("a", "b"), "c")
.+ 2 .@ Not :< (("x", "y"), "z")
:: Alg Double (HatBase ((Text, Text), Text))
query = HatNot :< (("a", ""), "c")
assertTest "nested wildcard: reported projection and collapse values"
(norm (proj [query] ledger) == 13
&& projNetNorm [query] ledger == 7
&& norm (Transfer.collapseEntries [query] (const (("a", "b"), "d")) ledger) == 26)
assertTest "nested wildcard: reported case agrees with flat coordinates"
(propNestedFlattening ("a", "", "c") (10, 3, 2))
-- | A fixture identifier with no Element instance and deliberately colliding hashes.
newtype FixtureId = FixtureId Int
deriving (Eq, Ord, Show)
instance Hashable FixtureId where
hashWithSalt salt _ = salt
instance Binary.Binary FixtureId where
put (FixtureId number) = Binary.put number
get = FixtureId <$> Binary.get
instance Journal.Note FixtureId where
plank = FixtureId 0
-- | An algebra whose distinct concrete bases have the same hash.
type CollisionAlg = Alg Double (HatBase (Wild FixtureId))
-- | A destination shared by every generated source base.
collisionTarget :: Wild FixtureId
collisionTarget = Exactly (FixtureId 0)
-- | Compare the results of reading or relabeling equal algebras.
collisionOrderLaws :: CollisionAlg -> CollisionAlg -> [(String, Bool)]
collisionOrderLaws left right =
[ ("equal inputs", left == right)
, ("mapBasePart", agrees (mapBasePart (const collisionTarget) :: CollisionAlg -> CollisionAlg))
, ("map", agrees (Algebra.map (\(value :@ basis) -> relabel value basis)))
, ("mapMaybePosting", agrees (mapMaybePosting (\value basis -> Just (value, target basis))))
, ("extendBy", agrees (extendBy relabel))
, ("transferEntries", transferAgrees)
, ("foldEntries", agrees (foldEntries (\terms value basis -> (value, basis) : terms) []))
, ("toList", agrees toList)
, ("vals", agrees vals)
, ("bases", agrees bases)
]
where
agrees observeResult = observeResult left == observeResult right
target basis = hat basis :< collisionTarget
relabel value basis = value .@ target basis
transferAgrees = case Transfer.mkTransferRules
[Transfer.TransferRule (HatNot :< Wild) (HatNot :< collisionTarget) Transfer.Relabel] of
Left _ -> False
Right rules ->
let apply = Transfer.transferEntries rules
in case (apply left, apply right) of
(Right actual, Right expected) -> actual == expected
_ -> False
-- | Change the interleaving of whole base groups, keeping each group's sequence.
genCollisionLedgers :: Gen (CollisionAlg, CollisionAlg)
genCollisionLedgers = frequency [(4, multipleBases), (1, oneTerm)]
where
multipleBases = do
count <- chooseInt (2, 8)
groups <- traverse genTerms [1 .. count]
reordered <- shuffle groups
pure (fromList (concat groups), fromList (concat reordered))
genTerms identifier = listOf1 $ do
side <- elements [Hat, Not]
value <- fromIntegral <$> chooseInt (1, 10)
pure (value .@ side :< Exactly (FixtureId identifier))
oneTerm = do
firstValue <- fromIntegral <$> chooseInt (1, 10)
secondValue <- fromIntegral <$> chooseInt (1, 10)
side <- elements [Hat, Not]
let basis = side :< Exactly (FixtureId 1)
single = (firstValue + secondValue) .@ basis
composed = bar (firstValue .@ basis .+ secondValue .@ basis)
pure (single, composed)
-- | Check equal-hash placement, including equal one-term values of different shapes.
testCollisionOrder :: IO ()
testCollisionOrder = do
assertTest "Hash collisions: distinct concrete bases have equal hashes"
(first /= second && hash first == hash second)
mapM_ (\(operation, success) -> assertTest ("Hash collisions: " ++ operation) success)
(collisionOrderLaws left right)
assertEqual "Hash collisions: foldEntries visits equal-hash bases in Ord order"
[second, first] (foldEntries (\keys _ basis -> base basis : keys) [] right)
assertEqual "Hash collisions: Alg Binary bytes" (Binary.encode left) (Binary.encode right)
quickProperty "Hash collisions: equal Alg readouts and encodings" $
forAll genCollisionLedgers $ \(before, after) ->
let keys = case before of
Internal.Liner {} -> HashMap.keys (Internal._realg before)
_ -> []
hashes = map hash keys
colliding = length hashes /= length (List.nub hashes)
in checkCoverage
$ cover 60 colliding "multiple colliding bases"
$ cover 10 (not colliding) "equal singleton and one-term map"
$ conjoin
([counterexample operation (property success)
| (operation, success) <- collisionOrderLaws before after]
++ [Binary.encode before === Binary.encode after | colliding])
where
first = Exactly (FixtureId 1)
second = Exactly (FixtureId 2)
left = 1 .@ Not :< first .+ 2 .@ Not :< second :: CollisionAlg
right = 2 .@ Not :< second .+ 1 .@ Not :< first :: CollisionAlg
-- | Reversing two equal-hash notes leaves journal readouts equal.
testNoteCollisionOrder :: IO ()
testNoteCollisionOrder = do
assertEqual "Hash collisions: equal journal maps" (Journal.toMap left) (Journal.toMap right)
assertEqual "Hash collisions: Journal.toAlg" (Journal.toAlg left) (Journal.toAlg right)
assertEqual "Hash collisions: Journal Binary bytes" (Binary.encode left) (Binary.encode right)
assertEqual "Hash collisions: Journal Show" (show left) (show right)
where
first = (1 .@ Not :< collisionTarget :: CollisionAlg) Journal..| FixtureId 1
second = (2 .@ Not :< collisionTarget :: CollisionAlg) Journal..| FixtureId 2
left = first .+ second
right = second .+ first
-- | Preserve the previous output direction and wire order for distinct hashes.
testDistinctHashOrder :: IO ()
testDistinctHashOrder = do
assertTest "Distinct hashes: fixture keys do not collide"
(length hashes == length (List.nub hashes))
assertEqual "Distinct hashes: toList keeps reverse HashMap key order"
(reverse keys) outputKeys
assertEqual "Distinct hashes: Alg Binary retains HashMap traversal order"
previousBytes (Binary.encode algebra)
assertTest "Distinct hashes: fixture notes do not collide"
(length noteHashes == length (List.nub noteHashes))
assertEqual "Distinct hashes: Journal.toAlg retains HashMap traversal order"
(unionsMerge notes) (Journal.toAlg journal)
assertEqual "Distinct hashes: Journal Binary retains HashMap traversal order"
previousJournalBytes (Binary.encode journal)
where
algebra = 1 .@ Hat :< Yen .+ 2 .@ Hat :< Yen .+ 3 .@ Not :< Yen
.+ 4 .@ Not :< Dollar .+ 5 .@ Hat :< Amount :: Alg Double (HatBase CountUnit)
entries = Internal._realg algebra
keys = HashMap.keys entries
hashes = map hash keys
outputKeys = mapMaybe listToMaybe $ List.group $ map (base . _hatBase) (toList algebra)
previousBytes = Binary.runPut $ do
Binary.put (2 :: Int)
Binary.put (HashMap.size entries)
HashMap.foldrWithKey
(\key pair remaining -> Binary.put key >> Binary.put pair >> remaining)
(pure ())
entries
journal = (1 .@ Not :< Yen) Journal..| (3 :: Int)
.+ (2 .@ Not :< Yen) Journal..| 1
.+ (3 .@ Not :< Yen) Journal..| 2 :: Journal.Journal Int Double (HatBase CountUnit)
notes = Journal.toMap journal
noteHashes = map hash (HashMap.keys notes)
previousJournalBytes = Binary.runPut $ do
Binary.put (HashMap.size notes)
HashMap.foldrWithKey
(\note terms remaining -> Binary.put note >> Binary.put terms >> remaining)
(pure ())
notes
-- | Compare exact coordinates, sides, and amounts independently of posting order.
observeWild :: forall coordinates. (BaseClass coordinates, HasElementKeys coordinates)
=> Alg MoneyDecimal (HatBase coordinates)
-> [(Hat, coordinates, MoneyDecimal)]
observeWild = List.sort . foldEntries collect []
where
collect :: [(Hat, coordinates, MoneyDecimal)]
-> MoneyDecimal
-> HatBase coordinates
-> [(Hat, coordinates, MoneyDecimal)]
collect postings amount (side :< coordinates) = (side, coordinates, amount) : postings
-- | Match one coordinate without using the library's matching implementation.
matchesAxis :: Element coordinate => coordinate -> coordinate -> Bool
matchesAxis query entry = isWildcard query || query == entry
-- | Check every ordered pair in an ascending list of concrete coordinates plus Wild.
testWildAxis :: forall coordinate.
(Ord coordinate, Show coordinate, Hashable coordinate, Typeable coordinate)
=> String
-> [coordinate]
-> IO ()
testWildAxis axis concrete = do
assertEqual (axis ++ " matchesQuery is one-way")
[query == Wild || query == entry | (query, entry) <- pairs]
[matchesQuery query entry | (query, entry) <- pairs]
assertEqual (axis ++ " .== is symmetric")
[query == Wild || entry == Wild || query == entry | (query, entry) <- pairs]
[query .== entry | (query, entry) <- pairs]
assertEqual (axis ++ " compareElement treats Wild as EQ in either position")
[wildcardOrder query entry | (query, entry) <- pairs]
[compareElement query entry | (query, entry) <- pairs]
assertEqual (axis ++ " compare orders Wild first and agrees with Eq")
[(compare left right, left == right) | (left, right) <- indexPairs]
[(compare left right, left == right) | (left, right) <- pairs]
where
coordinates :: [Wild coordinate]
coordinates = Wild : fmap Exactly concrete
pairs :: [(Wild coordinate, Wild coordinate)]
pairs = [(query, entry) | query <- coordinates, entry <- coordinates]
indices :: [Int]
indices = take (length coordinates) [0 ..]
indexPairs :: [(Int, Int)]
indexPairs = [(left, right) | left <- indices, right <- indices]
wildcardOrder :: Ord payload => Wild payload -> Wild payload -> Ordering
wildcardOrder Wild _ = EQ
wildcardOrder _ Wild = EQ
wildcardOrder left right = compare left right
-- | Account, time, and identifier coordinates shared by both projection shapes.
type WildCoordinates = (AccountTitles, Wild TimeOfDay, Wild Text)
-- | Flat tuples expose each axis key. A nested tuple stays one key at its outer position.
testWildElementKeys :: IO ()
testWildElementKeys = do
assertTest "Wild flat tuple has one wildcard at the time axis" $
haveWildcard flat && not (haveWildcard (Cash, time, identifier))
assertTest "Wild nested tuple has an inner wildcard" $
haveWildcard nested && not (haveWildcard (Cash, (time, identifier)))
assertTest "Wild flat tuple exposes three axis keys in coordinate order" $
toElementKeys flat == [ElementKey Cash, ElementKey noTime, ElementKey identifier]
assertTest "Wild nested tuple exposes two outer keys with the pair in position two" $
toElementKeys nested == [ElementKey Cash, ElementKey (noTime, identifier)]
where
noTime :: Wild TimeOfDay
noTime = Wild
time :: Wild TimeOfDay
time = Exactly midnight
identifier :: Wild Text
identifier = Exactly ""
flat :: WildCoordinates
flat = (Cash, noTime, identifier)
nested :: (AccountTitles, (Wild TimeOfDay, Wild Text))
nested = (Cash, (noTime, identifier))
-- | Exercise single and indexed projections against the same per-axis oracle.
testWildProjection :: forall coordinates. (BaseClass coordinates, HasElementKeys coordinates)
=> String
-> (WildCoordinates -> coordinates)
-> IO ()
testWildProjection shape pack = mapM_ checkQueries querySets
where
entries :: [(MoneyDecimal, Hat, WildCoordinates)]
entries =
[ (10, Not, (Cash, Exactly midnight, Exactly ""))
, ( 4, Hat, (Cash, Exactly midnight, Exactly ""))
, (20, Not, (Cash, Wild, Exactly ""))
, (30, Not, (Cash, Exactly midnight, Wild))
, (40, Not, (Sales, Exactly midnight, Exactly ""))
]
querySets :: [[(Hat, WildCoordinates)]]
querySets =
[ [(HatNot, (Cash, Exactly midnight, Exactly ""))]
, [(HatNot, (Cash, Wild, Exactly ""))]
, [(Not, (Cash, Exactly midnight, Wild))]
, [(HatNot, (Cash, Wild, Exactly "")), (Not, (Cash, Exactly midnight, Wild))]
]
matches :: (Hat, WildCoordinates) -> (MoneyDecimal, Hat, WildCoordinates) -> Bool
matches (querySide, (queryTitle, queryTime, queryId))
(_, entrySide, (entryTitle, entryTime, entryId)) =
matchesAxis querySide entrySide
&& matchesAxis queryTitle entryTitle
&& matchesAxis queryTime entryTime
&& matchesAxis queryId entryId
posting :: (MoneyDecimal, Hat, WildCoordinates) -> Alg MoneyDecimal (HatBase coordinates)
posting (amount, side, entry) = amount .@ side :< pack entry
ledger :: Alg MoneyDecimal (HatBase coordinates)
ledger = fromList (fmap posting entries)
checkQueries :: [(Hat, WildCoordinates)] -> IO ()
checkQueries patterns = do
let queries = [side :< pack query | (side, query) <- patterns]
selected = fromList [posting entry | entry <- entries, selects entry]
context = "Wild " ++ shape ++ " " ++ show patterns
note = "receipts" :: Text
journal = Journal.projWithBase queries (ledger Journal..| note)
singles = fmap posting entries
selects :: (MoneyDecimal, Hat, WildCoordinates) -> Bool
selects entry = any (`matches` entry) patterns
assertEqual (context ++ " single-posting proj")
[observeWild (fromList [posting entry | selects entry]) | entry <- entries]
[observeWild (proj queries single) | single <- singles]
assertEqual (context ++ " single-posting projNetNorm")
[norm (fromList [posting entry | selects entry]) | entry <- entries]
[projNetNorm queries single | single <- singles]
assertEqual (context ++ " indexed proj") (observeWild selected)
(observeWild (proj queries ledger))
assertEqual (context ++ " indexed projNetNorm") (norm (bar selected))
(projNetNorm queries ledger)
assertEqual (context ++ " journal base projection")
(observeWild selected)
(observeWild (Journal.toAlg (Journal.projWithNote [note] journal)))
-- | Check source matching and transfer targets for concrete and wildcard times.
testWildTransfer :: IO ()
testWildTransfer = do
assertTest "Wild concrete transfer source skips the stored Wild" $
agrees ledger (Exactly midnight) Wild
(4 .@ Hat :< (Cash, Exactly midnight)
.+ 4 .@ Not :< (Deposits, Exactly midnight))
assertTest "Wild transfer target retains both stored time coordinates" $
agrees ledger Wild Wild
(7 .@ Hat :< (Cash, Wild)
.+ 7 .@ Not :< (Deposits, Wild)
.+ 4 .@ Hat :< (Cash, Exactly midnight)
.+ 4 .@ Not :< (Deposits, Exactly midnight))
assertTest "Exactly midnight sets the transfer target time to 00:00" $
agrees (5 .@ Not :< (Cash, morning)) morning (Exactly midnight)
(5 .@ Hat :< (Cash, morning)
.+ 5 .@ Not :< (Deposits, Exactly midnight))
where
ledger :: Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
ledger = 7 .@ Not :< (Cash, Wild)
.+ 4 .@ Not :< (Cash, Exactly midnight)
morning = Exactly (TimeOfDay 9 0 0)
agrees :: Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
-> Wild TimeOfDay
-> Wild TimeOfDay
-> Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
-> Bool
agrees input source target expected =
case Transfer.mkTransferRules [rule] of
Left _ -> False
Right rules -> case Transfer.transferEntries rules input of
Left _ -> False
Right postings -> observeWild postings == observeWild expected
where
rule = Transfer.TransferRule (Not :< (Cash, source)) (Not :< (Deposits, target))
Transfer.Relabel
-- | Replacing an account title preserves the Hat/Not side and every other coordinate.
testWildAccountTitles :: IO ()
testWildAccountTitles = do
assertEqual "Wild Day: setAccountTitle preserves the day and side"
(Hat :< (Deposits, epoch)) (setAccountTitle (Hat :< (Cash, epoch)) Deposits)
assertEqual "Wild Day: setAccountTitle preserves the five-axis base"
(Not :< (Deposits, item, Yen, subject, noDay))
(setAccountTitle (Not :< (Cash, item, Yen, subject, noDay)) Deposits)
assertEqual "Wild Day/TimeOfDay: setAccountTitle preserves the six-axis base"
(Hat :< (Deposits, item, Yen, subject, epoch, Exactly midnight))
(setAccountTitle (Hat :< (Cash, item, Yen, subject, epoch, Exactly midnight)) Deposits)
where
epoch = Exactly (ModifiedJulianDay 0)
noDay = Wild :: Wild Day
item = "notebook" :: Name
subject = "stock" :: Subject
-- | Wrapped identifiers retain equality, serialization, and indexed lookup under collision.
testWildPayload :: IO ()
testWildPayload = do
testWildAxis "Wild FixtureId" [FixtureId 1, FixtureId 2]
assertTest "Wild Text treats empty text as an ordinary coordinate" $
not (isWildcard empty) && not (haveWildcard empty)
&& matchesQuery empty empty && not (matchesQuery empty Wild)
quickProperty "Wild equal coordinates hash equally at every salt" $
forAll arbitrary $ \salt ->
let pairs = [(Wild, Wild), (Exactly (FixtureId 1), first)]
in all (\(left, right) -> hashWithSalt salt left == hashWithSalt salt right) pairs
assertEqual "Wild Binary round trips both constructors"
[Wild, first, second] (fmap (Binary.decode . Binary.encode) [Wild, first, second])
assertTest "Wild index keys collide at the same salt" $
firstKey /= secondKey && hashWithSalt 7 firstKey == hashWithSalt 7 secondKey
let ledger = 7 .@ Not :< first .+ 9 .@ Not :< second
:: Alg MoneyDecimal (HatBase (Wild FixtureId))
assertEqual "Wild index retrieves the first colliding key" 7
(norm (proj [Not :< first] ledger))
assertEqual "Wild index retrieves the second colliding key" 9
(norm (proj [Not :< second] ledger))
where
empty = Exactly "" :: Wild Text
first = Exactly (FixtureId 1)
second = Exactly (FixtureId 2)
firstKey = ElementKey first
secondKey = ElementKey second
-- | Check that midnight and 1858-11-17 can be selected as ordinary elements.
testWildCalendarRegression :: IO ()
testWildCalendarRegression = do
assertEqual "Wild TimeOfDay: midnight selects only the midnight posting" 100
(norm (proj [Not :< (Cash, Exactly midnight)] receipts))
assertEqual "Wild TimeOfDay: Wild selects all three postings" 600
(norm (proj [Not :< (Cash, Wild)] receipts))
assertEqual "Wild Day: 1858-11-17 selects only that day's posting" 500
(norm (proj [Not :< (Cash, Exactly (ModifiedJulianDay 0))] dated))
assertEqual "Wild Day: sale totals 500 on both debit and credit" (500, 500)
(norm (decL sale), norm (decR sale))
where
receipts :: Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
receipts = 100 .@ Not :< (Cash, Exactly midnight)
.+ 200 .@ Not :< (Cash, Exactly (TimeOfDay 9 0 0))
.+ 300 .@ Not :< (Cash, Wild)
sale :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Day))
sale = 500 .@ Not :< (Cash, Exactly (fromGregorian 2026 1 10))
.+ 500 .@ Not :< (Sales, Exactly (fromGregorian 2026 1 10))
dated :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Day))
dated = sale .+ 500 .@ Not :< (Cash, Exactly (ModifiedJulianDay 0))
.+ 300 .@ Not :< (Cash, Wild)
runTests :: IO ()
runTests = do
testReportedTable
testProjByAccountTitle
testMapRewritesWildcardAxis
testNestedWildcard
quickProperty "nested wildcard: flattening preserves projection, collapse, and matching"
(forAll ((,,) <$> elements ["a", ""] <*> elements ["b", ""]
<*> elements ["c", ""]) $ \coordinates ->
forAll ((,,) <$> (fromIntegral <$> chooseInt (0, 1000))
<*> (fromIntegral <$> chooseInt (0, 1000))
<*> (fromIntegral <$> chooseInt (0, 1000)))
(propNestedFlattening coordinates))
quickProperty "proj: Liner result equals the one-way reference (ledger wildcards)"
(forAll genProjCase propLedger)
quickProperty "proj: single-element result equals the one-way reference"
(forAll genProjCase propSingle)
quickProperty "projNetNorm: equals the one-way reference for both shapes"
(forAll genProjCase propNetNorm)
quickProperty "proj: concrete ledgers keep the old symmetric result"
(forAll genConcreteCase propConcreteRegression)
testWildAxis "Wild TimeOfDay" [midnight, TimeOfDay 9 0 0]
testWildAxis "Wild Day" [ModifiedJulianDay (-1), ModifiedJulianDay 0, ModifiedJulianDay 1]
testWildElementKeys
testWildProjection "flat tuple" id
testWildProjection "nested tuple" (\(title, time, identifier) -> (title, (time, identifier)))
testWildTransfer
testWildAccountTitles
testWildPayload
testWildCalendarRegression
testCollisionOrder
testNoteCollisionOrder
testDistinctHashOrder