2021-07-14 14:14:35 +00:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
|
import { AppModule } from '../../src/app.module';
|
|
|
|
|
import { DataQueriesModule } from '../../src/modules/data_queries/data_queries.module';
|
|
|
|
|
import { DataSourcesModule } from '../../src/modules/data_sources/data_sources.module';
|
2021-10-11 15:15:58 +00:00
|
|
|
import { DataQueriesService } from '../../src/services/data_queries.service';
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
describe('DataQueriesService', () => {
|
|
|
|
|
let service: DataQueriesService;
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
|
imports: [DataSourcesModule, DataQueriesModule, AppModule],
|
|
|
|
|
providers: [],
|
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
|
|
service = module.get<DataQueriesService>(DataQueriesService);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options without dynamic variables', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = { foo: 'bar' };
|
|
|
|
|
const options = {};
|
2021-07-14 14:14:35 +00:00
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['foo']).toBe('bar');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options with whole value as a dynamic variable', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = { foo: '{{bar}}' };
|
2021-07-14 14:14:35 +00:00
|
|
|
const options = { '{{bar}}': 'bar' };
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['foo']).toBe('bar');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options with one dynamic variable', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = { foo: 'is a {{bar}}' };
|
2021-07-14 14:14:35 +00:00
|
|
|
const options = { '{{bar}}': 'bar' };
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['foo']).toBe('is a bar');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options with whole value as a dynamic variable that contains js code', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = { foo: '{{bar * 100 + parseInt("500")}}' };
|
2021-07-14 14:14:35 +00:00
|
|
|
const options = { '{{bar * 100 + parseInt("500")}}': 20 };
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['foo']).toBe(20);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options with the value containing more than one dynamic variable', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = { email: '{{sam}}@{{example.com}}' };
|
|
|
|
|
const options = {
|
2021-07-14 14:14:35 +00:00
|
|
|
'{{sam}}': 'sam',
|
2021-09-21 13:48:28 +00:00
|
|
|
'{{example.com}}': 'example.com',
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['email']).toBe('sam@example.com');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options that has an object', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = {
|
2021-07-14 14:14:35 +00:00
|
|
|
user: {
|
|
|
|
|
email: '{{email}}',
|
2021-09-21 13:48:28 +00:00
|
|
|
name: '{{name}}',
|
|
|
|
|
},
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
2021-09-21 13:48:28 +00:00
|
|
|
const options = {
|
2021-07-14 14:14:35 +00:00
|
|
|
'{{email}}': 'sam@example.com',
|
2021-09-21 13:48:28 +00:00
|
|
|
'{{name}}': 'sam',
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['user']['name']).toBe('sam');
|
|
|
|
|
expect(parsedOptions['user']['email']).toBe('sam@example.com');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options that has an array', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = {
|
|
|
|
|
user: ['{{email}}', '{{name}}'],
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const options = {
|
2021-07-14 14:14:35 +00:00
|
|
|
'{{email}}': 'sam@example.com',
|
2021-09-21 13:48:28 +00:00
|
|
|
'{{name}}': 'sam',
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['user']).toContain('sam');
|
|
|
|
|
expect(parsedOptions['user']).toContain('sam@example.com');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be able to parse query options that has an array of objects', async () => {
|
2021-09-21 13:48:28 +00:00
|
|
|
const queryOptions = {
|
|
|
|
|
user: [{ email: '{{email}}' }, { name: '{{name}}' }],
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const options = {
|
2021-07-14 14:14:35 +00:00
|
|
|
'{{email}}': 'sam@example.com',
|
2021-09-21 13:48:28 +00:00
|
|
|
'{{name}}': 'sam',
|
2021-07-14 14:14:35 +00:00
|
|
|
};
|
|
|
|
|
|
2022-07-01 10:50:37 +00:00
|
|
|
const parsedOptions = await service.parseQueryOptions(queryOptions, options, null);
|
2021-07-14 14:14:35 +00:00
|
|
|
|
|
|
|
|
expect(parsedOptions['user'][1]['name']).toBe('sam');
|
|
|
|
|
expect(parsedOptions['user'][0]['email']).toBe('sam@example.com');
|
|
|
|
|
});
|
|
|
|
|
});
|