6import qualified Data.Set as S
7import qualified Data.Map as M
8import qualified Data.HashSet as HS
13testList = TestList [testRelation relationTrue, testRelation relationFalse,
14 testRename1, testRename2]
16 counts <- runTestTT testList
17 if errors counts + failures counts > 0 then exitFailure else exitSuccess
19testRelation :: Relation -> Test
20testRelation rel = TestCase $ assertEqual "nope" relationValidation (Right rel)
22 relationValidation = validateRelation rel
24-- run common relation checks
25validateRelation :: Relation -> Either RelationalError Relation
26validateRelation rel = do
27 validateAttrNamesMatchTupleAttrNames rel
28 validateAttrTypesMatchTupleAttrTypes rel
30validateAttrNamesMatchTupleAttrNames :: Relation -> Either RelationalError Relation
31validateAttrNamesMatchTupleAttrNames rel@(Relation _ tupMapSet)
32 | HS.null invalidSet = Right rel
33 | otherwise = Left (RelationalError 1 "Tuple attributes do not match relation attributes")
35 nameCheck (RelationTuple tupMap) = attributeNames rel == M.keysSet tupMap
36 relAttrNames = attributeNames rel
37 invalidSet = HS.filter (not . nameCheck) tupMapSet
40validateAttrTypesMatchTupleAttrTypes :: Relation -> Either RelationalError Relation
41validateAttrTypesMatchTupleAttrTypes rel@(Relation _ tupMapSet)
42 | HS.null invalidSet = Right rel
43 | otherwise = Left (RelationalError 1 "Tuple types do not match relation attribute types")
45 tupleTypeCheck (RelationTuple tupMap) = M.null $ M.filterWithKey attrTypeCheck tupMap
46 attrTypeCheck attrName atomValue = case attributeTypeForName attrName rel of
47 Just t -> atomValueType atomValue == t
49 invalidSet = HS.filter (not . tupleTypeCheck) tupMapSet
51simpleRel = case mkRelation attrs tupleSet of
55 attrs = M.fromList [("a", Attribute "a" StringAtomType), ("b", Attribute "b" StringAtomType)]
56 tupleSet = HS.fromList $ mkRelationTuples attrs [
57 M.fromList [("a", StringAtom "spam"), ("b", StringAtom "spam2")]
62testRename1 = TestCase $ assertEqual "attribute invalid" (rename "a" "b" relationTrue) (Left $ RelationalError 1 "No such attribute")
65testRename2 = TestCase $ assertEqual "attribute in use" (rename "b" "a" simpleRel) (Left $ RelationalError 1 "Attribute \"a\" already in use.")
68--test tupleset key mismatch failure
69testMkRelation1 :: Test
70testMkRelation1 = TestCase $ assertEqual "key mismatch" (mkRelation testAttrs testTupSet) (Left $ RelationalError 1 "Tuple values not of the same type for same keys")
72 testAttrs = M.singleton "a" (Attribute "a" StringAtomType)
73 testTupSet = HS.fromList [(RelationTuple (M.singleton "a" $ StringAtom "v")),
74 RelationTuple (M.singleton "a" $ 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