@@ -338,84 +338,6 @@ def test_querystringsearch_sort(self):
338338 f"{ self .portal .absolute_url ()} /testdocument9" ,
339339 )
340340
341-
342- class TestVHMPathCorrectionUnit (unittest .TestCase ):
343-
344- def test_path_correction_string (self ):
345- from plone .restapi .services .querystringsearch .get import QuerystringSearch
346- request = mock .Mock ()
347- request .get .return_value = ("Plone" , "en" )
348- # Mock json_body to return our query
349- with mock .patch ("plone.restapi.services.querystringsearch.get.json_body" ) as mock_json_body :
350- mock_json_body .return_value = {
351- "query" : [
352- {"i" : "path" , "o" : "op" , "v" : "/folder::1" }
353- ]
354- }
355- context = mock .Mock ()
356- service = QuerystringSearch (context , request )
357-
358- # We only want to test the path correction part, so we'll mock the rest of __call__
359- # by causing it to fail after the correction
360- with mock .patch ("plone.restapi.services.querystringsearch.get.getMultiAdapter" ) as mock_adapter :
361- mock_adapter .side_effect = Exception ("Stop here" )
362- try :
363- service ()
364- except Exception as e :
365- if str (e ) != "Stop here" :
366- raise
367-
368- # Check if query was modified in place (since it's a list of dicts)
369- query = mock_json_body .return_value ["query" ]
370- self .assertEqual (query [0 ]["v" ], "/Plone/en/folder::1" )
371-
372- def test_path_correction_list (self ):
373- from plone .restapi .services .querystringsearch .get import QuerystringSearch
374- request = mock .Mock ()
375- request .get .return_value = ("Plone" , "en" )
376- with mock .patch ("plone.restapi.services.querystringsearch.get.json_body" ) as mock_json_body :
377- mock_json_body .return_value = {
378- "query" : [
379- {"i" : "path" , "o" : "op" , "v" : ["/folder1" , "/folder2::2" , "some-uid" ]}
380- ]
381- }
382- context = mock .Mock ()
383- service = QuerystringSearch (context , request )
384-
385- with mock .patch ("plone.restapi.services.querystringsearch.get.getMultiAdapter" ) as mock_adapter :
386- mock_adapter .side_effect = Exception ("Stop here" )
387- try :
388- service ()
389- except Exception :
390- pass
391-
392- query = mock_json_body .return_value ["query" ]
393- self .assertEqual (query [0 ]["v" ], ["/Plone/en/folder1" , "/Plone/en/folder2::2" , "some-uid" ])
394-
395- def test_no_path_correction_without_vhm (self ):
396- from plone .restapi .services .querystringsearch .get import QuerystringSearch
397- request = mock .Mock ()
398- request .get .return_value = None
399- with mock .patch ("plone.restapi.services.querystringsearch.get.json_body" ) as mock_json_body :
400- mock_json_body .return_value = {
401- "query" : [
402- {"i" : "path" , "o" : "op" , "v" : "/folder::1" }
403- ]
404- }
405- context = mock .Mock ()
406- service = QuerystringSearch (context , request )
407-
408- with mock .patch ("plone.restapi.services.querystringsearch.get.getMultiAdapter" ) as mock_adapter :
409- mock_adapter .side_effect = Exception ("Stop here" )
410- try :
411- service ()
412- except Exception :
413- pass
414-
415- query = mock_json_body .return_value ["query" ]
416- self .assertEqual (query [0 ]["v" ], "/folder::1" )
417-
418-
419341 def test_querystringsearch__bad_json (self ):
420342 response = self .api_session .get ("/@querystring-search?query={" )
421343 self .assertEqual (response .status_code , 400 )
@@ -584,3 +506,89 @@ def test_querystringsearch_vhm_multiple_paths(self):
584506 items = {item ["@id" ] for item in response .json ().get ("items" , [])}
585507 self .assertIn (f"{ self .portal .absolute_url ()} /folder" , items )
586508 self .assertIn (f"{ self .portal .absolute_url ()} /folder2" , items )
509+
510+
511+ class TestVHMPathCorrectionUnit (unittest .TestCase ):
512+
513+ def test_path_correction_string (self ):
514+ from plone .restapi .services .querystringsearch .get import QuerystringSearch
515+
516+ request = mock .Mock ()
517+ request .get .return_value = ("Plone" , "en" )
518+ # Mock json_body to return our query
519+ with mock .patch (
520+ "plone.restapi.services.querystringsearch.get.json_body"
521+ ) as mock_json_body :
522+ mock_json_body .return_value = {"query" : [{"i" : "path" , "o" : "op" , "v" : "/folder::1" }]}
523+ context = mock .Mock ()
524+ service = QuerystringSearch (context , request )
525+
526+ # We only want to test the path correction part, so we'll mock the rest of __call__
527+ # by causing it to fail after the correction
528+ with mock .patch (
529+ "plone.restapi.services.querystringsearch.get.getMultiAdapter"
530+ ) as mock_adapter :
531+ mock_adapter .side_effect = Exception ("Stop here" )
532+ try :
533+ service ()
534+ except Exception as e :
535+ if str (e ) != "Stop here" :
536+ raise
537+
538+ # Check if query was modified in place (since it's a list of dicts)
539+ query = mock_json_body .return_value ["query" ]
540+ self .assertEqual (query [0 ]["v" ], "/Plone/en/folder::1" )
541+
542+ def test_path_correction_list (self ):
543+ from plone .restapi .services .querystringsearch .get import QuerystringSearch
544+
545+ request = mock .Mock ()
546+ request .get .return_value = ("Plone" , "en" )
547+ with mock .patch (
548+ "plone.restapi.services.querystringsearch.get.json_body"
549+ ) as mock_json_body :
550+ mock_json_body .return_value = {
551+ "query" : [
552+ {"i" : "path" , "o" : "op" , "v" : ["/folder1" , "/folder2::2" , "some-uid" ]}
553+ ]
554+ }
555+ context = mock .Mock ()
556+ service = QuerystringSearch (context , request )
557+
558+ with mock .patch (
559+ "plone.restapi.services.querystringsearch.get.getMultiAdapter"
560+ ) as mock_adapter :
561+ mock_adapter .side_effect = Exception ("Stop here" )
562+ try :
563+ service ()
564+ except Exception :
565+ pass
566+
567+ query = mock_json_body .return_value ["query" ]
568+ self .assertEqual (
569+ query [0 ]["v" ], ["/Plone/en/folder1" , "/Plone/en/folder2::2" , "some-uid" ]
570+ )
571+
572+ def test_no_path_correction_without_vhm (self ):
573+ from plone .restapi .services .querystringsearch .get import QuerystringSearch
574+
575+ request = mock .Mock ()
576+ request .get .return_value = None
577+ with mock .patch (
578+ "plone.restapi.services.querystringsearch.get.json_body"
579+ ) as mock_json_body :
580+ mock_json_body .return_value = {"query" : [{"i" : "path" , "o" : "op" , "v" : "/folder::1" }]}
581+ context = mock .Mock ()
582+ service = QuerystringSearch (context , request )
583+
584+ with mock .patch (
585+ "plone.restapi.services.querystringsearch.get.getMultiAdapter"
586+ ) as mock_adapter :
587+ mock_adapter .side_effect = Exception ("Stop here" )
588+ try :
589+ service ()
590+ except Exception :
591+ pass
592+
593+ query = mock_json_body .return_value ["query" ]
594+ self .assertEqual (query [0 ]["v" ], "/folder::1" )
0 commit comments