Skip to content

Commit

Permalink
test: add missing tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Thenkei committed Sep 13, 2024
1 parent 1f149b6 commit 1b57c2c
Showing 1 changed file with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ describe('RelationCollectionDecorator', () => {
list: jest.fn().mockImplementation((_, filter, projection) => {
let result = passportRecords.slice();

console.log('passportRecords', filter);

Check failure on line 102 in packages/datasource-customizer/test/decorators/relation/collection.test.ts

View workflow job for this annotation

GitHub Actions / Linting & Testing (datasource-customizer)

Unexpected console statement

if (filter?.conditionTree) {
result = filter.conditionTree.apply(result, passports, 'Europe/Paris');
}
Expand Down Expand Up @@ -130,6 +132,8 @@ describe('RelationCollectionDecorator', () => {
list: jest.fn().mockImplementation((_, filter, projection) => {
let result = personsRecords.slice();

console.log(filter);

Check failure on line 135 in packages/datasource-customizer/test/decorators/relation/collection.test.ts

View workflow job for this annotation

GitHub Actions / Linting & Testing (datasource-customizer)

Unexpected console statement

if (filter?.conditionTree) {
result = filter.conditionTree.apply(result, persons, 'Europe/Paris');
}
Expand Down Expand Up @@ -572,6 +576,98 @@ describe('RelationCollectionDecorator', () => {
{ personId: 203, name: 'Joseph P. Rodriguez', passport: null },
]);
});

describe('performances enhancement (operator: NotEqual)', () => {
describe('when NotIn filterOperator is available', () => {
test('should fetch fields from a many to one relation using NotIn', async () => {
const schema = passports.schema.fields.ownerId as ColumnSchema;
schema.filterOperators = new Set(['In', 'NotIn']);

newPassports.addRelation('owner', {
type: 'ManyToOne',
foreignCollection: 'persons',
foreignKey: 'ownerId',
});

const records = await newPassports.list(
factories.caller.build(),
new Filter({
conditionTree: new ConditionTreeLeaf('owner:name', 'NotEqual', 'Mae S. Waldron'),
}),
new Projection('passportId', 'owner:name'),
);

expect(records).toStrictEqual([
{ passportId: 102, owner: { name: 'Sharon J. Whalen' } },
{ passportId: 103, owner: null },
]);

// Inverted condition NotEqual becomes Equal
expect(persons.list).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
conditionTree: { field: 'name', operator: 'Equal', value: 'Mae S. Waldron' },
}),
expect.anything(),
);

// Using NotIn match the previous inverted condition result
expect(passports.list).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
conditionTree: { field: 'ownerId', operator: 'NotIn', value: [202] },
}),
expect.anything(),
);
});
});
});

describe('when NotIn filterOperator is not available', () => {
test('should fetch fields from a many to one relation using In', async () => {
newPassports.addRelation('owner', {
type: 'ManyToOne',
foreignCollection: 'persons',
foreignKey: 'ownerId',
});

const logger = jest.spyOn(console, 'warn').mockImplementation();

const records = await newPassports.list(
factories.caller.build(),
new Filter({
conditionTree: new ConditionTreeLeaf('owner:name', 'NotEqual', 'Mae S. Waldron'),
}),
new Projection('passportId', 'owner:name'),
);

// WARNING: Null results are not returned
expect(records).toStrictEqual([{ passportId: 102, owner: { name: 'Sharon J. Whalen' } }]);

expect(persons.list).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
conditionTree: { field: 'name', operator: 'NotEqual', value: 'Mae S. Waldron' },
}),
expect.anything(),
);

// Using IN match the previous condition result
expect(passports.list).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
conditionTree: { field: 'ownerId', operator: 'In', value: [201, 203] },
}),
expect.anything(),
);

expect(logger).toHaveBeenCalledWith(
'Performances could be improved by implementing the NotIn operator on ownerId (use replaceFieldOperator)',
);

logger.mockRestore();
});
});
});

describe('with two emulated relations', () => {
Expand Down

0 comments on commit 1b57c2c

Please sign in to comment.