on cfa7253 help sign in
73 lines 3.0 kB history raw
1import ProjectM36.Client.Simple
2import Test.HUnit
3import System.Exit
4import ProjectM36.Relation
5import qualified ProjectM36.Client as C
6import ProjectM36.DateExamples
7import ProjectM36.DatabaseContext
8import System.IO.Temp
9import System.FilePath
10import ProjectM36.TupleSet
11import ProjectM36.Attribute
12import qualified Data.Vector as V
13import qualified Data.Map as M
15main :: IO ()
16main = do
17 tcounts <- runTestTT testList
18 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
20testList :: Test
21testList = TestList [testSimpleCommitSuccess, testSimpleCommitFailure, testSimpleUpdate]
23assertEither :: (Show a) => IO (Either a b) -> IO b
24assertEither x = do
25 res <- x
26 case res of
27 Left err -> assertFailure (show err) >> undefined
28 Right val -> pure val
30testSimpleCommitSuccess :: Test
31testSimpleCommitSuccess = TestCase $
32 withSystemTempDirectory "m36tempdb" $ \tempdir -> do
33 let connInfo = InProcessConnectionInfo (MinimalPersistence (tempdir </> "db")) emptyNotificationCallback []
34 relExpr = Union (RelationVariable "x" ()) (RelationVariable "y" ())
36 dbconn <- assertEither (simpleConnectProjectM36 connInfo)
37 Right rel <- withTransaction dbconn $ do
39 execute (Assign "x" (ExistingRelation relationTrue))
40 execute (Assign "y" (ExistingRelation relationFalse))
41 query relExpr
43 assertEqual "true/false simple" relationTrue rel
44 -- re-open with standard API and validate that the relvars are available
45 conn <- assertEither $ C.connectProjectM36 connInfo
47 sess <- assertEither $ C.createSessionAtHead conn "master"
48 eRes <- C.executeRelationalExpr sess conn relExpr
49 assertEqual "x and y" (Right relationTrue) eRes
52testSimpleCommitFailure :: Test
53testSimpleCommitFailure = TestCase $ do
54 let failAttrs = attributesFromList [Attribute "fail" IntAtomType]
55 err <- withSystemTempDirectory "m36tempdb" $ \tempdir -> do
56 let connInfo = InProcessConnectionInfo (MinimalPersistence (tempdir </> "db")) emptyNotificationCallback []
57 dbconn <- assertEither (simpleConnectProjectM36 connInfo)
58 withTransaction dbconn $ do
59 execute $ Assign "x" (ExistingRelation relationTrue)
60 --cause error
61 execute $ Assign "x" (MakeStaticRelation failAttrs emptyTupleSet)
62 let expectedErr = Left (RelError (RelationTypeMismatchError V.empty failAttrs))
63 assertEqual "dbc error" expectedErr err
65-- #176 default merge couldn't handle Update
66testSimpleUpdate :: Test
67testSimpleUpdate = TestCase $ do
68 let connInfo = InProcessConnectionInfo NoPersistence emptyNotificationCallback []
69 dbconn <- assertEither (simpleConnectProjectM36 connInfo)
70 assertEither $ withTransaction dbconn $
71 execute $ databaseContextAsDatabaseContextExpr dateExamples
72 assertEither $ withTransaction dbconn $
73 execute $ Update "s" (M.singleton "sname" (C.NakedAtomExpr (C.TextAtom "Blakey"))) (C.AttributeEqualityPredicate "sname" (C.NakedAtomExpr (C.TextAtom "Blake")))