1{-# LANGUAGE OverloadedStrings #-}
4import ProjectM36.Relation
8import qualified ProjectM36.Attribute as A
9import qualified Data.HashSet as HS
10import qualified Data.Vector as V
11import qualified Data.Set as S
13import ProjectM36.RelationalExpression
16testList = TestList [testRelation "relationTrue" relationTrue, testRelation "relationFalse" relationFalse,
18 testRename1, testRename2,
19 testRelation "suppliers" suppliersRel,
20 testRelation "products" productsRel,
21 testRelation "supplierProducts" supplierProductsRel]
25 tcounts <- runTestTT testList
26 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
28testRelation :: String -> Relation -> Test
29testRelation testName rel = TestCase $ assertEqual testName relationValidation (Right rel)
31 relationValidation = validateRelation rel
33-- run common relation checks
34validateRelation :: Relation -> Either RelationalError Relation
35validateRelation rel = do
36 _ <- validateAttrNamesMatchTupleAttrNames rel
37 validateAttrTypesMatchTupleAttrTypes rel
39validateAttrNamesMatchTupleAttrNames :: Relation -> Either RelationalError Relation
40validateAttrNamesMatchTupleAttrNames rel@(Relation _ tupMapSet)
41 | HS.null invalidSet = Right rel
42 | otherwise = Left $ AttributeNamesMismatchError S.empty
44 nameCheck tuple = attributeNames rel == tupleAttributeNameSet tuple
45 invalidSet = HS.filter (not . nameCheck) tupMapSet
47validateAttrTypesMatchTupleAttrTypes :: Relation -> Either RelationalError Relation
48validateAttrTypesMatchTupleAttrTypes rel@(Relation attrs tupSet) = HS.foldr (\tuple acc ->
49 if (tupleAttributes tuple) == attrs && tupleAtomCheck tuple then
52 Left $ TupleAttributeTypeMismatchError A.emptyAttributes
55 tupleAtomCheck tuple = V.all (== True) (attrChecks tuple)
56 attrChecks tuple = V.map (\attr -> case atomForAttributeName (A.attributeName attr) tuple of
58 Right atom -> (Right $ atomTypeForAtom atom) ==
59 A.atomTypeForAttributeName (A.attributeName attr) attrs) (attributes rel)
62simpleRel = case mkRelation attrs tupleSet of
66 attrs = A.attributesFromList [Attribute "a" StringAtomType, Attribute "b" StringAtomType]
67 tupleSet = HS.singleton $ mkRelationTuple attrs (V.fromList [StringAtom "spam", StringAtom "spam2"])
71testRename1 = TestCase $ assertEqual "attribute invalid" (rename "a" "b" relationTrue) (Left $ AttributeNamesMismatchError (S.singleton "a"))
74testRename2 = TestCase $ assertEqual "attribute in use" (rename "b" "a" simpleRel) (Left $ AttributeNameInUseError "a")
77--test tupleset key mismatch failure
78testMkRelation1 :: Test
79testMkRelation1 = TestCase $ assertEqual "key mismatch" (Left $ TupleAttributeTypeMismatchError A.emptyAttributes) (mkRelation testAttrs testTupSet) -- the error attribute set is empty due to an optimization- the tuple attrs do not match the atoms' types
81 testAttrs = A.attributesFromList [Attribute "a" StringAtomType]
82 testTupSet = HS.fromList [mkRelationTuple testAttrs $ V.fromList [StringAtom "v"],
83 mkRelationTuple testAttrs $ V.fromList [IntAtom 2]]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list