on a3de6fd help sign in
project-m36/test/TutorialD/Interpreter/DatabaseContextFunctionScript.hs
57 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 executeTutorialD sess conn "execute addTrue2()"
23 let true2Expr = RelationVariable "true2" ()
24 result <- executeRelationalExpr sess conn true2Expr
25 assertEqual "simple atom function equality" (Right relationTrue) result
27testErrorDBCFunction :: Test
28testErrorDBCFunction = TestCase $ do
29 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
30 let addfunc = "adddatabasecontextfunction \"retErr\" DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\[] ctx -> Left (DatabaseContextFunctionUserError \"err\")) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
31 executeTutorialD sess conn addfunc
32 expectTutorialDErr sess conn (T.isPrefixOf "DatabaseContextFunctionUserError (DatabaseContextFunctionUserError \"err\")") "execute retErr()"
34testExceptionDBCFunction :: Test
35testExceptionDBCFunction = TestCase $ do
36 -- throw an error, make sure it is caught- it might not be caught, for example, if the function is not forced
37 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
38 let addfunc = "adddatabasecontextfunction \"bomb\" DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext \"\"\"(\\[] _ -> error \"boom\") :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext\"\"\""
39 executeTutorialD sess conn addfunc
40 expectTutorialDErr sess conn (\err -> "UnhandledExceptionError" `T.isPrefixOf` err) "execute bomb()"
43testDBCFunctionWithAtomArguments :: Test
44testDBCFunctionWithAtomArguments = TestCase $ do
45 --test function with creation of a relvar with some arguments
46 (sess, conn) <- dateExamplesConnection emptyNotificationCallback
47 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\"\"\""
48 executeTutorialD sess conn addfunc
49 executeTutorialD sess conn "execute multiArgFunc(30,\"Steve\")"
50 result <- executeRelationalExpr sess conn (RelationVariable "person" ())
51 let expectedPerson = mkRelationFromList (attributesFromList [Attribute "name" TextAtomType,
52 Attribute "age" IntegerAtomType]) [
53 [TextAtom "Steve", IntegerAtom 30]]
54 assertEqual "person relation" expectedPerson result
55 expectTutorialDErr sess conn (T.isPrefixOf "AtomTypeMismatchError") "execute multiArgFunc(\"fail\", \"fail\")"