on 2d313ac help sign in
project-m36/test/TutorialD/Interpreter/DatabaseContextFunctionScript.hs
60 lines 3.8 kB history raw
1import TutorialD.Interpreter.TestBase
2import System.Exit
3import Test.HUnit
4import ProjectM36.Client
5import ProjectM36.Relation
6import qualified Data.Text as T
8main :: IO ()
9main = do
10 tcounts <- runTestTT (TestList [testBasicDBCFunction,
11 testErrorDBCFunction,
12 testExceptionDBCFunction,
13 testDBCFunctionWithAtomArguments
14 ])
15 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
17testBasicDBCFunction :: Test
18testBasicDBCFunction = TestCase $ do
19 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
20 let addfunc = "adddatabasecontextfunction \"addTrue2\" DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\[] ctx -> executeDatabaseContextExpr (Assign \"true2\" (ExistingRelation relationTrue)) ctx) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
21 executeTutorialD sess conn addfunc
22 putStrLn "spam1"
23 executeTutorialD sess conn "execute addTrue2()"
24 putStrLn "spam2"
25 {-
26 let true2Expr = RelationVariable "true2" ()
27 result <- executeRelationalExpr sess conn true2Expr
28 assertEqual "simple atom function equality" (Right relationTrue) result-}
30testErrorDBCFunction :: Test
31testErrorDBCFunction = TestCase $ do
32 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
33 let addfunc = "adddatabasecontextfunction \"retErr\" DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\[] ctx -> Left (DatabaseContextFunctionUserError \"err\")) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
34 executeTutorialD sess conn addfunc
35 expectTutorialDErr sess conn (T.isPrefixOf "DatabaseContextFunctionUserError (DatabaseContextFunctionUserError \"err\")") "execute retErr()"
37testExceptionDBCFunction :: Test
38testExceptionDBCFunction = TestCase $ do
39 -- throw an error, make sure it is caught- it might not be caught, for example, if the function is not forced
40 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
41 let addfunc = "adddatabasecontextfunction \"bomb\" DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\[] _ -> error \"boom\") :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
42 executeTutorialD sess conn addfunc
43 expectTutorialDErr sess conn (\err -> "UnhandledExceptionError" `T.isPrefixOf` err) "execute bomb()"
46testDBCFunctionWithAtomArguments :: Test
47testDBCFunctionWithAtomArguments = TestCase $ do
48 --test function with creation of a relvar with some arguments
49 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
50 let addfunc = "adddatabasecontextfunction \"multiArgFunc\" Integer -> Text -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\(age:name:_) ctx -> executeDatabaseContextExpr (Assign \"person\" (MakeRelationFromExprs Nothing [TupleExpr (fromList [(\"name\", NakedAtomExpr name), (\"age\", NakedAtomExpr age)])])) ctx) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
51 executeTutorialD sess conn addfunc
52 executeTutorialD sess conn "execute multiArgFunc(30,\"Steve\")"
53 result <- executeRelationalExpr sess conn (RelationVariable "person" ())
54 let expectedPerson = mkRelationFromList (attributesFromList [Attribute "name" TextAtomType,
55 Attribute "age" IntegerAtomType]) [
56 [TextAtom "Steve", IntegerAtom 30]]
57 assertEqual "person relation" expectedPerson result
58 expectTutorialDErr sess conn (T.isPrefixOf "AtomTypeMismatchError") "execute multiArgFunc(\"fail\", \"fail\")"