3import ProjectM36.Relation
5import ProjectM36.DateExamples
6import ProjectM36.TupleSet
7import ProjectM36.Attribute hiding (null, attributeNames)
8import ProjectM36.DataTypes.Primitive
9import ProjectM36.RelationalExpression
10import ProjectM36.Tuple
11import qualified ProjectM36.DatabaseContext as DBC
12import Control.Monad.Trans.Reader
13import qualified ProjectM36.Attribute as A
14import qualified Data.Map as M
15import qualified Data.Vector as V
16import qualified Data.Set as S
21testList = TestList [testRelation "relationTrue" relationTrue, testRelation "relationFalse" relationFalse,
23 testRename1, testRename2,
24 testRelation "suppliers" suppliersRel,
25 testRelation "products" productsRel,
26 testRelation "supplierProducts" supplierProductsRel,
27 testMkRelationFromExprsBadAttrs,
28 testDuplicateAttributes
33 tcounts <- runTestTT testList
34 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
36testRelation :: String -> Relation -> Test
37testRelation testName rel = TestCase $ assertEqual testName relationValidation (Right rel)
39 relationValidation = validateRelation rel
41-- run common relation checks
42validateRelation :: Relation -> Either RelationalError Relation
43validateRelation rel = do
44 _ <- validateAttrNamesMatchTupleAttrNames rel
45 validateAttrTypesMatchTupleAttrTypes rel
47validateAttrNamesMatchTupleAttrNames :: Relation -> Either RelationalError Relation
48validateAttrNamesMatchTupleAttrNames rel@(Relation _ tupSet)
49 | null invalidSet = Right rel
50 | otherwise = Left $ AttributeNamesMismatchError S.empty
52 nameCheck tuple = attributeNames rel == tupleAttributeNameSet tuple
53 invalidSet = filter (not . nameCheck) (asList tupSet)
55validateAttrTypesMatchTupleAttrTypes :: Relation -> Either RelationalError Relation
56validateAttrTypesMatchTupleAttrTypes rel@(Relation attrs tupSet) = foldr (\tuple acc ->
57 if tupleAttributes tuple == attrs && tupleAtomCheck tuple then
60 Left $ TupleAttributeTypeMismatchError A.emptyAttributes
61 ) (Right rel) (asList tupSet)
63 tupleAtomCheck tuple = V.all (== True) (attrChecks tuple)
64 attrChecks tuple = V.map (\attr -> case atomForAttributeName (A.attributeName attr) tuple of
66 Right atom -> (Right $ atomTypeForAtom atom) ==
67 A.atomTypeForAttributeName (A.attributeName attr) attrs) (attributes rel)
70simpleRel = case mkRelation attrs tupleSet of
74 attrs = A.attributesFromList [Attribute "a" TextAtomType, Attribute "b" TextAtomType]
75 tupleSet = RelationTupleSet [mkRelationTuple attrs (V.fromList [TextAtom "spam", TextAtom "spam2"])]
79testRename1 = TestCase $ assertEqual "attribute invalid" (rename "a" "b" relationTrue) (Left $ AttributeNamesMismatchError (S.singleton "a"))
82testRename2 = TestCase $ assertEqual "attribute in use" (rename "b" "a" simpleRel) (Left $ AttributeNameInUseError "a")
85--test tupleset key mismatch failure
86testMkRelation1 :: Test
87testMkRelation1 = TestCase $ assertEqual "key mismatch" expectedError (mkRelation testAttrs testTupSet) -- the error attribute set is empty due to an optimization- the tuple attrs do not match the atoms' types
89 expectedError = Left (AtomTypeMismatchError TextAtomType IntAtomType)
90 testAttrs = A.attributesFromList [Attribute "a" TextAtomType]
91 testTupSet = RelationTupleSet [mkRelationTuple testAttrs $ V.fromList [TextAtom "v"],
92 mkRelationTuple testAttrs $ V.fromList [IntAtom 2]]
94testMkRelationFromExprsBadAttrs :: Test
95testMkRelationFromExprsBadAttrs = TestCase $ do
96 let context = DBC.empty
97 case runReader (evalRelationalExpr (MakeRelationFromExprs (Just [AttributeAndTypeNameExpr "badAttr1" (PrimitiveTypeConstructor "Int" IntAtomType) ()]) [TupleExpr (M.singleton "badAttr2" (NakedAtomExpr (IntAtom 1)))])) (mkRelationalExprState context) of
98 Left err -> assertEqual "tuple type mismatch" (TupleAttributeTypeMismatchError (A.attributesFromList [Attribute "badAttr2" IntAtomType])) err
99 Right _ -> assertFailure "expected tuple type mismatch"
101--creating an empty relation with duplicate attribute names should fail
102testDuplicateAttributes :: Test
103testDuplicateAttributes = TestCase $ do
104 let eRel = mkRelation attrs emptyTupleSet
105 attrs = attributesFromList [Attribute "a" IntAtomType, Attribute "a" TextAtomType]
106 assertEqual "duplicate attribute names" (Left (DuplicateAttributeNamesError (S.singleton "a"))) eRel
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