1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE TypeApplications #-}
3 module Ngrams.Query where
7 import Data.Map.Strict (Map)
9 import Gargantext.API.Ngrams.Types (mSetFromList)
10 import Gargantext.API.Ngrams
11 import Gargantext.API.Ngrams.Types
12 import Gargantext.Core.Types.Main
13 import Gargantext.Core.Types.Query
14 import Gargantext.Prelude
15 import qualified Data.Map.Strict as Map
16 import qualified Data.Patch.Class as Patch
17 import qualified Data.Validity as Validity
18 import qualified Data.Text as T
20 import Ngrams.Query.PaginationCorpus
22 import Test.Tasty.HUnit
26 main = defaultMain tests
29 tests = testGroup "Ngrams" [unitTests]
31 curryElem :: NgramsElement
32 curryElem = mkNgramsElement "curry" MapTerm Nothing mempty
34 elbaElem :: NgramsElement
35 elbaElem = mkNgramsElement "elba" MapTerm Nothing mempty
37 mockFlatCorpus :: Versioned (Map NgramsTerm NgramsElement)
38 mockFlatCorpus = Versioned 0 $ Map.fromList [
39 ( "haskell", curryElem)
40 , ( "idris", elbaElem)
43 mockQueryFn :: Maybe T.Text -> NgramsTerm -> Bool
44 mockQueryFn searchQuery (NgramsTerm nt) =
45 maybe (const True) T.isInfixOf (T.toLower <$> searchQuery) (T.toLower nt)
48 unitTests = testGroup "Query tests"
50 testCase "Simple query mockFlatCorpus" testFlat01
51 , testCase "Simple query (desc sorting)" testFlat02
53 , testCase "Simple query (listType = MapTerm)" testFlat03
54 , testCase "Simple query (listType = StopTerm)" testFlat04
55 -- -- Full text search
56 , testCase "Simple query (search with match)" testFlat05
58 , testCase "Simple pagination on all terms" test_pagination_allTerms
59 , testCase "Simple pagination on MapTerm" test_pagination01
60 , testCase "Simple pagination on MapTerm (limit < total terms)" test_pagination02
61 , testCase "Simple pagination on MapTerm (offset works)" test_pagination02_offset
62 , testCase "Simple pagination on ListTerm (limit < total terms)" test_pagination03
63 , testCase "Simple pagination on ListTerm (offset works)" test_pagination03_offset
64 , testCase "Simple pagination on CandidateTerm (limit < total terms)" test_pagination04
65 , testCase "paginating QuantumComputing corpus works (MapTerms)" test_paginationQuantum
66 , testCase "paginating QuantumComputing corpus works (CandidateTerm)" test_paginationQuantum_02
68 , testCase "I can apply a patch to term mapTerms to stopTerms (issue #217)" test_217
71 -- Let's test that if we request elements sorted in
72 -- /ascending/ order, we get them.
73 testFlat01 :: Assertion
75 let res = searchTableNgrams mockFlatCorpus searchQuery
76 res @?= VersionedWithCount 0 2 ( NgramsTable [curryElem, elbaElem] )
78 searchQuery = NgramsSearchQuery {
80 , _nsq_offset = Nothing
81 , _nsq_listType = Nothing
82 , _nsq_minSize = Nothing
83 , _nsq_maxSize = Nothing
84 , _nsq_orderBy = Just TermAsc
85 , _nsq_searchQuery = mockQueryFn Nothing
88 -- Let's test that if we request elements sorted in
89 -- /descending/ order, we get them.
90 testFlat02 :: Assertion
92 let res = searchTableNgrams mockFlatCorpus searchQuery
93 res @?= VersionedWithCount 0 2 ( NgramsTable [elbaElem, curryElem] )
95 searchQuery = NgramsSearchQuery {
97 , _nsq_offset = Nothing
98 , _nsq_listType = Nothing
99 , _nsq_minSize = Nothing
100 , _nsq_maxSize = Nothing
101 , _nsq_orderBy = Just TermDesc
102 , _nsq_searchQuery = mockQueryFn Nothing
105 testFlat03 :: Assertion
107 let res = searchTableNgrams mockFlatCorpus searchQuery
108 res @?= VersionedWithCount 0 2 ( NgramsTable [elbaElem, curryElem] )
110 searchQuery = NgramsSearchQuery {
112 , _nsq_offset = Nothing
113 , _nsq_listType = Just MapTerm
114 , _nsq_minSize = Nothing
115 , _nsq_maxSize = Nothing
116 , _nsq_orderBy = Just TermDesc
117 , _nsq_searchQuery = mockQueryFn Nothing
120 -- Here we are searching for all the stop terms, but
121 -- due to the fact we don't have any inside 'mockFlatCorpus',
122 -- we should get no results.
123 testFlat04 :: Assertion
125 let res = searchTableNgrams mockFlatCorpus searchQuery
126 res @?= VersionedWithCount 0 0 ( NgramsTable [] )
128 searchQuery = NgramsSearchQuery {
130 , _nsq_offset = Nothing
131 , _nsq_listType = Just StopTerm
132 , _nsq_minSize = Nothing
133 , _nsq_maxSize = Nothing
134 , _nsq_orderBy = Just TermDesc
135 , _nsq_searchQuery = mockQueryFn Nothing
138 -- For this test, we run a full text search on the word
139 -- \"curry\", and we expect back a result.
140 testFlat05 :: Assertion
142 let res = searchTableNgrams mockFlatCorpus searchQuery
143 res @?= VersionedWithCount 0 1 ( NgramsTable [curryElem] )
145 searchQuery = NgramsSearchQuery {
147 , _nsq_offset = Nothing
148 , _nsq_listType = Nothing
149 , _nsq_minSize = Nothing
150 , _nsq_maxSize = Nothing
151 , _nsq_orderBy = Just TermDesc
152 , _nsq_searchQuery = mockQueryFn (Just "curry")
157 test_pagination_allTerms :: Assertion
158 test_pagination_allTerms = do
159 let res = searchTableNgrams paginationCorpus searchQuery
160 res @?= VersionedWithCount 0 10 ( NgramsTable [ haskellElem
170 searchQuery = NgramsSearchQuery {
172 , _nsq_offset = Nothing
173 , _nsq_listType = Nothing
174 , _nsq_minSize = Nothing
175 , _nsq_maxSize = Nothing
176 , _nsq_orderBy = Nothing
177 , _nsq_searchQuery = mockQueryFn Nothing
180 -- In this test, I'm asking for 5 /map terms/, and as the
181 -- corpus has only 2, that's what I should get back.
182 test_pagination01 :: Assertion
183 test_pagination01 = do
184 let res = searchTableNgrams paginationCorpus searchQuery
185 res @?= VersionedWithCount 0 4 ( NgramsTable [implementationElem, languagesElem, termsElem, proofElem] )
187 searchQuery = NgramsSearchQuery {
189 , _nsq_offset = Nothing
190 , _nsq_listType = Just MapTerm
191 , _nsq_minSize = Nothing
192 , _nsq_maxSize = Nothing
193 , _nsq_orderBy = Just ScoreDesc
194 , _nsq_searchQuery = mockQueryFn Nothing
197 test_pagination02 :: Assertion
198 test_pagination02 = do
199 let res = searchTableNgrams paginationCorpus searchQuery
200 res @?= VersionedWithCount 0 4 ( NgramsTable [implementationElem, languagesElem, termsElem] )
202 searchQuery = NgramsSearchQuery {
204 , _nsq_offset = Nothing
205 , _nsq_listType = Just MapTerm
206 , _nsq_minSize = Nothing
207 , _nsq_maxSize = Nothing
208 , _nsq_orderBy = Just ScoreDesc
209 , _nsq_searchQuery = mockQueryFn Nothing
212 test_pagination02_offset :: Assertion
213 test_pagination02_offset = do
214 let res = searchTableNgrams paginationCorpus searchQuery
215 res @?= VersionedWithCount 0 4 ( NgramsTable [termsElem, proofElem] )
217 searchQuery = NgramsSearchQuery {
219 , _nsq_offset = Just (Offset 2)
220 , _nsq_listType = Just MapTerm
221 , _nsq_minSize = Nothing
222 , _nsq_maxSize = Nothing
223 , _nsq_orderBy = Just ScoreDesc
224 , _nsq_searchQuery = mockQueryFn Nothing
227 test_pagination03 :: Assertion
228 test_pagination03 = do
229 let res = searchTableNgrams paginationCorpus searchQuery
230 res @?= VersionedWithCount 0 4 ( NgramsTable [sideEffectsElem, ooElem, javaElem] )
232 searchQuery = NgramsSearchQuery {
234 , _nsq_offset = Nothing
235 , _nsq_listType = Just StopTerm
236 , _nsq_minSize = Nothing
237 , _nsq_maxSize = Nothing
238 , _nsq_orderBy = Just ScoreDesc
239 , _nsq_searchQuery = mockQueryFn Nothing
242 test_pagination03_offset :: Assertion
243 test_pagination03_offset = do
244 let res = searchTableNgrams paginationCorpus searchQuery
245 res @?= VersionedWithCount 0 4 ( NgramsTable [javaElem, pascalElem] )
247 searchQuery = NgramsSearchQuery {
249 , _nsq_offset = Just (Offset 2)
250 , _nsq_listType = Just StopTerm
251 , _nsq_minSize = Nothing
252 , _nsq_maxSize = Nothing
253 , _nsq_orderBy = Just ScoreDesc
254 , _nsq_searchQuery = mockQueryFn Nothing
257 test_pagination04 :: Assertion
258 test_pagination04 = do
259 let res = searchTableNgrams paginationCorpus searchQuery
260 res @?= VersionedWithCount 0 2 ( NgramsTable [haskellElem] )
262 searchQuery = NgramsSearchQuery {
264 , _nsq_offset = Nothing
265 , _nsq_listType = Just CandidateTerm
266 , _nsq_minSize = Nothing
267 , _nsq_maxSize = Nothing
268 , _nsq_orderBy = Just ScoreDesc
269 , _nsq_searchQuery = mockQueryFn Nothing
272 test_paginationQuantum :: Assertion
273 test_paginationQuantum = do
274 let res = searchTableNgrams quantumComputingCorpus searchQuery
275 let elems = coerce @NgramsTable @[NgramsElement] $ _vc_data res
277 forM_ elems $ \term ->
278 assertBool ("found " <> show (_ne_list term) <> " in: " <> show elems) (_ne_list term == MapTerm)
280 searchQuery = NgramsSearchQuery {
281 _nsq_limit = Limit 10
282 , _nsq_offset = Nothing
283 , _nsq_listType = Just MapTerm
284 , _nsq_minSize = Nothing
285 , _nsq_maxSize = Nothing
286 , _nsq_orderBy = Nothing
287 , _nsq_searchQuery = mockQueryFn Nothing
290 test_paginationQuantum_02 :: Assertion
291 test_paginationQuantum_02 = do
292 let res = searchTableNgrams quantumComputingCorpus searchQuery
293 let elems = coerce @NgramsTable @[NgramsElement] $ _vc_data res
294 assertBool ("found only " <> show (length elems) <> " in: " <> show elems) (length elems == 10)
296 searchQuery = NgramsSearchQuery {
297 _nsq_limit = Limit 10
298 , _nsq_offset = Nothing
299 , _nsq_listType = Just CandidateTerm
300 , _nsq_minSize = Nothing
301 , _nsq_maxSize = Nothing
302 , _nsq_orderBy = Nothing
303 , _nsq_searchQuery = mockQueryFn Nothing
306 issue217Corpus :: NgramsTableMap
307 issue217Corpus = Map.fromList [
308 ( "advantages", NgramsRepoElement 1 MapTerm Nothing Nothing (mSetFromList ["advantage"]))
309 , ( "advantage" , NgramsRepoElement 1 MapTerm (Just "advantages") (Just "advantages") mempty)
312 patched217Corpus :: NgramsTableMap
313 patched217Corpus = Map.fromList [
314 ( "advantages", NgramsRepoElement 1 StopTerm Nothing Nothing (mSetFromList ["advantage"]))
315 , ( "advantage" , NgramsRepoElement 1 StopTerm (Just "advantages") (Just "advantages") mempty)
318 -- In this patch we simulate turning the subtree composed by 'advantages' and 'advantage'
319 -- from map terms to stop terms.
320 patch217 :: NgramsTablePatch
321 patch217 = mkNgramsTablePatch $ Map.fromList [
322 (NgramsTerm "advantages", NgramsPatch
323 { _patch_children = mempty
324 , _patch_list = Patch.Replace MapTerm StopTerm
329 test_217 :: Assertion
331 -- Check the patch is applicable
332 Validity.validationIsValid (Patch.applicable patch217 (Just issue217Corpus)) @?= True
333 Patch.act patch217 (Just issue217Corpus) @?= Just patched217Corpus