-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-data-processing.js
More file actions
286 lines (243 loc) · 9.39 KB
/
test-data-processing.js
File metadata and controls
286 lines (243 loc) · 9.39 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
/**
* Node.js Test Runner for H1B Data Processing
* This file tests the core data processing functionality
*/
// Mock DataManager class for testing (extracted from the HTML file)
class DataManager {
constructor(rawData = []) {
this.rawData = rawData;
this.processedData = [];
this.filteredData = [];
if (rawData.length > 0) {
this.processData();
}
}
parseDate(dateString) {
if (!dateString || dateString === '' || dateString === 'N/A') {
return null;
}
try {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return null;
}
return date;
} catch (error) {
console.warn(`Failed to parse date: ${dateString}`, error);
return null;
}
}
normalizeSalary(wageAmount, wageUnit) {
if (!wageAmount || isNaN(parseFloat(wageAmount))) {
return 0;
}
const amount = parseFloat(wageAmount);
const unit = (wageUnit || '').toLowerCase().trim();
const conversionFactors = {
'year': 1,
'yearly': 1,
'annual': 1,
'month': 12,
'monthly': 12,
'week': 52,
'weekly': 52,
'hour': 2080,
'hourly': 2080,
'bi-weekly': 26,
'biweekly': 26
};
for (const [key, factor] of Object.entries(conversionFactors)) {
if (unit.includes(key)) {
return Math.round(amount * factor);
}
}
return Math.round(amount);
}
validateRecord(record) {
const errors = [];
if (!record.CASE_NUMBER) {
errors.push('Missing case number');
}
if (!record.EMPLOYER_NAME) {
errors.push('Missing employer name');
}
if (!record.JOB_TITLE) {
errors.push('Missing job title');
}
if (!record.CASE_STATUS) {
errors.push('Missing case status');
}
return errors;
}
processData() {
this.processedData = this.rawData.map(record => {
const processed = { ...record };
processed.RECEIVED_DATE_PARSED = this.parseDate(record.RECEIVED_DATE);
processed.DECISION_DATE_PARSED = this.parseDate(record.DECISION_DATE);
processed.BEGIN_DATE_PARSED = this.parseDate(record.BEGIN_DATE);
processed.END_DATE_PARSED = this.parseDate(record.END_DATE);
processed.ANNUAL_SALARY = this.normalizeSalary(
record.WAGE_RATE_OF_PAY_FROM,
record.WAGE_UNIT_OF_PAY
);
return processed;
});
this.filteredData = [...this.processedData];
}
}
// Test framework
function runTests() {
console.log('🧪 Running H1B Data Processing Unit Tests...\n');
let totalTests = 0;
let passedTests = 0;
let failedTests = 0;
function runTest(testName, testFunction) {
totalTests++;
try {
testFunction();
console.log(`✅ ${testName}`);
passedTests++;
} catch (error) {
console.error(`❌ ${testName}: ${error.message}`);
failedTests++;
}
}
function expect(actual) {
return {
toBe: (expected) => {
if (actual !== expected) {
throw new Error(`Expected ${expected}, but got ${actual}`);
}
},
toBeNull: () => {
if (actual !== null) {
throw new Error(`Expected null, but got ${actual}`);
}
},
toBeInstanceOf: (expectedClass) => {
if (!(actual instanceof expectedClass)) {
throw new Error(`Expected instance of ${expectedClass.name}, but got ${typeof actual}`);
}
},
toHaveLength: (expected) => {
if (actual.length !== expected) {
throw new Error(`Expected length ${expected}, but got ${actual.length}`);
}
},
toContain: (expected) => {
if (!actual.includes(expected)) {
throw new Error(`Expected ${actual} to contain ${expected}`);
}
}
};
}
console.log('📅 Testing Date Parsing Functionality...');
runTest('Parse valid ISO date strings', () => {
const dataManager = new DataManager();
const result = dataManager.parseDate('2023-01-15');
expect(result).toBeInstanceOf(Date);
expect(result.getFullYear()).toBe(2023);
expect(result.getMonth()).toBe(0);
// Use getUTCDate() to avoid timezone issues
expect(result.getUTCDate()).toBe(15);
});
runTest('Return null for empty date strings', () => {
const dataManager = new DataManager();
expect(dataManager.parseDate('')).toBeNull();
expect(dataManager.parseDate(null)).toBeNull();
expect(dataManager.parseDate(undefined)).toBeNull();
expect(dataManager.parseDate('N/A')).toBeNull();
});
runTest('Return null for invalid date strings', () => {
const dataManager = new DataManager();
expect(dataManager.parseDate('invalid-date')).toBeNull();
expect(dataManager.parseDate('not-a-date')).toBeNull();
});
console.log('\n💰 Testing Salary Normalization Functionality...');
runTest('Normalize hourly wages to annual salary', () => {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('50', 'Hour');
expect(result).toBe(104000);
});
runTest('Normalize monthly wages to annual salary', () => {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('5000', 'Month');
expect(result).toBe(60000);
});
runTest('Return 0 for invalid wage amounts', () => {
const dataManager = new DataManager();
expect(dataManager.normalizeSalary('', 'Hour')).toBe(0);
expect(dataManager.normalizeSalary(null, 'Hour')).toBe(0);
expect(dataManager.normalizeSalary('invalid', 'Hour')).toBe(0);
});
console.log('\n🚨 Testing Error Handling for Malformed Data...');
runTest('Validate required fields and return errors', () => {
const dataManager = new DataManager();
const malformedRecord = {};
const errors = dataManager.validateRecord(malformedRecord);
expect(errors).toContain('Missing case number');
expect(errors).toContain('Missing employer name');
expect(errors).toContain('Missing job title');
expect(errors).toContain('Missing case status');
});
runTest('Return no errors for valid record', () => {
const dataManager = new DataManager();
const validRecord = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Test Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified'
};
const errors = dataManager.validateRecord(validRecord);
expect(errors).toHaveLength(0);
});
runTest('Process data with mixed valid and invalid records', () => {
const mixedData = [
{
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Valid Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified',
WAGE_RATE_OF_PAY_FROM: '75000',
WAGE_UNIT_OF_PAY: 'Year',
RECEIVED_DATE: '2023-01-15',
DECISION_DATE: '2023-03-20'
},
{
CASE_NUMBER: 'I-200-12345-123457',
EMPLOYER_NAME: 'Another Company',
JOB_TITLE: 'Data Scientist',
CASE_STATUS: 'Denied',
WAGE_RATE_OF_PAY_FROM: 'invalid-salary',
WAGE_UNIT_OF_PAY: 'Hour',
RECEIVED_DATE: 'invalid-date',
DECISION_DATE: '2023-04-15'
}
];
const dataManager = new DataManager(mixedData);
expect(dataManager.processedData).toHaveLength(2);
expect(dataManager.processedData[0].ANNUAL_SALARY).toBe(75000);
expect(dataManager.processedData[0].RECEIVED_DATE_PARSED).toBeInstanceOf(Date);
expect(dataManager.processedData[1].ANNUAL_SALARY).toBe(0);
expect(dataManager.processedData[1].RECEIVED_DATE_PARSED).toBeNull();
});
// Print test summary
console.log(`\n📊 Test Results Summary:`);
console.log(`Total Tests: ${totalTests}`);
console.log(`✅ Passed: ${passedTests}`);
console.log(`❌ Failed: ${failedTests}`);
console.log(`Success Rate: ${Math.round((passedTests / totalTests) * 100)}%`);
if (failedTests === 0) {
console.log('\n🎉 All tests passed! Data processing functionality is working correctly.');
} else {
console.log(`\n⚠️ ${failedTests} test(s) failed. Please review the implementation.`);
}
return {
total: totalTests,
passed: passedTests,
failed: failedTests,
successRate: Math.round((passedTests / totalTests) * 100)
};
}
// Run the tests
runTests();