3import ProjectM36.Relation
5import ProjectM36.DateExamples
6import ProjectM36.DataTypes.Primitive
7import ProjectM36.RelationalExpression
9import qualified ProjectM36.DatabaseContext as DBC
10import Control.Monad.State
11import qualified ProjectM36.Attribute as A
12import qualified Data.Map as M
13import qualified Data.Vector as V
14import qualified Data.Set as S
19testList = TestList [testRelation "relationTrue" relationTrue, testRelation "relationFalse" relationFalse,
21 testRename1, testRename2,
22 testRelation "suppliers" suppliersRel,
23 testRelation "products" productsRel,
24 testRelation "supplierProducts" supplierProductsRel,
25 testMkRelationFromExprsBadAttrs]
29 tcounts <- runTestTT testList
30 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
32testRelation :: String -> Relation -> Test
33testRelation testName rel = TestCase $ assertEqual testName relationValidation (Right rel)
35 relationValidation = validateRelation rel
37-- run common relation checks
38validateRelation :: Relation -> Either RelationalError Relation
39validateRelation rel = do
40 _ <- validateAttrNamesMatchTupleAttrNames rel
41 validateAttrTypesMatchTupleAttrTypes rel
43validateAttrNamesMatchTupleAttrNames :: Relation -> Either RelationalError Relation
44validateAttrNamesMatchTupleAttrNames rel@(Relation _ tupSet)
45 | null invalidSet = Right rel
46 | otherwise = Left $ AttributeNamesMismatchError S.empty
48 nameCheck tuple = attributeNames rel == tupleAttributeNameSet tuple
49 invalidSet = filter (not . nameCheck) (asList tupSet)
51validateAttrTypesMatchTupleAttrTypes :: Relation -> Either RelationalError Relation
52validateAttrTypesMatchTupleAttrTypes rel@(Relation attrs tupSet) = foldr (\tuple acc ->
53 if (tupleAttributes tuple) == attrs && tupleAtomCheck tuple then
56 Left $ TupleAttributeTypeMismatchError A.emptyAttributes
57 ) (Right rel) (asList tupSet)
59 tupleAtomCheck tuple = V.all (== True) (attrChecks tuple)
60 attrChecks tuple = V.map (\attr -> case atomForAttributeName (A.attributeName attr) tuple of
62 Right atom -> (Right $ atomTypeForAtom atom) ==
63 A.atomTypeForAttributeName (A.attributeName attr) attrs) (attributes rel)
66simpleRel = case mkRelation attrs tupleSet of
70 attrs = A.attributesFromList [Attribute "a" TextAtomType, Attribute "b" TextAtomType]
71 tupleSet = RelationTupleSet [mkRelationTuple attrs (V.fromList [TextAtom "spam", TextAtom "spam2"])]
75testRename1 = TestCase $ assertEqual "attribute invalid" (rename "a" "b" relationTrue) (Left $ AttributeNamesMismatchError (S.singleton "a"))
78testRename2 = TestCase $ assertEqual "attribute in use" (rename "b" "a" simpleRel) (Left $ AttributeNameInUseError "a")
81--test tupleset key mismatch failure
82testMkRelation1 :: Test
83testMkRelation1 = 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
85 testAttrs = A.attributesFromList [Attribute "a" TextAtomType]
86 testTupSet = RelationTupleSet [mkRelationTuple testAttrs $ V.fromList [TextAtom "v"],
87 mkRelationTuple testAttrs $ V.fromList [IntAtom 2]]
89testMkRelationFromExprsBadAttrs :: Test
90testMkRelationFromExprsBadAttrs = TestCase $ do
91 let context = DBC.empty
92 case evalState (evalRelationalExpr (MakeRelationFromExprs (Just [AttributeAndTypeNameExpr "badAttr1" (PrimitiveTypeConstructor "Int" IntAtomType) ()]) [TupleExpr (M.singleton "badAttr2" (NakedAtomExpr (IntAtom 1)))])) (mkRelationalExprState context) of
93 Left err -> assertEqual "tuple type mismatch" (TupleAttributeTypeMismatchError (A.attributesFromList [Attribute "badAttr2" IntAtomType])) err
94 Right _ -> assertFailure "expected tuple type mismatch"
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