on master help sign in
add testIsoUnion
commit3d1960346e3cca9388758dff900134966eb15440 authorAgentM <agentm@themactionfaction.com> dateSun 6 Nov 2016, 21:35 (10 years ago) parent6ba123a
1 file changed+40−7
test/IsomorphicSchema.hs+40−7
test/IsomorphicSchema.hs+40−7
@@ -10,7 +10,7 @@ import qualified Data.Map as M
1010import Control.Monad.State
1111
1212testList :: Test
13−testList = TestList [testIsoRelVarName, testIsoRestrict]
13+testList = TestList [testIsoRename, testIsoRestrict, testIsoUnion]
1414
1515main :: IO ()
1616main = do
@@ -22,15 +22,20 @@ assertEither x = case x of
2222 Left err -> assertFailure (show err) >> undefined
2323 Right val -> pure val
2424
25−testIsoRelVarName :: Test
26−testIsoRelVarName = TestCase $ do
25+-- create some potential schemas which should not be accepted
26+testSchemaValidation :: Test
27+testSchemaValidation = TestCase $ do
28+ undefined
29+
30+testIsoRename :: Test
31+testIsoRename = TestCase $ do
2732 -- create a schema with two relvars and rename one while the other remains the same in the isomorphic schema
2833 let schemaA = DBC.empty {
2934 relationVariables = M.fromList [("employee", relationTrue),
3035 ("department", relationFalse)]
3136 }
32− isomorphsAtoB = [isomorphRename "emp" "employee",
33− isomorphRename "department" "department"]
37+ isomorphsAtoB = [IsoRename "emp" "employee",
38+ IsoRename "department" "department"]
3439 unionExpr = Union (RelationVariable "emp" ()) (RelationVariable "department" ())
3540 relExpr <- assertEither (applyRelationalExprSchemaIsomorphs isomorphsAtoB unionExpr)
3641 let relResult = evalState (evalRelationalExpr relExpr) schemaA
@@ -54,7 +59,7 @@ testIsoRestrict = TestCase $ do
5459 ("boss", bossRel)]
5560 }
5661
57− isomorphsAtoB = [IsoRestrict "employee" predicate (Just "boss", Just "nonboss")]
62+ isomorphsAtoB = [IsoRestrict "employee" predicate ("boss", "nonboss")]
5863 bossq = RelationVariable "boss" ()
5964 nonbossq = RelationVariable "nonboss" ()
6065 employeeq = RelationVariable "employee" ()
@@ -62,4 +67,32 @@ testIsoRestrict = TestCase $ do
6267 empExpr <- assertEither (applyRelationalExprSchemaIsomorphs isomorphsAtoB employeeq)
6368 let empResult = evalState (evalRelationalExpr empExpr) schemaA
6469 unionResult = evalState (evalRelationalExpr unionq) schemaA
65− assertEqual "boss/nonboss isorestrict" unionResult empResult
70+ assertEqual "boss/nonboss isorestrict" unionResult empResult
71+ -- TODO: add databaseContextExprs
72+
73+testIsoUnion :: Test
74+testIsoUnion = TestCase $ do
75+ --create motors relation which is split into low-power (<50 horsepower) and high-power (>=50 horsepower) motors
76+ --the schema is contains the split relvars
77+ motorsRel <- assertEither $ mkRelationFromList (A.attributesFromList [Attribute "name" TextAtomType,
78+ Attribute "power" IntAtomType])
79+ [[TextAtom "Puny", IntAtom 10],
80+ [TextAtom "Scooter", IntAtom 49],
81+ [TextAtom "Auto", IntAtom 200],
82+ [TextAtom "Tractor", IntAtom 500]]
83+ let baseSchema = DBC.basicDatabaseContext {
84+ relationVariables = M.singleton "motor" motorsRel
85+ }
86+ splitPredicate = AtomExprPredicate (FunctionAtomExpr "lt" [AttributeAtomExpr "power", NakedAtomExpr (IntAtom 50)] ())
87+ splitIsomorphs = [IsoUnion ("lowpower", "highpower") splitPredicate "motor",
88+ IsoRename "true" "true",
89+ IsoRename "false" "false"]
90+ lowmotors = evalState (evalRelationalExpr (Restrict splitPredicate (RelationVariable "motor" ()))) baseSchema
91+ highmotors = evalState (evalRelationalExpr (Restrict (NotPredicate splitPredicate) (RelationVariable "motor" ()))) baseSchema
92+ relResult expr = evalState (evalRelationalExpr expr) baseSchema
93+ lowpowerExpr <- assertEither (processRelationalExprInSchema (Schema splitIsomorphs) (RelationVariable "lowpower" ()))
94+ lowpowerRel <- assertEither (relResult lowpowerExpr)
95+ highpowerExpr <- assertEither (processRelationalExprInSchema (Schema splitIsomorphs) (RelationVariable "highpower" ()))
96+ highpowerRel <- assertEither (relResult highpowerExpr)
97+ assertEqual "lowpower relation difference" (Right lowpowerRel) lowmotors
98+ assertEqual "highpower relation difference" (Right highpowerRel) highmotors