on master help sign in
add tests for toInsert/Update/Delete/DefineExpr
commit225331cade4ab2149e8e751d248105fc0baa0fc3 authorAgentM <agentm@themactionfaction.com> dateFri 15 Dec 2017, 18:24 (9 years ago) parent9483da9
1 file changed+84−3
test/Relation/Tupleable.hs+84−3
test/Relation/Tupleable.hs+84−3
@@ -48,6 +48,14 @@ data Test7T = Test7C Test7A
4848
4949data Test8T = Test8C
5050 deriving (Generic, Show, Eq)
51+
52+data Test9T = Test9C
53+ {
54+ attr9A :: Integer,
55+ attr9B :: T.Text,
56+ attr9C :: Double
57+ }
58+ deriving (Generic, Show, Eq)
5159
5260instance Tupleable Test1T
5361
@@ -65,20 +73,21 @@ instance Tupleable Test7T
6573
6674instance Tupleable Test8T
6775
76+instance Tupleable Test9T
77+
6878main :: IO ()
6979main = do
7080 tcounts <- runTestTT testList
7181 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
7282
7383testList :: Test
74−testList = TestList [testADT1, testADT2, testADT3, testADT4, testADT6, testADT7, testADT8]
84+testList = TestList [testADT1, testADT2, testADT3, testADT4, testADT6, testADT7, testADT8, testInsertExpr, testDefineExpr, testUpdateExpr, testUpdateExprEmptyAttrs, testDeleteExpr, testUpdateExprWrongAttr]
7585
7686testADT1 :: Test
7787testADT1 = TestCase $ assertEqual "one record constructor" (Right example) (fromTuple (toTuple example))
7888 where
7989 example = Test1C {attrA = 3}
8090
81−
8291testADT2 :: Test
8392testADT2 = TestCase $ do
8493 let example = Test2C { attrB = 4, attrC = 6 }
@@ -116,4 +125,76 @@ testADT7 = TestCase $ do
116125testADT8 :: Test
117126testADT8 = TestCase $ assertEqual "single value" (Right example) (fromTuple (toTuple example))
118127 where
119− example = Test8C
128+ example = Test8C
129+
130+testInsertExpr :: Test
131+testInsertExpr = TestCase $ assertEqual "insert expr" expected (toInsertExpr [Test9C {
132+ attr9A = 4,
133+ attr9B = "a",
134+ attr9C = 3.4}] "rv")
135+ where expected = Right (Insert "rv" (MakeStaticRelation attrs tuples))
136+ attrs = attributesFromList [Attribute "attr9A" IntegerAtomType,
137+ Attribute "attr9B" TextAtomType,
138+ Attribute "attr9C" DoubleAtomType]
139+ tuples = RelationTupleSet [RelationTuple attrs (V.fromList [IntegerAtom 4,
140+ TextAtom "a",
141+ DoubleAtom 3.4])]
142+
143+testDefineExpr :: Test
144+testDefineExpr = TestCase $ assertEqual "define expr" expected actual
145+ where
146+ expected = Define "rv" (map NakedAttributeExpr attrs)
147+ attrs = [Attribute "attr9A" IntegerAtomType,
148+ Attribute "attr9B" TextAtomType,
149+ Attribute "attr9C" DoubleAtomType]
150+ actual = toDefineExpr (Proxy :: Proxy Test9T) "rv"
151+
152+testUpdateExpr :: Test
153+testUpdateExpr = TestCase $ do
154+ let expected = Update "rv" updateMap restriction
155+ updateMap = M.fromList [("attr9B", NakedAtomExpr (TextAtom "b")),
156+ ("attr9C", NakedAtomExpr (DoubleAtom 5.5))]
157+ restriction = AttributeEqualityPredicate "attr9A" (NakedAtomExpr (IntegerAtom 5))
158+ actual = toUpdateExpr "rv" ["attr9A"] (Test9C {attr9A = 5,
159+ attr9B = "b",
160+ attr9C = 5.5})
161+
162+ assertEqual "update expr1" expected actual
163+
164+testUpdateExprEmptyAttrs :: Test
165+testUpdateExprEmptyAttrs = TestCase $ do
166+ let expected = Update "rv" updateMap TruePredicate
167+ updateMap = M.fromList [("attr9B", NakedAtomExpr (TextAtom "b")),
168+ ("attr9C", NakedAtomExpr (DoubleAtom 5.5)),
169+ ("attr9A", NakedAtomExpr (IntegerAtom 5))]
170+ actual = toUpdateExpr "rv" [] (Test9C {attr9A = 5,
171+ attr9B = "b",
172+ attr9C = 5.5})
173+ assertEqual "update with empty attrs" expected actual
174+
175+testUpdateExprWrongAttr :: Test
176+testUpdateExprWrongAttr = TestCase $ do
177+ --currently, passing in the wrong attribute replaces the whole relvar with a single tuple- is this what we want?
178+ let expected = Update "rv" updateMap TruePredicate
179+ updateMap = M.fromList [
180+ ("attr9A", NakedAtomExpr (IntegerAtom 5)),
181+ ("attr9B", NakedAtomExpr (TextAtom "b")),
182+ ("attr9C", NakedAtomExpr (DoubleAtom 5.5))
183+ ]
184+ actual = toUpdateExpr "rv" ["nonexistentattr"] (Test9C {attr9A = 5,
185+ attr9B = "b",
186+ attr9C = 5.5})
187+ assertEqual "update with wrong attr" expected actual
188+
189+testDeleteExpr :: Test
190+testDeleteExpr = TestCase $ do
191+ let expected = Delete "rv" (AndPredicate (AttributeEqualityPredicate "attr9A"
192+ (NakedAtomExpr (IntegerAtom 5)))
193+ (AttributeEqualityPredicate "attr9B"
194+ (NakedAtomExpr (TextAtom "b"))))
195+ actual = toDeleteExpr "rv" ["attr9A", "attr9B"] (Test9C {attr9A = 5,
196+ attr9B = "b",
197+ attr9C = 5.5})
198+ assertEqual "delete expr" expected actual
199+
200+