2023-11-20 14:00:51 +03:00
|
|
|
import assert from 'assert/strict';
|
2023-05-10 16:23:16 +03:00
|
|
|
import {InMemoryRepository} from '../src/index';
|
|
|
|
|
|
|
|
type SimpleEntity = {
|
|
|
|
id: string;
|
|
|
|
deleted: boolean;
|
|
|
|
name: string;
|
|
|
|
age: number;
|
2023-07-31 10:17:40 +03:00
|
|
|
birthday: string;
|
2023-05-10 16:23:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class SimpleInMemoryRepository extends InMemoryRepository<string, SimpleEntity> {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
protected toPrimitive(entity: SimpleEntity): object {
|
|
|
|
return {
|
|
|
|
name: entity.name,
|
2023-07-31 10:17:40 +03:00
|
|
|
age: entity.age,
|
|
|
|
birthday: entity.birthday
|
2023-05-10 16:23:16 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('InMemoryRepository', function () {
|
|
|
|
it('Can save, retrieve, update and delete entities', async function () {
|
|
|
|
const repository = new SimpleInMemoryRepository();
|
|
|
|
|
|
|
|
checkRetrieving: {
|
|
|
|
const entity = {
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
const result = await repository.getById('1');
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.name === 'John');
|
|
|
|
assert(result.age === 30);
|
|
|
|
assert(result.id === '1');
|
|
|
|
|
|
|
|
break checkRetrieving;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkUpdating: {
|
|
|
|
const entity = {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
entity.name = 'Kym';
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
const result = await repository.getById('2');
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.name === 'Kym');
|
|
|
|
assert(result.age === 24);
|
|
|
|
assert(result.id === '2');
|
|
|
|
|
|
|
|
break checkUpdating;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDeleting: {
|
|
|
|
const entity = {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Egg',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 180,
|
|
|
|
birthday: new Date('2010-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
assert(await repository.getById('3'));
|
|
|
|
|
|
|
|
entity.deleted = true;
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
assert(!await repository.getById('3'));
|
|
|
|
|
|
|
|
break checkDeleting;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can save and retrieve all entities', async function () {
|
|
|
|
const repository = new SimpleInMemoryRepository();
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
2023-07-31 10:17:40 +03:00
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
2023-05-10 16:23:16 +03:00
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getAll({
|
|
|
|
order: [{
|
|
|
|
field: 'age',
|
|
|
|
direction: 'desc'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.length === 3);
|
|
|
|
assert(result[0].age === 30);
|
|
|
|
assert(result[1].age === 24);
|
|
|
|
assert(result[2].age === 5);
|
|
|
|
});
|
|
|
|
|
2023-07-31 10:17:40 +03:00
|
|
|
it('Can save and retrieve a filtered page of entities', async function () {
|
2023-05-10 16:23:16 +03:00
|
|
|
const repository = new SimpleInMemoryRepository();
|
2023-07-31 10:17:40 +03:00
|
|
|
const entities = [
|
|
|
|
{
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Egg',
|
|
|
|
age: 180,
|
|
|
|
birthday: new Date('2010-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '4',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 36,
|
|
|
|
birthday: new Date('2010-01-01').toISOString()
|
|
|
|
}];
|
2023-05-10 16:23:16 +03:00
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getPage({
|
|
|
|
filter: 'age:>25',
|
|
|
|
page: 1,
|
|
|
|
limit: 3,
|
|
|
|
order: [{
|
|
|
|
field: 'age',
|
|
|
|
direction: 'asc'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
const count = await repository.getCount({
|
|
|
|
filter: 'name:John'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.length === 3);
|
|
|
|
assert(count === 1);
|
2023-07-31 10:17:40 +03:00
|
|
|
|
|
|
|
const resultBirthdayFilter = await repository.getPage({
|
|
|
|
filter: 'birthday:>2005-01-01T00:00:00.000Z',
|
|
|
|
page: 1,
|
|
|
|
limit: 3,
|
|
|
|
order: [{
|
|
|
|
field: 'age',
|
|
|
|
direction: 'asc'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(resultBirthdayFilter);
|
|
|
|
assert.equal(resultBirthdayFilter.length, 2);
|
|
|
|
assert.equal(resultBirthdayFilter[0].name, 'Kevin');
|
|
|
|
assert.equal(resultBirthdayFilter[1].name, 'Egg');
|
2023-05-10 16:23:16 +03:00
|
|
|
});
|
|
|
|
});
|