mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-03 20:45:58 +01:00
feat: add failing test for list append/prepend with list (#9303)
* feat: add failing test for list append/prepend with list * feat: mongo/psql * feat: improve test
This commit is contained in:
committed by
GitHub
parent
b5b92768e2
commit
8f0386d9ac
@@ -7,12 +7,23 @@ module.exports = function (module) {
|
|||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
value = Array.isArray(value) ? value : [value];
|
||||||
value = helpers.valueToString(value);
|
value = value.map(helpers.valueToString);
|
||||||
|
value.reverse();
|
||||||
const exists = await module.isObjectField(key, 'array');
|
const exists = await module.isObjectField(key, 'array');
|
||||||
if (exists) {
|
if (exists) {
|
||||||
await module.client.collection('objects').updateOne({ _key: key }, { $push: { array: { $each: [value], $position: 0 } } }, { upsert: true });
|
await module.client.collection('objects').updateOne({
|
||||||
|
_key: key,
|
||||||
|
}, {
|
||||||
|
$push: {
|
||||||
|
array: {
|
||||||
|
$each: value,
|
||||||
|
$position: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
upsert: true,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
await module.listAppend(key, value);
|
await module.listAppend(key, value);
|
||||||
}
|
}
|
||||||
@@ -22,8 +33,19 @@ module.exports = function (module) {
|
|||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value = helpers.valueToString(value);
|
value = Array.isArray(value) ? value : [value];
|
||||||
await module.client.collection('objects').updateOne({ _key: key }, { $push: { array: value } }, { upsert: true });
|
value = value.map(helpers.valueToString);
|
||||||
|
await module.client.collection('objects').updateOne({
|
||||||
|
_key: key,
|
||||||
|
}, {
|
||||||
|
$push: {
|
||||||
|
array: {
|
||||||
|
$each: value,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
upsert: true,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.listRemoveLast = async function (key) {
|
module.listRemoveLast = async function (key) {
|
||||||
|
|||||||
@@ -9,16 +9,28 @@ module.exports = function (module) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await module.transaction(async (client) => {
|
await module.transaction(async (client) => {
|
||||||
await helpers.ensureLegacyObjectType(client, key, 'list');
|
async function doPrepend(value) {
|
||||||
await client.query({
|
await client.query({
|
||||||
name: 'listPrepend',
|
name: 'listPrepend',
|
||||||
text: `
|
text: `
|
||||||
INSERT INTO "legacy_list" ("_key", "array")
|
INSERT INTO "legacy_list" ("_key", "array")
|
||||||
VALUES ($1::TEXT, ARRAY[$2::TEXT])
|
VALUES ($1::TEXT, ARRAY[$2::TEXT])
|
||||||
ON CONFLICT ("_key")
|
ON CONFLICT ("_key")
|
||||||
DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
|
DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
|
||||||
values: [key, value],
|
values: [key, value],
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await helpers.ensureLegacyObjectType(client, key, 'list');
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
// TODO: perf make single query
|
||||||
|
for (const v of value) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
await doPrepend(v);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await doPrepend(value);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -26,18 +38,28 @@ DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
|
|||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await module.transaction(async (client) => {
|
await module.transaction(async (client) => {
|
||||||
await helpers.ensureLegacyObjectType(client, key, 'list');
|
async function doAppend(value) {
|
||||||
await client.query({
|
await client.query({
|
||||||
name: 'listAppend',
|
name: 'listAppend',
|
||||||
text: `
|
text: `
|
||||||
INSERT INTO "legacy_list" ("_key", "array")
|
INSERT INTO "legacy_list" ("_key", "array")
|
||||||
VALUES ($1::TEXT, ARRAY[$2::TEXT])
|
VALUES ($1::TEXT, ARRAY[$2::TEXT])
|
||||||
ON CONFLICT ("_key")
|
ON CONFLICT ("_key")
|
||||||
DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
|
DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
|
||||||
values: [key, value],
|
values: [key, value],
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
await helpers.ensureLegacyObjectType(client, key, 'list');
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
// TODO: perf make single query
|
||||||
|
for (const v of value) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
await doAppend(v);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await doAppend(value);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,16 @@ describe('List methods', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should append each element to list', async () => {
|
||||||
|
await db.listAppend('arrayListAppend', ['a', 'b', 'c']);
|
||||||
|
let values = await db.getListRange('arrayListAppend', 0, -1);
|
||||||
|
assert.deepStrictEqual(values, ['a', 'b', 'c']);
|
||||||
|
|
||||||
|
await db.listAppend('arrayListAppend', ['d', 'e']);
|
||||||
|
values = await db.getListRange('arrayListAppend', 0, -1);
|
||||||
|
assert.deepStrictEqual(values, ['a', 'b', 'c', 'd', 'e']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('listPrepend()', () => {
|
describe('listPrepend()', () => {
|
||||||
@@ -52,6 +62,16 @@ describe('List methods', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should prepend each element to list', async () => {
|
||||||
|
await db.listPrepend('arrayListPrepend', ['a', 'b', 'c']);
|
||||||
|
let values = await db.getListRange('arrayListPrepend', 0, -1);
|
||||||
|
assert.deepStrictEqual(values, ['c', 'b', 'a']);
|
||||||
|
|
||||||
|
await db.listPrepend('arrayListPrepend', ['d', 'e']);
|
||||||
|
values = await db.getListRange('arrayListPrepend', 0, -1);
|
||||||
|
assert.deepStrictEqual(values, ['e', 'd', 'c', 'b', 'a']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getListRange()', () => {
|
describe('getListRange()', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user