@@ -5,6 +5,7 @@ import Common
55import Database.PostgreSQL.Simple.FromField (FromField)
66import Database.PostgreSQL.Simple.Types(Query(..),Values(..))
77import Database.PostgreSQL.Simple.HStore
8+import Database.PostgreSQL.Simple.Copy
89import qualified Database.PostgreSQL.Simple.Transaction as ST
910import Control.Applicative
1011import Control.Exception as E
@@ -41,6 +42,7 @@ tests =
4142 , TestLabel "Savepoint" . testSavepoint
4243 , TestLabel "Unicode" . testUnicode
4344 , TestLabel "Values" . testValues
45+ , TestLabel "Copy" . testCopy
4446 ]
4547
4648testBytea :: TestEnv -> Test
@@ -275,6 +277,31 @@ testValues TestEnv{..} = TestCase $ do
275277 vals' <- query_ conn "DELETE FROM values_test RETURNING *"
276278 sort vals @?= sort vals'
277279
280+
281+testCopy :: TestEnv -> Test
282+testCopy TestEnv{..} = TestCase $ do
283+ execute_ conn "CREATE TEMPORARY TABLE copy_test (x int, y text)"
284+ copy_ conn "COPY copy_test FROM STDIN (FORMAT CSV)"
285+ mapM_ (putCopyData conn) copyRows
286+ putCopyEnd conn
287+ copy_ conn "COPY copy_test FROM STDIN (FORMAT CSV)"
288+ mapM_ (putCopyData conn) abortRows
289+ putCopyError conn "aborted"
290+ copy_ conn "COPY copy_test TO STDOUT (FORMAT CSV)"
291+ rows <- loop []
292+ -- Hmm, does postgres always produce \n as an end-of-line here, or
293+ -- are there cases where it will use a \r\n as well?
294+ sort rows @?= sort copyRows
295+ where
296+ copyRows = ["1,foo\n"
297+ ,"2,bar\n"]
298+ abortRows = ["3,baz\n"]
299+ loop rows = do
300+ mrow <- getCopyData conn
301+ case mrow of
302+ CopyOutDone _ -> return rows
303+ CopyOutRow row -> loop (row:rows)
304+
278305data TestException
279306 = TestException
280307 deriving (Eq, Show, Typeable)