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
v1.18.x
Barış Soner Uşaklı 4 years ago committed by GitHub
parent b5b92768e2
commit 8f0386d9ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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) => {
async function doPrepend(value) {
await client.query({
name: 'listPrepend',
text: `
INSERT INTO "legacy_list" ("_key", "array")
VALUES ($1::TEXT, ARRAY[$2::TEXT])
ON CONFLICT ("_key")
DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
values: [key, value],
});
}
await helpers.ensureLegacyObjectType(client, key, 'list'); await helpers.ensureLegacyObjectType(client, key, 'list');
await client.query({ if (Array.isArray(value)) {
name: 'listPrepend', // TODO: perf make single query
text: ` for (const v of value) {
INSERT INTO "legacy_list" ("_key", "array") // eslint-disable-next-line
VALUES ($1::TEXT, ARRAY[$2::TEXT]) await doPrepend(v);
ON CONFLICT ("_key") }
DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`, return;
values: [key, value], }
}); 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) => {
async function doAppend(value) {
await client.query({
name: 'listAppend',
text: `
INSERT INTO "legacy_list" ("_key", "array")
VALUES ($1::TEXT, ARRAY[$2::TEXT])
ON CONFLICT ("_key")
DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
values: [key, value],
});
}
await helpers.ensureLegacyObjectType(client, key, 'list'); await helpers.ensureLegacyObjectType(client, key, 'list');
await client.query({ if (Array.isArray(value)) {
name: 'listAppend', // TODO: perf make single query
text: ` for (const v of value) {
INSERT INTO "legacy_list" ("_key", "array") // eslint-disable-next-line
VALUES ($1::TEXT, ARRAY[$2::TEXT]) await doAppend(v);
ON CONFLICT ("_key") }
DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`, return;
values: [key, value], }
}); 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()', () => {

Loading…
Cancel
Save