mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
test: add test for undefined fields in getObjectsFields
This commit is contained in:
@@ -120,7 +120,7 @@ module.exports = function (module) {
|
||||
cache.set(key, cachedData[key]);
|
||||
});
|
||||
|
||||
if (!fields.length) {
|
||||
if (!Array.isArray(fields) || !fields.length) {
|
||||
return keys.map(key => (cachedData[key] ? { ...cachedData[key] } : null));
|
||||
}
|
||||
return keys.map((key) => {
|
||||
|
||||
@@ -142,7 +142,9 @@ SELECT h."data"->>$2::TEXT f
|
||||
if (!key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Array.isArray(fields) || !fields.length) {
|
||||
return await module.getObject(key);
|
||||
}
|
||||
const res = await module.pool.query({
|
||||
name: 'getObjectFields',
|
||||
text: `
|
||||
@@ -174,7 +176,8 @@ SELECT (SELECT jsonb_object_agg(f, d."value")
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return [];
|
||||
}
|
||||
if (!fields.length) {
|
||||
|
||||
if (!Array.isArray(fields) || !fields.length) {
|
||||
return await module.getObjects(keys);
|
||||
}
|
||||
const res = await module.pool.query({
|
||||
|
||||
@@ -324,6 +324,15 @@ describe('Hash methods', () => {
|
||||
assert.strictEqual(Number(objects[1].age), 3);
|
||||
assert.strictEqual(!!objects[2], false);
|
||||
});
|
||||
|
||||
it('should return objects if fields is not an array', async () => {
|
||||
const objects = await db.getObjectsFields(['testObject8', 'testObject9', 'doesnotexist'], undefined);
|
||||
assert.strictEqual(objects[0].name, 'baris');
|
||||
assert.strictEqual(Number(objects[0].age), 99);
|
||||
assert.strictEqual(objects[1].name, 'ginger');
|
||||
assert.strictEqual(Number(objects[1].age), 3);
|
||||
assert.strictEqual(!!objects[2], false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getObjectKeys()', () => {
|
||||
|
||||
Reference in New Issue
Block a user