-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_appwrite_integration.py
More file actions
319 lines (248 loc) · 10.1 KB
/
Copy pathtest_appwrite_integration.py
File metadata and controls
319 lines (248 loc) · 10.1 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Test Script for Appwrite Integration
This script tests the Appwrite video metadata management system and provides
setup and migration functionality.
"""
import asyncio
import os
import sys
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Add src to path for imports
sys.path.append('src')
async def test_appwrite_setup():
"""Test Appwrite database and storage setup."""
print("🧪 Testing Appwrite Integration Setup")
print("=" * 50)
try:
from src.core.appwrite_integration import AppwriteVideoManager
# Initialize Appwrite manager
print("1. Initializing Appwrite manager...")
manager = AppwriteVideoManager()
if not manager.enabled:
print("❌ Appwrite not enabled. Check your credentials.")
return False
print("✅ Appwrite manager initialized")
# Setup database structure
print("\n2. Setting up database structure...")
setup_success = await manager.setup_database_structure()
if setup_success:
print("✅ Database structure setup completed")
else:
print("❌ Database setup failed")
return False
return True
except ImportError as e:
print(f"❌ Import error: {e}")
print("Make sure appwrite-sdk is installed: pip install appwrite")
return False
except Exception as e:
print(f"❌ Setup failed: {e}")
return False
async def test_video_management():
"""Test video record management."""
print("\n3. Testing Video Management...")
try:
from src.core.appwrite_integration import AppwriteVideoManager
manager = AppwriteVideoManager()
if not manager.enabled:
return False
# Create a test video record
video_id = await manager.create_video_record(
topic="Test Calculus Video",
description="A test video about derivatives",
scene_count=3,
session_id="test_session"
)
if video_id:
print(f"✅ Created test video: {video_id}")
# Update video status
await manager.update_video_status(video_id, "planning")
print("✅ Updated video status")
# Get video record
video_record = await manager.get_video_record(video_id)
if video_record:
print(f"✅ Retrieved video record: {video_record['topic']}")
# Create test scenes
for i in range(1, 4):
scene_id = await manager.create_scene_record(
video_id=video_id,
scene_number=i,
scene_plan=f"Scene {i} plan",
storyboard=f"Scene {i} storyboard"
)
if scene_id:
print(f"✅ Created scene {i}: {scene_id}")
# Get video scenes
scenes = await manager.get_video_scenes(video_id)
print(f"✅ Retrieved {len(scenes)} scenes for video")
return True
else:
print("❌ Failed to create test video")
return False
except Exception as e:
print(f"❌ Video management test failed: {e}")
return False
async def test_agent_memory():
"""Test agent memory functionality."""
print("\n4. Testing Agent Memory...")
try:
from src.core.appwrite_integration import AppwriteVideoManager
from src.core.appwrite_agent_memory import AppwriteAgentMemory
manager = AppwriteVideoManager()
if not manager.enabled:
return False
memory = AppwriteAgentMemory(manager)
# Store a test error-fix pattern
success = await memory.store_error_fix(
error_message="'Circle' object has no attribute 'animate'",
original_code="circle = Circle()\nself.play(circle.animate.shift(UP))",
fixed_code="circle = Circle()\nself.play(circle.animate.shift(UP))",
topic="geometry",
scene_type="animation",
fix_method="llm"
)
if success:
print("✅ Stored test error-fix pattern")
# Search for similar fixes
similar_fixes = await memory.search_similar_fixes(
error_message="Circle object error",
code_context="circle = Circle()",
topic="geometry",
scene_type="animation"
)
print(f"✅ Found {len(similar_fixes)} similar fixes")
# Get memory statistics
stats = await memory.get_memory_stats()
print(f"✅ Memory stats: {stats}")
return True
except Exception as e:
print(f"❌ Agent memory test failed: {e}")
return False
async def test_migration():
"""Test migration of existing data."""
print("\n5. Testing Data Migration...")
try:
from src.core.appwrite_integration import AppwriteVideoManager
manager = AppwriteVideoManager()
if not manager.enabled:
return False
# Test migration if output directory exists
output_dir = "output"
if os.path.exists(output_dir):
print(f"Found existing output directory: {output_dir}")
# Count existing data
video_count = 0
for item in os.listdir(output_dir):
if os.path.isdir(os.path.join(output_dir, item)):
video_count += 1
print(f"Found {video_count} potential videos to migrate")
if video_count > 0:
# Ask user if they want to migrate
response = input("Would you like to migrate existing data? (y/N): ")
if response.lower() == 'y':
success = await manager.migrate_existing_data(output_dir)
if success:
print("✅ Migration completed successfully")
else:
print("❌ Migration failed")
else:
print("Migration skipped")
else:
print("No data to migrate")
else:
print("No output directory found - skipping migration test")
return True
except Exception as e:
print(f"❌ Migration test failed: {e}")
return False
async def test_statistics():
"""Test statistics and analytics."""
print("\n6. Testing Statistics...")
try:
from src.core.appwrite_integration import AppwriteVideoManager
manager = AppwriteVideoManager()
if not manager.enabled:
return False
# Get video statistics
stats = await manager.get_video_statistics()
print("📊 Video Statistics:")
for key, value in stats.items():
print(f" {key}: {value}")
return True
except Exception as e:
print(f"❌ Statistics test failed: {e}")
return False
async def test_file_management():
"""Test file upload functionality."""
print("\n7. Testing File Management...")
try:
from src.core.appwrite_integration import AppwriteVideoManager
manager = AppwriteVideoManager()
if not manager.enabled:
return False
# Create a test file
test_file = "test_upload.txt"
with open(test_file, 'w') as f:
f.write("This is a test file for Appwrite upload")
try:
# Test file upload
file_id = await manager.upload_source_code(test_file, "test_scene_id")
if file_id:
print(f"✅ Uploaded test file: {file_id}")
# Get file URL
file_url = manager._get_file_url(manager.source_code_bucket_id, file_id)
print(f"✅ File URL: {file_url}")
else:
print("❌ File upload failed")
finally:
# Clean up test file
if os.path.exists(test_file):
os.remove(test_file)
return True
except Exception as e:
print(f"❌ File management test failed: {e}")
return False
async def main():
"""Run all tests."""
print("🚀 Appwrite Video Metadata Management System Test Suite")
print("=" * 60)
# Check environment variables
required_env_vars = ['APPWRITE_API_KEY', 'APPWRITE_PROJECT_ID']
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
if missing_vars:
print(f"❌ Missing environment variables: {', '.join(missing_vars)}")
print("Please set these in your .env file:")
print("APPWRITE_API_KEY=your_api_key_here")
print("APPWRITE_PROJECT_ID=your_project_id_here")
print("APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1 # optional")
return
tests = [
test_appwrite_setup,
test_video_management,
test_agent_memory,
test_migration,
test_statistics,
test_file_management
]
passed = 0
total = len(tests)
for test in tests:
try:
result = await test()
if result:
passed += 1
except Exception as e:
print(f"❌ Test failed with exception: {e}")
print(f"\n🎯 Test Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All tests passed! Appwrite integration is working correctly.")
print("\nNext steps:")
print("1. Update your video generation code to use AppwriteVideoManager")
print("2. Replace AgentMemory with AppwriteAgentMemory")
print("3. Use the migration function to move existing data")
else:
print("⚠️ Some tests failed. Please check the error messages above.")
if __name__ == "__main__":
asyncio.run(main())