-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql-driver-schema.test.ts
More file actions
265 lines (217 loc) · 7.42 KB
/
sql-driver-schema.test.ts
File metadata and controls
265 lines (217 loc) · 7.42 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { SqlDriver } from '../src/index.js';
describe('SqlDriver Schema Sync (SQLite)', () => {
let driver: SqlDriver;
let knexInstance: any;
beforeEach(async () => {
driver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
knexInstance = (driver as any).knex;
});
afterEach(async () => {
await knexInstance.destroy();
});
it('should create table if not exists', async () => {
const objects = [
{
name: 'test_obj',
fields: {
name: { type: 'string' },
age: { type: 'integer' },
},
},
];
await driver.initObjects(objects);
const exists = await knexInstance.schema.hasTable('test_obj');
expect(exists).toBe(true);
const columns = await knexInstance('test_obj').columnInfo();
expect(columns).toHaveProperty('id');
expect(columns).toHaveProperty('created_at');
expect(columns).toHaveProperty('updated_at');
expect(columns).toHaveProperty('name');
expect(columns).toHaveProperty('age');
});
it('should add new columns if table exists', async () => {
await knexInstance.schema.createTable('test_obj', (t: any) => {
t.string('id').primary();
t.string('name');
});
await knexInstance('test_obj').insert({ id: '1', name: 'Old Data' });
const objects = [
{
name: 'test_obj',
fields: {
name: { type: 'string' },
age: { type: 'integer' },
active: { type: 'boolean' },
},
},
];
await driver.initObjects(objects);
const columns = await knexInstance('test_obj').columnInfo();
expect(columns).toHaveProperty('age');
expect(columns).toHaveProperty('active');
const row = await knexInstance('test_obj').where('id', '1').first();
expect(row.name).toBe('Old Data');
});
it('should not delete existing columns', async () => {
await knexInstance.schema.createTable('test_obj', (t: any) => {
t.string('id').primary();
t.string('name');
t.string('extra_column');
});
const objects = [
{
name: 'test_obj',
fields: {
name: { type: 'string' },
},
},
];
await driver.initObjects(objects);
const columns = await knexInstance('test_obj').columnInfo();
expect(columns).toHaveProperty('name');
expect(columns).toHaveProperty('extra_column');
});
it('should not fail if table creation is repeated', async () => {
const objects = [
{
name: 'test_obj',
fields: {
name: { type: 'string' },
},
},
];
await driver.initObjects(objects);
await driver.initObjects(objects);
const exists = await knexInstance.schema.hasTable('test_obj');
expect(exists).toBe(true);
});
it('should create json column for multiple=true fields', async () => {
const objects = [
{
name: 'multi_test',
fields: {
tags: { type: 'select', multiple: true } as any,
users: { type: 'lookup', reference_to: 'user', multiple: true } as any,
},
},
];
await driver.initObjects(objects);
await driver.create('multi_test', {
tags: ['a', 'b'],
users: ['u1', 'u2'],
});
const results = await driver.find('multi_test', {});
const row = results[0];
expect(row.tags).toEqual(['a', 'b']);
expect(row.users).toEqual(['u1', 'u2']);
});
it('should create percent column', async () => {
const objects = [
{
name: 'percent_test',
fields: {
completion: { type: 'percent' } as any,
},
},
];
await driver.initObjects(objects);
const columns = await knexInstance('percent_test').columnInfo();
expect(columns).toHaveProperty('completion');
await driver.create('percent_test', { completion: 0.85 });
const res = await driver.find('percent_test', {});
expect(res[0].completion).toBe(0.85);
});
it('should handle special fields (formula, summary, auto_number)', async () => {
const objects = [
{
name: 'special_fields_test',
fields: {
total: { type: 'formula', expression: 'price * qty', data_type: 'number' } as any,
child_count: { type: 'summary', summary_object: 'child_obj', summary_type: 'count' } as any,
invoice_no: { type: 'auto_number', auto_number_format: 'INV-{0000}' } as any,
},
},
];
await driver.initObjects(objects);
const columns = await knexInstance('special_fields_test').columnInfo();
expect(columns).not.toHaveProperty('total');
expect(columns).toHaveProperty('child_count');
expect(columns).toHaveProperty('invoice_no');
});
it('should create database constraints (unique, required)', async () => {
const objects = [
{
name: 'constraint_test',
fields: {
unique_field: { type: 'string', unique: true } as any,
required_field: { type: 'string', required: true } as any,
},
},
];
await driver.initObjects(objects);
await driver.create('constraint_test', { unique_field: 'u1', required_field: 'r1' });
try {
await driver.create('constraint_test', { unique_field: 'u1', required_field: 'r2' });
expect.unreachable('Should throw error for unique violation');
} catch (e: any) {
expect(e.message).toMatch(/UNIQUE constraint failed|duplicate key value/);
}
try {
await driver.create('constraint_test', { unique_field: 'u2' });
expect.unreachable('Should throw error for not null violation');
} catch (e: any) {
expect(e.message).toMatch(/NOT NULL constraint failed|null value in column/);
}
});
it('should handle new field types (email, file, location)', async () => {
const objects = [
{
name: 'new_types_test',
fields: {
email: { type: 'email' } as any,
profile_pic: { type: 'image' } as any,
resume: { type: 'file' } as any,
office_loc: { type: 'location' } as any,
work_hours: { type: 'time' } as any,
},
},
];
await driver.initObjects(objects);
const cols = await knexInstance('new_types_test').columnInfo();
expect(cols).toHaveProperty('email');
expect(cols).toHaveProperty('profile_pic');
expect(cols).toHaveProperty('resume');
await driver.create('new_types_test', {
email: 'test@example.com',
profile_pic: { url: 'http://img.com/1.png' },
office_loc: { lat: 10, lng: 20 },
work_hours: '09:00:00',
});
const res = await driver.find('new_types_test', {});
const row = res[0];
expect(row.email).toBe('test@example.com');
expect(row.office_loc).toEqual({ lat: 10, lng: 20 });
});
it('should skip ensureDatabaseExists for SQLite (no-op)', async () => {
// SQLite auto-creates database files, so ensureDatabaseExists should be a no-op
// This test verifies that initObjects works normally for SQLite without errors
const objects = [
{
name: 'db_check_test',
fields: {
value: { type: 'string' },
},
},
];
// Should not throw — SQLite skips ensureDatabaseExists
await driver.initObjects(objects);
const exists = await knexInstance.schema.hasTable('db_check_test');
expect(exists).toBe(true);
});
});