@@ -22,8 +22,6 @@ import System.Exit
2222import Data.Maybe (isJust)
2323import qualified Data.Vector as V
2424import Data.Text.Encoding as TE
25−import Data.Typeable (Proxy(..))
26−import Data.Text (Text)
2725import Control.Concurrent.STM
2826import Control.Concurrent
2927import qualified STMContainers.Map as STMMap
@@ -69,46 +67,48 @@ main = do
6967 simpleBAttributes = A.attributesFromList [Attribute "d" intAtomType]
7068 simpleCAttributes = A.attributesFromList [Attribute "a" textAtomType, Attribute "b" intAtomType]
7169 simpleDAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" intAtomType]
72− simpleMaybeTextAttributes = A.attributesFromList [Attribute "a" $ atomTypeForProxy (Proxy :: Proxy (Maybe Text))]
73− simpleMaybeIntAttributes = A.attributesFromList [Attribute "a" $ atomTypeForProxy (Proxy :: Proxy (Maybe Int))]
70+ maybeTextAtomType = ConstructedAtomType "Maybe" (M.singleton "a" textAtomType)
71+ maybeIntAtomType = ConstructedAtomType "Maybe" (M.singleton "a" intAtomType)
72+ simpleMaybeTextAttributes = A.attributesFromList [Attribute "a" maybeTextAtomType]
73+ simpleMaybeIntAttributes = A.attributesFromList [Attribute "a" maybeIntAtomType]
7474 simpleProjectionAttributes = A.attributesFromList [Attribute "c" intAtomType]
7575 nestedRelationAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" (RelationAtomType $ A.attributesFromList [Attribute "a" intAtomType])]
7676 extendTestAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" $ RelationAtomType (attributes suppliersRel)]
7777 byteStringAttributes = A.attributesFromList [Attribute "y" byteStringAtomType]
7878 groupCountAttrs = A.attributesFromList [Attribute "z" intAtomType]
79− minMaxAttrs = A.attributesFromList [Attribute "S#" textAtomType, Attribute "z" intAtomType]
80− dateExampleRelTests = [("x:=S where true", Right suppliersRel),
81− ("x:=S where CITY = \"London\"", restrict (\tuple -> atomForAttributeName "CITY" tuple == (Right $ textAtom "London")) suppliersRel),
82− ("x:=S where false", Right $ Relation (attributes suppliersRel) emptyTupleSet),
83− ("x:=P where COLOR=\"Blue\" and CITY=\"Paris\"", mkRelationFromList (attributes productsRel) [[textAtom "P5", textAtom "Cam", textAtom "Blue", intAtom 12, textAtom "Paris"]]),
84− ("a:=S; update a (STATUS:=50); x:=a{STATUS}", mkRelation (A.attributesFromList [Attribute "STATUS" intAtomType]) (RelationTupleSet [mkRelationTuple (A.attributesFromList [Attribute "STATUS" intAtomType]) (V.fromList [intAtom 50])])),
79+ minMaxAttrs = A.attributesFromList [Attribute "s#" textAtomType, Attribute "z" intAtomType]
80+ dateExampleRelTests = [("x:=s where true", Right suppliersRel),
81+ ("x:=s where city = \"London\"", restrict (\tuple -> atomForAttributeName "city" tuple == (Right $ textAtom "London")) suppliersRel),
82+ ("x:=s where false", Right $ Relation (attributes suppliersRel) emptyTupleSet),
83+ ("x:=p where color=\"Blue\" and city=\"Paris\"", mkRelationFromList (attributes productsRel) [[textAtom "P5", textAtom "Cam", textAtom "Blue", intAtom 12, textAtom "Paris"]]),
84+ ("a:=s; update a (status:=50); x:=a{status}", mkRelation (A.attributesFromList [Attribute "status" intAtomType]) (RelationTupleSet [mkRelationTuple (A.attributesFromList [Attribute "status" intAtomType]) (V.fromList [intAtom 50])])),
8585 --atom function tests
86− ("x:=((S : {STATUS2 := add(10,@STATUS)}) where STATUS2=add(10,@STATUS)){CITY,S#,SNAME,STATUS}", Right suppliersRel),
87− ("x:=S; update x where SNAME=\"Blake\" (CITY:=\"Boston\")", relMap (\tuple -> if atomForAttributeName "SNAME" tuple == (Right $ textAtom "Blake") then updateTuple (M.singleton "CITY" (textAtom "Boston")) tuple else tuple) suppliersRel),
88− ("x:=relation{tuple{a 5}} : {b:=S}", mkRelation extendTestAttributes (RelationTupleSet [mkRelationTuple extendTestAttributes (V.fromList [intAtom 5, Atom suppliersRel])])),
86+ ("x:=((s : {status2 := add(10,@status)}) where status2=add(10,@status)){city,s#,sname,status}", Right suppliersRel),
87+ ("x:=s; update x where sname=\"Blake\" (city:=\"Boston\")", relMap (\tuple -> if atomForAttributeName "sname" tuple == (Right $ textAtom "Blake") then updateTuple (M.singleton "city" (textAtom "Boston")) tuple else tuple) suppliersRel),
88+ ("x:=relation{tuple{a 5}} : {b:=s}", mkRelation extendTestAttributes (RelationTupleSet [mkRelationTuple extendTestAttributes (V.fromList [intAtom 5, Atom suppliersRel])])),
8989 --relatom function tests
90− ("x:=((S group ({CITY} as y)):{z:=count(@y)}){z}", mkRelation groupCountAttrs (RelationTupleSet [mkRelationTuple groupCountAttrs (V.singleton $ intAtom 1)])),
91− ("x:=(SP group ({S#} as y)) ungroup y", Right supplierProductsRel),
92− ("x:=((SP{S#,QTY}) group ({QTY} as x):{z:=max(@x)}){S#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 400), ("S2", 400), ("S3", 200), ("S4", 400)])),
93− ("x:=((SP{S#,QTY}) group ({QTY} as x):{z:=min(@x)}){S#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 100), ("S2", 300), ("S3", 200), ("S4", 200)])),
94− ("x:=((SP{S#,QTY}) group ({QTY} as x):{z:=sum(@x)}){S#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 1000), ("S2", 700), ("S3", 200), ("S4", 900)])),
90+ ("x:=((s group ({city} as y)):{z:=count(@y)}){z}", mkRelation groupCountAttrs (RelationTupleSet [mkRelationTuple groupCountAttrs (V.singleton $ intAtom 1)])),
91+ ("x:=(sp group ({s#} as y)) ungroup y", Right supplierProductsRel),
92+ ("x:=((sp{s#,qty}) group ({qty} as x):{z:=max(@x)}){s#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 400), ("S2", 400), ("S3", 200), ("S4", 400)])),
93+ ("x:=((sp{s#,qty}) group ({qty} as x):{z:=min(@x)}){s#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 100), ("S2", 300), ("S3", 200), ("S4", 200)])),
94+ ("x:=((sp{s#,qty}) group ({qty} as x):{z:=sum(@x)}){s#,z}", mkRelationFromList minMaxAttrs (map (\(s,i) -> [textAtom s,intAtom i]) [("S1", 1000), ("S2", 700), ("S3", 200), ("S4", 900)])),
9595 --boolean function restriction
96− ("x:=S where ^lt(@STATUS,20)", mkRelationFromList (attributes suppliersRel) [[textAtom "S2", textAtom "Jones", intAtom 10, textAtom "Paris"]]),
97− ("x:=S where ^gt(@STATUS,20)", mkRelationFromList (attributes suppliersRel) [[textAtom "S3", textAtom "Blake", intAtom 30, textAtom "Paris"],
96+ ("x:=s where ^lt(@status,20)", mkRelationFromList (attributes suppliersRel) [[textAtom "S2", textAtom "Jones", intAtom 10, textAtom "Paris"]]),
97+ ("x:=s where ^gt(@status,20)", mkRelationFromList (attributes suppliersRel) [[textAtom "S3", textAtom "Blake", intAtom 30, textAtom "Paris"],
9898 [textAtom "S5", textAtom "Adams", intAtom 30, textAtom "Athens"]]),
99− ("x:=S where ^sum(@STATUS)", Left $ AtomTypeMismatchError intAtomType boolAtomType),
100− ("x:=S where ^not(gte(@STATUS,20))", mkRelationFromList (attributes suppliersRel) [[textAtom "S2", textAtom "Jones", intAtom 10, textAtom "Paris"]]),
99+ ("x:=s where ^sum(@status)", Left $ AtomTypeMismatchError intAtomType boolAtomType),
100+ ("x:=s where ^not(gte(@status,20))", mkRelationFromList (attributes suppliersRel) [[textAtom "S2", textAtom "Jones", intAtom 10, textAtom "Paris"]]),
101101 --test "all but" attribute inversion syntax
102− ("x:=S{all but S#} = S{CITY,SNAME,STATUS}", Right $ relationTrue),
102+ ("x:=s{all but s#} = s{city,sname,status}", Right $ relationTrue),
103103 --test key syntax
104− ("x:=S; key testconstraint {S#,CITY} x; insert x relation{tuple{CITY \"London\", S# \"S1\", SNAME \"gonk\", STATUS 50}}", Left (InclusionDependencyCheckError "testconstraint")),
105− ("y:=S; key testconstraint {S#} y; insert y relation{tuple{CITY \"London\", S# \"S6\", SNAME \"gonk\", STATUS 50}}; x:=y{S#} = S{S#} union relation{tuple{S# \"S6\"}}", Right $ relationTrue),
104+ ("x:=s; key testconstraint {s#,city} x; insert x relation{tuple{city \"London\", s# \"S1\", sname \"gonk\", status 50}}", Left (InclusionDependencyCheckError "testconstraint")),
105+ ("y:=s; key testconstraint {s#} y; insert y relation{tuple{city \"London\", s# \"S6\", sname \"gonk\", status 50}}; x:=y{s#} = s{s#} union relation{tuple{s# \"S6\"}}", Right $ relationTrue),
106106 --test binary bytestring data type
107− ("x:=relation{tuple{y \"dGVzdGRhdGE=\"::bytestring}}", mkRelationFromList byteStringAttributes [[Atom (TE.encodeUtf8 "testdata")]]),
107+ ("x:=relation{tuple{y makeByteString(\"dGVzdGRhdGE=\")}}", mkRelationFromList byteStringAttributes [[Atom (TE.encodeUtf8 "testdata")]]),
108108 --test Maybe Text
109− ("x:=relation{tuple{a Just \"spam\"}}", mkRelationFromList simpleMaybeTextAttributes [[ConstructedAtom "Just" (ConstructedAtomType "Maybe" (M.singleton "a" textAtomType)) [textAtom "spam"]]]),
109+ ("x:=relation{tuple{a Just \"spam\"}}", mkRelationFromList simpleMaybeTextAttributes [[ConstructedAtom "Just" maybeTextAtomType [textAtom "spam"]]]),
110110 --test Maybe Int
111− ("x:=relation{tuple{a Just 3}}", mkRelationFromList simpleMaybeIntAttributes [[Atom (Just 3 :: Maybe Int)]])
111+ ("x:=relation{tuple{a Just 3}}", mkRelationFromList simpleMaybeIntAttributes [[ConstructedAtom "Just" maybeIntAtomType [intAtom 3]]])
112112 ]
113113
114114assertTutdEqual :: DatabaseContext -> Either RelationalError Relation -> String -> Assertion
@@ -130,7 +130,7 @@ transactionGraphBasicTest = TestCase $ do
130130transactionGraphAddCommitTest :: Test
131131transactionGraphAddCommitTest = TestCase $ do
132132 (sessionId, dbconn) <- dateExamplesConnection emptyNotificationCallback
133− case parseTutorialD "x:=S" of
133+ case parseTutorialD "x:=s" of
134134 Left err -> assertFailure (show err)
135135 Right parsed -> do
136136 result <- evalTutorialD sessionId dbconn parsed
@@ -148,7 +148,7 @@ transactionRollbackTest :: Test
148148transactionRollbackTest = TestCase $ do
149149 (sessionId, dbconn) <- dateExamplesConnection emptyNotificationCallback
150150 graph <- transactionGraph dbconn
151− maybeErr <- executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "S"))
151+ maybeErr <- executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "s"))
152152 case maybeErr of
153153 Just err -> assertFailure (show err)
154154 Nothing -> do
@@ -163,7 +163,7 @@ transactionJumpTest :: Test
163163transactionJumpTest = TestCase $ do
164164 (sessionId, dbconn) <- dateExamplesConnection emptyNotificationCallback
165165 (DisconnectedTransaction firstUUID _) <- disconnectedTransaction sessionId dbconn
166− maybeErr <- executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "S"))
166+ maybeErr <- executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "s"))
167167 case maybeErr of
168168 Just err -> assertFailure (show err)
169169 Nothing -> do
@@ -186,24 +186,24 @@ transactionBranchTest :: Test
186186transactionBranchTest = TestCase $ do
187187 (sessionId, dbconn) <- dateExamplesConnection emptyNotificationCallback
188188 mapM_ (\x -> x >>= maybeFail) [executeGraphExpr sessionId dbconn (Branch "test"),
189− executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "S")),
189+ executeDatabaseContextExpr sessionId dbconn (Assign "x" (RelationVariable "s")),
190190 commit sessionId dbconn,
191191 executeGraphExpr sessionId dbconn (JumpToHead "master"),
192− executeDatabaseContextExpr sessionId dbconn (Assign "y" (RelationVariable "S"))
192+ executeDatabaseContextExpr sessionId dbconn (Assign "y" (RelationVariable "s"))
193193 ]
194194 graph <- transactionGraph dbconn
195195 assertBool "master branch exists" $ isJust (transactionForHead "master" graph)
196196 assertBool "test branch exists" $ isJust (transactionForHead "test" graph)
197197
198198simpleJoinTest :: Test
199−simpleJoinTest = TestCase $ assertTutdEqual dateExamples joinedRel "x:=S join SP"
199+simpleJoinTest = TestCase $ assertTutdEqual dateExamples joinedRel "x:=s join sp"
200200 where
201− attrs = A.attributesFromList [Attribute "CITY" textAtomType,
202− Attribute "QTY" intAtomType,
203− Attribute "P#" textAtomType,
204− Attribute "S#" textAtomType,
205− Attribute "SNAME" textAtomType,
206− Attribute "STATUS" intAtomType]
201+ attrs = A.attributesFromList [Attribute "city" textAtomType,
202+ Attribute "qty" intAtomType,
203+ Attribute "p#" textAtomType,
204+ Attribute "s#" textAtomType,
205+ Attribute "sname" textAtomType,
206+ Attribute "status" intAtomType]
207207 joinedRel = mkRelationFromList attrs [[textAtom "London", intAtom 100, textAtom "P6", textAtom "S1", textAtom "Smith", intAtom 20],
208208 [textAtom "London", intAtom 400, textAtom "P3", textAtom "S1", textAtom "Smith", intAtom 20],
209209 [textAtom "London", intAtom 400, textAtom "P5", textAtom "S4", textAtom "Clark", intAtom 20],
@@ -260,7 +260,7 @@ testNotification = TestCase $ do
260260 let notifCallback mvar = \_ _ -> putMVar mvar ()
261261 relvarx = RelationVariable "x"
262262 (sess, conn) <- dateExamplesConnection (notifCallback notifmvar)
263− let check' x = x >>= maybe (pure ()) (\err -> assertFailure (show err))
263+ let check' x = x >>= maybe (pure ()) (\err -> assertFailure (show err))
264264 check' $ executeDatabaseContextExpr sess conn (Assign "x" (ExistingRelation relationTrue))
265265 check' $ executeDatabaseContextExpr sess conn (AddNotification "test notification" relvarx relvarx)
266266 check' $ commit sess conn