-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_sql_exercises.ts
More file actions
64 lines (54 loc) · 2.01 KB
/
validate_sql_exercises.ts
File metadata and controls
64 lines (54 loc) · 2.01 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
import { QUESTION_DATABASE } from './services/exerciseGenerator';
import alasql from 'alasql';
import { runQuery, initDatabase } from './services/sqlService';
import { TopicId, Difficulty } from './types';
import fs from 'fs';
async function validate() {
initDatabase(Difficulty.Medium);
let totalExercises = 0;
const issues: any[] = [];
for (const topic of Object.values(TopicId)) {
for (const difficulty of Object.values(Difficulty)) {
const exercises = QUESTION_DATABASE[topic as string]?.[difficulty as string] || [];
totalExercises += exercises.length;
for (let i = 0; i < exercises.length; i++) {
const ex = exercises[i];
let testQuery = ex.queryTemplate;
if (ex.replacements) {
Object.entries(ex.replacements).forEach(([key, values]) => {
const replacement = String(values[0]);
const regex = new RegExp(`{${key}}`, "g");
testQuery = testQuery.replace(regex, replacement);
});
}
const result = runQuery(testQuery);
if (!result.success) {
issues.push({
type: 'ERROR',
topic, difficulty, index: i,
title: ex.titleTemplate,
query: testQuery,
error: result.error
});
} else if (!result.data || result.data.length === 0) {
if (testQuery.toUpperCase().startsWith("SELECT") || testQuery.toUpperCase().startsWith("(SELECT")) {
issues.push({
type: 'WARNING',
topic, difficulty, index: i,
title: ex.titleTemplate,
query: testQuery,
error: 'Empty Output'
});
}
}
}
}
}
fs.writeFileSync('errors.json', JSON.stringify({
total: totalExercises,
issuesCount: issues.length,
issues
}, null, 2));
console.log(`Saved ${issues.length} issues to errors.json`);
}
validate().catch(console.error);