|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE TypeApplications #-} |
| 3 | + |
| 4 | +module IO.CSV where |
| 5 | + |
| 6 | +import qualified Data.ByteString.Lazy as BL |
| 7 | +import qualified Data.Text as T |
| 8 | +import qualified Data.Text.Encoding as TE |
| 9 | +import qualified DataFrame as D |
| 10 | +import qualified DataFrame.Internal.Column as DI |
| 11 | +import DataFrame.Internal.DataFrame (DataFrame (..), toCsv) |
| 12 | +import DataFrame.IO.CSV (fromCsv, fromCsvBytes) |
| 13 | +import qualified DataFrame.Operations.Core as D |
| 14 | +import Test.HUnit ( |
| 15 | + Test (TestCase, TestLabel), |
| 16 | + assertEqual, |
| 17 | + assertFailure, |
| 18 | + ) |
| 19 | + |
| 20 | +-- | Happy path: parse a simple CSV string with Int and Text columns. |
| 21 | +fromCsvHappyPath :: Test |
| 22 | +fromCsvHappyPath = TestLabel "fromCsv_happy_path" $ TestCase $ do |
| 23 | + result <- fromCsv "name,age\nAlice,30\nBob,25\nCharlie,35\n" |
| 24 | + case result of |
| 25 | + Left err -> assertFailure $ "Unexpected Left: " ++ err |
| 26 | + Right df -> do |
| 27 | + assertEqual "rows" 3 (D.nRows df) |
| 28 | + assertEqual "columns" 2 (D.nColumns df) |
| 29 | + |
| 30 | +-- | Empty input should return a Left. |
| 31 | +fromCsvEmpty :: Test |
| 32 | +fromCsvEmpty = TestLabel "fromCsv_empty" $ TestCase $ do |
| 33 | + result <- fromCsv "" |
| 34 | + case result of |
| 35 | + Left _ -> return () |
| 36 | + Right _ -> assertFailure "Expected Left for empty input" |
| 37 | + |
| 38 | +-- | fromCsvBytes happy path. |
| 39 | +fromCsvBytesHappyPath :: Test |
| 40 | +fromCsvBytesHappyPath = TestLabel "fromCsvBytes_happy_path" $ TestCase $ do |
| 41 | + let bs = BL.fromStrict (TE.encodeUtf8 "x,y\n1,2\n3,4\n") |
| 42 | + df <- fromCsvBytes bs |
| 43 | + assertEqual "rows" 2 (D.nRows df) |
| 44 | + assertEqual "columns" 2 (D.nColumns df) |
| 45 | + |
| 46 | +-- | Round trip: toCsv then fromCsv preserves data. |
| 47 | +fromCsvRoundTrip :: Test |
| 48 | +fromCsvRoundTrip = TestLabel "fromCsv_roundTrip" $ TestCase $ do |
| 49 | + let df = |
| 50 | + D.fromNamedColumns |
| 51 | + [ ("a", DI.fromList @Int [1, 2, 3]) |
| 52 | + , ("b", DI.fromList @T.Text ["hello", "world", "test"]) |
| 53 | + ] |
| 54 | + let csvString = T.unpack (toCsv df) |
| 55 | + result <- fromCsv csvString |
| 56 | + case result of |
| 57 | + Left err -> assertFailure $ "Unexpected Left: " ++ err |
| 58 | + Right df' -> do |
| 59 | + assertEqual "round trip dimensions" (dataframeDimensions df) (dataframeDimensions df') |
| 60 | + assertEqual "round trip data" df df' |
| 61 | + |
| 62 | +-- | Round trip via fromCsvBytes. |
| 63 | +fromCsvBytesRoundTrip :: Test |
| 64 | +fromCsvBytesRoundTrip = TestLabel "fromCsvBytes_roundTrip" $ TestCase $ do |
| 65 | + let df = |
| 66 | + D.fromNamedColumns |
| 67 | + [ ("x", DI.fromList @Int [10, 20]) |
| 68 | + , ("y", DI.fromList @Double [1.5, 2.5]) |
| 69 | + ] |
| 70 | + let bs = BL.fromStrict (TE.encodeUtf8 (toCsv df)) |
| 71 | + df' <- fromCsvBytes bs |
| 72 | + assertEqual "round trip dimensions" (dataframeDimensions df) (dataframeDimensions df') |
| 73 | + |
| 74 | +-- | Single column CSV. |
| 75 | +fromCsvSingleColumn :: Test |
| 76 | +fromCsvSingleColumn = TestLabel "fromCsv_single_column" $ TestCase $ do |
| 77 | + result <- fromCsv "id\n10\n20\n30\n" |
| 78 | + case result of |
| 79 | + Left err -> assertFailure $ "Unexpected Left: " ++ err |
| 80 | + Right df -> do |
| 81 | + assertEqual "rows" 3 (D.nRows df) |
| 82 | + assertEqual "columns" 1 (D.nColumns df) |
| 83 | + |
| 84 | +tests :: [Test] |
| 85 | +tests = |
| 86 | + [ fromCsvHappyPath |
| 87 | + , fromCsvEmpty |
| 88 | + , fromCsvBytesHappyPath |
| 89 | + , fromCsvRoundTrip |
| 90 | + , fromCsvBytesRoundTrip |
| 91 | + , fromCsvSingleColumn |
| 92 | + ] |
0 commit comments