on 2f6b871 help sign in
project-m36/TutorialDInterpreterTest.hs
49 lines 3.1 kB history raw
1import TutorialDInterpreter
2import Test.HUnit
3import RelationExpr
4import Relation
5import RelationalError
6import RelationType
7import qualified Data.HashSet as HS
8import qualified Data.Map as M
9import System.Exit
11import Debug.Trace
13main = do
14 counts <- runTestTT (TestList tests)
15 if errors counts + failures counts > 0 then exitFailure else exitSuccess
16 where
17 tests = map (\(tutd, expected) -> TestCase $ assertTutdEqual basicDatabaseContext tutd expected) simpleRelTests ++ map (\(tutd, expected) -> TestCase $ assertTutdEqual dateExamples tutd expected) dateExampleRelTests
18 simpleRelTests = [("x:=true", Right relationTrue),
19 ("x:=false", Right relationFalse),
20 ("x:=true union false", Right relationTrue),
21 ("x:=true; x:=false", Right relationFalse),
22 ("x:=relation{a int}", mkRelation simpleAAttributes HS.empty),
23 ("x:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty),
24 ("y:=relation{b int, c int}; x:=y{c}", mkRelation simpleProjectionAttributes HS.empty),
25 ("constraint failc true in false; x:=true", Left $ InclusionDependencyCheckError "failc"),
26 ("x:=true where true", Right relationTrue),
27 ("x:=true where false", Right relationFalse),
28 ("x:=true where true or false", Right relationTrue),
29 ("x:=true where false or false", Right relationFalse),
30 ("x:=true where true and false", Right relationFalse),
31 ("x:=true where true and true", Right relationTrue)
32 ]
33 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
34 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
35 simpleProjectionAttributes = M.fromList [("c", Attribute "c" IntAtomType)]
36 dateExampleRelTests = [("x:=S where true", Right s),
37 ("x:=S where CITY = \"London\"", restrict (\(RelationTuple tupMap) -> tupMap M.! "CITY" == StringAtom "London") s),
38 ("x:=S where false", Right $ Relation (attributes s) HS.empty),
39 ("a:=S; update a (STATUS:=50); x:=a{STATUS}", mkRelation (M.fromList [("STATUS", Attribute "STATUS" IntAtomType)]) (HS.fromList [RelationTuple $ M.fromList [("STATUS", IntAtom 50)]])),
40 ("x:=S; update x where SNAME=\"Blake\" (CITY:=\"Boston\")", relMap (\(RelationTuple tupMap) -> RelationTuple $ if (traceShowId tupMap) M.! "SNAME" == StringAtom "Blake" then M.insert "CITY" (StringAtom "Boston") tupMap else tupMap) s)
41 ]
43testSimple1 = TestCase $ assertTutdEqual basicDatabaseContext "true" (Right relationTrue)
45assertTutdEqual databaseContext tutd expected = assertEqual tutd interpreted expected
46 where
47 interpreted = case interpret databaseContext tutd of
48 (Just err, _) -> Left err
49 (Nothing, context) -> Right $ (relationVariables context) M.! "x"