Skip to content

Commit 63e738e

Browse files
committed
Add configuration options tests (uniqueFields and autoId)
1 parent 82df0c0 commit 63e738e

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

test/test-config-options.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import JsonFileCRUD from "../lib/json-file-crud.js";
2+
import fs from "fs";
3+
4+
// Test setup
5+
let passed = 0;
6+
let total = 0;
7+
let completed = 0;
8+
9+
function test(description, testFn) {
10+
total++;
11+
testFn((err, success) => {
12+
completed++;
13+
14+
if (err || !success) {
15+
console.log(`✗ ${description}: ${err?.message || 'failed'}`);
16+
} else {
17+
console.log(`✓ ${description}`);
18+
passed++;
19+
}
20+
21+
// Check if all async tests are done
22+
if (completed === total) {
23+
console.log(`\n${passed}/${total} tests passed`);
24+
process.exit(passed === total ? 0 : 1);
25+
}
26+
});
27+
}
28+
29+
function cleanupFile(filePath) {
30+
if (fs.existsSync(filePath)) {
31+
fs.unlinkSync(filePath);
32+
}
33+
}
34+
35+
// Test unique fields in create
36+
test('unique fields prevent duplicates in create', (done) => {
37+
const testFile = "./test-unique-create.json";
38+
cleanupFile(testFile);
39+
40+
const crud = new JsonFileCRUD(testFile, {
41+
uniqueFields: ['email', 'username']
42+
});
43+
44+
crud.create({ name: "Ariel", email: "ariel@example.com", username: "ariel123" }, (err, item1) => {
45+
if (err) {
46+
cleanupFile(testFile);
47+
return done(err);
48+
}
49+
50+
// Try to create another item with same email
51+
crud.create({ name: "Yoni", email: "ariel@example.com", username: "yoni456" }, (err2, item2) => {
52+
if (err2 && err2.message.includes("email")) {
53+
// Try to create another item with same username
54+
crud.create({ name: "Moshe", email: "moshe@example.com", username: "ariel123" }, (err3, item3) => {
55+
cleanupFile(testFile);
56+
if (err3 && err3.message.includes("username")) {
57+
return done(null, true);
58+
}
59+
done(new Error(`should have failed for duplicate username, got: ${err3?.message || 'no error'}`));
60+
});
61+
} else {
62+
cleanupFile(testFile);
63+
done(new Error(`should have failed for duplicate email, got: ${err2?.message || 'no error'}`));
64+
}
65+
});
66+
});
67+
});
68+
69+
// Test unique fields in update
70+
test('unique fields prevent duplicates in update', (done) => {
71+
const testFile = "./test-unique-update.json";
72+
cleanupFile(testFile);
73+
74+
const crud = new JsonFileCRUD(testFile, {
75+
uniqueFields: ['email', 'username']
76+
});
77+
78+
// First create two items
79+
crud.create({ name: "UpdateUser1", email: "updateuser1@example.com", username: "updateuser1" }, (err, item1) => {
80+
if (err) {
81+
cleanupFile(testFile);
82+
return done(err);
83+
}
84+
85+
crud.create({ name: "UpdateUser2", email: "updateuser2@example.com", username: "updateuser2" }, (err2, item2) => {
86+
if (err2) {
87+
cleanupFile(testFile);
88+
return done(err2);
89+
}
90+
91+
// Try to update item2 with item1's email
92+
crud.update(item2.id, { email: "updateuser1@example.com" }, (err3, updatedItem) => {
93+
cleanupFile(testFile);
94+
if (err3 && err3.message.includes("email")) {
95+
return done(null, true);
96+
}
97+
done(new Error('should have failed for duplicate email in update'));
98+
});
99+
});
100+
});
101+
});
102+
103+
// Test autoId can be disabled
104+
test('autoId can be disabled', (done) => {
105+
const testFile = "./test-auto-id-disabled.json";
106+
cleanupFile(testFile);
107+
108+
const crud = new JsonFileCRUD(testFile, { autoId: false });
109+
110+
// Create item without ID (should work when autoId is disabled)
111+
crud.create({ name: "NoAutoId" }, (err, item) => {
112+
if (err) {
113+
cleanupFile(testFile);
114+
return done(err);
115+
}
116+
117+
// Should not have auto-generated ID
118+
if (item.id === undefined) {
119+
cleanupFile(testFile);
120+
return done(null, true);
121+
}
122+
123+
cleanupFile(testFile);
124+
done(new Error('should not have auto-generated ID when autoId is false'));
125+
});
126+
});
127+
128+
// Test autoId enabled (default)
129+
test('autoId works when enabled', (done) => {
130+
const testFile = "./test-auto-id-enabled.json";
131+
cleanupFile(testFile);
132+
133+
const crud = new JsonFileCRUD(testFile, { autoId: true });
134+
135+
// Create item without ID (should get auto-generated ID)
136+
crud.create({ name: "WithAutoId" }, (err, item) => {
137+
cleanupFile(testFile);
138+
if (err) return done(err);
139+
140+
// Should have auto-generated ID
141+
if (item.id === 1) {
142+
return done(null, true);
143+
}
144+
145+
done(new Error(`expected auto-generated ID of 1, got: ${item.id}`));
146+
});
147+
});

0 commit comments

Comments
 (0)