-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-stock-price-service.js
More file actions
56 lines (49 loc) · 2.74 KB
/
Copy pathtest-stock-price-service.js
File metadata and controls
56 lines (49 loc) · 2.74 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
// Test for the new stock price service
import {
fetchAndStoreLatestPrice,
fetchAndStoreHistoricalPrices,
calculatePerformanceFromStoredPrices,
ensureSufficientHistoricalData,
cleanupOldStockPrices
} from './server/stockPriceService.ts';
async function runTests() {
const ticker = 'FOXF';
console.log(`\n===== Testing stock price service with ticker: ${ticker} =====\n`);
try {
// Step 1: Fetch and store latest price
console.log('Step 1: Fetching and storing latest price...');
const latestPriceResult = await fetchAndStoreLatestPrice(ticker);
console.log('Latest price stored:', latestPriceResult ? 'Success' : 'Failed');
// Step 2: Ensure we have sufficient historical data for performance calculations
console.log('\nStep 2: Ensuring we have sufficient historical data...');
const historicalDataResult = await ensureSufficientHistoricalData(ticker);
console.log('Historical data check:', historicalDataResult ? 'Sufficient data exists or was fetched' : 'Failed to fetch data');
// Step 3: Calculate performance metrics based on stored data
console.log('\nStep 3: Calculating performance metrics from stored prices...');
const performanceMetrics = await calculatePerformanceFromStoredPrices(ticker);
if (performanceMetrics) {
console.log('Performance metrics:');
console.log(`- Current Price: $${performanceMetrics.price.toFixed(2)}`);
console.log(`- 1 Day Change: ${performanceMetrics.oneDayChange?.toFixed(2)}%`);
console.log(`- 1 Week Change: ${performanceMetrics.oneWeekChange?.toFixed(2)}%`);
console.log(`- 1 Month Change: ${performanceMetrics.oneMonthChange?.toFixed(2)}%`);
console.log(`- 6 Month Change: ${performanceMetrics.sixMonthChange?.toFixed(2)}%`);
console.log(`- YTD Change: ${performanceMetrics.ytdChange?.toFixed(2)}%`);
console.log(`- 1 Year Change: ${performanceMetrics.oneYearChange?.toFixed(2)}%`);
console.log(`- 5 Year Change: ${performanceMetrics.fiveYearChange?.toFixed(2)}%`);
} else {
console.log('Failed to calculate performance metrics');
}
// Step 4: Test cleanup of old stock prices (will not actually delete any recent data)
// Just testing that the function works
const cutoffDate = new Date();
cutoffDate.setFullYear(cutoffDate.getFullYear() - 10); // 10 years ago, shouldn't delete anything
console.log('\nStep 4: Testing cleanup of very old stock prices (older than 10 years)...');
const deletedCount = await cleanupOldStockPrices(3650); // 10 years retention
console.log(`Deleted ${deletedCount} old price records`);
console.log('\n===== Stock price service test completed =====\n');
} catch (error) {
console.error('Test failed with error:', error);
}
}
runTests();