tests for ingesting facts

This commit is contained in:
Felix Dilke 2026-01-22 14:25:09 +00:00
parent 36622caf8b
commit 5caf061fce
3 changed files with 30 additions and 49 deletions

View File

@ -77,7 +77,7 @@ test-suite haskell-exps-test
main-is: Main.hs main-is: Main.hs
-- Test dependencies. -- Test dependencies.
build-depends: base, hspec, langfeatures, megaparsec build-depends: base, containers, megaparsec, hspec, langfeatures
other-modules: Test.OlogsSpec, other-modules: Test.OlogsSpec,
Test.SimpleParserSpec, Test.SimpleParserSpec,
Test.ArithmeticParserSpec, Test.ArithmeticParserSpec,

View File

@ -30,7 +30,7 @@ data NaiveDatabase = NaiveDatabase {
data Relation = Relation { data Relation = Relation {
arity :: Int, arity :: Int,
tuples :: Set [Constant] tuples :: Set [Constant]
} } deriving (Show, Eq)
-- newtype RelationId = RelationId Text -- newtype RelationId = RelationId Text
-- deriving (Eq, Ord, Show) -- deriving (Eq, Ord, Show)
@ -60,10 +60,15 @@ withFacts facts =
addFact :: NaiveDatabase -> Literal -> NaiveDatabase addFact :: NaiveDatabase -> Literal -> NaiveDatabase
addFact (NaiveDatabase relations constants) (Literal neg relationName terms) = addFact (NaiveDatabase relations constants) (Literal neg relationName terms) =
NaiveDatabase newRelations newConstants where NaiveDatabase newRelations newConstants where
newRelations = newArity = length terms
newRelation =
case Map.lookup relationName relations of case Map.lookup relationName relations of
Nothing -> relations Nothing -> Relation (length terms) (Set.singleton terms)
Just relation -> relations Just relation ->
if (arity relation == newArity)
then Relation (length terms) (Set.singleton terms)
else throw $ BadArityException relationName newArity
newRelations = Map.insert relationName newRelation relations
newConstants = Set.union constants $ Set.fromList terms newConstants = Set.union constants $ Set.fromList terms
query :: NaiveDatabase -> Text -> Text query :: NaiveDatabase -> Text -> Text
@ -76,7 +81,8 @@ query db qText =
data NaiveDatabaseException data NaiveDatabaseException
= CannotParseStatementException Text (ParseErrorBundle Text Void) | = CannotParseStatementException Text (ParseErrorBundle Text Void) |
NonFactException Text Statement | NonFactException Text Statement |
NonQueryException Text Statement NonQueryException Text Statement |
BadArityException Text Int
deriving (Show) deriving (Show)
instance Exception NaiveDatabaseException instance Exception NaiveDatabaseException

View File

@ -8,60 +8,35 @@
{-# LANGUAGE NoFieldSelectors #-} {-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ImportQualifiedPost #-}
module Test.Datalog.NaiveDatabaseSpec where module Test.Datalog.NaiveDatabaseSpec where
import Test.Hspec import Test.Hspec
import Datalog.NaiveDatabase import Datalog.NaiveDatabase
import qualified Datalog.NaiveDatabase as NaiveDatabase import qualified Datalog.NaiveDatabase as NaiveDatabase
import Data.Set qualified as Set
-- checkParse :: String -> Expr -> Expectation import Datalog.DatalogParser
-- checkParse text expectedExpr = import qualified Data.Map as Map
-- parse parseExpr "" text `shouldBe` Right expectedExpr
-- checkEval :: String -> Int -> Expectation
-- checkEval text expectedVal =
-- fmap eval (parse parseExpr "" text) `shouldBe` Right expectedVal
-- getConfig :: IO Config
-- getConfig = do
-- env <- lookupEnv "APP_ENV"
-- let fallbackHosts =
-- [ "localhost:8080"
-- , "127.0.0.1:9000"
-- , "backup.example.com"
-- ]
-- pure Config
-- { port = 3000
-- , hosts = fromMaybe fallbackHosts env
-- , logLevel = Info
-- }
spec :: Spec spec :: Spec
spec = do spec = do
describe "dummy test" $ do describe "dummy test" $ do
it "..." $ do it "..." $ do
1 `shouldBe` (1 :: Int) 1 `shouldBe` (1 :: Int)
it "can accept facts and do basic queries" $ do it "can ingest facts into relations & a universe" $ do
-- let fallbackHosts = let db = NaiveDatabase.withFacts
-- [ "localhost:8080" [ "parent(\"alice\", \"bob\")."
-- , "127.0.0.1:9000" , "parent(\"bob\", \"carol\")." ]
-- , "backup.example.com" constants db `shouldBe`
-- ] (Set.fromList $ Sym <$> ["alice", "bob", "carol"])
-- let names = relations db `shouldBe`
-- [ "Alice" Map.fromList [
-- , "Bob" ("parent",
-- , "Charlie" Relation 2 $ Set.fromList $ [Sym <$> ["bob", "carol"]])
-- , "Daphne" ]
-- , "Eve"
-- ] it "can do basic queries" $ do
-- let twig =
-- [ "xx"
-- , "yy " ]
-- let pig =
-- [ "parent(\"alice\", \"bob\")."
-- , "parent(\"bob\", \"carol\")."
-- ]
let db = NaiveDatabase.withFacts let db = NaiveDatabase.withFacts
[ "parent(\"alice\", \"bob\")." [ "parent(\"alice\", \"bob\")."
, "parent(\"bob\", \"carol\")." ] , "parent(\"bob\", \"carol\")." ]