-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsql-driver-numeric-fidelity.test.ts
More file actions
211 lines (188 loc) · 7.94 KB
/
Copy pathsql-driver-numeric-fidelity.test.ts
File metadata and controls
211 lines (188 loc) · 7.94 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Scalar field types must round-trip with TYPE fidelity, not just value
* survival. Regression (#field-zoo): `rating`/`slider`/`toggle`/`progress`
* were absent from the DDL column-type switch, so they fell to
* `default → table.string` and got TEXT affinity — SQLite then coerced the
* written number/boolean to a string ('4' not 4, '1' not true). The value
* persisted, so value-loss tests stayed green; only the JS type leaked, which
* is a runtime-fidelity trap on a low-code platform where an AI authors
* arbitrary field types.
*
* The fix maps `rating`/`slider`/`progress` to a REAL (numeric) column and
* `toggle` to a boolean column + the `booleanFields` read-coercion registry,
* and folds the object-valued `record`/`video`/`audio` into the shared
* `JSON_COLUMN_TYPES` source so they store/parse as JSON like `composite`.
*
* `number`/`currency`/`percent`/`boolean` are included as the working control:
* the SAME harness must keep returning them with correct types.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { SqlDriver } from '../src/index.js';
describe('SqlDriver scalar type fidelity (rating/slider/toggle/progress)', () => {
let driver: SqlDriver;
beforeEach(async () => {
driver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
await driver.initObjects([
{
name: 'zoo',
fields: {
name: { type: 'string' },
// control — these already round-tripped with correct types
f_number: { type: 'number' },
f_currency: { type: 'currency' },
f_percent: { type: 'percent' },
f_boolean: { type: 'boolean' },
// the leak: numeric scalars that used to land as TEXT
f_rating: { type: 'rating', max: 5 },
f_slider: { type: 'slider', min: 0, max: 100, step: 5 },
f_progress: { type: 'progress', min: 0, max: 100 },
// the leak: a switch-rendered boolean
f_toggle: { type: 'toggle' },
// object-valued types folded into JSON_COLUMN_TYPES
f_record: { type: 'record' },
f_video: { type: 'video' },
f_audio: { type: 'audio' },
},
},
]);
});
afterEach(async () => {
await driver.disconnect();
});
it('round-trips numeric scalars as numbers, not strings', async () => {
await driver.create(
'zoo',
{
id: 'z1',
name: 'Specimen',
f_number: 42,
f_currency: 1234.56,
f_percent: 75,
f_rating: 4,
f_slider: 25,
f_progress: 60,
},
{ bypassTenantAudit: true },
);
const row = await driver.findOne('zoo', 'z1', { bypassTenantAudit: true });
// control
expect(row.f_number).toBe(42);
expect(row.f_currency).toBeCloseTo(1234.56, 5);
expect(row.f_percent).toBe(75);
// the fix: these used to come back as '4' / '25' / '60'
expect(typeof row.f_rating).toBe('number');
expect(row.f_rating).toBe(4);
expect(typeof row.f_slider).toBe('number');
expect(row.f_slider).toBe(25);
expect(typeof row.f_progress).toBe('number');
expect(row.f_progress).toBe(60);
});
it('round-trips toggle as a boolean, not 1/0 or a string', async () => {
await driver.create('zoo', { id: 'z2', name: 'B', f_boolean: true, f_toggle: true }, { bypassTenantAudit: true });
await driver.create('zoo', { id: 'z3', name: 'C', f_boolean: false, f_toggle: false }, { bypassTenantAudit: true });
const on = await driver.findOne('zoo', 'z2', { bypassTenantAudit: true });
const off = await driver.findOne('zoo', 'z3', { bypassTenantAudit: true });
// control
expect(on.f_boolean).toBe(true);
expect(off.f_boolean).toBe(false);
// the fix: toggle used to come back as '1' / '0'
expect(typeof on.f_toggle).toBe('boolean');
expect(on.f_toggle).toBe(true);
expect(typeof off.f_toggle).toBe('boolean');
expect(off.f_toggle).toBe(false);
});
it('round-trips object-valued record/video/audio as objects, not strings', async () => {
await driver.create(
'zoo',
{
id: 'z4',
name: 'D',
f_record: { home: '+1', work: '+2' },
f_video: { url: 'https://cdn/v.mp4', duration: 12 },
f_audio: { url: 'https://cdn/a.mp3', duration: 30 },
},
{ bypassTenantAudit: true },
);
const row = await driver.findOne('zoo', 'z4', { bypassTenantAudit: true });
expect(row.f_record).toEqual({ home: '+1', work: '+2' });
expect(row.f_video).toEqual({ url: 'https://cdn/v.mp4', duration: 12 });
expect(row.f_audio).toEqual({ url: 'https://cdn/a.mp3', duration: 30 });
});
});
/**
* The column-affinity fix only governs NEWLY created columns; SQLite never
* alters a column's type in place, and the schema reconciler only adds missing
* columns. So a `rating`/`slider`/`progress` column created BEFORE the fix keeps
* its TEXT affinity and would still hand back '4' forever. The `numericFields`
* read coercion is what makes the fix retroactive — this reproduces the legacy
* column (pre-create it as TEXT, then initObjects, which must NOT alter it) and
* proves the stored string still reads back as a number.
*/
describe('SqlDriver numeric read coercion repairs legacy TEXT columns', () => {
let driver: SqlDriver;
beforeEach(async () => {
driver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
// Simulate a pre-fix database: the table already exists with TEXT-affinity
// columns for the numeric scalars (what the old `default → table.string`
// mapping produced).
const knex = (driver as unknown as { knex: import('knex').Knex }).knex;
await knex.schema.createTable('legacy', (t) => {
t.string('id').primary();
t.text('name');
t.text('f_rating'); // legacy TEXT affinity
t.text('f_slider');
t.text('f_progress');
});
// initObjects sees the existing table and must leave the columns as TEXT
// (it only adds missing columns) — but it still registers numericFields.
await driver.initObjects([
{
name: 'legacy',
fields: {
name: { type: 'string' },
f_rating: { type: 'rating', max: 5 },
f_slider: { type: 'slider', min: 0, max: 100 },
f_progress: { type: 'progress', min: 0, max: 100 },
},
},
]);
});
afterEach(async () => {
await driver.disconnect();
});
it('coerces stored strings back to numbers on read', async () => {
// Write through the driver — on a TEXT column SQLite stores the number as
// the string '4'/'25'/'60' (exactly the pre-fix leak).
await driver.create(
'legacy',
{ id: 'L1', name: 'old-row', f_rating: 4, f_slider: 25, f_progress: 60 },
{ bypassTenantAudit: true },
);
const row = await driver.findOne('legacy', 'L1', { bypassTenantAudit: true });
expect(typeof row.f_rating).toBe('number');
expect(row.f_rating).toBe(4);
expect(typeof row.f_slider).toBe('number');
expect(row.f_slider).toBe(25);
expect(typeof row.f_progress).toBe('number');
expect(row.f_progress).toBe(60);
});
it('leaves null and genuinely non-numeric legacy values intact', async () => {
const knex = (driver as unknown as { knex: import('knex').Knex }).knex;
// Hand-write a row with a null and a non-numeric string straight to the
// TEXT columns, bypassing the driver, to model messy legacy data.
await knex('legacy').insert({ id: 'L2', name: 'messy', f_rating: null, f_slider: 'n/a', f_progress: '60' });
const row = await driver.findOne('legacy', 'L2', { bypassTenantAudit: true });
expect(row.f_rating).toBeNull(); // null stays null, not 0
expect(row.f_slider).toBe('n/a'); // non-numeric junk is preserved, not NaN
expect(row.f_progress).toBe(60); // numeric-looking string is repaired
});
});