masterrecord 0.3.68 → 1.0.0
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/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +44 -113
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +8 -39
- package/Migrations/migrations.js +16 -14
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +26 -7
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +26 -6
- package/package.json +34 -5
- package/postgresEngine.js +29 -8
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
package/test/newMethodTest.js
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: .new() Method for Creating Empty Entity Instances
|
|
3
|
-
*
|
|
4
|
-
* Verifies that all dbsets have access to .new() method
|
|
5
|
-
* which creates empty entity instances ready for property assignment.
|
|
6
|
-
*
|
|
7
|
-
* Bug Report: QaIntelligenceJob.new() was not a function
|
|
8
|
-
* Root Cause: .new() method was never implemented in queryMethods.js
|
|
9
|
-
* Fix: Added .new() method to create tracked entity instances
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const masterrecord = require('../MasterRecord.js');
|
|
13
|
-
const path = require('path');
|
|
14
|
-
|
|
15
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
16
|
-
console.log("║ .new() Method Test Suite ║");
|
|
17
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
18
|
-
|
|
19
|
-
let passed = 0;
|
|
20
|
-
let failed = 0;
|
|
21
|
-
|
|
22
|
-
// Test 1: Define test entities
|
|
23
|
-
console.log("📝 Test 1: Define test entities");
|
|
24
|
-
console.log("──────────────────────────────────────────────────");
|
|
25
|
-
|
|
26
|
-
class TestUser {
|
|
27
|
-
id(db) {
|
|
28
|
-
db.integer().primary().auto();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
name(db) {
|
|
32
|
-
db.string().notNullable();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
email(db) {
|
|
36
|
-
db.string().notNullable().unique();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
age(db) {
|
|
40
|
-
db.integer().nullable();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
created_at(db) {
|
|
44
|
-
db.string().notNullable();
|
|
45
|
-
db.get(function(value) {
|
|
46
|
-
return value || Date.now().toString();
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class TestPost {
|
|
52
|
-
id(db) {
|
|
53
|
-
db.integer().primary().auto();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
title(db) {
|
|
57
|
-
db.string().notNullable();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
content(db) {
|
|
61
|
-
db.string().nullable();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
user_id(db) {
|
|
65
|
-
db.integer().notNullable();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
console.log(" ✓ TestUser entity defined");
|
|
70
|
-
console.log(" ✓ TestPost entity defined");
|
|
71
|
-
passed++;
|
|
72
|
-
|
|
73
|
-
// Test 2: Create context and register dbsets
|
|
74
|
-
console.log("\n📝 Test 2: Create context and register dbsets");
|
|
75
|
-
console.log("──────────────────────────────────────────────────");
|
|
76
|
-
|
|
77
|
-
class TestContext extends masterrecord.context {
|
|
78
|
-
constructor() {
|
|
79
|
-
super();
|
|
80
|
-
// Use in-memory SQLite for testing
|
|
81
|
-
this.tablePrefix = "test_";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
onConfig(db) {
|
|
85
|
-
this.dbset(TestUser);
|
|
86
|
-
this.dbset(TestPost);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
const ctx = new TestContext();
|
|
92
|
-
ctx.onConfig();
|
|
93
|
-
|
|
94
|
-
if(ctx.TestUser && ctx.TestPost) {
|
|
95
|
-
console.log(" ✓ Context created");
|
|
96
|
-
console.log(" ✓ TestUser dbset registered");
|
|
97
|
-
console.log(" ✓ TestPost dbset registered");
|
|
98
|
-
passed++;
|
|
99
|
-
} else {
|
|
100
|
-
console.log(" ✗ Dbsets not registered properly");
|
|
101
|
-
failed++;
|
|
102
|
-
}
|
|
103
|
-
} catch(err) {
|
|
104
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
105
|
-
failed++;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Test 3: Verify .new() method exists on all dbsets
|
|
109
|
-
console.log("\n📝 Test 3: Verify .new() method exists");
|
|
110
|
-
console.log("──────────────────────────────────────────────────");
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
const ctx = new TestContext();
|
|
114
|
-
ctx.onConfig();
|
|
115
|
-
|
|
116
|
-
const hasNewOnUser = typeof ctx.TestUser.new === 'function';
|
|
117
|
-
const hasNewOnPost = typeof ctx.TestPost.new === 'function';
|
|
118
|
-
|
|
119
|
-
if(hasNewOnUser && hasNewOnPost) {
|
|
120
|
-
console.log(" ✓ TestUser.new is a function");
|
|
121
|
-
console.log(" ✓ TestPost.new is a function");
|
|
122
|
-
passed++;
|
|
123
|
-
} else {
|
|
124
|
-
console.log(` ✗ TestUser.new: ${typeof ctx.TestUser.new}`);
|
|
125
|
-
console.log(` ✗ TestPost.new: ${typeof ctx.TestPost.new}`);
|
|
126
|
-
failed++;
|
|
127
|
-
}
|
|
128
|
-
} catch(err) {
|
|
129
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
130
|
-
failed++;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Test 4: Create entity instance with .new()
|
|
134
|
-
console.log("\n📝 Test 4: Create entity instance with .new()");
|
|
135
|
-
console.log("──────────────────────────────────────────────────");
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
const ctx = new TestContext();
|
|
139
|
-
ctx.onConfig();
|
|
140
|
-
|
|
141
|
-
const user = ctx.TestUser.new();
|
|
142
|
-
|
|
143
|
-
if(user && typeof user === 'object') {
|
|
144
|
-
console.log(" ✓ .new() returns an object");
|
|
145
|
-
|
|
146
|
-
// Check internal properties
|
|
147
|
-
if(user.__state === 'insert') {
|
|
148
|
-
console.log(" ✓ Entity has __state = 'insert'");
|
|
149
|
-
} else {
|
|
150
|
-
console.log(` ✗ Expected __state='insert', got '${user.__state}'`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if(user.__entity) {
|
|
154
|
-
console.log(" ✓ Entity has __entity reference");
|
|
155
|
-
} else {
|
|
156
|
-
console.log(" ✗ Entity missing __entity reference");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if(user.__context) {
|
|
160
|
-
console.log(" ✓ Entity has __context reference");
|
|
161
|
-
} else {
|
|
162
|
-
console.log(" ✗ Entity missing __context reference");
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
passed++;
|
|
166
|
-
} else {
|
|
167
|
-
console.log(` ✗ .new() returned: ${typeof user}`);
|
|
168
|
-
failed++;
|
|
169
|
-
}
|
|
170
|
-
} catch(err) {
|
|
171
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
172
|
-
failed++;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Test 5: Set properties on new entity
|
|
176
|
-
console.log("\n📝 Test 5: Set properties on new entity");
|
|
177
|
-
console.log("──────────────────────────────────────────────────");
|
|
178
|
-
|
|
179
|
-
try {
|
|
180
|
-
const ctx = new TestContext();
|
|
181
|
-
ctx.onConfig();
|
|
182
|
-
|
|
183
|
-
const user = ctx.TestUser.new();
|
|
184
|
-
user.name = "John Doe";
|
|
185
|
-
user.email = "john@example.com";
|
|
186
|
-
user.age = 30;
|
|
187
|
-
|
|
188
|
-
if(user.name === "John Doe" && user.email === "john@example.com" && user.age === 30) {
|
|
189
|
-
console.log(" ✓ Properties set successfully");
|
|
190
|
-
console.log(` ✓ user.name = "${user.name}"`);
|
|
191
|
-
console.log(` ✓ user.email = "${user.email}"`);
|
|
192
|
-
console.log(` ✓ user.age = ${user.age}`);
|
|
193
|
-
passed++;
|
|
194
|
-
} else {
|
|
195
|
-
console.log(` ✗ Properties not set correctly`);
|
|
196
|
-
console.log(` ✗ user.name = "${user.name}"`);
|
|
197
|
-
console.log(` ✗ user.email = "${user.email}"`);
|
|
198
|
-
console.log(` ✗ user.age = ${user.age}`);
|
|
199
|
-
failed++;
|
|
200
|
-
}
|
|
201
|
-
} catch(err) {
|
|
202
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
203
|
-
failed++;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Test 6: Verify entity is tracked
|
|
207
|
-
console.log("\n📝 Test 6: Verify entity is tracked in context");
|
|
208
|
-
console.log("──────────────────────────────────────────────────");
|
|
209
|
-
|
|
210
|
-
try {
|
|
211
|
-
const ctx = new TestContext();
|
|
212
|
-
ctx.onConfig();
|
|
213
|
-
|
|
214
|
-
const user = ctx.TestUser.new();
|
|
215
|
-
user.name = "Jane Doe";
|
|
216
|
-
user.email = "jane@example.com";
|
|
217
|
-
|
|
218
|
-
// Check if entity is in tracked entities
|
|
219
|
-
const isTracked = ctx.__trackedEntities.some(e => e.__ID === user.__ID);
|
|
220
|
-
|
|
221
|
-
if(isTracked) {
|
|
222
|
-
console.log(" ✓ Entity is tracked in context");
|
|
223
|
-
console.log(` ✓ Tracked entities count: ${ctx.__trackedEntities.length}`);
|
|
224
|
-
passed++;
|
|
225
|
-
} else {
|
|
226
|
-
console.log(" ✗ Entity not found in tracked entities");
|
|
227
|
-
failed++;
|
|
228
|
-
}
|
|
229
|
-
} catch(err) {
|
|
230
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
231
|
-
failed++;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Test 7: Verify dirty fields tracking
|
|
235
|
-
console.log("\n📝 Test 7: Verify dirty fields are tracked");
|
|
236
|
-
console.log("──────────────────────────────────────────────────");
|
|
237
|
-
|
|
238
|
-
try {
|
|
239
|
-
const ctx = new TestContext();
|
|
240
|
-
ctx.onConfig();
|
|
241
|
-
|
|
242
|
-
const user = ctx.TestUser.new();
|
|
243
|
-
user.name = "Test User";
|
|
244
|
-
user.email = "test@example.com";
|
|
245
|
-
|
|
246
|
-
const hasDirtyFields = user.__dirtyFields && user.__dirtyFields.length > 0;
|
|
247
|
-
const hasNameInDirty = user.__dirtyFields.includes('name');
|
|
248
|
-
const hasEmailInDirty = user.__dirtyFields.includes('email');
|
|
249
|
-
|
|
250
|
-
if(hasDirtyFields && hasNameInDirty && hasEmailInDirty) {
|
|
251
|
-
console.log(" ✓ Dirty fields tracked");
|
|
252
|
-
console.log(` ✓ Dirty fields: [${user.__dirtyFields.join(', ')}]`);
|
|
253
|
-
passed++;
|
|
254
|
-
} else {
|
|
255
|
-
console.log(` ✗ Dirty fields not tracked properly`);
|
|
256
|
-
console.log(` ✗ Dirty fields: [${user.__dirtyFields.join(', ')}]`);
|
|
257
|
-
failed++;
|
|
258
|
-
}
|
|
259
|
-
} catch(err) {
|
|
260
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
261
|
-
failed++;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Test 8: Multiple entities from same dbset
|
|
265
|
-
console.log("\n📝 Test 8: Create multiple entities from same dbset");
|
|
266
|
-
console.log("──────────────────────────────────────────────────");
|
|
267
|
-
|
|
268
|
-
try {
|
|
269
|
-
const ctx = new TestContext();
|
|
270
|
-
ctx.onConfig();
|
|
271
|
-
|
|
272
|
-
const user1 = ctx.TestUser.new();
|
|
273
|
-
user1.name = "User 1";
|
|
274
|
-
user1.email = "user1@example.com";
|
|
275
|
-
|
|
276
|
-
const user2 = ctx.TestUser.new();
|
|
277
|
-
user2.name = "User 2";
|
|
278
|
-
user2.email = "user2@example.com";
|
|
279
|
-
|
|
280
|
-
const differentIDs = user1.__ID !== user2.__ID;
|
|
281
|
-
const bothTracked = ctx.__trackedEntities.length === 2;
|
|
282
|
-
|
|
283
|
-
if(differentIDs && bothTracked) {
|
|
284
|
-
console.log(" ✓ Multiple entities created");
|
|
285
|
-
console.log(" ✓ Entities have different IDs");
|
|
286
|
-
console.log(" ✓ Both entities tracked");
|
|
287
|
-
passed++;
|
|
288
|
-
} else {
|
|
289
|
-
console.log(` ✗ Multiple entity creation failed`);
|
|
290
|
-
console.log(` ✗ Different IDs: ${differentIDs}`);
|
|
291
|
-
console.log(` ✗ Both tracked: ${bothTracked}`);
|
|
292
|
-
failed++;
|
|
293
|
-
}
|
|
294
|
-
} catch(err) {
|
|
295
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
296
|
-
failed++;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// Summary
|
|
300
|
-
console.log("\n\n╔════════════════════════════════════════════════════════════════╗");
|
|
301
|
-
console.log("║ Test Summary ║");
|
|
302
|
-
console.log("╚════════════════════════════════════════════════════════════════╝");
|
|
303
|
-
|
|
304
|
-
const total = passed + failed;
|
|
305
|
-
const successRate = Math.round((passed/total)*100);
|
|
306
|
-
|
|
307
|
-
console.log(`\n Total Tests: ${total}`);
|
|
308
|
-
console.log(` ✅ Passed: ${passed}`);
|
|
309
|
-
console.log(` ❌ Failed: ${failed}`);
|
|
310
|
-
console.log(` Success Rate: ${successRate}%\n`);
|
|
311
|
-
|
|
312
|
-
if(failed === 0){
|
|
313
|
-
console.log("🎉 All .new() tests passed!");
|
|
314
|
-
console.log("\n✨ Feature Implemented!");
|
|
315
|
-
console.log("\n📖 What was fixed:");
|
|
316
|
-
console.log(" - Added .new() method to queryMethods.js");
|
|
317
|
-
console.log(" - Creates empty entity instances with property setters");
|
|
318
|
-
console.log(" - Automatically tracks entities for INSERT operations");
|
|
319
|
-
console.log(" - Sets __state = 'insert' for new entities");
|
|
320
|
-
console.log(" - Tracks dirty fields as properties are set");
|
|
321
|
-
console.log("\n Usage example:");
|
|
322
|
-
console.log(" const job = context.QaIntelligenceJob.new();");
|
|
323
|
-
console.log(" job.annotation_id = 123;");
|
|
324
|
-
console.log(" job.job_type = 'auto_rewrite';");
|
|
325
|
-
console.log(" context.saveChanges(); // Inserts the job\n");
|
|
326
|
-
process.exit(0);
|
|
327
|
-
} else {
|
|
328
|
-
console.log("⚠️ Some tests failed. Review and fix issues.");
|
|
329
|
-
process.exit(1);
|
|
330
|
-
}
|
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unit Test: .new() Method
|
|
3
|
-
*
|
|
4
|
-
* Tests the .new() method implementation directly without database setup
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const queryMethods = require('../QueryLanguage/queryMethods');
|
|
8
|
-
const QueryParameters = require('../QueryLanguage/queryParameters');
|
|
9
|
-
|
|
10
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
11
|
-
console.log("║ .new() Method Unit Test ║");
|
|
12
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
13
|
-
|
|
14
|
-
let passed = 0;
|
|
15
|
-
let failed = 0;
|
|
16
|
-
|
|
17
|
-
// Test 1: Verify .new() method exists
|
|
18
|
-
console.log("📝 Test 1: Verify .new() method exists in queryMethods");
|
|
19
|
-
console.log("──────────────────────────────────────────────────");
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
const hasNewMethod = typeof queryMethods.prototype.new === 'function';
|
|
23
|
-
|
|
24
|
-
if(hasNewMethod) {
|
|
25
|
-
console.log(" ✓ .new() method exists");
|
|
26
|
-
console.log(" ✓ Method is a function");
|
|
27
|
-
passed++;
|
|
28
|
-
} else {
|
|
29
|
-
console.log(` ✗ .new() method not found`);
|
|
30
|
-
console.log(` ✗ typeof queryMethods.prototype.new: ${typeof queryMethods.prototype.new}`);
|
|
31
|
-
failed++;
|
|
32
|
-
}
|
|
33
|
-
} catch(err) {
|
|
34
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
35
|
-
failed++;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Test 2: Create mock entity and context
|
|
39
|
-
console.log("\n📝 Test 2: Create entity with .new() method");
|
|
40
|
-
console.log("──────────────────────────────────────────────────");
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
// Mock entity definition
|
|
44
|
-
const mockEntity = {
|
|
45
|
-
__name: 'TestEntity',
|
|
46
|
-
id: { type: 'integer', primary: true, auto: true, isNavigational: false },
|
|
47
|
-
name: { type: 'string', nullable: false, isNavigational: false },
|
|
48
|
-
email: { type: 'string', nullable: false, unique: true, isNavigational: false },
|
|
49
|
-
age: { type: 'integer', nullable: true, isNavigational: false }
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// Mock context
|
|
53
|
-
const mockContext = {
|
|
54
|
-
__trackedEntities: [],
|
|
55
|
-
__track: function(entity) {
|
|
56
|
-
this.__trackedEntities.push(entity);
|
|
57
|
-
return entity;
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// Create queryMethods instance
|
|
62
|
-
const qm = new queryMethods(mockEntity, mockContext);
|
|
63
|
-
|
|
64
|
-
// Call .new()
|
|
65
|
-
const entity = qm.new();
|
|
66
|
-
|
|
67
|
-
if(entity && typeof entity === 'object') {
|
|
68
|
-
console.log(" ✓ .new() returns an object");
|
|
69
|
-
|
|
70
|
-
// Check properties
|
|
71
|
-
if(entity.__state === 'insert') {
|
|
72
|
-
console.log(" ✓ Entity.__state = 'insert'");
|
|
73
|
-
} else {
|
|
74
|
-
console.log(` ✗ Expected __state='insert', got '${entity.__state}'`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if(entity.__entity === mockEntity) {
|
|
78
|
-
console.log(" ✓ Entity.__entity reference set");
|
|
79
|
-
} else {
|
|
80
|
-
console.log(" ✗ Entity.__entity not set properly");
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if(entity.__context === mockContext) {
|
|
84
|
-
console.log(" ✓ Entity.__context reference set");
|
|
85
|
-
} else {
|
|
86
|
-
console.log(" ✗ Entity.__context not set properly");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if(entity.__name === 'TestEntity') {
|
|
90
|
-
console.log(" ✓ Entity.__name = 'TestEntity'");
|
|
91
|
-
} else {
|
|
92
|
-
console.log(` ✗ Expected __name='TestEntity', got '${entity.__name}'`);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if(Array.isArray(entity.__dirtyFields)) {
|
|
96
|
-
console.log(" ✓ Entity.__dirtyFields is an array");
|
|
97
|
-
} else {
|
|
98
|
-
console.log(" ✗ Entity.__dirtyFields not initialized");
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
passed++;
|
|
102
|
-
} else {
|
|
103
|
-
console.log(` ✗ .new() returned: ${typeof entity}`);
|
|
104
|
-
failed++;
|
|
105
|
-
}
|
|
106
|
-
} catch(err) {
|
|
107
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
108
|
-
console.log(` ✗ Stack: ${err.stack}`);
|
|
109
|
-
failed++;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Test 3: Set properties on new entity
|
|
113
|
-
console.log("\n📝 Test 3: Set and get properties on new entity");
|
|
114
|
-
console.log("──────────────────────────────────────────────────");
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
const mockEntity = {
|
|
118
|
-
__name: 'TestEntity',
|
|
119
|
-
id: { type: 'integer', primary: true, auto: true, isNavigational: false },
|
|
120
|
-
name: { type: 'string', nullable: false, isNavigational: false },
|
|
121
|
-
email: { type: 'string', nullable: false, unique: true, isNavigational: false },
|
|
122
|
-
age: { type: 'integer', nullable: true, isNavigational: false }
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const mockContext = {
|
|
126
|
-
__trackedEntities: [],
|
|
127
|
-
__track: function(entity) {
|
|
128
|
-
this.__trackedEntities.push(entity);
|
|
129
|
-
return entity;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const qm = new queryMethods(mockEntity, mockContext);
|
|
134
|
-
const entity = qm.new();
|
|
135
|
-
|
|
136
|
-
// Set properties
|
|
137
|
-
entity.name = "John Doe";
|
|
138
|
-
entity.email = "john@example.com";
|
|
139
|
-
entity.age = 30;
|
|
140
|
-
|
|
141
|
-
// Get properties
|
|
142
|
-
const nameValue = entity.name;
|
|
143
|
-
const emailValue = entity.email;
|
|
144
|
-
const ageValue = entity.age;
|
|
145
|
-
|
|
146
|
-
if(nameValue === "John Doe" && emailValue === "john@example.com" && ageValue === 30) {
|
|
147
|
-
console.log(" ✓ Properties set and retrieved correctly");
|
|
148
|
-
console.log(` ✓ name: "${nameValue}"`);
|
|
149
|
-
console.log(` ✓ email: "${emailValue}"`);
|
|
150
|
-
console.log(` ✓ age: ${ageValue}`);
|
|
151
|
-
passed++;
|
|
152
|
-
} else {
|
|
153
|
-
console.log(` ✗ Properties not working correctly`);
|
|
154
|
-
console.log(` ✗ name: "${nameValue}" (expected "John Doe")`);
|
|
155
|
-
console.log(` ✗ email: "${emailValue}" (expected "john@example.com")`);
|
|
156
|
-
console.log(` ✗ age: ${ageValue} (expected 30)`);
|
|
157
|
-
failed++;
|
|
158
|
-
}
|
|
159
|
-
} catch(err) {
|
|
160
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
161
|
-
failed++;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Test 4: Verify dirty fields tracking
|
|
165
|
-
console.log("\n📝 Test 4: Verify dirty fields tracking");
|
|
166
|
-
console.log("──────────────────────────────────────────────────");
|
|
167
|
-
|
|
168
|
-
try {
|
|
169
|
-
const mockEntity = {
|
|
170
|
-
__name: 'TestEntity',
|
|
171
|
-
name: { type: 'string', nullable: false, isNavigational: false },
|
|
172
|
-
email: { type: 'string', nullable: false, isNavigational: false }
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const mockContext = {
|
|
176
|
-
__trackedEntities: [],
|
|
177
|
-
__track: function(entity) {
|
|
178
|
-
this.__trackedEntities.push(entity);
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
const qm = new queryMethods(mockEntity, mockContext);
|
|
183
|
-
const entity = qm.new();
|
|
184
|
-
|
|
185
|
-
entity.name = "Test User";
|
|
186
|
-
entity.email = "test@example.com";
|
|
187
|
-
|
|
188
|
-
const hasName = entity.__dirtyFields.includes('name');
|
|
189
|
-
const hasEmail = entity.__dirtyFields.includes('email');
|
|
190
|
-
|
|
191
|
-
if(hasName && hasEmail) {
|
|
192
|
-
console.log(" ✓ Dirty fields tracked correctly");
|
|
193
|
-
console.log(` ✓ Dirty fields: [${entity.__dirtyFields.join(', ')}]`);
|
|
194
|
-
passed++;
|
|
195
|
-
} else {
|
|
196
|
-
console.log(` ✗ Dirty fields not tracked`);
|
|
197
|
-
console.log(` ✗ Dirty fields: [${entity.__dirtyFields.join(', ')}]`);
|
|
198
|
-
failed++;
|
|
199
|
-
}
|
|
200
|
-
} catch(err) {
|
|
201
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
202
|
-
failed++;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Test 5: Verify entity is tracked in context
|
|
206
|
-
console.log("\n📝 Test 5: Verify entity is tracked in context");
|
|
207
|
-
console.log("──────────────────────────────────────────────────");
|
|
208
|
-
|
|
209
|
-
try {
|
|
210
|
-
const mockEntity = {
|
|
211
|
-
__name: 'TestEntity',
|
|
212
|
-
name: { type: 'string', nullable: false, isNavigational: false }
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const mockContext = {
|
|
216
|
-
__trackedEntities: [],
|
|
217
|
-
__track: function(entity) {
|
|
218
|
-
this.__trackedEntities.push(entity);
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
const qm = new queryMethods(mockEntity, mockContext);
|
|
223
|
-
const entity = qm.new();
|
|
224
|
-
|
|
225
|
-
const isTracked = mockContext.__trackedEntities.length === 1;
|
|
226
|
-
const correctEntity = mockContext.__trackedEntities[0] === entity;
|
|
227
|
-
|
|
228
|
-
if(isTracked && correctEntity) {
|
|
229
|
-
console.log(" ✓ Entity tracked in context");
|
|
230
|
-
console.log(" ✓ Correct entity reference stored");
|
|
231
|
-
passed++;
|
|
232
|
-
} else {
|
|
233
|
-
console.log(` ✗ Entity not tracked properly`);
|
|
234
|
-
console.log(` ✗ Tracked count: ${mockContext.__trackedEntities.length}`);
|
|
235
|
-
failed++;
|
|
236
|
-
}
|
|
237
|
-
} catch(err) {
|
|
238
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
239
|
-
failed++;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Test 6: Verify navigational properties are skipped
|
|
243
|
-
console.log("\n📝 Test 6: Verify navigational properties are skipped");
|
|
244
|
-
console.log("──────────────────────────────────────────────────");
|
|
245
|
-
|
|
246
|
-
try {
|
|
247
|
-
const mockEntity = {
|
|
248
|
-
__name: 'TestEntity',
|
|
249
|
-
id: { type: 'integer', primary: true, isNavigational: false },
|
|
250
|
-
name: { type: 'string', isNavigational: false },
|
|
251
|
-
Posts: { type: 'hasMany', foreignTable: 'Post', isNavigational: true },
|
|
252
|
-
Profile: { type: 'hasOne', foreignTable: 'Profile', isNavigational: true }
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const mockContext = {
|
|
256
|
-
__trackedEntities: [],
|
|
257
|
-
__track: function(entity) {
|
|
258
|
-
this.__trackedEntities.push(entity);
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
const qm = new queryMethods(mockEntity, mockContext);
|
|
263
|
-
const entity = qm.new();
|
|
264
|
-
|
|
265
|
-
// Check that regular properties exist
|
|
266
|
-
const hasNameDescriptor = Object.getOwnPropertyDescriptor(entity, 'name') !== undefined;
|
|
267
|
-
// Check that navigational properties don't exist
|
|
268
|
-
const hasPostsDescriptor = Object.getOwnPropertyDescriptor(entity, 'Posts') !== undefined;
|
|
269
|
-
const hasProfileDescriptor = Object.getOwnPropertyDescriptor(entity, 'Profile') !== undefined;
|
|
270
|
-
|
|
271
|
-
if(hasNameDescriptor && !hasPostsDescriptor && !hasProfileDescriptor) {
|
|
272
|
-
console.log(" ✓ Regular properties created");
|
|
273
|
-
console.log(" ✓ Navigational properties skipped");
|
|
274
|
-
passed++;
|
|
275
|
-
} else {
|
|
276
|
-
console.log(` ✗ Property creation incorrect`);
|
|
277
|
-
console.log(` ✗ name: ${hasNameDescriptor ? 'exists' : 'missing'}`);
|
|
278
|
-
console.log(` ✗ Posts: ${hasPostsDescriptor ? 'exists (should not)' : 'skipped (correct)'}`);
|
|
279
|
-
console.log(` ✗ Profile: ${hasProfileDescriptor ? 'exists (should not)' : 'skipped (correct)'}`);
|
|
280
|
-
failed++;
|
|
281
|
-
}
|
|
282
|
-
} catch(err) {
|
|
283
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
284
|
-
failed++;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// Summary
|
|
288
|
-
console.log("\n\n╔════════════════════════════════════════════════════════════════╗");
|
|
289
|
-
console.log("║ Test Summary ║");
|
|
290
|
-
console.log("╚════════════════════════════════════════════════════════════════╝");
|
|
291
|
-
|
|
292
|
-
const total = passed + failed;
|
|
293
|
-
const successRate = Math.round((passed/total)*100);
|
|
294
|
-
|
|
295
|
-
console.log(`\n Total Tests: ${total}`);
|
|
296
|
-
console.log(` ✅ Passed: ${passed}`);
|
|
297
|
-
console.log(` ❌ Failed: ${failed}`);
|
|
298
|
-
console.log(` Success Rate: ${successRate}%\n`);
|
|
299
|
-
|
|
300
|
-
if(failed === 0){
|
|
301
|
-
console.log("🎉 All unit tests passed!");
|
|
302
|
-
console.log("\n✨ .new() Method Successfully Implemented!");
|
|
303
|
-
console.log("\n📖 What was added:");
|
|
304
|
-
console.log(" - queryMethods.prototype.new() creates empty entity instances");
|
|
305
|
-
console.log(" - Automatically sets __state = 'insert'");
|
|
306
|
-
console.log(" - Creates property getters/setters for all fields");
|
|
307
|
-
console.log(" - Tracks dirty fields as properties are set");
|
|
308
|
-
console.log(" - Skips navigational properties (hasMany, hasOne, etc.)");
|
|
309
|
-
console.log(" - Automatically tracks entity in context");
|
|
310
|
-
console.log("\n Usage:");
|
|
311
|
-
console.log(" const job = context.QaIntelligenceJob.new();");
|
|
312
|
-
console.log(" job.annotation_id = 123;");
|
|
313
|
-
console.log(" job.job_type = 'auto_rewrite';");
|
|
314
|
-
console.log(" job.status = 'queued';");
|
|
315
|
-
console.log(" context.saveChanges(); // Inserts into database\n");
|
|
316
|
-
process.exit(0);
|
|
317
|
-
} else {
|
|
318
|
-
console.log("⚠️ Some tests failed. Review implementation.");
|
|
319
|
-
process.exit(1);
|
|
320
|
-
}
|