-
Notifications
You must be signed in to change notification settings - Fork 2
/
testutilities.ts
81 lines (71 loc) · 2.18 KB
/
testutilities.ts
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
import { readdirSync, readFileSync } from 'fs';
import { sync as globSync } from 'glob';
import path from 'path';
export interface SchemaTestCollection {
schemaCollectionName: SchemaCollectionName;
schemasRecords: SchemaRecord[];
}
export interface Schema {
appId: string;
messageType: string;
data: any;
}
export interface SchemaRecord {
schemaName: string;
schema: Schema;
schemaTests: Schema[];
}
export enum SchemaCollectionName {
CloudToDevice = 'cloudToDevice',
DeviceToCloud = 'deviceToCloud',
DeviceShadow = 'deviceShadow',
}
export const getSchemaTestCollection = (
schemaCollectionName: SchemaCollectionName,
): SchemaTestCollection => {
const schemaTestCollection: SchemaTestCollection = {
schemaCollectionName,
schemasRecords: [],
};
try {
const schemaFolders = readdirSync(
path.resolve(__dirname, 'schemas', schemaCollectionName),
);
for (const schemaName of schemaFolders) {
const schemaPath = globSync(
path.join(
path.resolve(
__dirname,
'schemas',
schemaCollectionName,
schemaName,
),
'!(*example*).json',
),
)[0];
const schema = JSON.parse(readFileSync(schemaPath, 'utf-8'));
const schemaExamplePaths = globSync(
path.join(
path.resolve(
__dirname,
'schemas',
schemaCollectionName,
schemaName,
),
'*example*',
),
);
const schemaTests = schemaExamplePaths.map((exampleSchema) => {
return JSON.parse(readFileSync(exampleSchema, 'utf-8'));
});
schemaTestCollection.schemasRecords.push({
schemaName,
schema,
schemaTests,
});
}
} catch (e) {
console.error('Error getting collection', e);
}
return schemaTestCollection;
};