masterrecord 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +18 -0
- package/package.json +14 -1
- package/.claude/settings.local.json +0 -10
- package/Migrations/pathUtils.test.js +0 -37
- package/eslint.config.js +0 -40
- package/prettier.config.js +0 -22
- package/test/belongs-to-fk-setter.test.js +0 -154
- package/test/belongs-to-fk-type.test.js +0 -253
- package/test/broad-audit.test.js +0 -256
- package/test/context-errors.test.js +0 -43
- package/test/count-chained-where.test.js +0 -136
- package/test/count-no-from-audit.test.js +0 -124
- package/test/ctx-dollar-placeholder.test.js +0 -142
- package/test/drop-column-idempotent.test.js +0 -181
- package/test/edge-cases-audit.test.js +0 -180
- package/test/esm-imports.test.js +0 -110
- package/test/full-text-search.test.js +0 -340
- package/test/migration-template.test.js +0 -47
- package/test/new-setter-large-string.test.js +0 -191
- package/test/pool-key.test.js +0 -43
- package/test/postgres-camelcase-quoting.test.js +0 -249
- package/test/query-builder-parity.test.js +0 -260
- package/test/setter-and-update-drop.test.js +0 -168
- package/test/sqlite-lifecycle.test.js +0 -76
- package/test/text-types.test.js +0 -167
- package/test/update-database-multi.test.js +0 -239
package/test/broad-audit.test.js
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Broad integration audit against SQLite. Each test exercises a common
|
|
3
|
-
* consumer pattern; any failure points at a real bug. Uses raw SQL for
|
|
4
|
-
* schema setup so we don't depend on the migration CLI.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { test } from 'node:test';
|
|
8
|
-
import assert from 'node:assert/strict';
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
import path from 'node:path';
|
|
11
|
-
import { fileURLToPath } from 'node:url';
|
|
12
|
-
|
|
13
|
-
process.env.master = 'audit';
|
|
14
|
-
|
|
15
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'broad-audit');
|
|
17
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
18
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
19
|
-
const dbFile = path.join(dbDir, 'auditctx.sqlite');
|
|
20
|
-
|
|
21
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
22
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
23
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
24
|
-
|
|
25
|
-
fs.writeFileSync(
|
|
26
|
-
path.join(envDir, 'env.audit.json'),
|
|
27
|
-
JSON.stringify({
|
|
28
|
-
AuditCtx: {
|
|
29
|
-
type: 'better-sqlite3',
|
|
30
|
-
connection: dbDir + path.sep,
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
const { default: context } = await import('../context.js');
|
|
36
|
-
|
|
37
|
-
class Author {
|
|
38
|
-
id(db) { db.integer().primary().auto(); }
|
|
39
|
-
name(db) { db.string(); }
|
|
40
|
-
// camelCase field — bites any engine that capitalizes inconsistently
|
|
41
|
-
createdAt(db) { db.integer(); }
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class Post {
|
|
45
|
-
id(db) { db.integer().primary().auto(); }
|
|
46
|
-
title(db) { db.string(); }
|
|
47
|
-
views(db) { db.integer(); }
|
|
48
|
-
author_id(db) { db.integer(); }
|
|
49
|
-
Author(db) { db.belongsTo('Author'); }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class AuditCtx extends context {
|
|
53
|
-
constructor() {
|
|
54
|
-
super();
|
|
55
|
-
this.env(envDir);
|
|
56
|
-
this.dbset(Author);
|
|
57
|
-
this.dbset(Post);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
const ctx = new AuditCtx();
|
|
63
|
-
ctx._SQLEngine.db.exec(`
|
|
64
|
-
CREATE TABLE IF NOT EXISTS Author (
|
|
65
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
66
|
-
name TEXT,
|
|
67
|
-
createdAt INTEGER
|
|
68
|
-
);
|
|
69
|
-
CREATE TABLE IF NOT EXISTS Post (
|
|
70
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
71
|
-
title TEXT,
|
|
72
|
-
views INTEGER,
|
|
73
|
-
author_id INTEGER
|
|
74
|
-
);
|
|
75
|
-
`);
|
|
76
|
-
const insertAuthor = ctx._SQLEngine.db.prepare('INSERT INTO Author (name, createdAt) VALUES (?, ?)');
|
|
77
|
-
insertAuthor.run('Alice', 1000);
|
|
78
|
-
insertAuthor.run('Bob', 2000);
|
|
79
|
-
insertAuthor.run('Carol', 3000);
|
|
80
|
-
|
|
81
|
-
const insertPost = ctx._SQLEngine.db.prepare('INSERT INTO Post (title, views, author_id) VALUES (?, ?, ?)');
|
|
82
|
-
insertPost.run('Alice Post 1', 100, 1);
|
|
83
|
-
insertPost.run('Alice Post 2', 50, 1);
|
|
84
|
-
insertPost.run('Bob Post', 200, 2);
|
|
85
|
-
await ctx.close();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
test('camelCase field name round-trips through .where()', async () => {
|
|
89
|
-
const db = new AuditCtx();
|
|
90
|
-
const author = await db.Author.where('a => a.createdAt == $$', 2000).single();
|
|
91
|
-
assert.ok(author, 'row should be found');
|
|
92
|
-
assert.equal(author.name, 'Bob');
|
|
93
|
-
assert.equal(author.createdAt, 2000);
|
|
94
|
-
await db.close();
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('toList returns all rows with correct fields', async () => {
|
|
98
|
-
const db = new AuditCtx();
|
|
99
|
-
const rows = await db.Author.orderBy('a => a.id').toList();
|
|
100
|
-
assert.equal(rows.length, 3);
|
|
101
|
-
assert.deepEqual(rows.map(r => r.name), ['Alice', 'Bob', 'Carol']);
|
|
102
|
-
await db.close();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('single with no match returns null (not undefined or error)', async () => {
|
|
106
|
-
const db = new AuditCtx();
|
|
107
|
-
const result = await db.Author.where('a => a.name == $$', '__nonexistent__').single();
|
|
108
|
-
assert.equal(result, null);
|
|
109
|
-
await db.close();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test('count returns a number', async () => {
|
|
113
|
-
const db = new AuditCtx();
|
|
114
|
-
const n = await db.Author.count();
|
|
115
|
-
assert.equal(n, 3);
|
|
116
|
-
await db.close();
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('count with chained where filters correctly', async () => {
|
|
120
|
-
const db = new AuditCtx();
|
|
121
|
-
const n = await db.Author.where('a => a.createdAt > $$', 1500).count();
|
|
122
|
-
assert.equal(n, 2, 'Bob (2000) and Carol (3000)');
|
|
123
|
-
await db.close();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('update persists via tracked entity modification', async () => {
|
|
127
|
-
const db = new AuditCtx();
|
|
128
|
-
const alice = await db.Author.where('a => a.name == $$', 'Alice').single();
|
|
129
|
-
alice.name = 'Alice Updated';
|
|
130
|
-
await alice.save();
|
|
131
|
-
|
|
132
|
-
const reloaded = await db.Author.where('a => a.id == $$', alice.id).single();
|
|
133
|
-
assert.equal(reloaded.name, 'Alice Updated');
|
|
134
|
-
await db.close();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('update only writes dirty fields (unchanged fields stay intact)', async () => {
|
|
138
|
-
const db = new AuditCtx();
|
|
139
|
-
const bob = await db.Author.where('a => a.name == $$', 'Bob').single();
|
|
140
|
-
const originalCreatedAt = bob.createdAt;
|
|
141
|
-
bob.name = 'Robert';
|
|
142
|
-
await bob.save();
|
|
143
|
-
|
|
144
|
-
const reloaded = await db.Author.where('a => a.id == $$', bob.id).single();
|
|
145
|
-
assert.equal(reloaded.name, 'Robert');
|
|
146
|
-
assert.equal(reloaded.createdAt, originalCreatedAt, 'untouched field must not be altered');
|
|
147
|
-
await db.close();
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
test('delete via entity.delete() removes the row', async () => {
|
|
151
|
-
const db = new AuditCtx();
|
|
152
|
-
const carol = await db.Author.where('a => a.name == $$', 'Carol').single();
|
|
153
|
-
assert.ok(carol);
|
|
154
|
-
carol.__state = 'delete';
|
|
155
|
-
db.__track(carol);
|
|
156
|
-
await db.saveChanges();
|
|
157
|
-
|
|
158
|
-
const gone = await db.Author.where('a => a.name == $$', 'Carol').single();
|
|
159
|
-
assert.equal(gone, null);
|
|
160
|
-
await db.close();
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
test('.new() + save produces a row visible to subsequent queries', async () => {
|
|
164
|
-
const db = new AuditCtx();
|
|
165
|
-
const row = db.Author.new();
|
|
166
|
-
row.name = 'Dave';
|
|
167
|
-
row.createdAt = 4000;
|
|
168
|
-
await row.save();
|
|
169
|
-
assert.ok(row.id, 'auto-increment id populated');
|
|
170
|
-
|
|
171
|
-
const reloaded = await db.Author.where('a => a.id == $$', row.id).single();
|
|
172
|
-
assert.equal(reloaded.name, 'Dave');
|
|
173
|
-
assert.equal(reloaded.createdAt, 4000);
|
|
174
|
-
await db.close();
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('raw-loaded belongsTo foreign key column is readable as a scalar', async () => {
|
|
178
|
-
const db = new AuditCtx();
|
|
179
|
-
const post = await db.Post.where('p => p.title == $$', 'Bob Post').single();
|
|
180
|
-
assert.ok(post);
|
|
181
|
-
assert.equal(post.author_id, 2);
|
|
182
|
-
assert.equal(post.views, 200);
|
|
183
|
-
await db.close();
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test('bulkCreate on context creates multiple rows', async () => {
|
|
187
|
-
const db = new AuditCtx();
|
|
188
|
-
await db.bulkCreate('Post', [
|
|
189
|
-
{ title: 'Bulk 1', views: 1, author_id: 1 },
|
|
190
|
-
{ title: 'Bulk 2', views: 2, author_id: 1 },
|
|
191
|
-
{ title: 'Bulk 3', views: 3, author_id: 2 },
|
|
192
|
-
]);
|
|
193
|
-
const rows = await db.Post.where('p => p.title == $$', 'Bulk 1').toList();
|
|
194
|
-
assert.equal(rows.length, 1);
|
|
195
|
-
const all = await db.Post.toList();
|
|
196
|
-
assert.ok(all.length >= 6, `expected at least 6 posts, got ${all.length}`);
|
|
197
|
-
await db.close();
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
test('orderBy ASC then OrderBy DESC give opposite orderings', async () => {
|
|
201
|
-
const db = new AuditCtx();
|
|
202
|
-
const asc = await db.Post.orderBy('p => p.views').toList();
|
|
203
|
-
const desc = await db.Post.orderByDescending('p => p.views').toList();
|
|
204
|
-
|
|
205
|
-
const ascViews = asc.map(p => p.views);
|
|
206
|
-
const descViews = desc.map(p => p.views);
|
|
207
|
-
assert.deepEqual(descViews, [...ascViews].reverse());
|
|
208
|
-
await db.close();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('skip/take pagination works end-to-end', async () => {
|
|
212
|
-
const db = new AuditCtx();
|
|
213
|
-
const page = await db.Post.orderBy('p => p.id').skip(1).take(2).toList();
|
|
214
|
-
assert.equal(page.length, 2);
|
|
215
|
-
await db.close();
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test('cache invalidation: modifying a row invalidates subsequent .toList()', async () => {
|
|
219
|
-
const db = new AuditCtx();
|
|
220
|
-
// Warm the cache
|
|
221
|
-
const first = await db.Post.cache().orderBy('p => p.id').toList();
|
|
222
|
-
const firstCount = first.length;
|
|
223
|
-
|
|
224
|
-
// Create a new row
|
|
225
|
-
const p = db.Post.new();
|
|
226
|
-
p.title = 'Cache Invalidator';
|
|
227
|
-
p.views = 999;
|
|
228
|
-
p.author_id = 1;
|
|
229
|
-
await p.save();
|
|
230
|
-
|
|
231
|
-
// A fresh query should see the new row (cache must be invalidated)
|
|
232
|
-
const second = await db.Post.cache().orderBy('p => p.id').toList();
|
|
233
|
-
assert.equal(second.length, firstCount + 1, 'cache should be invalidated after insert');
|
|
234
|
-
await db.close();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test('saveChanges with no changes is safe and returns truthy', async () => {
|
|
238
|
-
const db = new AuditCtx();
|
|
239
|
-
const result = await db.saveChanges();
|
|
240
|
-
assert.ok(result !== false, 'saveChanges on empty tracker should not error');
|
|
241
|
-
await db.close();
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
test('validation: required field with empty string throws on save', async () => {
|
|
245
|
-
// Reuse AuditCtx with an Author entity that has a required name.
|
|
246
|
-
// (We can't add a new context class without a matching env entry.)
|
|
247
|
-
const db = new AuditCtx();
|
|
248
|
-
const row = db.Author.new();
|
|
249
|
-
row.name = ''; // empty string on a nullable field — insertManager catches this for non-null required fields
|
|
250
|
-
row.createdAt = 9999;
|
|
251
|
-
// Author.name is nullable by default, so this will NOT throw. Instead,
|
|
252
|
-
// verify the persist succeeded — catching a different real behavior.
|
|
253
|
-
await row.save();
|
|
254
|
-
assert.ok(row.id, 'nullable empty string saves without error');
|
|
255
|
-
await db.close();
|
|
256
|
-
});
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies the custom error classes exported from context.js
|
|
3
|
-
* behave correctly (inheritance, name, context metadata).
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { test } from 'node:test';
|
|
7
|
-
import assert from 'node:assert/strict';
|
|
8
|
-
import {
|
|
9
|
-
ContextError,
|
|
10
|
-
ConfigurationError,
|
|
11
|
-
DatabaseConnectionError,
|
|
12
|
-
EntityValidationError,
|
|
13
|
-
} from '../context.js';
|
|
14
|
-
|
|
15
|
-
test('ContextError is a subclass of Error', () => {
|
|
16
|
-
const err = new ContextError('test', { foo: 'bar' });
|
|
17
|
-
assert.ok(err instanceof Error);
|
|
18
|
-
assert.ok(err instanceof ContextError);
|
|
19
|
-
assert.equal(err.name, 'ContextError');
|
|
20
|
-
assert.equal(err.message, 'test');
|
|
21
|
-
assert.deepEqual(err.context, { foo: 'bar' });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('ConfigurationError inherits from ContextError', () => {
|
|
25
|
-
const err = new ConfigurationError('bad config');
|
|
26
|
-
assert.ok(err instanceof ContextError);
|
|
27
|
-
assert.ok(err instanceof ConfigurationError);
|
|
28
|
-
assert.equal(err.name, 'ConfigurationError');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('DatabaseConnectionError attaches dbType to context', () => {
|
|
32
|
-
const err = new DatabaseConnectionError('cannot connect', 'mysql', { host: 'localhost' });
|
|
33
|
-
assert.ok(err instanceof ContextError);
|
|
34
|
-
assert.equal(err.context.dbType, 'mysql');
|
|
35
|
-
assert.equal(err.context.host, 'localhost');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('EntityValidationError attaches entityName to context', () => {
|
|
39
|
-
const err = new EntityValidationError('invalid', 'User', { field: 'email' });
|
|
40
|
-
assert.ok(err instanceof ContextError);
|
|
41
|
-
assert.equal(err.context.entityName, 'User');
|
|
42
|
-
assert.equal(err.context.field, 'email');
|
|
43
|
-
});
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies chained .where() calls are actually applied to COUNT queries,
|
|
3
|
-
* not silently dropped. Related to the "missing AND clause" family of bugs.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { test } from 'node:test';
|
|
7
|
-
import assert from 'node:assert/strict';
|
|
8
|
-
import fs from 'node:fs';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import { fileURLToPath } from 'node:url';
|
|
11
|
-
|
|
12
|
-
process.env.master = 'countchained';
|
|
13
|
-
|
|
14
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'count-chained-where');
|
|
16
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
17
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
18
|
-
const dbFile = path.join(dbDir, 'countctx.sqlite');
|
|
19
|
-
|
|
20
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
21
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
22
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
23
|
-
|
|
24
|
-
fs.writeFileSync(
|
|
25
|
-
path.join(envDir, 'env.countchained.json'),
|
|
26
|
-
JSON.stringify({
|
|
27
|
-
CountCtx: {
|
|
28
|
-
type: 'better-sqlite3',
|
|
29
|
-
connection: dbDir + path.sep,
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
const { default: context } = await import('../context.js');
|
|
35
|
-
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
36
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
37
|
-
|
|
38
|
-
class Lead {
|
|
39
|
-
id(db) { db.integer().primary().auto(); }
|
|
40
|
-
stage(db) { db.string(); }
|
|
41
|
-
score(db) { db.integer(); }
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class CountCtx extends context {
|
|
45
|
-
constructor() {
|
|
46
|
-
super();
|
|
47
|
-
this.env(envDir);
|
|
48
|
-
this.dbset(Lead);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Seed 5 rows: 3 stage="New", 2 stage="Won". Of the 3 New, 2 have score > 10.
|
|
53
|
-
{
|
|
54
|
-
const ctx = new CountCtx();
|
|
55
|
-
ctx._SQLEngine.db.exec(`
|
|
56
|
-
CREATE TABLE IF NOT EXISTS Lead (
|
|
57
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
58
|
-
stage TEXT,
|
|
59
|
-
score INTEGER
|
|
60
|
-
);
|
|
61
|
-
`);
|
|
62
|
-
const insert = ctx._SQLEngine.db.prepare('INSERT INTO Lead (stage, score) VALUES (?, ?)');
|
|
63
|
-
insert.run('New', 5);
|
|
64
|
-
insert.run('New', 15);
|
|
65
|
-
insert.run('New', 20);
|
|
66
|
-
insert.run('Won', 30);
|
|
67
|
-
insert.run('Won', 40);
|
|
68
|
-
await ctx.close();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
test('SQLite: chained .where() before .count() applies both conditions', async () => {
|
|
72
|
-
const ctx = new CountCtx();
|
|
73
|
-
// stage == 'New' AND score > 10 → rows 2 and 3 → count 2
|
|
74
|
-
const n = await ctx.Lead
|
|
75
|
-
.where('l => l.stage == $$', 'New')
|
|
76
|
-
.where('l => l.score > $$', 10)
|
|
77
|
-
.count();
|
|
78
|
-
assert.equal(n, 2, 'only Leads with stage=New AND score>10 counted');
|
|
79
|
-
await ctx.close();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('SQLite: single .where() before .count() still works (sanity)', async () => {
|
|
83
|
-
const ctx = new CountCtx();
|
|
84
|
-
const n = await ctx.Lead.where('l => l.stage == $$', 'New').count();
|
|
85
|
-
assert.equal(n, 3);
|
|
86
|
-
await ctx.close();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
function buildScript(chainFn) {
|
|
90
|
-
const ctx = new CountCtx();
|
|
91
|
-
const builder = chainFn(ctx.Lead);
|
|
92
|
-
const script = builder.__queryObject.script;
|
|
93
|
-
const entity = ctx.Lead.__entity;
|
|
94
|
-
ctx.close();
|
|
95
|
-
return { script, entity };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Smoke test MySQL/Postgres engines' count query path — we can't run them
|
|
99
|
-
// against a real DB, but we can verify the generated SQL contains both
|
|
100
|
-
// predicate columns when chained .where() is used.
|
|
101
|
-
//
|
|
102
|
-
// NOTE: MySQL's getCount and Postgres's getCount build the COUNT SQL inline
|
|
103
|
-
// (they don't delegate to buildQuery), so these are separate code paths that
|
|
104
|
-
// also need to emit the AND clause.
|
|
105
|
-
test('MySQL engine should emit chained-AND predicate columns in count SQL', async () => {
|
|
106
|
-
const { script, entity } = buildScript((Lead) =>
|
|
107
|
-
Lead.where('l => l.stage == $$', 'New').where('l => l.score > $$', 10)
|
|
108
|
-
);
|
|
109
|
-
script.count = 'none';
|
|
110
|
-
const engine = new MySQLEngine();
|
|
111
|
-
// Emulate what getCount now builds inline.
|
|
112
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
113
|
-
assert.match(sql, /\bstage\b/i, 'first predicate column present');
|
|
114
|
-
assert.match(sql, /\bscore\b/i, 'second predicate column present');
|
|
115
|
-
assert.match(sql, /^SELECT COUNT\(\*\)/, 'starts with SELECT COUNT(*)');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('MySQL buildCount: SELECT COUNT(*) when no column specified', () => {
|
|
119
|
-
const { script, entity } = buildScript((Lead) =>
|
|
120
|
-
Lead.where('l => l.stage == $$', 'New')
|
|
121
|
-
);
|
|
122
|
-
script.count = 'none';
|
|
123
|
-
const engine = new MySQLEngine();
|
|
124
|
-
assert.equal(engine.buildCount(script, entity), 'COUNT(*)');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('Postgres engine COUNT path should include chained-AND predicate columns', () => {
|
|
128
|
-
const { script, entity } = buildScript((Lead) =>
|
|
129
|
-
Lead.where('l => l.stage == $$', 'New').where('l => l.score > $$', 10)
|
|
130
|
-
);
|
|
131
|
-
script.count = 'none';
|
|
132
|
-
const engine = new PostgresEngine();
|
|
133
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
134
|
-
assert.match(sql, /\bstage\b/i);
|
|
135
|
-
assert.match(sql, /\bscore\b/i, 'second .where() predicate must survive into the COUNT SQL');
|
|
136
|
-
});
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies the `.count()` without-prior-`.where()` bug is resolved on
|
|
3
|
-
* all three engines. The SQLite bug manifested because SQLite's getEntity
|
|
4
|
-
* returned "" on fallthrough (MySQL/Postgres returned the name), plus
|
|
5
|
-
* queryMethods.count() didn't bootstrap the entityMap like single()/toList()
|
|
6
|
-
* did. Both fixes are engine-agnostic; this test verifies no engine
|
|
7
|
-
* regresses.
|
|
8
|
-
*
|
|
9
|
-
* Strategy: build a query state via the real fluent API, then feed it to
|
|
10
|
-
* each engine's COUNT path. Assert the generated SQL has a FROM clause
|
|
11
|
-
* and includes COUNT(*).
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { test } from 'node:test';
|
|
15
|
-
import assert from 'node:assert/strict';
|
|
16
|
-
import fs from 'node:fs';
|
|
17
|
-
import path from 'node:path';
|
|
18
|
-
import { fileURLToPath } from 'node:url';
|
|
19
|
-
|
|
20
|
-
process.env.master = 'countfrom';
|
|
21
|
-
|
|
22
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'count-no-from');
|
|
24
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
25
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
26
|
-
const dbFile = path.join(dbDir, 'countctx.sqlite');
|
|
27
|
-
|
|
28
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
29
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
30
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
31
|
-
|
|
32
|
-
fs.writeFileSync(
|
|
33
|
-
path.join(envDir, 'env.countfrom.json'),
|
|
34
|
-
JSON.stringify({
|
|
35
|
-
CountFromCtx: {
|
|
36
|
-
type: 'better-sqlite3',
|
|
37
|
-
connection: dbDir + path.sep,
|
|
38
|
-
},
|
|
39
|
-
})
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
const { default: context } = await import('../context.js');
|
|
43
|
-
const { default: SQLiteEngine } = await import('../SQLLiteEngine.js');
|
|
44
|
-
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
45
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
46
|
-
|
|
47
|
-
class Thing {
|
|
48
|
-
id(db) { db.integer().primary().auto(); }
|
|
49
|
-
label(db) { db.string(); }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class CountFromCtx extends context {
|
|
53
|
-
constructor() {
|
|
54
|
-
super();
|
|
55
|
-
this.env(envDir);
|
|
56
|
-
this.dbset(Thing);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
{
|
|
61
|
-
const ctx = new CountFromCtx();
|
|
62
|
-
ctx._SQLEngine.db.exec(`
|
|
63
|
-
CREATE TABLE IF NOT EXISTS Thing (
|
|
64
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
65
|
-
label TEXT
|
|
66
|
-
);
|
|
67
|
-
`);
|
|
68
|
-
const insert = ctx._SQLEngine.db.prepare('INSERT INTO Thing (label) VALUES (?)');
|
|
69
|
-
insert.run('a'); insert.run('b'); insert.run('c'); insert.run('d');
|
|
70
|
-
await ctx.close();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// End-to-end SQLite test — the only engine we can actually run.
|
|
74
|
-
test('SQLite: .count() with no prior .where() returns the correct row count (not 1)', async () => {
|
|
75
|
-
const db = new CountFromCtx();
|
|
76
|
-
const n = await db.Thing.count();
|
|
77
|
-
assert.equal(n, 4, 'count() must reflect actual row count, not silently return 1');
|
|
78
|
-
await db.close();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// For MySQL and Postgres, we snapshot the SQL that getCount would build.
|
|
82
|
-
// We can't run them without a connection, but the string output is enough
|
|
83
|
-
// to prove the FROM clause and COUNT(*) are present.
|
|
84
|
-
function snapshotFluentState() {
|
|
85
|
-
const ctx = new CountFromCtx();
|
|
86
|
-
// Mirror what `.count()` does internally: bootstrap entityMap via skipClause
|
|
87
|
-
// when the user calls count() without a prior .where().
|
|
88
|
-
const builder = ctx.Thing;
|
|
89
|
-
if (builder.__queryObject.script.entityMap.length === 0) {
|
|
90
|
-
builder.__queryObject.skipClause(ctx.Thing.__entity.__name);
|
|
91
|
-
}
|
|
92
|
-
const script = builder.__queryObject.script;
|
|
93
|
-
script.count = 'none'; // matches what getCount sets when count is undefined
|
|
94
|
-
const entity = ctx.Thing.__entity;
|
|
95
|
-
ctx.close();
|
|
96
|
-
return { script, entity };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
test('MySQL: COUNT SQL has a FROM clause even with no prior .where()', () => {
|
|
100
|
-
const { script, entity } = snapshotFluentState();
|
|
101
|
-
const engine = new MySQLEngine();
|
|
102
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
103
|
-
assert.match(sql, /SELECT\s+COUNT\(\*\)/, 'emits COUNT(*)');
|
|
104
|
-
assert.match(sql, /FROM\s+`Thing`/, 'has FROM clause with backtick-quoted table');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('Postgres: COUNT SQL has a FROM clause even with no prior .where()', () => {
|
|
108
|
-
const { script, entity } = snapshotFluentState();
|
|
109
|
-
const engine = new PostgresEngine();
|
|
110
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
111
|
-
// Postgres buildCount emits COUNT(alias.*) when count is 'none' — we
|
|
112
|
-
// accept either shape here since the bug under test is about FROM, not
|
|
113
|
-
// the exact COUNT expression.
|
|
114
|
-
assert.match(sql, /SELECT\s+COUNT\(/, 'emits SELECT COUNT(...)');
|
|
115
|
-
assert.match(sql, /FROM\s+"?Thing"?/, 'has FROM clause naming the table (quoted or unquoted)');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('SQLite engine directly: buildFrom on empty entityMap now produces a valid FROM', () => {
|
|
119
|
-
const engine = new SQLiteEngine();
|
|
120
|
-
const fakeScript = { entityMap: [], parentName: '' };
|
|
121
|
-
const fakeEntity = { __name: 'Thing' };
|
|
122
|
-
const from = engine.buildFrom(fakeScript, fakeEntity);
|
|
123
|
-
assert.match(from, /^FROM\s+Thing\b/, `empty entityMap should now produce a FROM clause; got "${from}"`);
|
|
124
|
-
});
|