@@ -605,6 +605,54 @@ def test_handle_close_pair_decision_same_tag_folder(test_db):
605605 args , _ = mock_move .call_args
606606 assert args [0 ] == file_b ['id' ]
607607
608+ def test_get_course_suggestions_top_level ():
609+ mock_folders = {
610+ 'course1' : {'public' : 'link1' , 'private' : 'link2' },
611+ 'course2' : {'public' : 'link3' , 'private' : 'link4' },
612+ }
613+ with mock .patch ('gdrive.FOLDERS_DATA' , return_value = mock_folders ):
614+ suggestions = gdrive .get_course_suggestions ('course' )
615+ assert set (suggestions ) == {'course1' , 'course2' }
616+
617+ suggestions = gdrive .get_course_suggestions ('course1' )
618+ assert suggestions == ['course1' ]
619+
620+ def test_get_course_suggestions_subfolders (test_db ):
621+ mock_folders = {
622+ 'course1' : {'public' : 'pub1' , 'private' : 'priv1' },
623+ }
624+
625+ # Mock folderlink_to_id
626+ with mock .patch ('gdrive.folderlink_to_id' , return_value = 'priv1_id' ), \
627+ mock .patch ('gdrive.FOLDERS_DATA' , return_value = mock_folders ), \
628+ mock .patch ('gdrive.gcache' , test_db ):
629+
630+ # Inject items into test_db
631+ with test_db ._lock :
632+ sql = "INSERT INTO drive_items (id, version, name, mime_type, parent_id, modified_time, owner) VALUES (?, ?, ?, ?, ?, ?, 1)"
633+ now = '2024-01-01T00:00:00Z'
634+ test_db .cursor .execute (sql , ('priv1_id' , 1 , 'Course 1' , 'application/vnd.google-apps.folder' , 'root' , now ))
635+ test_db .cursor .execute (sql , ('sub1_id' , 1 , 'Sub Folder 1' , 'application/vnd.google-apps.folder' , 'priv1_id' , now ))
636+ test_db .cursor .execute (sql , ('sub2_id' , 1 , 'Another Sub' , 'application/vnd.google-apps.folder' , 'priv1_id' , now ))
637+ test_db .cursor .execute (sql , ('subsub1_id' , 1 , 'Nested One' , 'application/vnd.google-apps.folder' , 'sub1_id' , now ))
638+ test_db .conn .commit ()
639+
640+ # course1/ -> matches subfolders of priv1_id
641+ suggestions = gdrive .get_course_suggestions ('course1/' )
642+ assert 'course1/Sub Folder 1/' in suggestions
643+ assert 'course1/Another Sub' in suggestions
644+
645+ # course1/sub -> matches Sub Folder 1 (Another Sub doesn't contain "sub")
646+ # Wait, q in f['name'].lower()
647+ # "Sub Folder 1" contains "sub"
648+ # "Another Sub" contains "sub"
649+ suggestions = gdrive .get_course_suggestions ('course1/sub' )
650+ assert set (suggestions ) == {'course1/Sub Folder 1/' , 'course1/Another Sub' }
651+
652+ # course1/Sub Folder 1/n -> matches Nested One
653+ suggestions = gdrive .get_course_suggestions ('course1/Sub Folder 1/n' )
654+ assert suggestions == ['course1/Sub Folder 1/Nested One' ]
655+
608656if __name__ == "__main__" :
609657 if len (sys .argv ) > 2 and sys .argv [1 ] == "extract" :
610658 extract_to_test_db (sys .argv [2 :])
0 commit comments