use data format for xToOne relations in findExisting

This commit is contained in:
Pierre Noël 2022-10-13 14:42:53 +02:00
parent 1c047dbcd8
commit d4b8c00a5c
3 changed files with 42 additions and 46 deletions

View File

@ -198,11 +198,8 @@ module.exports = {
ctx.body = res; ctx.body = res;
} else { } else {
const result = await strapi.entityService.load(model, { id }, targetField, queryParams); const result = await strapi.entityService.load(model, { id }, targetField, queryParams);
// const result = await strapi.db.query(sourceModelUid).load({ id }, targetField, queryParams);
// TODO: Temporary fix (use data instead)
ctx.body = { ctx.body = {
results: result ? [result] : [], data: result,
pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 },
}; };
} }
}, },

View File

@ -203,7 +203,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
}, },
]); ]);
} else { } else {
expect(res.body.results[0]).toMatchObject({ expect(res.body.data).toMatchObject({
id: expect.any(Number), id: expect.any(Number),
name: 'Skate', name: 'Skate',
...addPublishedAtCheck(expect.any(String)), ...addPublishedAtCheck(expect.any(String)),
@ -218,7 +218,11 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
}); });
expect(res.status).toBe(200); expect(res.status).toBe(200);
if (isManyRelation) {
expect(res.body.results).toHaveLength(0); expect(res.body.results).toHaveLength(0);
} else {
expect(res.body.data).toBe(null);
}
}); });
if (isManyRelation) { if (isManyRelation) {
@ -274,7 +278,7 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
}, },
]); ]);
} else { } else {
expect(res.body.results[0]).toMatchObject({ expect(res.body.data).toMatchObject({
id: expect.any(Number), id: expect.any(Number),
name: 'Skate', name: 'Skate',
...addPublishedAtCheck(expect.any(String)), ...addPublishedAtCheck(expect.any(String)),

View File

@ -22,7 +22,8 @@ const getRelations = async (modelName, field, id) => {
method: 'GET', method: 'GET',
url: `/content-manager/relations/api::${modelName}.${modelName}/${id}/${field}`, url: `/content-manager/relations/api::${modelName}.${modelName}/${id}/${field}`,
}); });
return res.body.results || res.body;
return res.body;
}; };
const deleteFixtures = async () => { const deleteFixtures = async () => {
@ -177,7 +178,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
}); });
@ -209,7 +210,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(1); expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[0].id); expect(tags[0].id).toBe(data.tags[0].id);
}); });
@ -240,7 +241,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(1); expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[1].id); expect(tags[0].id).toBe(data.tags[1].id);
}); });
@ -267,7 +268,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(3); expect(tags.length).toBe(3);
}); });
@ -293,7 +294,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(2); expect(tags.length).toBe(2);
}); });
@ -323,7 +324,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
}); });
@ -430,7 +431,7 @@ describe('Relations', () => {
username: null, username: null,
}); });
const tags = await getRelations('articlewithtag', 'tags', body.id); const tags = (await getRelations('articlewithtag', 'tags', body.id)).results;
expect(tags.length).toBe(1); expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[0].id); expect(tags[0].id).toBe(data.tags[0].id);
}); });
@ -471,7 +472,7 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const articles = await getRelations('category', 'articles', body.id); const articles = (await getRelations('category', 'articles', body.id)).results;
expect(articles.length).toBe(0); expect(articles.length).toBe(0);
}); });
@ -497,7 +498,7 @@ describe('Relations', () => {
username: null, username: null,
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const articles = await getRelations('category', 'articles', body.id); const articles = (await getRelations('category', 'articles', body.id)).results;
expect(articles.length).toBe(0); expect(articles.length).toBe(0);
}); });
@ -529,11 +530,11 @@ describe('Relations', () => {
}); });
expect(body.publishedAt).toBeUndefined(); expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id); const category = (await getRelations('article', 'category', body.id)).data;
expect(category[0].name).toBe(data.categories[0].name); expect(category.name).toBe(data.categories[0].name);
}); });
test('Update article1 with cat2', async () => { test('Update article1 with cat2', async () => {
@ -559,11 +560,11 @@ describe('Relations', () => {
username: null, username: null,
}); });
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id); const category = (await getRelations('article', 'category', body.id)).data;
expect(category[0].name).toBe(data.categories[1].name); expect(category.name).toBe(data.categories[1].name);
}); });
test('Create article2', async () => { test('Create article2', async () => {
@ -592,7 +593,7 @@ describe('Relations', () => {
username: null, username: null,
}); });
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
}); });
@ -619,11 +620,11 @@ describe('Relations', () => {
username: null, username: null,
}); });
const tags = await getRelations('article', 'tags', body.id); const tags = (await getRelations('article', 'tags', body.id)).results;
expect(tags.length).toBe(0); expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id); const category = (await getRelations('article', 'category', body.id)).data;
expect(category[0].name).toBe(data.categories[1].name); expect(category.name).toBe(data.categories[1].name);
}); });
test('Update cat1 with article1', async () => { test('Update cat1 with article1', async () => {
@ -648,7 +649,7 @@ describe('Relations', () => {
username: null, username: null,
}); });
const articles = await getRelations('category', 'articles', body.id); const articles = (await getRelations('category', 'articles', body.id)).results;
expect(articles.length).toBe(1); expect(articles.length).toBe(1);
}); });
@ -677,7 +678,7 @@ describe('Relations', () => {
username: null, username: null,
}); });
const articles = await getRelations('category', 'articles', body.id); const articles = (await getRelations('category', 'articles', body.id)).results;
expect(articles.length).toBe(1); expect(articles.length).toBe(1);
}); });
@ -687,10 +688,7 @@ describe('Relations', () => {
method: 'GET', method: 'GET',
}); });
expect(body).toMatchObject({ expect(body).toMatchObject({ data: { name: 'cat3' } });
results: [{ name: 'cat3' }],
pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 },
});
}); });
test('Get article2 with cat2', async () => { test('Get article2 with cat2', async () => {
@ -699,10 +697,7 @@ describe('Relations', () => {
method: 'GET', method: 'GET',
}); });
expect(body).toMatchObject({ expect(body).toMatchObject({ data: { name: 'cat2' } });
results: [{ name: 'cat2' }],
pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 },
});
}); });
test('Get cat1 without relations', async () => { test('Get cat1 without relations', async () => {
@ -833,8 +828,8 @@ describe('Relations', () => {
username: null, username: null,
}); });
const reference = await getRelations('article', 'reference', body.id); const reference = (await getRelations('article', 'reference', body.id)).data;
expect(reference[0].id).toBe(data.references[0].id); expect(reference.id).toBe(data.references[0].id);
}); });
test('Create article2 with ref1', async () => { test('Create article2 with ref1', async () => {
@ -863,8 +858,8 @@ describe('Relations', () => {
id: 1, id: 1,
username: null, username: null,
}); });
const reference = await getRelations('article', 'reference', body.id); const reference = (await getRelations('article', 'reference', body.id)).data;
expect(reference[0].id).toBe(data.references[0].id); expect(reference.id).toBe(data.references[0].id);
}); });
}); });
@ -889,8 +884,8 @@ describe('Relations', () => {
expect(createdReference.id).toBeDefined(); expect(createdReference.id).toBeDefined();
const tag = await getRelations('reference', 'tag', createdReference.id); const tag = (await getRelations('reference', 'tag', createdReference.id)).data;
expect(tag[0].id).toBe(createdTag.id); expect(tag.id).toBe(createdTag.id);
}); });
test('Detach Tag to a Reference', async () => { test('Detach Tag to a Reference', async () => {
@ -911,8 +906,8 @@ describe('Relations', () => {
}, },
}); });
let tag = await getRelations('reference', 'tag', createdReference.id); let tag = (await getRelations('reference', 'tag', createdReference.id)).data;
expect(tag[0].id).toBe(createdTag.id); expect(tag.id).toBe(createdTag.id);
const { body: referenceToUpdate } = await rq({ const { body: referenceToUpdate } = await rq({
url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`, url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`,
@ -922,7 +917,7 @@ describe('Relations', () => {
}, },
}); });
tag = await getRelations('reference', 'tag', referenceToUpdate.id); tag = (await getRelations('reference', 'tag', referenceToUpdate.id)).results;
expect(isEmpty(tag)).toBe(true); expect(isEmpty(tag)).toBe(true);
}); });