-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqa_test.js
More file actions
164 lines (150 loc) · 4.71 KB
/
qa_test.js
File metadata and controls
164 lines (150 loc) · 4.71 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
/**
* QA Test Script - Test SDK against live API
*/
import { OilPriceAPI } from './dist/index.js';
const API_KEY = process.env.OIL_PRICE_API_KEY || '3839c085460dd3a9dac1291f937f5a6d1740e8c668c766bc9f95e166af59cb11';
async function runQATests() {
console.log('=== Node.js SDK v0.3.0 QA Tests ===\n');
const client = new OilPriceAPI({
apiKey: API_KEY,
retries: 2,
timeout: 120000, // 2 minutes for slow historical queries
debug: false
});
let passed = 0;
let failed = 0;
// Test 1: Get Latest Prices (All)
try {
console.log('Test 1: getLatestPrices() - All commodities');
const prices = await client.getLatestPrices();
if (prices && Array.isArray(prices) && prices.length > 0) {
console.log(`✅ PASS - Got ${prices.length} prices`);
console.log(` Sample: ${prices[0].code} = ${prices[0].formatted}`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 2: Get Latest Price (Specific Commodity)
try {
console.log('Test 2: getLatestPrices({ commodity: "WTI_USD" })');
const prices = await client.getLatestPrices({ commodity: 'WTI_USD' });
if (prices && prices.length === 1 && prices[0].code === 'WTI_USD') {
console.log(`✅ PASS - WTI = ${prices[0].formatted}`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 3: Get Historical Prices
try {
console.log('Test 3: getHistoricalPrices({ period: "past_week", commodity: "WTI_USD" })');
const prices = await client.getHistoricalPrices({
period: 'past_week',
commodity: 'WTI_USD'
});
if (prices && Array.isArray(prices) && prices.length > 0) {
console.log(`✅ PASS - Got ${prices.length} historical data points`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 4: Get Commodities
try {
console.log('Test 4: getCommodities()');
const result = await client.getCommodities();
if (result && result.commodities && result.commodities.length > 0) {
console.log(`✅ PASS - Got ${result.commodities.length} commodities`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 5: Get Commodity Categories
try {
console.log('Test 5: getCommodityCategories()');
const categories = await client.getCommodityCategories();
if (categories && typeof categories === 'object') {
const keys = Object.keys(categories);
console.log(`✅ PASS - Got ${keys.length} categories`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 6: Get Specific Commodity
try {
console.log('Test 6: getCommodity("WTI_USD")');
const commodity = await client.getCommodity('WTI_USD');
if (commodity && commodity.code === 'WTI_USD') {
console.log(`✅ PASS - Got ${commodity.name} metadata`);
passed++;
} else {
console.log('❌ FAIL - Invalid response');
failed++;
}
} catch (error) {
console.log('❌ FAIL -', error.message);
failed++;
}
console.log();
// Test 7: Error Handling (Invalid Commodity)
try {
console.log('Test 7: Error Handling - getCommodity("INVALID_CODE")');
await client.getCommodity('INVALID_CODE');
console.log('❌ FAIL - Should have thrown error');
failed++;
} catch (error) {
if (error.name === 'NotFoundError' || error.statusCode === 404) {
console.log('✅ PASS - Correctly threw NotFoundError');
passed++;
} else {
console.log('❌ FAIL - Wrong error type:', error.name);
failed++;
}
}
console.log();
// Summary
console.log('=== QA Test Results ===');
console.log(`Total Tests: ${passed + failed}`);
console.log(`✅ Passed: ${passed}`);
console.log(`❌ Failed: ${failed}`);
console.log(`Success Rate: ${Math.round((passed / (passed + failed)) * 100)}%`);
if (failed === 0) {
console.log('\n🎉 All tests passed! SDK is ready for users.');
process.exit(0);
} else {
console.log('\n⚠️ Some tests failed. Review before sharing with users.');
process.exit(1);
}
}
runQATests().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});