function compare_with_expected_folders
Compares SharePoint folders found via Microsoft Graph API against a predefined list of expected folder names from a reference screenshot, reporting matches, missing folders, and additional folders.
/tf/active/vicechatdev/SPFCsync/test_folder_structure.py
144 - 213
moderate
Purpose
This function validates SharePoint folder structure by comparing actual folders retrieved from a SharePoint site against an expected list. It's useful for testing, auditing, or verifying that a SharePoint site has the correct folder organization. The function provides detailed output showing which folders match expectations, which are missing, and which are additional/unexpected.
Source Code
def compare_with_expected_folders():
"""Compare found folders with what we expect from the screenshot."""
expected_folders = [
"00 Strategy",
"01 UCJ Research",
"02 Toxicology",
"03 CMC",
"04 Quality",
"05 Clinical",
"06 Regulatory",
"07 Electronic Data Room",
"08 IP",
"09 Compliance",
"1 - 1 ViceBio Management Team meeting",
"1 - 2 R&V Development Project Team meeting",
"1 - 3 Development Project Team meeting",
"1 - 4 Board meetings",
"10 Program Management",
"11 DEVELOPMENT DOCUMENTATION Management",
"13 - Source Documents - All functions"
]
print("\nš COMPARISON WITH EXPECTED FOLDERS:")
print("-" * 45)
try:
from sharepoint_graph_client import SharePointGraphClient
from config import Config
client = SharePointGraphClient(
Config.SHAREPOINT_SITE_URL,
Config.AZURE_CLIENT_ID,
Config.AZURE_CLIENT_SECRET
)
found_folders = get_root_folders(client)
found_names = [f['name'] for f in found_folders]
print("ā
FOUND FOLDERS:")
for name in sorted(found_names):
print(f" š {name}")
print(f"\nš Found {len(found_names)} folders")
print(f"š Expected ~{len(expected_folders)} folders from screenshot")
# Check for matches
matches = set(found_names) & set(expected_folders)
missing = set(expected_folders) - set(found_names)
extra = set(found_names) - set(expected_folders)
if matches:
print(f"\nā
MATCHING FOLDERS ({len(matches)}):")
for name in sorted(matches):
print(f" š {name}")
if missing:
print(f"\nā ļø EXPECTED BUT NOT FOUND ({len(missing)}):")
for name in sorted(missing):
print(f" š {name}")
if extra:
print(f"\nš ADDITIONAL FOLDERS FOUND ({len(extra)}):")
for name in sorted(extra):
print(f" š {name}")
return len(found_names) > 0
except Exception as e:
print(f"ā Comparison failed: {e}")
return False
Return Value
Returns a boolean value: True if at least one folder was found in the SharePoint site (indicating successful connection and retrieval), False if the comparison failed due to an exception or no folders were found.
Dependencies
sharepoint_graph_clientconfigrequests
Required Imports
from sharepoint_graph_client import SharePointGraphClient
from config import Config
Conditional/Optional Imports
These imports are only needed under specific conditions:
from sharepoint_graph_client import SharePointGraphClient
Condition: imported inside try block when executing the comparison logic
Required (conditional)from config import Config
Condition: imported inside try block when executing the comparison logic
Required (conditional)Usage Example
# Ensure config.py has required settings:
# Config.SHAREPOINT_SITE_URL = 'https://yourcompany.sharepoint.com/sites/yoursite'
# Config.AZURE_CLIENT_ID = 'your-client-id'
# Config.AZURE_CLIENT_SECRET = 'your-client-secret'
# Ensure get_root_folders function is defined:
def get_root_folders(client):
# Implementation to retrieve folders
return client.list_folders('/')
# Call the comparison function
success = compare_with_expected_folders()
if success:
print('Comparison completed successfully')
else:
print('Comparison failed or no folders found')
Best Practices
- Ensure the Config class has all required SharePoint and Azure AD credentials configured before calling this function
- The function depends on a 'get_root_folders' function being available in the same scope - ensure this dependency is satisfied
- The expected_folders list is hardcoded and should be updated if the reference screenshot or expected structure changes
- This function prints output directly to console - consider capturing stdout if you need to process the output programmatically
- The function catches all exceptions broadly - consider more specific exception handling for production use
- The SharePointGraphClient must be properly implemented with authentication handling for Microsoft Graph API
- Ensure proper Azure AD app permissions are configured (Sites.Read.All or similar) for SharePoint access
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function search_for_folders 73.5% similar
-
function test_folder_structure 73.1% similar
-
function main_v21 72.9% similar
-
function analyze_structure 72.7% similar
-
function explore_alternative_endpoints 70.9% similar