File size: 10,332 Bytes
d6d843f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
تست کامل بانک اطلاعاتی رمزارز
Complete Crypto Data Bank Test Suite
"""

import asyncio
import sys
from pathlib import Path

# Add to path
sys.path.insert(0, str(Path(__file__).parent))

from crypto_data_bank.collectors.free_price_collector import FreePriceCollector
from crypto_data_bank.collectors.rss_news_collector import RSSNewsCollector
from crypto_data_bank.collectors.sentiment_collector import SentimentCollector
from crypto_data_bank.ai.huggingface_models import get_analyzer
from crypto_data_bank.database import get_db
from crypto_data_bank.orchestrator import get_orchestrator


async def test_price_collectors():
    """Test price collectors"""
    print("\n" + "="*70)
    print("💰 Testing Price Collectors")
    print("="*70)

    collector = FreePriceCollector()

    symbols = ["BTC", "ETH", "SOL"]

    # Test individual sources
    print("\nTesting individual sources...")

    try:
        coincap = await collector.collect_from_coincap(symbols)
        print(f"✅ CoinCap: {len(coincap)} prices")
    except Exception as e:
        print(f"⚠️  CoinCap: {e}")

    try:
        coingecko = await collector.collect_from_coingecko(symbols)
        print(f"✅ CoinGecko: {len(coingecko)} prices")
    except Exception as e:
        print(f"⚠️  CoinGecko: {e}")

    try:
        binance = await collector.collect_from_binance_public(symbols)
        print(f"✅ Binance: {len(binance)} prices")
    except Exception as e:
        print(f"⚠️  Binance: {e}")

    # Test aggregation
    print("\nTesting aggregation...")
    all_prices = await collector.collect_all_free_sources(symbols)
    aggregated = collector.aggregate_prices(all_prices)

    print(f"\n✅ Aggregated {len(aggregated)} prices from multiple sources")

    if aggregated:
        print("\nSample prices:")
        for price in aggregated[:3]:
            print(f"  {price['symbol']}: ${price['price']:,.2f} (from {price.get('sources_count', 0)} sources)")

    return len(aggregated) > 0


async def test_news_collectors():
    """Test news collectors"""
    print("\n" + "="*70)
    print("📰 Testing News Collectors")
    print("="*70)

    collector = RSSNewsCollector()

    print("\nTesting RSS feeds...")

    try:
        cointelegraph = await collector.collect_from_cointelegraph()
        print(f"✅ CoinTelegraph: {len(cointelegraph)} news")
    except Exception as e:
        print(f"⚠️  CoinTelegraph: {e}")

    try:
        coindesk = await collector.collect_from_coindesk()
        print(f"✅ CoinDesk: {len(coindesk)} news")
    except Exception as e:
        print(f"⚠️  CoinDesk: {e}")

    # Test all feeds
    print("\nTesting all RSS feeds...")
    all_news = await collector.collect_all_rss_feeds()
    total = sum(len(v) for v in all_news.values())
    print(f"\n✅ Collected {total} news items from {len(all_news)} sources")

    # Test deduplication
    unique_news = collector.deduplicate_news(all_news)
    print(f"✅ Deduplicated to {len(unique_news)} unique items")

    if unique_news:
        print("\nLatest news:")
        for news in unique_news[:3]:
            print(f"  • {news['title'][:60]}...")
            print(f"    Source: {news['source']}")

    # Test trending coins
    trending = collector.get_trending_coins(unique_news)
    if trending:
        print("\nTrending coins:")
        for coin in trending[:5]:
            print(f"  {coin['coin']}: {coin['mentions']} mentions")

    return len(unique_news) > 0


async def test_sentiment_collectors():
    """Test sentiment collectors"""
    print("\n" + "="*70)
    print("😊 Testing Sentiment Collectors")
    print("="*70)

    collector = SentimentCollector()

    # Test Fear & Greed
    print("\nTesting Fear & Greed Index...")
    try:
        fg = await collector.collect_fear_greed_index()
        if fg:
            print(f"✅ Fear & Greed: {fg['fear_greed_value']}/100 ({fg['fear_greed_classification']})")
        else:
            print("⚠️  Fear & Greed: No data")
    except Exception as e:
        print(f"⚠️  Fear & Greed: {e}")

    # Test all sentiment
    print("\nTesting all sentiment sources...")
    all_sentiment = await collector.collect_all_sentiment_data()

    if all_sentiment.get('overall_sentiment'):
        overall = all_sentiment['overall_sentiment']
        print(f"\n✅ Overall Sentiment: {overall['overall_sentiment']}")
        print(f"   Score: {overall['sentiment_score']}/100")
        print(f"   Confidence: {overall['confidence']:.2%}")

    return all_sentiment.get('overall_sentiment') is not None


async def test_ai_models():
    """Test AI models"""
    print("\n" + "="*70)
    print("🤖 Testing AI Models")
    print("="*70)

    analyzer = get_analyzer()

    # Test sentiment analysis
    print("\nTesting sentiment analysis...")
    test_texts = [
        "Bitcoin surges past $50,000 as institutional adoption accelerates",
        "SEC delays crypto ETF decision, causing market uncertainty",
        "Ethereum successfully completes major network upgrade"
    ]

    for i, text in enumerate(test_texts, 1):
        result = await analyzer.analyze_news_sentiment(text)
        print(f"\n{i}. {text[:50]}...")
        print(f"   Sentiment: {result['sentiment']}")
        print(f"   Confidence: {result.get('confidence', 0):.2%}")

    return True


async def test_database():
    """Test database operations"""
    print("\n" + "="*70)
    print("💾 Testing Database")
    print("="*70)

    db = get_db()

    # Test saving price
    print("\nTesting price storage...")
    test_price = {
        'price': 50000.0,
        'priceUsd': 50000.0,
        'change24h': 2.5,
        'volume24h': 25000000000,
        'marketCap': 980000000000,
    }

    try:
        db.save_price('BTC', test_price, 'test')
        print("✅ Price saved successfully")
    except Exception as e:
        print(f"❌ Failed to save price: {e}")
        return False

    # Test retrieving prices
    print("\nTesting price retrieval...")
    try:
        latest_prices = db.get_latest_prices(['BTC'], 1)
        print(f"✅ Retrieved {len(latest_prices)} prices")
    except Exception as e:
        print(f"❌ Failed to retrieve prices: {e}")
        return False

    # Get statistics
    print("\nDatabase statistics:")
    stats = db.get_statistics()
    print(f"  Prices: {stats.get('prices_count', 0)}")
    print(f"  News: {stats.get('news_count', 0)}")
    print(f"  AI Analysis: {stats.get('ai_analysis_count', 0)}")
    print(f"  Database size: {stats.get('database_size', 0):,} bytes")

    return True


async def test_orchestrator():
    """Test orchestrator"""
    print("\n" + "="*70)
    print("🎯 Testing Orchestrator")
    print("="*70)

    orchestrator = get_orchestrator()

    # Test single collection cycle
    print("\nTesting single collection cycle...")
    results = await orchestrator.collect_all_data_once()

    print(f"\n✅ Collection Results:")
    if results.get('prices', {}).get('success'):
        print(f"   Prices: {results['prices'].get('prices_saved', 0)} saved")
    else:
        print(f"   Prices: ⚠️  {results.get('prices', {}).get('error', 'Failed')}")

    if results.get('news', {}).get('success'):
        print(f"   News: {results['news'].get('news_saved', 0)} saved")
    else:
        print(f"   News: ⚠️  {results.get('news', {}).get('error', 'Failed')}")

    if results.get('sentiment', {}).get('success'):
        print(f"   Sentiment: ✅ Success")
    else:
        print(f"   Sentiment: ⚠️  Failed")

    # Get status
    status = orchestrator.get_collection_status()
    print(f"\n📊 Collection Status:")
    print(f"   Running: {status['is_running']}")
    print(f"   Last collection: {status.get('last_collection', {})}")

    return results.get('prices', {}).get('success', False)


async def main():
    """Run all tests"""
    print("\n" + "🧪"*35)
    print("CRYPTO DATA BANK - COMPREHENSIVE TEST SUITE")
    print("تست جامع بانک اطلاعاتی رمزارز")
    print("🧪"*35)

    results = {}

    # Run all tests
    try:
        results['price_collectors'] = await test_price_collectors()
    except Exception as e:
        print(f"\n❌ Price collectors test failed: {e}")
        results['price_collectors'] = False

    try:
        results['news_collectors'] = await test_news_collectors()
    except Exception as e:
        print(f"\n❌ News collectors test failed: {e}")
        results['news_collectors'] = False

    try:
        results['sentiment_collectors'] = await test_sentiment_collectors()
    except Exception as e:
        print(f"\n❌ Sentiment collectors test failed: {e}")
        results['sentiment_collectors'] = False

    try:
        results['ai_models'] = await test_ai_models()
    except Exception as e:
        print(f"\n❌ AI models test failed: {e}")
        results['ai_models'] = False

    try:
        results['database'] = await test_database()
    except Exception as e:
        print(f"\n❌ Database test failed: {e}")
        results['database'] = False

    try:
        results['orchestrator'] = await test_orchestrator()
    except Exception as e:
        print(f"\n❌ Orchestrator test failed: {e}")
        results['orchestrator'] = False

    # Summary
    print("\n" + "="*70)
    print("📊 TEST SUMMARY | خلاصه تست‌ها")
    print("="*70)

    passed = sum(1 for v in results.values() if v)
    total = len(results)

    for test_name, success in results.items():
        status = "✅ PASSED" if success else "❌ FAILED"
        print(f"{status} - {test_name.replace('_', ' ').title()}")

    print("\n" + "="*70)
    print(f"Results: {passed}/{total} tests passed ({passed/total*100:.0f}%)")
    print("="*70)

    if passed == total:
        print("\n🎉 ALL TESTS PASSED! System is ready to use!")
        print("🎉 همه تست‌ها موفق! سیستم آماده استفاده است!")
        return 0
    else:
        print(f"\n⚠️  {total - passed} test(s) failed. Please review the errors above.")
        print(f"⚠️  {total - passed} تست ناموفق. لطفاً خطاها را بررسی کنید.")
        return 1


if __name__ == "__main__":
    exit_code = asyncio.run(main())
    sys.exit(exit_code)