Skip to content

Commit 7dd40b0

Browse files
committed
fix: enable lb4 relation generate relation with same table
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent 12e0542 commit 7dd40b0

2 files changed

Lines changed: 149 additions & 2 deletions

File tree

packages/cli/generators/relation/templates/controller-relation-template-has-one.ts.ejs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import {
1515
post,
1616
requestBody,
1717
} from '@loopback/rest';
18-
import {
19-
<%= sourceModelClassName %>,
18+
import {<%if (sourceModelClassName != targetModelClassName) { %>
19+
<%= sourceModelClassName %>,<% } %>
20+
<%= targetModelClassName %>,
2021
<%= targetModelClassName %>,
2122
} from '../models';
2223
import {<%= sourceRepositoryClassName %>} from '../repositories';

packages/cli/test/integration/generators/relation.has-one.integration.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
2424
const sourceFileName = 'customer.model.ts';
2525
const targetFileName = 'address.model.ts';
2626
const controllerFileName = 'customer-address.controller.ts';
27+
const controllerFileNameForSameTableRelation = 'employee.controller.ts';
2728
const repositoryFileName = 'customer.repository.ts';
29+
const repositoryFileNameForSameTableRelation = 'employee.repository.ts';
30+
2831
// speed up tests by avoiding reading docs
2932
const options = {
3033
sourceModelPrimaryKey: 'id',
@@ -366,4 +369,147 @@ describe('lb4 relation HasOne', /** @this {Mocha.Suite} */ function () {
366369
);
367370
}
368371
});
372+
373+
context(
374+
'generates model relation with same table with default foreignKeyName',
375+
() => {
376+
const promptList = [
377+
{
378+
relationType: 'hasOne',
379+
sourceModel: 'Employee',
380+
destinationModel: 'Employee',
381+
relationName: 'reportingEmployee',
382+
},
383+
];
384+
385+
it('verifies that a preexisting property will be overwritten', async () => {
386+
await sandbox.reset();
387+
388+
await testUtils
389+
.executeGenerator(generator)
390+
.inDir(sandbox.path, () =>
391+
testUtils.givenLBProject(sandbox.path, {
392+
additionalFiles: SANDBOX_FILES,
393+
}),
394+
)
395+
.withOptions(options)
396+
.withPrompts(promptList[0]);
397+
398+
const sourceFilePath = path.join(
399+
sandbox.path,
400+
MODEL_APP_PATH,
401+
'employee.model.ts',
402+
);
403+
404+
assert.file(sourceFilePath);
405+
expectFileToMatchSnapshot(sourceFilePath);
406+
});
407+
},
408+
);
409+
410+
context(
411+
'checks if the controller file created for same table relation',
412+
() => {
413+
const promptArray = [
414+
{
415+
relationType: 'hasOne',
416+
sourceModel: 'Employee',
417+
destinationModel: 'Employee',
418+
},
419+
];
420+
421+
promptArray.forEach(function (multiItemPrompt) {
422+
describe('answers ' + JSON.stringify(multiItemPrompt), () => {
423+
suite(multiItemPrompt);
424+
});
425+
});
426+
427+
function suite(multiItemPrompt) {
428+
before(async function runGeneratorWithAnswers() {
429+
await sandbox.reset();
430+
await testUtils
431+
.executeGenerator(generator)
432+
.inDir(sandbox.path, () =>
433+
testUtils.givenLBProject(sandbox.path, {
434+
additionalFiles: SANDBOX_FILES,
435+
}),
436+
)
437+
.withOptions(options)
438+
.withPrompts(multiItemPrompt);
439+
});
440+
441+
it('checks controller content with hasOne relation with same table', async () => {
442+
const filePath = path.join(
443+
sandbox.path,
444+
CONTROLLER_PATH,
445+
controllerFileNameForSameTableRelation,
446+
);
447+
assert.file(filePath);
448+
expectFileToMatchSnapshot(filePath);
449+
});
450+
451+
it('the new controller file added to index.ts file', async () => {
452+
const indexFilePath = path.join(
453+
sandbox.path,
454+
CONTROLLER_PATH,
455+
'index.ts',
456+
);
457+
458+
expectFileToMatchSnapshot(indexFilePath);
459+
});
460+
}
461+
},
462+
);
463+
464+
context(
465+
'checks generated source class repository for same table relation',
466+
() => {
467+
const promptArray = [
468+
{
469+
relationType: 'hasOne',
470+
sourceModel: 'Employee',
471+
destinationModel: 'Employee',
472+
},
473+
];
474+
475+
const sourceClassnames = ['Employee'];
476+
477+
promptArray.forEach(function (multiItemPrompt, i) {
478+
describe('answers ' + JSON.stringify(multiItemPrompt), () => {
479+
suite(multiItemPrompt, i);
480+
});
481+
});
482+
483+
function suite(multiItemPrompt, i) {
484+
before(async function runGeneratorWithAnswers() {
485+
await sandbox.reset();
486+
await testUtils
487+
.executeGenerator(generator)
488+
.inDir(sandbox.path, () =>
489+
testUtils.givenLBProject(sandbox.path, {
490+
additionalFiles: SANDBOX_FILES,
491+
}),
492+
)
493+
.withOptions(options)
494+
.withPrompts(multiItemPrompt);
495+
});
496+
497+
it(
498+
'generates ' +
499+
sourceClassnames[i] +
500+
' repository file with different inputs',
501+
async () => {
502+
const sourceFilePath = path.join(
503+
sandbox.path,
504+
REPOSITORY_APP_PATH,
505+
repositoryFileNameForSameTableRelation,
506+
);
507+
508+
assert.file(sourceFilePath);
509+
expectFileToMatchSnapshot(sourceFilePath);
510+
},
511+
);
512+
}
513+
},
514+
);
369515
});

0 commit comments

Comments
 (0)