Fix tests and cleanups

This commit is contained in:
Alexandre Bodin 2021-08-11 09:34:55 +02:00
parent 6a75affa81
commit d3a1d6fc98
7 changed files with 142 additions and 114 deletions

View File

@ -28,13 +28,15 @@ const deleteFixtures = async () => {
]) {
const uid = `application::${modelName}.${modelName}`;
await rq({
method: 'POST',
url: `/content-manager/collection-types/${uid}/actions/bulkDelete`,
body: {
ids: (data[name] || []).map(({ id }) => id),
},
});
if (data[name] && data[name].length > 0) {
await rq({
method: 'POST',
url: `/content-manager/collection-types/${uid}/actions/bulkDelete`,
body: {
ids: (data[name] || []).map(({ id }) => id),
},
});
}
}
};
@ -184,7 +186,7 @@ describe('Content Manager End to End', () => {
const entry = {
title: 'Article 2',
content: 'Content 2',
tags: [data.tags[0]],
tags: [data.tags[0].id],
};
let { body } = await rq({
@ -198,17 +200,17 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(1);
// expect(body.tags[0].id).toBe(data.tags[0].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
test('Update article1 add tag2', async () => {
const entry = Object.assign({}, data.articles[0], {
tags: [data.tags[1]],
tags: [data.tags[1].id],
});
cleanDate(entry);
@ -224,18 +226,20 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(1);
// expect(body.tags[0].id).toBe(data.tags[1].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
test('Update article1 add tag1 and tag3', async () => {
const entry = Object.assign({}, data.articles[0]);
// entry.tags.push(data.tags[0]);
// entry.tags.push(data.tags[2]);
entry.tags = entry.tags.map(tag => tag.id);
entry.tags.push(data.tags[0].id);
entry.tags.push(data.tags[2].id);
cleanDate(entry);
@ -250,16 +254,16 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(3);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(3);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
test('Update article1 remove one tag', async () => {
const entry = Object.assign({}, data.articles[0]);
// entry.tags = entry.tags.slice(1);
entry.tags = entry.tags.slice(1).map(tag => tag.id);
cleanDate(entry);
@ -274,10 +278,10 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(2);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(2);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
@ -299,10 +303,10 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(0);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(0);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
@ -409,11 +413,11 @@ describe('Content Manager End to End', () => {
data.articlesWithTag.push(body);
expect(body.id).toBeDefined();
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(1);
// expect(body.tags[0].id).toBe(data.tags[0].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
});
@ -441,10 +445,10 @@ describe('Content Manager End to End', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('cat1');
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
@ -460,10 +464,10 @@ describe('Content Manager End to End', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('cat2');
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
@ -471,7 +475,7 @@ describe('Content Manager End to End', () => {
const entry = {
title: 'Article 1',
content: 'Content 1',
category: data.categories[0],
category: data.categories[0].id,
};
let { body } = await rq({
@ -485,15 +489,15 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.category.name).toBe(entry.category.name);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.category.name).toBe(data.categories[0].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Update article1 with cat2', async () => {
const entry = Object.assign({}, data.articles[0], {
category: data.categories[1],
category: data.categories[1].id,
});
cleanDate(entry);
@ -509,10 +513,10 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.category.name).toBe(entry.category.name);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Create article2', async () => {
@ -532,14 +536,14 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Update article2 with cat2', async () => {
const entry = Object.assign({}, data.articles[1], {
category: data.categories[1],
category: data.categories[1].id,
});
cleanDate(entry);
@ -555,15 +559,16 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.category.name).toBe(entry.category.name);
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Update cat1 with article1', async () => {
const entry = Object.assign({}, data.categories[0]);
// entry.articles.push(data.articles[0]);
entry.articles = entry.articles.map(article => article.id);
entry.articles.push(data.articles[0].id);
cleanDate(entry);
@ -576,17 +581,17 @@ describe('Content Manager End to End', () => {
data.categories[0] = body;
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
// expect(body.articles.length).toBe(1);
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
expect(body.name).toBe(entry.name);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Create cat3 with article1', async () => {
const entry = {
name: 'cat3',
articles: [data.articles[0]],
articles: [data.articles[0].id],
};
let { body } = await rq({
@ -598,11 +603,11 @@ describe('Content Manager End to End', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
// expect(body.articles.length).toBe(1);
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
expect(body.name).toBe(entry.name);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Get article1 with cat3', async () => {
@ -612,9 +617,9 @@ describe('Content Manager End to End', () => {
});
expect(body.id).toBeDefined();
// expect(body.category.id).toBe(data.categories[2].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.category.id).toBe(data.categories[2].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Get article2 with cat2', async () => {
@ -624,9 +629,9 @@ describe('Content Manager End to End', () => {
});
expect(body.id).toBeDefined();
// expect(body.category.id).toBe(data.categories[1].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.category.id).toBe(data.categories[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Get cat1 without relations', async () => {
@ -636,9 +641,9 @@ describe('Content Manager End to End', () => {
});
expect(body.id).toBeDefined();
// expect(body.articles.length).toBe(0);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.articles.length).toBe(0);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Get cat2 with article2', async () => {
@ -648,10 +653,10 @@ describe('Content Manager End to End', () => {
});
expect(body.id).toBeDefined();
// expect(body.articles.length).toBe(1);
// expect(body.articles[0].id).toBe(data.articles[1].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.articles.length).toBe(1);
expect(body.articles[0].id).toBe(data.articles[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Get cat3 with article1', async () => {
@ -661,10 +666,10 @@ describe('Content Manager End to End', () => {
});
expect(body.id).toBeDefined();
// expect(body.articles.length).toBe(1);
// expect(body.articles[0].id).toBe(data.articles[0].id);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.articles.length).toBe(1);
expect(body.articles[0].id).toBe(data.articles[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
});
@ -693,8 +698,8 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.name).toBe('ref1');
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Create article1', async () => {
@ -714,8 +719,8 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
@ -737,9 +742,9 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.reference.id).toBe(entry.reference);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.reference.id).toBe(entry.reference);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
test('Create article2 with ref1', async () => {
@ -760,9 +765,9 @@ describe('Content Manager End to End', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
// expect(body.reference.id).toBe(entry.reference);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.reference.id).toBe(entry.reference);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
});
});
@ -781,12 +786,12 @@ describe('Content Manager End to End', () => {
method: 'POST',
body: {
name: 'cat111',
tag: createdTag,
tag: createdTag.id,
},
});
expect(createdReference.id).toBeDefined();
// expect(createdReference.tag.id).toBe(createdTag.id);
expect(createdReference.tag.id).toBe(createdTag.id);
});
test('Detach Tag to a Reference', async () => {
@ -834,7 +839,7 @@ describe('Content Manager End to End', () => {
method: 'POST',
body: {
name: 'cat111',
tag: createdTag,
tag: createdTag.id,
},
});

View File

@ -1107,10 +1107,11 @@ const applySearch = (qb, query, ctx) => {
}
switch (db.dialect.client) {
case 'pg': {
case 'postgres': {
searchColumns.forEach(attr =>
qb.orWhereRaw(`"${alias}"."${attr}"::text ILIKE ?`, `%${escapeQuery(query, '*%\\')}%`)
);
break;
}
case 'sqlite': {

View File

@ -32,7 +32,19 @@ const createQueryBuilder = (uid, db) => {
select(args) {
state.type = 'select';
state.select = _.castArray(args).map(col => this.aliasColumn(col));
state.select = _.uniq(_.castArray(args)).map(col => this.aliasColumn(col));
return this;
},
addSelect(args) {
_.uniq(_.castArray(args))
.map(col => this.aliasColumn(col))
.forEach(toSelect => {
if (!state.select.includes(toSelect)) {
state.select.push(toSelect);
}
});
return this;
},
@ -72,11 +84,6 @@ const createQueryBuilder = (uid, db) => {
return this;
},
addSelect(args) {
state.select.push(..._.castArray(args).map(col => this.aliasColumn(col)));
return this;
},
limit(limit) {
state.limit = limit;
return this;

View File

@ -75,10 +75,14 @@ describe('Core API - Basic + dz', () => {
},
],
};
const res = await rq({
method: 'POST',
url: '/product-with-dzs',
body: product,
qs: {
populate: ['dz'],
},
});
expect(res.statusCode).toBe(200);
@ -91,6 +95,9 @@ describe('Core API - Basic + dz', () => {
const res = await rq({
method: 'GET',
url: '/product-with-dzs',
qs: {
populate: ['dz'],
},
});
expect(res.statusCode).toBe(200);
@ -112,10 +119,14 @@ describe('Core API - Basic + dz', () => {
},
],
};
const res = await rq({
method: 'PUT',
url: `/product-with-dzs/${data.productWithDz[0].id}`,
body: product,
qs: {
populate: ['dz'],
},
});
expect(res.statusCode).toBe(200);
@ -129,6 +140,9 @@ describe('Core API - Basic + dz', () => {
const res = await rq({
method: 'DELETE',
url: `/product-with-dzs/${data.productWithDz[0].id}`,
qs: {
populate: ['dz'],
},
});
expect(res.statusCode).toBe(200);

View File

@ -1318,7 +1318,7 @@ describe('Filtering API', () => {
});
});
describe.skip('Text query', () => {
describe('Text query', () => {
test('Cyrillic query', async () => {
const res = await rq({
method: 'GET',

View File

@ -60,7 +60,7 @@ describe('Roles API', () => {
body: {
...internals.role,
permissions: [],
users: [data.user],
users: [data.user.id],
},
});

View File

@ -30,9 +30,10 @@ const databases = {
},
sqlite: {
client: 'sqlite',
connecton: {
connection: {
filename: './tmp/data.db',
},
useNullAsDefault: true,
},
};
@ -51,7 +52,7 @@ const main = async (database, args) => {
await cleanTestApp(appName);
await generateTestApp({ appName, database });
await runAllTests(args).catch(()=> {
await runAllTests(args).catch(() => {
process.stdout.write('Tests failed\n', () => {
process.exit(1);
});