Skip to content

Commit

Permalink
fixup! preserve backwards compat for lb4 rest-crud
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 7920ed8 commit 891ab1d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 rest-crud legacy JSON-based configuration loads config from \`{name}.datasource.config.json\` 1`] = `
import {ModelCrudRestApiConfig} from '@loopback/rest-crud';
import {DefaultModel} from '../models';
const config: ModelCrudRestApiConfig = {
model: DefaultModel,
pattern: 'CrudRest',
dataSource: 'dbmem',
basePath: '/default-models',
};
module.exports = config;
`;
42 changes: 11 additions & 31 deletions packages/cli/test/fixtures/rest-crud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
const DATASOURCE_APP_PATH = 'src/datasources';
const MODEL_APP_PATH = 'src/models';
const CONFIG_PATH = '.';
const DUMMY_CONTENT = '--DUMMY VALUE--';
const fs = require('fs');
const {getSourceForDataSourceClassWithConfig} = require('../../test-utils');

exports.SANDBOX_FILES = [
{
Expand All @@ -20,64 +20,44 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbkv.datasource.config.json',
content: JSON.stringify({
file: 'dbkv.datasource.ts',
content: getSourceForDataSourceClassWithConfig('DbkvDataSource', {
name: 'dbkv',
connector: 'kv-redis',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'dbkv.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: 'my-ds.datasource.config.json',
content: JSON.stringify({
file: 'my-ds.datasource.ts',
content: getSourceForDataSourceClassWithConfig('MyDsDataSource', {
name: 'MyDS',
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: 'sqlite3.datasource.config.json',
content: JSON.stringify({
file: 'sqlite3.datasource.ts',
content: getSourceForDataSourceClassWithConfig('Sqlite3DataSource', {
name: 'sqlite3',
connector: 'loopback-connector-sqlite3',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'sqlite3.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.ts',
content: DUMMY_CONTENT,
},
{
path: MODEL_APP_PATH,
file: 'decorator-defined.model.ts',
Expand Down
42 changes: 42 additions & 0 deletions packages/cli/test/integration/generators/rest-crud.integration.js
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/rest-crud');
const SANDBOX_FILES = require('../../fixtures/rest-crud').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 @@ -237,6 +238,47 @@ describe('lb4 rest-crud', function () {
).to.be.rejectedWith(/No datasources found/);
});
});

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: 'memory',
}),
},
{
path: 'src/datasources',
file: 'legacy.datasource.ts',
content: '// dummy source file',
},
];

await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: [...SANDBOX_FILES, ...additionalFiles],
}),
)
.withPrompts({
datasource: 'legacy',
modelNameList: ['DefaultModel'],
});

const expectedDefaultModelFile = path.join(
sandbox.path,
MODEL_ENDPOINT_PATH,
'default-model.rest-config.ts',
);

expectFileToMatchSnapshot(expectedDefaultModelFile);
});
});
});

// Sandbox constants
Expand Down

0 comments on commit 891ab1d

Please sign in to comment.