Skip to content

Commit

Permalink
fixup! preserve backwards compat for lb4 service
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Apr 21, 2020
1 parent 6bc9228 commit c6f9c1c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// IMPORTANT
// This snapshot file is auto-generated, but designed for humans.
// It should be checked into source control and tracked carefully.
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests.
// Make sure to inspect the changes in the snapshots below.
// Do not ignore changes!

'use strict';

exports[`lb4 service (remote) legacy JSON-based configuration loads config from \`{name}.datasource.config.json\` 1`] = `
import {getService} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {LegacyDataSource} from '../datasources';
export interface MyService {
// this is where you define the Node.js methods that will be
// mapped to REST/SOAP/gRPC operations as stated in the datasource
// json file.
}
export class MyServiceProvider implements Provider<MyService> {
constructor(
// legacy must match the name property in the datasource json file
@inject('datasources.legacy')
protected dataSource: LegacyDataSource = new LegacyDataSource(),
) {}
value(): Promise<MyService> {
return getService(this.dataSource);
}
}
`;
47 changes: 11 additions & 36 deletions packages/cli/test/fixtures/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const DATASOURCE_APP_PATH = 'src/datasources';
const CONFIG_PATH = '.';
const DUMMY_CONTENT = '--DUMMY VALUE--';
const {getSourceForDataSourceClassWithConfig} = require('../../test-utils');

exports.SANDBOX_FILES = [
{
Expand All @@ -26,66 +26,41 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'myds.datasource.config.json',
content: JSON.stringify({
file: 'myds.datasource.ts',
content: getSourceForDataSourceClassWithConfig('MydsDataSource', {
name: 'myds',
connector: 'soap',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'map-ds.datasource.config.json',
content: JSON.stringify({
file: 'map-ds.datasource.ts',
content: getSourceForDataSourceClassWithConfig('MapdsDataSource', {
name: 'MapDS',
connector: 'soap',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'map-ds.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: DATASOURCE_APP_PATH,
file: 'myds.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.config.json',
content: JSON.stringify({
file: 'dbmem.datasource.ts',
content: getSourceForDataSourceClassWithConfig('DbmemDataSource', {
name: 'dbmem',
connector: 'memory',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.config.json',
content: JSON.stringify({
file: 'restdb.datasource.ts',
content: getSourceForDataSourceClassWithConfig('RestdbDataSource', {
name: 'restdb',
connector: 'rest',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: DATASOURCE_APP_PATH,
file: 'no-name.datasource.config.json',
content: JSON.stringify({
file: 'no-name.datasource.ts',
content: getSourceForDataSourceClassWithConfig('NoNameDataSource', {
connector: 'rest',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'no-name.datasource.ts',
content: DUMMY_CONTENT,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const TestSandbox = testlab.TestSandbox;
const generator = path.join(__dirname, '../../../generators/service');
const SANDBOX_FILES = require('../../fixtures/service').SANDBOX_FILES;
const testUtils = require('../../test-utils');
const {expectFileToMatchSnapshot} = require('../../snapshots');

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down Expand Up @@ -262,6 +263,41 @@ describe('lb4 service (remote)', () => {
assert.fileContent(INDEX_FILE, /export \* from '.\/myservice.service';/);
});
});

describe('legacy JSON-based configuration', () => {
it('loads config from `{name}.datasource.config.json`', async () => {
const additionalFiles = [
...SANDBOX_FILES,
{
path: 'src/datasources',
file: 'legacy.datasource.config.json',
content: JSON.stringify({
name: 'legacy',
connector: 'soap',
}),
},
{
path: 'src/datasources',
file: 'legacy.datasource.ts',
content: '// dummy source file',
},
];

await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {additionalFiles}),
)
.withArguments('myService --datasource legacy');

const expectedFile = path.join(
sandbox.path,
SERVICE_APP_PATH,
'my-service.service.ts',
);
expectFileToMatchSnapshot(expectedFile);
});
});
});

// Sandbox constants
Expand Down

0 comments on commit c6f9c1c

Please sign in to comment.