on 095294d help sign in
project-m36/TutorialDInterpreterTest.hs
51 lines 3.4 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
11main = do
12 counts <- runTestTT (TestList tests)
13 if errors counts + failures counts > 0 then exitFailure else exitSuccess
14 where
15 tests = map (\(tutd, expected) -> TestCase $ assertTutdEqual basicDatabaseContext tutd expected) simpleRelTests ++ map (\(tutd, expected) -> TestCase $ assertTutdEqual dateExamples tutd expected) dateExampleRelTests
16 simpleRelTests = [("x:=true", Right relationTrue),
17 ("x:=false", Right relationFalse),
18 ("x:=true union false", Right relationTrue),
19 ("x:=true; x:=false", Right relationFalse),
20 ("x:=relation{a int}", mkRelation simpleAAttributes HS.empty),
21 ("x:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty),
22 ("y:=relation{b int, c int}; x:=y{c}", mkRelation simpleProjectionAttributes HS.empty),
23 ("x:=relation{tuple{a char(\"spam\"), b int(5)}}", mkRelation simpleCAttributes (HS.fromList $ map RelationTuple [M.fromList [("a", StringAtom "spam"),("b",IntAtom 5)]])),
24 ("constraint failc true in false; x:=true", Left $ InclusionDependencyCheckError "failc"),
25 ("x:=true where true", Right relationTrue),
26 ("x:=true where false", Right relationFalse),
27 ("x:=true where true or false", Right relationTrue),
28 ("x:=true where false or false", Right relationFalse),
29 ("x:=true where true and false", Right relationFalse),
30 ("x:=true where true and true", Right relationTrue),
31 ("x:=true=true", Right relationTrue),
32 ("x:=true=false", Right relationFalse)
33 ]
34 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
35 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
36 simpleCAttributes = M.fromList [("a", Attribute "a" StringAtomType), ("b", Attribute "b" IntAtomType)]
37 simpleProjectionAttributes = M.fromList [("c", Attribute "c" IntAtomType)]
38 dateExampleRelTests = [("x:=S where true", Right s),
39 ("x:=S where CITY = \"London\"", restrict (\(RelationTuple tupMap) -> tupMap M.! "CITY" == StringAtom "London") s),
40 ("x:=S where false", Right $ Relation (attributes s) HS.empty),
41 ("a:=S; update a (STATUS:=50); x:=a{STATUS}", mkRelation (M.fromList [("STATUS", Attribute "STATUS" IntAtomType)]) (HS.fromList [RelationTuple $ M.fromList [("STATUS", IntAtom 50)]])),
42 ("x:=S; update x where SNAME=\"Blake\" (CITY:=\"Boston\")", relMap (\(RelationTuple tupMap) -> RelationTuple $ if tupMap M.! "SNAME" == StringAtom "Blake" then M.insert "CITY" (StringAtom "Boston") tupMap else tupMap) s)
43 ]
45testSimple1 = TestCase $ assertTutdEqual basicDatabaseContext "true" (Right relationTrue)
47assertTutdEqual databaseContext tutd expected = assertEqual tutd interpreted expected
48 where
49 interpreted = case interpret databaseContext tutd of
50 (Just err, _) -> Left err
51 (Nothing, context) -> Right $ (relationVariables context) M.! "x"