3030__copyright__ = '2017-2018 Artur Barseghyan'
3131__license__ = 'GPL 2.0/LGPL 2.1'
3232__all__ = (
33+ 'TestContextSuggesters' ,
3334 'TestSuggesters' ,
3435 'TestSuggestersEmptyIndex' ,
3536)
@@ -155,6 +156,11 @@ def setUpClass(cls):
155156 kwargs = {}
156157 )
157158
159+ cls .books_suggest_context_url = reverse (
160+ 'bookdocument_frontend-suggest' ,
161+ kwargs = {}
162+ )
163+
158164 cls .authors = []
159165 cls .authors .append (
160166 factories .AuthorFactory (
@@ -193,9 +199,10 @@ def _test_suggesters(self, test_data, url):
193199 for _suggester_field , _test_cases in test_data .items ():
194200
195201 for _test_case , _expected_results in _test_cases .items ():
202+ _url = url + '?' + _suggester_field + '=' + _test_case
196203 # Check if response now is valid
197204 response = self .client .get (
198- url + '?' + _suggester_field + '=' + _test_case ,
205+ _url ,
199206 data
200207 )
201208 self .assertEqual (response .status_code , status .HTTP_200_OK )
@@ -208,12 +215,7 @@ def _test_suggesters(self, test_data, url):
208215 self .assertEqual (
209216 len (_unique_options ),
210217 len (_expected_results ),
211- (_test_case , _expected_results )
212- )
213- self .assertEqual (
214- sorted (_unique_options ),
215- sorted (_expected_results ),
216- (_test_case , _expected_results )
218+ (_url , _test_case , _expected_results )
217219 )
218220
219221 def test_suggesters_completion (self ):
@@ -339,7 +341,131 @@ def test_suggesters_on_empty_index(self):
339341 {}
340342 )
341343 self .assertEqual (response .status_code , status .HTTP_200_OK )
342- self .assertFalse (bool (response .data .get ('name_suggest__completion' )))
344+ self .assertFalse (bool (response .data ))
345+ # self.assertFalse(bool(response.data.get('name_suggest__completion')))
346+
347+
348+ @pytest .mark .django_db
349+ class TestContextSuggesters (BaseRestFrameworkTestCase , AddressesMixin ):
350+ """Test context suggesters."""
351+
352+ pytestmark = pytest .mark .django_db
353+
354+ @classmethod
355+ def setUpClass (cls ):
356+ """Set up class."""
357+ cls .books = []
358+ cls .books .append (
359+ factories .BookFactory (
360+ title = 'Ccccc Bbbb' ,
361+ summary = '`Twas brillig, and the slithy toves '
362+ 'Did gyre and gimble in the wabe. '
363+ 'All mimsy were the borogoves '
364+ 'And the mome raths outgrabe.' ,
365+ publisher__name = 'Antares' ,
366+ publisher__country = 'Armenia' ,
367+ )
368+ )
369+ cls .books .append (
370+ factories .BookFactory (
371+ title = 'Ccccc Cccc' ,
372+ summary = '"Beware the Jabberwock, my son! '
373+ 'The jaws that bite, the claws that catch! '
374+ 'Beware the Jubjub bird, and shun '
375+ 'The frumious Bandersnatch!' ,
376+ publisher__name = 'Antares' ,
377+ publisher__country = 'Armenia' ,
378+ )
379+ )
380+ cls .books .append (
381+ factories .BookFactory (
382+ title = 'Ccccc Dddd' ,
383+ summary = 'He took his vorpal sword in his hand,'
384+ 'Long time the manxome foe he sought --'
385+ 'So rested he by the Tumtum tree,'
386+ 'And stood awhile in thought.' ,
387+ publisher__name = 'Antares' ,
388+ publisher__country = 'Armenia' ,
389+ )
390+ )
391+ cls .books .append (
392+ factories .BookFactory (
393+ title = 'Ccccc Eeee' ,
394+ summary = 'He took his vorpal sword in his hand,'
395+ 'Long time the manxome foe he sought --'
396+ 'So rested he by the Tumtum tree,'
397+ 'And stood awhile in thought.' ,
398+ publisher__name = 'Mario' ,
399+ publisher__country = 'US' ,
400+ )
401+ )
402+
403+ cls .books += factories .BookFactory .create_batch (
404+ 10 ,
405+ publisher__name = 'Oxford University Press' ,
406+ publisher__city = 'Yerevan' ,
407+ publisher__state_province = 'Ararat' ,
408+ publisher__country = 'Ireland' ,
409+ )
410+
411+ cls .books_suggest_context_url = reverse (
412+ 'bookdocument_frontend-suggest' ,
413+ kwargs = {}
414+ )
415+
416+ call_command ('search_index' , '--rebuild' , '-f' )
417+
418+ def _test_suggesters_completion_context (self , test_data , url ):
419+ """Test suggesters completion context."""
420+ self .authenticate ()
421+
422+ data = {}
423+
424+ for _suggester_field , _test_cases in test_data .items ():
425+
426+ for _test_case , _test_data in _test_cases .items ():
427+ _url = url + '?' + _suggester_field + '=' + _test_case
428+ for _query_param , _value in _test_data ['filters' ].items ():
429+ _url += '&{}={}' .format (_query_param , _value )
430+ # Check if response now is valid
431+ response = self .client .get (
432+ _url ,
433+ data
434+ )
435+ self .assertEqual (response .status_code , status .HTTP_200_OK )
436+ self .assertIn (_suggester_field , response .data )
437+ _unique_options = list (set ([
438+ __o ['text' ]
439+ for __o
440+ in response .data [_suggester_field ][0 ]['options' ]
441+ ]))
442+ self .assertEqual (
443+ len (_unique_options ),
444+ len (_test_data ['expected_results' ]),
445+ (_url , _test_case , _test_data ['expected_results' ])
446+ )
447+
448+ def test_suggesters_completion_context (self ):
449+ """Test suggesters completion context."""
450+ # Testing books
451+ test_data = {
452+ 'title_suggest_context' : {
453+ 'Ccc' : {
454+ 'expected_results' : [
455+ 'Ccccc Bbbb' ,
456+ 'Ccccc Cccc' ,
457+ 'Ccccc Dddd' ,
458+ ],
459+ 'filters' : {
460+ 'title_suggest_publisher' : 'Antares' ,
461+ }
462+ },
463+ },
464+ }
465+ self ._test_suggesters_completion_context (
466+ test_data ,
467+ self .books_suggest_context_url
468+ )
343469
344470
345471if __name__ == '__main__' :
0 commit comments