-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_test.py
More file actions
executable file
·136 lines (110 loc) · 5.05 KB
/
Copy pathlocal_test.py
File metadata and controls
executable file
·136 lines (110 loc) · 5.05 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""
Local testing script for the get-transcript function
This allows testing the core functionality without deploying to Firebase
"""
import os
import sys
import json
from unittest.mock import Mock, patch
# Add the functions directory to the path
sys.path.insert(0, 'functions')
# Mock environment variables for local testing
os.environ['GCP_PROJECT'] = 'test-project'
os.environ['GOOGLE_CLOUD_PROJECT'] = 'test-project'
def mock_secret_manager():
"""Mock Secret Manager for local testing"""
secrets = {
'PROXY_USERNAME': 'spm8v50ymm',
'PROXY_PASSWORD': 'gVxie4oev28muJwZM3',
'API_KEY': 'test-api-key-12345'
}
def mock_access_secret_version(request):
secret_name = request['name'].split('/')[-3] # Extract secret name
if secret_name in secrets:
mock_response = Mock()
mock_response.payload.data.decode.return_value = secrets[secret_name]
return mock_response
else:
raise Exception(f"Secret {secret_name} not found")
return mock_access_secret_version
def test_transcript_service():
"""Test the TranscriptService class locally"""
# Mock the Secret Manager
with patch('google.cloud.secretmanager.SecretManagerServiceClient') as mock_client:
mock_client.return_value.access_secret_version = mock_secret_manager()
# Import after mocking
from main import TranscriptService
service = TranscriptService()
# Test video ID validation
print("Testing video ID validation...")
assert service._validate_video_id("dQw4w9WgXcQ") == True
assert service._validate_video_id("invalid123") == False
assert service._validate_video_id("") == False
assert service._validate_video_id(None) == False
print("✓ Video ID validation tests passed")
# Test proxy configuration
print("\nTesting proxy configuration...")
try:
proxy_config = service._get_proxy_config()
print(f"✓ Proxy configuration created successfully")
print(f" HTTP URL: {proxy_config.http_url}")
print(f" HTTPS URL: {proxy_config.https_url}")
except Exception as e:
print(f"✗ Proxy configuration failed: {e}")
# Test transcript retrieval (this will make actual API calls)
print("\nTesting transcript retrieval...")
test_video_id = "dQw4w9WgXcQ" # Rick Astley - Never Gonna Give You Up
try:
result = service.get_transcript(test_video_id)
print(f"✓ Transcript retrieved successfully")
print(f" Video ID: {result['videoId']}")
print(f" Language: {result['language']}")
print(f" Title: {result['title']}")
print(f" Transcript length: {len(result['transcript'])} characters")
print(f" First 100 chars: {result['transcript'][:100]}...")
except Exception as e:
print(f"✗ Transcript retrieval failed: {e}")
print(f" This might be due to proxy issues or YouTube blocking")
def test_flask_request_simulation():
"""Test the Flask request handling"""
with patch('google.cloud.secretmanager.SecretManagerServiceClient') as mock_client:
mock_client.return_value.access_secret_version = mock_secret_manager()
from main import get_transcript
# Mock Flask request for GET
print("\nTesting GET request simulation...")
mock_request = Mock()
mock_request.method = 'GET'
mock_request.headers = {'Authorization': 'Bearer test-api-key-12345'}
mock_request.args = {'videoId': 'dQw4w9WgXcQ'}
try:
with patch('functions_framework.http'):
response = get_transcript(mock_request)
print(f"✓ GET request handled successfully")
print(f" Response type: {type(response)}")
except Exception as e:
print(f"✗ GET request failed: {e}")
# Mock Flask request for POST
print("\nTesting POST request simulation...")
mock_request.method = 'POST'
mock_request.is_json = True
mock_request.get_json.return_value = {'videoId': 'dQw4w9WgXcQ'}
try:
response = get_transcript(mock_request)
print(f"✓ POST request handled successfully")
except Exception as e:
print(f"✗ POST request failed: {e}")
def main():
"""Run local tests"""
print("=== LOCAL TESTING FOR GET-TRANSCRIPT FUNCTION ===")
print("This script tests the core functionality without deploying to Firebase")
print()
# Test the service class
test_transcript_service()
# Test Flask request handling
test_flask_request_simulation()
print("\n=== LOCAL TESTING COMPLETED ===")
print("\nNote: Some tests may fail due to network restrictions or proxy issues.")
print("This is normal for local testing. Deploy to Firebase for full functionality.")
if __name__ == "__main__":
main()