Commit be44a926 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Prepare for corpusQuery tests

parent 03b5f28d
...@@ -848,6 +848,7 @@ test-suite garg-test ...@@ -848,6 +848,7 @@ test-suite garg-test
main-is: Main.hs main-is: Main.hs
other-modules: other-modules:
Core.Text Core.Text
Core.Text.Corpus.Query
Core.Text.Examples Core.Text.Examples
Core.Text.Flow Core.Text.Flow
Core.Utils Core.Utils
...@@ -896,6 +897,7 @@ test-suite garg-test ...@@ -896,6 +897,7 @@ test-suite garg-test
QuickCheck QuickCheck
, aeson , aeson
, base , base
, boolexpr
, bytestring , bytestring
, containers , containers
, duckling , duckling
...@@ -909,6 +911,7 @@ test-suite garg-test ...@@ -909,6 +911,7 @@ test-suite garg-test
, quickcheck-instances , quickcheck-instances
, tasty , tasty
, tasty-hunit , tasty-hunit
, tasty-quickcheck
, text , text
, time , time
, unordered-containers , unordered-containers
......
...@@ -516,6 +516,7 @@ tests: ...@@ -516,6 +516,7 @@ tests:
dependencies: dependencies:
- aeson - aeson
- base - base
- boolexpr
- bytestring - bytestring
- containers - containers
- gargantext - gargantext
...@@ -530,6 +531,7 @@ tests: ...@@ -530,6 +531,7 @@ tests:
- duckling - duckling
- tasty - tasty
- tasty-hunit - tasty-hunit
- tasty-quickcheck
- text - text
- unordered-containers - unordered-containers
- validity - validity
......
{-# LANGUAGE OverloadedStrings #-}
module Core.Text.Corpus.Query where
import Data.BoolExpr
import Gargantext.Core.Text.Corpus.Query
import Prelude
import Test.Tasty
import Test.Tasty.QuickCheck hiding (Positive)
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Boolean Query Engine" [
testProperty "Parses 'A OR B'" testParse01
]
testParse01 :: Property
testParse01 =
(renderQuery <$> parseQuery "A OR B") === (renderQuery <$> Right (unsafeMkQuery $ (BConst (Positive "A") `BOr` BConst (Positive "B"))))
...@@ -11,6 +11,7 @@ Portability : POSIX ...@@ -11,6 +11,7 @@ Portability : POSIX
import Gargantext.Prelude import Gargantext.Prelude
import qualified Core.Text.Corpus.Query as CorpusQuery
import qualified Core.Utils as Utils import qualified Core.Utils as Utils
--import qualified Ngrams.Lang.Fr as Fr --import qualified Ngrams.Lang.Fr as Fr
--import qualified Ngrams.Lang as Lang --import qualified Ngrams.Lang as Lang
...@@ -34,3 +35,4 @@ main = do ...@@ -34,3 +35,4 @@ main = do
Crypto.test Crypto.test
NLP.main NLP.main
NgramsQuery.main NgramsQuery.main
CorpusQuery.main
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingStrategies #-}
module Gargantext.Core.Text.Corpus.Query ( module Gargantext.Core.Text.Corpus.Query (
Query -- * opaque Query -- * opaque
...@@ -5,7 +6,11 @@ module Gargantext.Core.Text.Corpus.Query ( ...@@ -5,7 +6,11 @@ module Gargantext.Core.Text.Corpus.Query (
, Limit(..) , Limit(..)
, getQuery , getQuery
, parseQuery , parseQuery
, renderQuery
, ExternalAPIs(..) , ExternalAPIs(..)
-- * Useful for testing
, unsafeMkQuery
) where ) where
import Data.Bifunctor import Data.Bifunctor
...@@ -13,13 +18,14 @@ import Data.String ...@@ -13,13 +18,14 @@ import Data.String
import Gargantext.API.Admin.Orchestrator.Types import Gargantext.API.Admin.Orchestrator.Types
import Gargantext.Core.Types import Gargantext.Core.Types
import Prelude import Prelude
import qualified Data.Aeson as Aeson import qualified Data.Aeson as Aeson
import qualified Data.BoolExpr as BoolExpr import qualified Data.BoolExpr as BoolExpr
import qualified Data.BoolExpr.Parser as BoolExpr import qualified Data.BoolExpr.Parser as BoolExpr
import qualified Data.Swagger as Swagger import qualified Data.BoolExpr.Printer as BoolExpr
import qualified Data.Text as T import qualified Data.Swagger as Swagger
import qualified Servant.API as Servant import qualified Data.Text as T
import qualified Text.Parsec as P import qualified Servant.API as Servant
import qualified Text.Parsec as P
-- | A raw query, as typed by the user from the frontend. -- | A raw query, as typed by the user from the frontend.
newtype RawQuery = RawQuery { getRawQuery :: T.Text } newtype RawQuery = RawQuery { getRawQuery :: T.Text }
...@@ -41,7 +47,13 @@ newtype Limit = Limit { getLimit :: Int } ...@@ -41,7 +47,13 @@ newtype Limit = Limit { getLimit :: Int }
newtype Query = Query { getQuery :: (BoolExpr.CNF Term) } newtype Query = Query { getQuery :: (BoolExpr.CNF Term) }
deriving Show deriving Show
unsafeMkQuery :: BoolExpr.BoolExpr Term -> Query
unsafeMkQuery = Query . BoolExpr.boolTreeToCNF
-- | Parses an input 'Text' into a 'Query', reporting an error if it fails. -- | Parses an input 'Text' into a 'Query', reporting an error if it fails.
parseQuery :: RawQuery -> Either String Query parseQuery :: RawQuery -> Either String Query
parseQuery (RawQuery txt) = bimap show (Query . BoolExpr.boolTreeToCNF) $ parseQuery (RawQuery txt) = bimap show (Query . BoolExpr.boolTreeToCNF) $
P.runParser (BoolExpr.parseBoolExpr (Term . T.pack <$> BoolExpr.identifier)) () "Corpus.Query" (T.unpack txt) P.runParser (BoolExpr.parseBoolExpr (Term . T.pack <$> BoolExpr.identifier)) () "Corpus.Query" (T.unpack txt)
renderQuery :: Query -> RawQuery
renderQuery (Query cnf) = RawQuery . T.pack $ BoolExpr.boolExprPrinter (showsPrec 0) (BoolExpr.fromCNF cnf) ""
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