Fixes to mongoose groups

This commit is contained in:
Alexandre Bodin 2019-08-09 11:45:31 +02:00
parent 1792338cbd
commit 34a5424a8a
7 changed files with 64 additions and 18 deletions

View File

@ -347,10 +347,10 @@ const createOnFetchPopulateFn = ({
) { ) {
this._mongooseOptions.populate[name].path = `${name}.ref`; this._mongooseOptions.populate[name].path = `${name}.ref`;
} else { } else {
this._mongooseOptions.populate[name] = { _.set(this._mongooseOptions, ['populate', name], {
path: `${name}.ref`, path: `${name}.ref`,
_docs: {}, _docs: {},
}; });
} }
}); });

View File

@ -267,9 +267,10 @@ module.exports = ({ model, modelKey, strapi }) => {
const relations = pickRelations(values); const relations = pickRelations(values);
const data = omitExernalValues(values); const data = omitExernalValues(values);
// update groups first in case it fails don't update the entity
await updateGroups(entry, values);
// Update entry with no-relational data. // Update entry with no-relational data.
await entry.updateOne(data); await entry.updateOne(data);
await updateGroups(entry, values);
// Update relational data and return the entry. // Update relational data and return the entry.
return model.updateRelations(Object.assign(params, { values: relations })); return model.updateRelations(Object.assign(params, { values: relations }));

View File

@ -227,7 +227,10 @@ describe.each([
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
} }
); );
@ -307,12 +310,18 @@ describe.each([
}); });
expect(updateRes.statusCode).toBe(200); expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual(res.body); expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}); });
test('Throws when not enough items', async () => { test('Throws when not enough items', async () => {

View File

@ -197,7 +197,10 @@ describe.each([
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
} }
); );
@ -277,12 +280,18 @@ describe.each([
}); });
expect(updateRes.statusCode).toBe(200); expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual(res.body); expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}); });
test('Removes previous groups if empty array sent', async () => { test('Removes previous groups if empty array sent', async () => {

View File

@ -196,7 +196,10 @@ describe.each([
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
} }
); );
@ -276,12 +279,18 @@ describe.each([
}); });
expect(updateRes.statusCode).toBe(200); expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual(res.body); expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}); });
test('Removes previous groups if empty array sent', async () => { test('Removes previous groups if empty array sent', async () => {

View File

@ -153,7 +153,10 @@ describe.each([
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
} }
); );
@ -171,12 +174,18 @@ describe.each([
}); });
expect(updateRes.statusCode).toBe(200); expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual(res.body); expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}); });
test('Removes previous group if null sent', async () => { test('Removes previous group if null sent', async () => {

View File

@ -151,7 +151,10 @@ describe.each([
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
} }
); );
@ -169,12 +172,18 @@ describe.each([
}); });
expect(updateRes.statusCode).toBe(200); expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual(res.body); expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`); const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(200); expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(res.body); expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}); });
test('Throws if group is null', async () => { test('Throws if group is null', async () => {