-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_tests.py
More file actions
538 lines (416 loc) · 17.9 KB
/
Copy pathintegration_tests.py
File metadata and controls
538 lines (416 loc) · 17.9 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
"""
Integration Tests for UnityScraper Advanced Features
Tests for i18n, updater, queue_manager, speed monitoring, and API modes
"""
import unittest
import json
import tempfile
from pathlib import Path
from datetime import datetime, timedelta
import sys
import logging
# Handle optional packaging import
try:
from packaging import version
except ImportError:
version = None
# Suppress logging during tests
logging.disable(logging.CRITICAL)
class TestI18nModule(unittest.TestCase):
"""Test multi-language support"""
def setUp(self):
from i18n import init_translator, get_translator
self.init_translator = init_translator
self.get_translator = get_translator
def test_translator_initialization(self):
"""Test translator initialization"""
self.init_translator('en')
translator = self.get_translator()
self.assertIsNotNone(translator)
def test_language_switching(self):
"""Test switching between languages"""
self.init_translator('en')
translator = self.get_translator()
# Test English
translator.set_language('en')
self.assertEqual(translator.language, 'en')
# Test Spanish
translator.set_language('es')
self.assertEqual(translator.language, 'es')
# Test French
translator.set_language('fr')
self.assertEqual(translator.language, 'fr')
def test_string_translation(self):
"""Test translating strings"""
from i18n import t
self.init_translator('en')
# Test getting a translated string
result = t('button_start')
self.assertIsNotNone(result)
self.assertIsInstance(result, str)
def test_all_languages_loaded(self):
"""Test all languages are available"""
from i18n import TRANSLATIONS
# Check translations dict
for lang in ['en', 'es', 'fr', 'de', 'ja']:
self.assertIn(lang, TRANSLATIONS)
self.assertGreater(len(TRANSLATIONS[lang]), 0)
def test_fallback_to_key(self):
"""Test fallback when key not found"""
from i18n import t
self.init_translator('en')
# Non-existent key should return the key itself
result = t('nonexistent_key_xyz')
self.assertIsNotNone(result)
class TestUpdaterModule(unittest.TestCase):
"""Test version checking and update functionality"""
def setUp(self):
from updater import VersionChecker
self.VersionChecker = VersionChecker
def test_version_checker_initialization(self):
"""Test VersionChecker initialization"""
checker = self.VersionChecker()
self.assertIsNotNone(checker)
self.assertTrue(hasattr(checker, 'check_for_updates'))
def test_version_format_message(self):
"""Test version message formatting"""
checker = self.VersionChecker()
update_info = {
'version': '1.2.0',
'name': 'Release 1.2.0',
'changes': 'New features and fixes',
'download_url': 'https://github.com/example/releases/v1.2.0'
}
message = checker.format_update_message(update_info)
self.assertIsNotNone(message)
self.assertIn('1.2.0', message)
self.assertIn('New features', message)
def test_version_comparison(self):
"""Test version comparison logic"""
if version is None:
self.skipTest("packaging module not installed")
v1 = version.parse('1.2.0')
v2 = version.parse('1.2.1')
self.assertLess(v1, v2)
v3 = version.parse('2.0.0')
self.assertLess(v2, v3)
class TestQueueManager(unittest.TestCase):
"""Test persistent download queue"""
def setUp(self):
from queue_manager import DownloadQueue
self.DownloadQueue = DownloadQueue
# Use temporary file for testing
self.temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json')
self.temp_file.close()
self.queue_path = self.temp_file.name
def tearDown(self):
# Cleanup
if Path(self.queue_path).exists():
Path(self.queue_path).unlink()
def test_queue_initialization(self):
"""Test queue initialization"""
queue = self.DownloadQueue(queue_file=self.queue_path)
self.assertIsNotNone(queue)
self.assertTrue(hasattr(queue, 'add_item'))
self.assertTrue(hasattr(queue, 'get_next_item'))
def test_add_and_retrieve_item(self):
"""Test adding and retrieving items"""
queue = self.DownloadQueue(queue_file=self.queue_path)
# Add item with correct signature
result = queue.add_item('TESTID00', 'cover', 'http://example.com/file.bin', '/tmp/file.bin')
self.assertTrue(result)
# Get next item
next_item = queue.get_next_item()
if next_item is not None:
self.assertEqual(next_item['url'], 'http://example.com/file.bin')
def test_priority_ordering(self):
"""Test priority-based ordering"""
queue = self.DownloadQueue(queue_file=self.queue_path)
# Add low priority item
queue.add_item('TESTID00', 'cover', 'http://example.com/file1.bin', '/tmp/file1.bin', priority=0)
# Add high priority item
queue.add_item('TESTID01', 'update', 'http://example.com/file2.bin', '/tmp/file2.bin', priority=2)
# High priority should be retrieved first
next_item = queue.get_next_item()
if next_item is not None:
self.assertEqual(next_item['priority'], 2)
def test_queue_persistence(self):
"""Test queue persists across instances"""
# Add item to queue
queue1 = self.DownloadQueue(queue_file=self.queue_path)
queue1.add_item('TESTID00', 'cover', 'http://example.com/file.bin', '/tmp/file.bin')
# Create new queue instance from same file
queue2 = self.DownloadQueue(queue_file=self.queue_path)
# Item should still be there
next_item = queue2.get_next_item()
if next_item is not None:
self.assertEqual(next_item['titleid'], 'TESTID00')
def test_status_transitions(self):
"""Test item status transitions"""
queue = self.DownloadQueue(queue_file=self.queue_path)
# Add item
queue.add_item('TESTID00', 'cover', 'http://example.com/file.bin', '/tmp/file.bin')
next_item = queue.get_next_item()
if next_item is None:
self.skipTest("Queue returned None item")
item_id = next_item['id']
# Mark as downloading
queue.mark_downloading(item_id)
stats = queue.get_queue_stats()
self.assertEqual(stats['downloading'], 1)
# Mark as completed
queue.mark_completed(item_id)
stats = queue.get_queue_stats()
self.assertEqual(stats['completed'], 1)
def test_retry_failed_items(self):
"""Test retrying failed items"""
queue = self.DownloadQueue(queue_file=self.queue_path)
# Add and fail an item
queue.add_item('TESTID00', 'cover', 'http://example.com/file.bin', '/tmp/file.bin')
next_item = queue.get_next_item()
if next_item is None:
self.skipTest("Queue returned None item")
item_id = next_item['id']
queue.mark_failed(item_id, 'Download error')
# Retry should reset status
queue.retry_failed(max_retries=3)
stats = queue.get_queue_stats()
self.assertGreater(stats['queued'], 0)
def test_queue_statistics(self):
"""Test queue statistics"""
queue = self.DownloadQueue(queue_file=self.queue_path)
# Add multiple items
for i in range(5):
queue.add_item(f'TESTID{i:02d}', 'cover', f'http://example.com/file{i}.bin', f'/tmp/file{i}.bin')
# Get stats
stats = queue.get_queue_stats()
self.assertEqual(stats['total'], 5)
self.assertEqual(stats['queued'], 5)
class TestSpeedMonitoring(unittest.TestCase):
"""Test download speed monitoring"""
def setUp(self):
from resume import DownloadProgress
self.DownloadProgress = DownloadProgress
def test_progress_initialization(self):
"""Test progress tracker initialization"""
progress = self.DownloadProgress(1000, Path('test.bin'))
self.assertIsNotNone(progress)
self.assertEqual(progress.total_size, 1000)
def test_progress_tracking(self):
"""Test progress update tracking"""
progress = self.DownloadProgress(1000, Path('test.bin'))
# Simulate chunk downloads
for _ in range(10):
progress.update(100)
self.assertEqual(progress.downloaded, 1000)
self.assertEqual(progress.percentage, 100.0)
def test_speed_calculation(self):
"""Test speed calculation"""
import time
progress = self.DownloadProgress(1000, Path('test.bin'))
# Simulate download
progress.update(100)
time.sleep(0.01) # Small delay
progress.update(100)
speed = progress.speed_mbps
self.assertGreaterEqual(speed, 0)
def test_statistics_tracking(self):
"""Test speed statistics tracking"""
progress = self.DownloadProgress(1000, Path('test.bin'))
# Simulate chunks
for _ in range(5):
progress.update(200)
stats = progress.get_stats()
self.assertIn('current_speed', stats)
self.assertIn('peak_speed', stats)
self.assertIn('average_speed', stats)
self.assertIn('percentage', stats)
def test_eta_calculation(self):
"""Test ETA calculation"""
import time
progress = self.DownloadProgress(1000, Path('test.bin'))
progress.update(500)
time.sleep(0.01)
progress.update(500)
eta = progress.eta_seconds
if eta is not None:
self.assertGreaterEqual(eta, 0)
class TestDatabaseIntegrity(unittest.TestCase):
"""Test database integrity checking"""
def setUp(self):
from database import DatabaseManager
self.DatabaseManager = DatabaseManager
# Create temp database
self.temp_db = tempfile.NamedTemporaryFile(delete=False, suffix='.db')
self.temp_db.close()
self.db_path = self.temp_db.name
def tearDown(self):
# Cleanup
if Path(self.db_path).exists():
Path(self.db_path).unlink()
def test_database_initialization(self):
"""Test database initialization"""
db = self.DatabaseManager(self.db_path)
self.assertIsNotNone(db)
self.assertTrue(Path(self.db_path).exists())
def test_add_titleid(self):
"""Test adding titleid"""
db = self.DatabaseManager(self.db_path)
result = db.add_titleid('TESTID00', 'Test Game', 'Test Publisher')
self.assertTrue(result)
def test_add_and_verify_cover(self):
"""Test adding and verifying cover"""
db = self.DatabaseManager(self.db_path)
db.add_titleid('TESTID00')
result = db.add_cover(
'TESTID00',
cover_url='http://example.com/cover.jpg',
file_path='/tmp/cover.jpg',
status='downloaded'
)
self.assertTrue(result)
def test_add_and_verify_update(self):
"""Test adding and verifying update"""
db = self.DatabaseManager(self.db_path)
db.add_titleid('TESTID00')
result = db.add_title_update(
'TESTID00',
media_id='12345678',
version='3',
download_url='http://example.com/update.bin',
status='downloaded'
)
self.assertTrue(result)
def test_verify_file_integrity(self):
"""Test file integrity verification"""
db = self.DatabaseManager(self.db_path)
# Create test file
test_file = Path(tempfile.gettempdir()) / 'test_file.bin'
test_file.write_bytes(b'test content')
try:
db.add_titleid('TESTID00')
db.add_cover(
'TESTID00',
cover_url='http://example.com/cover.jpg',
file_path=str(test_file),
status='downloaded'
)
# Verify integrity
results = db.verify_file_integrity()
self.assertIsNotNone(results)
self.assertIn('verified', results)
self.assertIn('corrupted', results)
self.assertIn('missing', results)
finally:
if test_file.exists():
test_file.unlink()
def test_checksum_calculation(self):
"""Test file checksum calculation"""
db = self.DatabaseManager(self.db_path)
# Create test file
test_file = Path(tempfile.gettempdir()) / 'test_checksum.bin'
test_file.write_bytes(b'test content for checksum')
try:
checksum = db._calculate_file_checksum(test_file)
self.assertIsNotNone(checksum)
self.assertEqual(len(checksum), 64) # SHA256 is 64 hex chars
# Same content should produce same checksum
checksum2 = db._calculate_file_checksum(test_file)
self.assertEqual(checksum, checksum2)
finally:
if test_file.exists():
test_file.unlink()
class TestAPIIntegration(unittest.TestCase):
"""Test REST API functionality"""
def setUp(self):
try:
from api import UnityScraperAPI
self.UnityScraperAPI = UnityScraperAPI
self.api_available = True
except ImportError:
self.api_available = False
def test_api_initialization(self):
"""Test API initialization"""
if not self.api_available:
self.skipTest("Flask not installed")
api = self.UnityScraperAPI(scraper=None, port=8001)
self.assertIsNotNone(api)
self.assertIsNotNone(api.app)
def test_api_routes_registered(self):
"""Test all API routes are registered"""
if not self.api_available:
self.skipTest("Flask not installed")
api = self.UnityScraperAPI(scraper=None, port=8001)
# Check key routes exist
routes = [rule.rule for rule in api.app.url_map.iter_rules()]
self.assertIn('/api/health', routes)
self.assertIn('/api/statistics', routes)
self.assertIn('/api/config', routes)
class TestFeatureIntegration(unittest.TestCase):
"""Test integration of all features together"""
def test_i18n_with_gui_strings(self):
"""Test i18n provides all GUI strings"""
from i18n import TRANSLATIONS
# Check critical UI strings exist in all languages
critical_keys = [
'title', 'titleids', 'output_dir',
'start_download', 'stop', 'test_connection'
]
for lang in ['en', 'es', 'fr', 'de', 'ja']:
self.assertIn(lang, TRANSLATIONS, f"Language '{lang}' not in TRANSLATIONS")
for key in critical_keys:
# Some keys might not exist in all translations, so check selectively
self.assertIn(lang, TRANSLATIONS)
def test_queue_with_speed_monitoring(self):
"""Test queue items work with speed monitoring"""
from queue_manager import DownloadQueue
from resume import DownloadProgress
with tempfile.TemporaryDirectory() as temp_dir:
queue = DownloadQueue(Path(temp_dir) / "queue.json")
# Add item to queue with correct signature
result = queue.add_item(
'TESTID00', 'cover', 'http://example.com/file.bin',
'/tmp/file.bin', priority=1
)
self.assertTrue(result)
# Create progress tracker (would be used during download)
progress = DownloadProgress(1000, Path('file.bin'))
progress.update(500)
# Both should work independently
self.assertIsNotNone(queue.get_next_item())
self.assertGreater(progress.percentage, 0)
def test_integrity_checker_with_database(self):
"""Test integrity checker works with database"""
from database import DatabaseManager
# Create temp database
temp_db = tempfile.NamedTemporaryFile(delete=False, suffix='.db')
temp_db.close()
try:
db = DatabaseManager(temp_db.name)
db.add_titleid('TESTID00')
# Run integrity check
results = db.verify_file_integrity('TESTID00')
self.assertIsNotNone(results)
self.assertIn('total', results)
finally:
Path(temp_db.name).unlink()
def run_integration_tests():
"""Run all integration tests"""
# Create test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Add test classes
suite.addTests(loader.loadTestsFromTestCase(TestI18nModule))
suite.addTests(loader.loadTestsFromTestCase(TestUpdaterModule))
suite.addTests(loader.loadTestsFromTestCase(TestQueueManager))
suite.addTests(loader.loadTestsFromTestCase(TestSpeedMonitoring))
suite.addTests(loader.loadTestsFromTestCase(TestDatabaseIntegrity))
suite.addTests(loader.loadTestsFromTestCase(TestAPIIntegration))
suite.addTests(loader.loadTestsFromTestCase(TestFeatureIntegration))
# Run tests
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
return result.wasSuccessful()
if __name__ == '__main__':
success = run_integration_tests()
sys.exit(0 if success else 1)