-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathschema-driver.test.ts
More file actions
153 lines (133 loc) · 5.49 KB
/
Copy pathschema-driver.test.ts
File metadata and controls
153 lines (133 loc) · 5.49 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
import { describe, it, expect } from 'vitest';
import type { ISchemaDriver } from './schema-driver';
describe('Schema Driver Contract', () => {
describe('ISchemaDriver interface', () => {
it('should allow a full implementation with all methods', () => {
const executed: string[] = [];
const driver: ISchemaDriver = {
createCollection: async (objectName, _schema) => {
executed.push(`createCollection:${objectName}`);
},
dropCollection: async (objectName) => {
executed.push(`dropCollection:${objectName}`);
},
addColumn: async (objectName, fieldName, _field) => {
executed.push(`addColumn:${objectName}.${fieldName}`);
},
modifyColumn: async (objectName, fieldName, _field) => {
executed.push(`modifyColumn:${objectName}.${fieldName}`);
},
dropColumn: async (objectName, fieldName) => {
executed.push(`dropColumn:${objectName}.${fieldName}`);
},
createIndex: async (objectName, indexName, _fields) => {
executed.push(`createIndex:${objectName}.${indexName}`);
},
dropIndex: async (objectName, indexName) => {
executed.push(`dropIndex:${objectName}.${indexName}`);
},
executeRaw: async (statement) => {
executed.push(`executeRaw:${statement}`);
return null;
},
};
expect(typeof driver.createCollection).toBe('function');
expect(typeof driver.dropCollection).toBe('function');
expect(typeof driver.addColumn).toBe('function');
expect(typeof driver.modifyColumn).toBe('function');
expect(typeof driver.dropColumn).toBe('function');
expect(typeof driver.createIndex).toBe('function');
expect(typeof driver.dropIndex).toBe('function');
expect(typeof driver.executeRaw).toBe('function');
});
it('should create and drop collections', async () => {
const collections = new Set<string>();
const driver: ISchemaDriver = {
createCollection: async (objectName) => { collections.add(objectName); },
dropCollection: async (objectName) => { collections.delete(objectName); },
addColumn: async () => {},
modifyColumn: async () => {},
dropColumn: async () => {},
createIndex: async () => {},
dropIndex: async () => {},
executeRaw: async () => null,
};
await driver.createCollection('users');
await driver.createCollection('orders');
expect(collections.size).toBe(2);
await driver.dropCollection('orders');
expect(collections.size).toBe(1);
expect(collections.has('users')).toBe(true);
});
it('should add, modify, and drop columns', async () => {
const columns = new Map<string, any>();
const driver: ISchemaDriver = {
createCollection: async () => {},
dropCollection: async () => {},
addColumn: async (_obj, fieldName, field) => {
columns.set(fieldName, field);
},
modifyColumn: async (_obj, fieldName, field) => {
columns.set(fieldName, field);
},
dropColumn: async (_obj, fieldName) => {
columns.delete(fieldName);
},
createIndex: async () => {},
dropIndex: async () => {},
executeRaw: async () => null,
};
const textField = { name: 'email', label: 'Email', type: 'text' } as any;
await driver.addColumn('users', 'email', textField);
expect(columns.has('email')).toBe(true);
const updatedField = { ...textField, maxLength: 255 } as any;
await driver.modifyColumn('users', 'email', updatedField);
expect(columns.get('email').maxLength).toBe(255);
await driver.dropColumn('users', 'email');
expect(columns.has('email')).toBe(false);
});
it('should create and drop indexes', async () => {
const indexes = new Map<string, string[]>();
const driver: ISchemaDriver = {
createCollection: async () => {},
dropCollection: async () => {},
addColumn: async () => {},
modifyColumn: async () => {},
dropColumn: async () => {},
createIndex: async (_obj, indexName, fields) => {
indexes.set(indexName, fields);
},
dropIndex: async (_obj, indexName) => {
indexes.delete(indexName);
},
executeRaw: async () => null,
};
await driver.createIndex('users', 'idx_email', ['email']);
expect(indexes.get('idx_email')).toEqual(['email']);
await driver.createIndex('users', 'idx_name_email', ['name', 'email']);
expect(indexes.get('idx_name_email')).toEqual(['name', 'email']);
await driver.dropIndex('users', 'idx_email');
expect(indexes.has('idx_email')).toBe(false);
expect(indexes.has('idx_name_email')).toBe(true);
});
it('should execute raw statements', async () => {
const driver: ISchemaDriver = {
createCollection: async () => {},
dropCollection: async () => {},
addColumn: async () => {},
modifyColumn: async () => {},
dropColumn: async () => {},
createIndex: async () => {},
dropIndex: async () => {},
executeRaw: async (statement) => {
if (statement === 'SELECT 1') return [{ result: 1 }];
return null;
},
};
const result = await driver.executeRaw('SELECT 1');
expect(result).toEqual([{ result: 1 }]);
const empty = await driver.executeRaw('DROP TABLE IF EXISTS temp');
expect(empty).toBeNull();
});
});
});