Commit ec9fe2d7 authored by Grégoire Locqueville's avatar Grégoire Locqueville

Make tests play nice with the changes

Not mergeable: the tests fail for now. I suspect the problem comes from
the tests for `testCorrectFile`, where the function `checkFromLine` tries
to look up a wrong line number. Some more investigation would be needed
to figure out exactly what happens (probably should show the TSV file
being tested).

Anyway, here are the changes in this commit:
- Encapsulation of the TSV module in an `Internal` module, so we can export
  just the interface, but still expose the internals to the tests
- Correction of type errors in the tests to have something that typechecks
  with the changes introduced before
parent 51c8a407
Pipeline #6836 canceled with stages
in 2 minutes and 45 seconds
...@@ -197,6 +197,7 @@ library ...@@ -197,6 +197,7 @@ library
Gargantext.Core.Text.Corpus.Parsers Gargantext.Core.Text.Corpus.Parsers
Gargantext.Core.Text.Corpus.Parsers.Date Gargantext.Core.Text.Corpus.Parsers.Date
Gargantext.Core.Text.Corpus.Parsers.TSV Gargantext.Core.Text.Corpus.Parsers.TSV
Gargantext.Core.Text.Corpus.Parsers.TSV.Internal
Gargantext.Core.Text.Corpus.Query Gargantext.Core.Text.Corpus.Query
Gargantext.Core.Text.List Gargantext.Core.Text.List
Gargantext.Core.Text.List.Formats.TSV Gargantext.Core.Text.List.Formats.TSV
......
This diff is collapsed.
...@@ -2,19 +2,16 @@ ...@@ -2,19 +2,16 @@
module Test.Core.Text.Corpus.TSV (tests) where module Test.Core.Text.Corpus.TSV (tests) where
import Gargantext.Core.Text.Corpus.Parsers.TSV import Gargantext.Core.Text.Corpus.Parsers.TSV.Internal
import Test.QuickCheck
import Test.QuickCheck.Instances ()
import Data.ByteString.Lazy.UTF8 as BLU
import Data.ByteString.Lazy as BL import Data.ByteString.Lazy as BL
import Data.Char ( ord ) import Data.ByteString.Lazy.UTF8 as BLU
import Data.Char (ord)
import Data.Text as DT (Text, pack, null, elem) import Data.Text as DT (Text, pack, null, elem)
import Data.Text.Encoding as DT import Data.Text.Encoding as DT
import Prelude import Prelude
import Test.QuickCheck
import Test.QuickCheck.Instances ()
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck hiding (Positive, Negative) import Test.Tasty.QuickCheck hiding (Positive, Negative)
tests :: TestTree tests :: TestTree
...@@ -30,12 +27,12 @@ tests = testGroup "TSV Parser" [ ...@@ -30,12 +27,12 @@ tests = testGroup "TSV Parser" [
delimiterBS :: Delimiter -> BL.ByteString delimiterBS :: ColumnDelimiter -> BL.ByteString
delimiterBS Tab = BLU.fromString "\t" delimiterBS Tab = BLU.fromString "\t"
delimiterBS Comma = BLU.fromString "," delimiterBS Comma = BLU.fromString ","
delimiterBS Line = BLU.fromString "\n" delimiterBS Semicolon = BLU.fromString ";"
data File = File { fDelimiter :: Delimiter data File = File { fDelimiter :: ColumnDelimiter
, allCorpus :: [RandomCorpus] , allCorpus :: [RandomCorpus]
} }
deriving (Show) deriving (Show)
...@@ -56,19 +53,19 @@ instance Arbitrary File where ...@@ -56,19 +53,19 @@ instance Arbitrary File where
arbitrarySizedFile :: Int -> Gen File arbitrarySizedFile :: Int -> Gen File
arbitrarySizedFile m = do arbitrarySizedFile m = do
del <- elements [Tab, Comma] del <- elements [Tab, Comma, Semicolon]
corp <- vectorOf m (generateRandomCorpus) corp <- vectorOf m (generateRandomCorpus)
return (File del corp) return (File del corp)
delimiterToText :: Delimiter -> Text delimiterToText :: ColumnDelimiter -> Text
delimiterToText Tab = DT.pack "\t" delimiterToText Tab = DT.pack "\t"
delimiterToText Comma = DT.pack "," delimiterToText Comma = DT.pack ","
delimiterToText Line = DT.pack "\n" delimiterToText Semicolon = DT.pack ";"
delimiterToString :: Delimiter -> Char delimiterToString :: ColumnDelimiter -> Char
delimiterToString Tab = '\t' delimiterToString Tab = '\t'
delimiterToString Comma = ',' delimiterToString Comma = ','
delimiterToString Line = '\n' delimiterToString Semicolon = ';'
textToBL :: Text -> BL.ByteString textToBL :: Text -> BL.ByteString
textToBL b = BL.fromChunks . return . DT.encodeUtf8 $ b textToBL b = BL.fromChunks . return . DT.encodeUtf8 $ b
...@@ -85,7 +82,7 @@ generateRandomCorpus = RandomCorpus ...@@ -85,7 +82,7 @@ generateRandomCorpus = RandomCorpus
generateFileDelimiter :: Gen File generateFileDelimiter :: Gen File
generateFileDelimiter = do generateFileDelimiter = do
del <- elements [Tab, Comma] del <- elements [Tab, Comma, Semicolon]
m <- choose (1,5) m <- choose (1,5)
corp <- vectorOf m (generateRandomCorpus) corp <- vectorOf m (generateRandomCorpus)
return (File del corp) return (File del corp)
...@@ -106,7 +103,7 @@ randomHeaderList = frequency [ ...@@ -106,7 +103,7 @@ randomHeaderList = frequency [
] ]
--TODO add delimiter --TODO add delimiter
createLineFromCorpus :: RandomCorpus -> Delimiter -> BL.ByteString createLineFromCorpus :: RandomCorpus -> ColumnDelimiter -> BL.ByteString
createLineFromCorpus corpus delD = do createLineFromCorpus corpus delD = do
let aut = (DT.pack "\"") <> (authors corpus) <> (DT.pack "\"") let aut = (DT.pack "\"") <> (authors corpus) <> (DT.pack "\"")
let tit = (DT.pack "\"") <> (title corpus) <> (DT.pack "\"") let tit = (DT.pack "\"") <> (title corpus) <> (DT.pack "\"")
...@@ -118,7 +115,7 @@ createLineFromCorpus corpus delD = do ...@@ -118,7 +115,7 @@ createLineFromCorpus corpus delD = do
let del = delimiterToText delD let del = delimiterToText delD
textToBL(pDay <> del <> pMonth <> del <> pYears <> del <> aut <> del <> tit <> del <> sou <> del <> abt) textToBL(pDay <> del <> pMonth <> del <> pYears <> del <> aut <> del <> tit <> del <> sou <> del <> abt)
createLineFromCorpusWithNewLine :: RandomCorpus -> Delimiter -> BL.ByteString createLineFromCorpusWithNewLine :: RandomCorpus -> ColumnDelimiter -> BL.ByteString
createLineFromCorpusWithNewLine corpus delD = do createLineFromCorpusWithNewLine corpus delD = do
let aut = (DT.pack "\"") <> (authors corpus) <> (DT.pack "\"") let aut = (DT.pack "\"") <> (authors corpus) <> (DT.pack "\"")
let tit = (DT.pack "\"") <> (title corpus) <> (DT.pack "\"") let tit = (DT.pack "\"") <> (title corpus) <> (DT.pack "\"")
...@@ -143,9 +140,9 @@ createFileWithNewLine file = do ...@@ -143,9 +140,9 @@ createFileWithNewLine file = do
let allLines = BL.intercalate (BLU.fromString "\n") $ Prelude.map (\x -> createLineFromCorpus x (fDelimiter file)) (allCorpus file) let allLines = BL.intercalate (BLU.fromString "\n") $ Prelude.map (\x -> createLineFromCorpus x (fDelimiter file)) (allCorpus file)
headers <> (BLU.fromString "\n") <> allLines headers <> (BLU.fromString "\n") <> allLines
validRandomCorpus :: RandomCorpus -> Delimiter -> Bool validRandomCorpus :: RandomCorpus -> ColumnDelimiter -> Bool
validRandomCorpus tsv del validRandomCorpus tsv del
| BL.length (BL.filter (==delimiter del) (createLineFromCorpus tsv del)) > 3= True | BL.length (BL.filter (== toWord8 del) (createLineFromCorpus tsv del)) > 3= True
| DT.null $ abstract tsv = True | DT.null $ abstract tsv = True
| DT.null $ title tsv = True | DT.null $ title tsv = True
| DT.null $ authors tsv = True | DT.null $ authors tsv = True
...@@ -161,7 +158,7 @@ testValidNumber :: Property ...@@ -161,7 +158,7 @@ testValidNumber :: Property
testValidNumber = forAll generateNumber (\s -> do testValidNumber = forAll generateNumber (\s -> do
let nbText = DT.pack $ show s let nbText = DT.pack $ show s
let bl = textToBL nbText let bl = textToBL nbText
case validNumber bl nbText 1 [] of case checkNumber bl nbText 1 of
Right _ -> True Right _ -> True
Left _ | BL.empty == bl -> True Left _ | BL.empty == bl -> True
| s < 1 -> True | s < 1 -> True
...@@ -171,7 +168,7 @@ testValidNumber = forAll generateNumber (\s -> do ...@@ -171,7 +168,7 @@ testValidNumber = forAll generateNumber (\s -> do
testValidText :: Property testValidText :: Property
testValidText = forAll generateString (\s -> testValidText = forAll generateString (\s ->
let bl = textToBL s in let bl = textToBL s in
case validTextField bl s 1 [] of case checkTextField bl s 1 of
Right _ -> True Right _ -> True
Left _ | BL.empty == bl -> True Left _ | BL.empty == bl -> True
| (fromIntegral $ ord '\"') `BL.elem` bl -> True | (fromIntegral $ ord '\"') `BL.elem` bl -> True
...@@ -184,15 +181,11 @@ testTestErrorPerLine = forAll generateRandomCorpus (\tsv -> do ...@@ -184,15 +181,11 @@ testTestErrorPerLine = forAll generateRandomCorpus (\tsv -> do
let del = Tab let del = Tab
let line = createLineFromCorpus tsv del let line = createLineFromCorpus tsv del
let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"] let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"]
let splitLine = BL.splitWith (==delimiter del) line let splitLine = BL.splitWith (== toWord8 del) line
case testErrorPerLine splitLine del headers 1 [] of case checkRow splitLine del headers 1 of
Right _ -> True Right _ -> True
Left _ -> validRandomCorpus tsv del) Left _ -> validRandomCorpus tsv del)
--check :
-- True Del
-- False Error
-- Test if a file is OK -- Test if a file is OK
testTestCorrectFile :: Property testTestCorrectFile :: Property
testTestCorrectFile = forAll generateFile (\file -> do testTestCorrectFile = forAll generateFile (\file -> do
...@@ -202,8 +195,8 @@ testTestCorrectFile = forAll generateFile (\file -> do ...@@ -202,8 +195,8 @@ testTestCorrectFile = forAll generateFile (\file -> do
Left _ -> Prelude.all (\x -> do Left _ -> Prelude.all (\x -> do
let del = fDelimiter file let del = fDelimiter file
let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"] let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"]
let splitLine = BL.splitWith (==delimiter del) $ createLineFromCorpus x del let splitLine = BL.splitWith (== toWord8 del) $ createLineFromCorpus x del
case testErrorPerLine splitLine del headers 1 [] of case checkRow splitLine del headers 1 of
Right _ -> True Right _ -> True
Left _ -> validRandomCorpus x del) (allCorpus file)) Left _ -> validRandomCorpus x del) (allCorpus file))
...@@ -217,20 +210,20 @@ testTestCorrectFileWithNewLine = forAll generateFile (\file -> do ...@@ -217,20 +210,20 @@ testTestCorrectFileWithNewLine = forAll generateFile (\file -> do
Left _ -> Prelude.all (\x -> do Left _ -> Prelude.all (\x -> do
let del = fDelimiter file let del = fDelimiter file
let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"] let headers = Prelude.map DT.pack ["Publication Day", "Publication Month", "Publication Year", "Authors", "Title", "Source", "Abstract"]
let splitLine = BL.splitWith (==delimiter del) $ createLineFromCorpus x del let splitLine = BL.splitWith (== toWord8 del) $ createLineFromCorpus x del
case testErrorPerLine splitLine del headers 1 [] of case checkRow splitLine del headers 1 of
Right _ -> True Right _ -> True
Left _ -> validRandomCorpus x del) (allCorpus file)) Left _ -> validRandomCorpus x del) (allCorpus file))
testFindDelimiter :: Property testFindDelimiter :: Property
testFindDelimiter = forAll generateFileDelimiter (\file -> do testFindDelimiter = forAll generateFileDelimiter (\file -> do
let tsv = createFile file let tsv = createFile file
case findDelimiter tsv of case findColumnDelimiter tsv of
Right _ -> True Right _ -> True
Left _ -> do Left _ -> do
let line = Prelude.head $ allCorpus file let line = Prelude.head $ allCorpus file
let del = delimiterToString $ fDelimiter file let del = delimiterToString $ fDelimiter file
let delLine = delimiterToString Line let delLine = '\n'
del `DT.elem` (abstract line) || del `DT.elem` (authors line) || del `DT.elem` (title line) || del `DT.elem` (source line) || delLine `DT.elem` (abstract line) || delLine `DT.elem` (authors line) || delLine `DT.elem` (title line) || delLine `DT.elem` (source line)) del `DT.elem` (abstract line) || del `DT.elem` (authors line) || del `DT.elem` (title line) || del `DT.elem` (source line) || delLine `DT.elem` (abstract line) || delLine `DT.elem` (authors line) || delLine `DT.elem` (title line) || delLine `DT.elem` (source line))
testGetHeader :: Property testGetHeader :: Property
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment