66import unittest
77import bz2
88import csv
9+ import time
910from io import TextIOWrapper
1011
1112import six
1819
1920TITLES_CSV = os .path .abspath (os .path .dirname (__file__ )) + '/titles.csv'
2021
22+ def waitForIndex (env , idx , timeout = None ):
23+ delay = 0.1
24+ while True :
25+ res = env .execute_command ('ft.info' , idx )
26+ try :
27+ res .index ('indexing' )
28+ except :
29+ break
30+
31+ if int (res [res .index ('indexing' ) + 1 ]) == 0 :
32+ break
33+
34+ time .sleep (delay )
35+ if timeout is not None :
36+ timeout -= delay
37+ if timeout <= 0 :
38+ break
39+
40+ def check_version_2 (env ):
41+ try :
42+ # Indexing the hash
43+ env .execute_command ('FT.ADDHASH foo bar 1' )
44+ except redis .ResponseError as e :
45+ # Support for FT.ADDHASH was removed in RediSearch 2.0
46+ print str (e )
47+ if str (e ).startswith ('unknown command `FT.ADDHASH`' ):
48+ return True
49+ return False
50+
2151class RedisSearchTestCase (ModuleTestCase ('../module.so' )):
2252
2353 def createIndex (self , client , num_docs = 100 , definition = None ):
@@ -70,6 +100,7 @@ def testClient(self):
70100 self .createIndex (client , num_docs = num_docs )
71101
72102 for _ in r .retry_with_rdb_reload ():
103+ waitForIndex (r , 'test' )
73104 #verify info
74105 info = client .info ()
75106 for k in [ 'index_name' , 'index_options' , 'fields' , 'num_docs' ,
@@ -176,6 +207,8 @@ def testAddHash(self):
176207 conn = self .redis ()
177208
178209 with conn as r :
210+ if check_version_2 (r ):
211+ return
179212 # Creating a client with a given index name
180213 client = Client ('idx' , port = conn .port )
181214
@@ -190,15 +223,9 @@ def testAddHash(self):
190223 'title' : 'RediSearch' ,
191224 'body' : 'Redisearch impements a search engine on top of redis'
192225 })
193-
194- try :
195- # Indexing the hash
196- client .add_document_hash ('doc1' )
197- except redis .ResponseError as e :
198- # Support for FT.ADDHASH was removed in RediSearch 2.0
199- self .assertTrue ( str (e ).startswith ('unknown command `FT.ADDHASH`' ))
200- return
201-
226+
227+ client .add_document_hash ('doc1' )
228+
202229 # Searching with complext parameters:
203230 q = Query ("search engine" ).verbatim ().no_content ().paging (0 , 5 )
204231
@@ -309,8 +336,8 @@ def testFilters(self):
309336 client .add_document ('doc2' , txt = 'foo baz' , num = 2 , loc = '-0.1,51.2' )
310337
311338 for i in r .retry_with_rdb_reload ():
312-
313- # Test numerical filter
339+ waitForIndex ( r , 'idx' )
340+ # Test numerical filter
314341 q1 = Query ("foo" ).add_filter (NumericFilter ('num' , 0 , 2 )).no_content ()
315342 q2 = Query ("foo" ).add_filter (NumericFilter ('num' , 2 , NumericFilter .INF , minExclusive = True )).no_content ()
316343 res1 , res2 = client .search (q1 ), client .search (q2 )
@@ -498,6 +525,7 @@ def testPartial(self):
498525 client .add_document ('doc2' , f3 = 'f3_val' , replace = True )
499526
500527 for i in self .retry_with_reload ():
528+ waitForIndex (client .redis , 'idx' )
501529 # Search for f3 value. All documents should have it
502530 res = client .search ('@f3:f3_val' )
503531 self .assertEqual (2 , res .total )
@@ -519,6 +547,7 @@ def testNoCreate(self):
519547 client .add_document ('doc2' , f3 = 'f3_val' , no_create = True , partial = True )
520548
521549 for i in self .retry_with_reload ():
550+ waitForIndex (client .redis , 'idx' )
522551 # Search for f3 value. All documents should have it
523552 res = client .search ('@f3:f3_val' )
524553 self .assertEqual (2 , res .total )
@@ -542,18 +571,19 @@ def testSummarize(self):
542571 self .createIndex (client )
543572
544573 for i in self .retry_with_reload ():
574+ waitForIndex (client .redis , 'idx' )
545575 q = Query ('king henry' ).paging (0 , 1 )
546576 q .highlight (fields = ('play' , 'txt' ), tags = ('<b>' , '</b>' ))
547577 q .summarize ('txt' )
548578
549- res = client .search (q )
550- doc = res .docs [0 ]
579+ doc = sorted (client .search (q ).docs )[0 ]
551580 self .assertEqual ('<b>Henry</b> IV' , doc .play )
552581 self .assertEqual ('ACT I SCENE I. London. The palace. Enter <b>KING</b> <b>HENRY</b>, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR... ' ,
553582 doc .txt )
554583
555584 q = Query ('king henry' ).paging (0 , 1 ).summarize ().highlight ()
556- doc = client .search (q ).docs [0 ]
585+
586+ doc = sorted (client .search (q ).docs )[0 ]
557587 self .assertEqual ('<b>Henry</b> ... ' , doc .play )
558588 self .assertEqual ('ACT I SCENE I. London. The palace. Enter <b>KING</b> <b>HENRY</b>, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR... ' ,
559589 doc .txt )
@@ -571,7 +601,7 @@ def testTags(self):
571601 client .add_document ('doc1' , txt = 'fooz barz' , tags = 'foo,foo bar,hello;world' )
572602
573603 for i in r .retry_with_rdb_reload ():
574-
604+ waitForIndex ( r , 'idx' )
575605 q = Query ("@tags:{foo}" )
576606 res = client .search (q )
577607 self .assertEqual (1 , res .total )
@@ -636,6 +666,7 @@ def testSpellCheck(self):
636666 client .add_document ('doc2' , f1 = 'very important' , f2 = 'lorem ipsum' )
637667
638668 for i in self .retry_with_reload ():
669+ waitForIndex (client .redis , 'idx' )
639670 res = client .spellcheck ('impornant' )
640671 self .assertEqual ('important' , res ['impornant' ][0 ]['suggestion' ])
641672
@@ -647,6 +678,7 @@ def testDictOps(self):
647678 client .create_index ((TextField ('f1' ), TextField ('f2' )))
648679
649680 for i in self .retry_with_reload ():
681+ waitForIndex (client .redis , 'idx' )
650682 # Add three items
651683 res = client .dict_add ('custom_dict' , 'item1' , 'item2' , 'item3' )
652684 self .assertEqual (3 , res )
@@ -787,6 +819,8 @@ def testIndexDefiniontion(self):
787819
788820 with conn as r :
789821 r .flushdb ()
822+ if not check_version_2 (r ):
823+ return
790824 client = Client ('test' , port = conn .port )
791825
792826 definition = IndexDefinition (async = True , prefix = ['hset:' , 'henry' ],
@@ -807,6 +841,8 @@ def testCreateClientDefiniontion(self):
807841
808842 with conn as r :
809843 r .flushdb ()
844+ if not check_version_2 (r ):
845+ return
810846 client = Client ('test' , port = conn .port )
811847
812848 definition = IndexDefinition (prefix = ['hset:' , 'henry' ])
0 commit comments