nested tuple with one element: Tuple a Unit; and test

This commit is contained in:
Kamirus 2018-11-16 13:46:19 +01:00
parent 2fa1c0551e
commit e9d9e4ea0f
2 changed files with 18 additions and 4 deletions

View File

@ -24,14 +24,19 @@ instance toSQLRowTupleOfTuples :: (ToSQLRow (Tuple a ta), ToSQLRow (Tuple b t))
toSQLRow (Tuple a t) = toSQLRow a <> toSQLRow t toSQLRow (Tuple a t) = toSQLRow a <> toSQLRow t
else instance toSQLRowTuple :: (ToSQLValue a, ToSQLRow (Tuple b t)) => ToSQLRow (Tuple a (Tuple b t)) where else instance toSQLRowTuple :: (ToSQLValue a, ToSQLRow (Tuple b t)) => ToSQLRow (Tuple a (Tuple b t)) where
toSQLRow (Tuple a t) = toSQLValue a : toSQLRow t toSQLRow (Tuple a t) = toSQLValue a : toSQLRow t
else instance toSQLRowTupleEnd :: (ToSQLValue a, ToSQLValue b) => ToSQLRow (Tuple a b) where else instance toSQLRowTupleOne :: ToSQLValue a => ToSQLRow (Tuple a Unit) where
toSQLRow (Tuple a unit) = [ toSQLValue a ]
else instance toSQLRowTupleTwo :: (ToSQLValue a, ToSQLValue b) => ToSQLRow (Tuple a b) where
toSQLRow (Tuple a b) = [ toSQLValue a, toSQLValue b ] toSQLRow (Tuple a b) = [ toSQLValue a, toSQLValue b ]
instance fromSQLRowTuple :: (FromSQLValue a, FromSQLRow (Tuple b t)) => FromSQLRow (Tuple a (Tuple b t)) where instance fromSQLRowTuple :: (FromSQLValue a, FromSQLRow (Tuple b t)) => FromSQLRow (Tuple a (Tuple b t)) where
fromSQLRow r = do fromSQLRow r = do
{head, tail} note "Expecting more fields in a row" $ uncons r {head, tail} note "Expecting more fields in a row" $ uncons r
Tuple <$> fromSQLValue head <*> fromSQLRow tail Tuple <$> fromSQLValue head <*> fromSQLRow tail
else instance fromSQLRowTupleEnd :: (FromSQLValue a, FromSQLValue b) => FromSQLRow (Tuple a b) where else instance fromSQLRowTupleOne :: FromSQLValue a => FromSQLRow (Tuple a Unit) where
fromSQLRow [a] = Tuple <$> fromSQLValue a <@> unit
fromSQLRow _ = Left "Expecting exactly one field."
else instance fromSQLRowTupleTwo :: (FromSQLValue a, FromSQLValue b) => FromSQLRow (Tuple a b) where
fromSQLRow [a, b] = Tuple <$> fromSQLValue a <*> fromSQLValue b fromSQLRow [a, b] = Tuple <$> fromSQLValue a <*> fromSQLValue b
fromSQLRow _ = Left "Expecting exactly two more fields." fromSQLRow _ = Left "Expecting exactly two more fields."

View File

@ -27,7 +27,7 @@ import Foreign.Object (Object, fromFoldable)
import Math ((%)) import Math ((%))
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
import Test.Assert (assert) import Test.Assert (assert)
import Test.Example (run) as Example -- import Test.Example (run) as Example
import Test.Unit (TestF, suite) import Test.Unit (TestF, suite)
import Test.Unit as Test.Unit import Test.Unit as Test.Unit
import Test.Unit.Assert (equal) import Test.Unit.Assert (equal)
@ -66,7 +66,7 @@ main ∷ Effect Unit
main = do main = do
void $ launchAff do void $ launchAff do
-- Running guide from README -- Running guide from README
Example.run -- Example.run
-- Acctual test suite -- Acctual test suite
pool <- newPool config pool <- newPool config
@ -139,6 +139,15 @@ main = do
""") Row0 """) Row0
liftEffect <<< assert $ names == ["pork" /\ true, "rookworst" /\ true] liftEffect <<< assert $ names == ["pork" /\ true, "rookworst" /\ true]
test conn "nested tuples as rows - just one element" $ do
let row = date 2010 2 31 /\ unit
execute conn (Query """
INSERT INTO dates (date)
VALUES ($1)
""") row
rows <- query conn (Query "SELECT date FROM dates") Row0
liftEffect <<< assert $ rows == [row]
let let
insertFood = insertFood =
execute conn (Query """ execute conn (Query """