42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
def main(request):
|
|
"""
|
|
Handles the request to fetch libraries.
|
|
|
|
Args:
|
|
request (dict): Simulated request object.
|
|
|
|
Returns:
|
|
dict: A response containing the list of libraries or an empty list if none are available.
|
|
"""
|
|
try:
|
|
# Simulated backend logic for fetching libraries
|
|
if "simulate_empty" in request and request["simulate_empty"]:
|
|
libraries = [] # No libraries available
|
|
else:
|
|
libraries = ["Library A", "Library B"] # Mocked available libraries
|
|
|
|
# Ensure consistent response for no results
|
|
response = {"libraries": libraries if libraries else []}
|
|
|
|
except Exception as e:
|
|
# Handle any unexpected errors gracefully
|
|
print(f"Error fetching libraries: {e}")
|
|
response = {"libraries": []}
|
|
|
|
return response
|
|
|
|
|
|
# Test cases for validation
|
|
if __name__ == "__main__":
|
|
test_data = [
|
|
{"input": {"simulate_empty": True}, "expected_output": {"libraries": []}},
|
|
{"input": {"simulate_empty": False}, "expected_output": {"libraries": ["Library A", "Library B"]}},
|
|
{"input": {}, "expected_output": {"libraries": ["Library A", "Library B"]}},
|
|
]
|
|
|
|
for idx, test in enumerate(test_data):
|
|
print(f"Running Test Case {idx + 1}: {test['input']}")
|
|
result = main(test["input"])
|
|
print(f"Output: {result}")
|
|
assert result == test["expected_output"], f"Test Case {idx + 1} Failed"
|
|
print("Test Passed\n") |