mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 07:10:11 +00:00
add test for replaceIdByPrimaryKey
Signed-off-by: Pierre Noël <pierre.noel@strapi.io>
This commit is contained in:
parent
a1774db410
commit
15f4ae67d7
@ -0,0 +1,53 @@
|
|||||||
|
const { replaceIdByPrimaryKey } = require('../primary-key');
|
||||||
|
|
||||||
|
describe('Primary Key', () => {
|
||||||
|
describe('replaceIdByPrimaryKey', () => {
|
||||||
|
const defaultPostgresModel = { primaryKey: 'id' };
|
||||||
|
const defaultMongooseModel = { primaryKey: '_id' };
|
||||||
|
const customModel = { primaryKey: 'aRandomPrimaryKey' };
|
||||||
|
|
||||||
|
describe('Model primary key is "id"', () => {
|
||||||
|
test('Params has "id"', () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, defaultPostgresModel);
|
||||||
|
expect(result).toEqual({ id: '123', color: 'red' });
|
||||||
|
});
|
||||||
|
test(`Params doesn't have "id"`, () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ color: 'red' }, defaultPostgresModel);
|
||||||
|
expect(result).toEqual({ color: 'red' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Model primary key is "_id"', () => {
|
||||||
|
test('Params has "_id"', () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ _id: '123', color: 'red' }, defaultMongooseModel);
|
||||||
|
expect(result).toEqual({ _id: '123', color: 'red' });
|
||||||
|
});
|
||||||
|
test('Params has "id"', () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, defaultMongooseModel);
|
||||||
|
expect(result).toEqual({ _id: '123', color: 'red' });
|
||||||
|
});
|
||||||
|
test(`Params doesn't have "id" nor "_id"`, () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ color: 'red' }, defaultMongooseModel);
|
||||||
|
expect(result).toEqual({ color: 'red' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Model primary key is "aRandomPrimaryKey"', () => {
|
||||||
|
test('Params has "id"', () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, customModel);
|
||||||
|
expect(result).toEqual({ aRandomPrimaryKey: '123', color: 'red' });
|
||||||
|
});
|
||||||
|
test('Params has "aRandomPrimaryKey"', () => {
|
||||||
|
const result = replaceIdByPrimaryKey(
|
||||||
|
{ aRandomPrimaryKey: '123', color: 'red' },
|
||||||
|
customModel
|
||||||
|
);
|
||||||
|
expect(result).toEqual({ aRandomPrimaryKey: '123', color: 'red' });
|
||||||
|
});
|
||||||
|
test(`Params doesn't have "id" nor "aRandomPrimaryKey"`, () => {
|
||||||
|
const result = replaceIdByPrimaryKey({ color: 'red' }, customModel);
|
||||||
|
expect(result).toEqual({ color: 'red' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -2,13 +2,18 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
module.exports = {
|
/**
|
||||||
replaceIdByPrimaryKey: (params, model) => {
|
* If exists, rename the key "id" by the primary key name of the model ("_id" by default for mongoose).
|
||||||
const newParams = { ...params };
|
*/
|
||||||
if (_.has(params, 'id')) {
|
const replaceIdByPrimaryKey = (params, model) => {
|
||||||
delete newParams.id;
|
const newParams = { ...params };
|
||||||
newParams[model.primaryKey] = params[model.primaryKey] || params.id;
|
if (_.has(params, 'id')) {
|
||||||
}
|
delete newParams.id;
|
||||||
return newParams;
|
newParams[model.primaryKey] = params[model.primaryKey] || params.id;
|
||||||
},
|
}
|
||||||
|
return newParams;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
replaceIdByPrimaryKey,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,10 +6,7 @@ let modelsUtils;
|
|||||||
let rq;
|
let rq;
|
||||||
|
|
||||||
describe.each([
|
describe.each([
|
||||||
[
|
['CONTENT MANAGER', '/content-manager/explorer/application::withcomponent.withcomponent'],
|
||||||
'CONTENT MANAGER',
|
|
||||||
'/content-manager/explorer/application::withcomponent.withcomponent',
|
|
||||||
],
|
|
||||||
['GENERATED API', '/withcomponents'],
|
['GENERATED API', '/withcomponents'],
|
||||||
])('[%s] => Non repeatable and Not required component', (_, path) => {
|
])('[%s] => Non repeatable and Not required component', (_, path) => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user