generated from nogibjj/Allen_Wang_miniproj_7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
54 lines (46 loc) · 1.76 KB
/
test_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
server_h = os.getenv("SERVER_HOSTNAME")
access_token = os.getenv("ACCESS_TOKEN")
FILESTORE_PATH = "dbfs:/FileStore/Allen_mini_project11/zw_308_transformed_drink"
url = f"https://{server_h}/api/2.0"
# Function to check if a file path exists and if the authorization settings still work
def check_filestore_path(path, headers):
"""
Checks if the specified path exists in the Databricks Filestore.
"""
try:
# Make a GET request to check the file path status
response = requests.get(f"{url}/dbfs/get-status?path={path}", headers=headers)
# Raise an error for bad responses
response.raise_for_status()
# Check if path is found in the response
if response.json().get('path') is not None:
print(f"File path '{path}' exists in Databricks Filestore.")
return True
else:
print(f"File path '{path}' does not exist.")
return False
except requests.exceptions.RequestException as e:
# Print detailed error message
print(f"Error checking file path: {e}")
return False
# Test if the specified FILESTORE_PATH exists
def test_databricks():
"""
Test function to check if the FILESTORE_PATH exists.
"""
headers = {'Authorization': f'Bearer {access_token}'}
# Perform the path check
file_exists = check_filestore_path(FILESTORE_PATH, headers)
assert file_exists
# Assert if the file exists (or could handle failure more gracefully)
if file_exists:
print("Test Passed: File exists.")
else:
print("Test Failed: File does not exist.")
if __name__ == "__main__":
test_databricks()